0

I'm having a little trouble concisely describing what it is that I'm trying to do, which is hurting my ability to search for an answer. I'll try to be specific with my problem, if anyone could give a suggestion or point me in the direction of what to study, I'd greatly appreciate it.Tr

I'm trying to program a GUI version of the cardgame Dominion, where playing different cards will yield different results and choices. Many of these cards have similar starting choices (e.g. select a card from your hand and trash it/look at enemy hand), but different ending choices (e.g. upgrade that trashed card/give trashed card to another player). upon playing a card, the program looks for the unique numeric card code and begins executing code specific to that card.

Here's where I'm hung up:

I'd like to have more general methods that listen for user input INSIDE the unique card-code, but I keep getting errors. Ideally, I'd be able to do something like

for(int i = 0; i < totalPlayers; i++)
{
    showEnemyHand(i);
}

or

for(int i = 0; i < totalPlayers; i++)
{
    thiefEffect(i);
}

within a 'buttonclicked' event (the "play card" button, specifically.) The showEnemyHand(int) and thiefEffect(int) method would wait for user input, store responses, and then return right back to the for loop that it was called from, but its not as easy as I'd originally hoped.

I'm suffering most from not even knowing what it is that I should be searching for. I've been reading up on event handling and delegates, and I'm not sure that's what I need. Can anyone point me in the direction of what I need to learn, or maybe give me the topic of what I'm trying to solve so I can search for it a little easier? (of course, helping me solve it would be appreciated too =D)

Thanks a bunch! Jake

doktorjake
  • 15
  • 1
  • 3

1 Answers1

0

Your solution would be fine for a command line based game, in a language with continuations/coroutines, or maybe in a multi-threaded application where showEnemyHand etc would block on user input. For a GUI-based game, an event driven architecture is really what would work best for you, so in principle I'd suggest learning more about it.

But if you really want to do that using a loop, I'd suggest then reading about threading and blocking calls. Once you understand the concepts, you should be able to:

  1. Create a separate thread to host your loop;
  2. Create a lock that will block execution until the user inputs something (see the example in the linked question);
  3. Use that lock in your loop and on the callback for user input:
    1. In the beginning of your loop, you wait on your lock;
    2. When the user inputs something (which you'll detect using an event handler - see the docs for the particular GUI framework you're using) you save which action was chosen and frees the lock;
    3. Your loop will automatically continue, reading the saved action and performing an iteration, until it reachs the same point again and waits for another user input.

Whether this method is easier or harder than coding your rules using the event driven logic, it's debatable. The same can be said about coroutines (though being less experienced with that, I can not opinate). The pointers I gave should help you get started though.

Community
  • 1
  • 1
mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
  • wonderful. Thanks so much. I won't be able to say if it worked for a couple days probably, but I'll share my results when I get it working. This sounds like the kind of thing that I'm looking for though, thank you. – doktorjake Jul 04 '12 at 01:41