5 Replies - 1662 Views - Last Post: 28 April 2011 - 07:38 AM Rate Topic: -----

#1 RKrogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 11

Turn based card game. Help with creating turns.

Posted 26 April 2011 - 12:41 AM

Hi!
I'm currently working on a BlackJack-game. I can't figure out how I should create the "turns" as it is a turn based game.

Here is briefly how it works so far:
1. Players register (Dealer already created) and i put in a List<T> called PlayersInRound.
2. When the number of players that is participating is registered, the game starts through a click on the Start Game-button.
3. Every player in PlayersInRound is handed two randomly chosen cards and their value is summed up.
4. Every player's value is compared to the GameRules-class's methods, any player above the total value of 21 is removed from PlayersInRound (Impossible at this time as any player only have 2 cards).
5. ???

What I want to happen:
I figured that as the round goes on, any player is given the opportunity to Hit, Stand or Surrender through a simple click on any of the buttons in the Player Control groupbox.
The way to do this, I thought, was through a loop, pausing the loop until a choice was made, and moving on to the next player. Continuing the loop until the game had a winner(s).

The following is an example:
while(PlayersInRound.Count >1)
{
for(int i=0 ; i < PlayersInRound.Count ; i++)
{
CurrentPlayer = PlayersInRound.ElementAt(i);
EnablePlayerControls();
///Here I thought I could pause the loop somehow and wait until Hit, Stand or Surrender was clicked, before moving on.
}
}



But the above code, without a way to pause it, will be an infinite loop...
Please help! If there is a better way of doing this or if I'm on the right track only lacking the knowledge to move on, I don't mind. But I've been stuck for a while now...

Thank you in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Turn based card game. Help with creating turns.

#2 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: Turn based card game. Help with creating turns.

Posted 26 April 2011 - 01:23 AM

Hi,

It should have a value that allows it to exit the loop. In a game of blackjack every player has a chance to hit, raise, double etc.. right? Why don't you use that as a variable. If a player has hit complete, set the value to +1. When the value has reached the number of players, it should exit the loop.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,971
  • Joined: 02-June 10

Re: Turn based card game. Help with creating turns.

Posted 26 April 2011 - 04:49 AM

foreach (Player player in MyPlayerCollection)
{
   DisableAllPlayers();
   Enable(player);
   while (!player.done) application.DoEvents();// .done is a bool property inside the player class
}

Was This Post Helpful? 0
  • +
  • -

#4 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Turn based card game. Help with creating turns.

Posted 26 April 2011 - 01:02 PM

I think it is even easier than that. If you have a list of users who are in the game, you can ultimately do all your code in a do while, that checks to see how many players are valid. A little more arithmetic will need to be inserted into the structure, but that will be needed anyway. Just remove players from the list, and do a count on the list to ensure more than two players are still valid.

do{
// game logic
}while(PlayersInRound.Count > 1);


Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,971
  • Joined: 02-June 10

Re: Turn based card game. Help with creating turns.

Posted 26 April 2011 - 01:22 PM

I'm not sure how your do..while loop is any better than the OP's while loop, or how it resolves his question of "How do I pause to allow a player to finish his turn?"
The only difference between a while and a do...while is that a do...while has to execute a minimum of one time no matter the evaluation of the comparison, where a standard while has the possibility of never executing the loop.

So in all reality your do...while could try to execute a minimum of 1 time even if there are zero players, because it won't make the comparison check until AFTER it has completed the loop once. That could be a problem. Null references etc. because there would be no players.

while(PlayersInRound.Count >1)
{
for(int i=0 ; i < PlayersInRound.Count ; i++)
{
CurrentPlayer = PlayersInRound.ElementAt(i);
EnablePlayerControls();
///Here I thought I could pause the loop somehow and wait until Hit, Stand or Surrender was clicked, before moving on.
}
}




do{
// game logic
}while(PlayersInRound.Count > 1);




Your choice of loop constructs is really up to your style.
The point to this code was to provide a bool that would cause a pause while a given player played their turn, then release control back to the loop for the next player. Note line 5. It is basically an active pause loop waiting for the player to finish their turn by waiting for the bool done to become true.

foreach (Player player in MyPlayerCollection)
{
   DisableAllPlayers();
   Enable(player);
   while (!player.done) application.DoEvents();// .done is a bool property inside the player class
}


Was This Post Helpful? 0
  • +
  • -

#6 RKrogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 11

Re: Turn based card game. Help with creating turns.

Posted 28 April 2011 - 07:38 AM

OP:
Thank you for all the help! I learned a lot trying out different ways to solve this, however, ultimately I skipped the loop and coded Methods throwing one another until every player had made their choice. Every method is controlled by if-statements to make sure we never reach an infinite loop. The current player's turn is controlled by an int that equals the numbers of players left in the game.

StartPlayerRound()
PlayerChoice()
-Hit()
-Stand()
-Surrender()
NextPlayer()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1