質問

Possible Duplicate:
3 player card game turn system

I'm making a GUI 3 player card game (1 human and 2 computers). Each player has 3 cards in hand. In each round there is a dealer and the first player to play a card is the player at the right of the dealer. Each player can only play after the left player has played. (It's the basis of a card game)

After each player had put the 3 cards on the table. The dealer button passes to the player at the right and a new round begin.

I need help to do a function to control the turns, which can see whose turn it is to play. I it is human turn, it waits for input. If it is computer turn, it calls the action function of the calss.

What I have now is a Card class with a Suit and a Value.

A Deck class that is a list of Cards ( this class has a suffle function and a draw card function)

I have a Table class that had a list of cards ( that is where the cards go when a player plays them)

I have a Player class ( the same class is for human and computer player - is this good? )

The Player class have a Hand class ( thats where the 3 cards of the player are )

The Game class has one deck, one table and 3 players.

I have the function that the human is the first to play and moves one card to the table, then the 2 computer players move one card each. This only works if the human player is the first to play. Once the 3 cards of each player are played, begins another round, in this round is not the player but the computer to first play.

I don't have the idea how to make one system to control this. I don't want code, but the idea itself.

Thanks

役に立ちましたか?

解決

I would have some properties like the following in my Player class:

public bool IsComputer { get; set; }
public bool IsCurrentTurn { get; set; }
public int TurnOrder { get; set; }

That way, you can write your logic based on if they are human or not and if it is their turn or not. You would use the TurnOrder property to determine whose turn it is next.

So if you are creating your Player objects like this:

Player player1 = new Player();

You could add to your main logic something like this:

if (player1.IsComputer) 
{
   PerformTurn();
}
else //Human
{
   //Logic to wait and allow human player to do his or her turn here
}

Edit:

To determine whose turn it is, I would first have my players in a List of Player. Example:

List<Player> players = new List<Player>
{
new Player {Name = "Player 1", IsComputer = false, TurnOrder = 0, IsCurrentTurn = true},
new Player {Name = "Player 2", IsComputer = true, TurnOrder = 1, IsCurrentTurn = false},
new Player {Name = "Player 3", IsComputer = true, TurnOrder = 2, IsCurrentTurn = false}
};

Second, I would create a function that uses the TurnOrder property of each player to determine whose turn it is next. A method like this would do it:

public Player GetNextPlayer()
{
   Player currentPlayer = players.First(x => x.IsCurrentTurn);

   //if player is last in the turn order return first player
   if (currentPlayer.TurnOrder == players.Max(x => x.TurnOrder).TurnOrder)
   {
       return players.Min(x => x.TurnOrder);
   }
   else 
   {
      return players.First(x => x.TurnOrder == (currentPlayer.TurnOrder + 1));
   }


}

Of course, you could just use the index of the list and skip using the TurnOrder altogether, although if you wanted to expand your game, using the index as the turn order may hurt you later on. I think the TurnOrder property is much more flexible.

To get the next player in the turn, just use this:

Player currentPlayer = GetNextPlayer();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top