Advertisement

How to switch turn in a card game?

Started by July 07, 2015 01:46 PM
6 comments, last by Truerror 9 years, 5 months ago

hey guys!

I'm making a card game that has two players and each has a turn to play , it's nothing complex only a button so you can change shifts to the other player able to play can anyone help me?
thanks md4friends

Thats a rather vague question, can you be more precise with what exactly you need help? What is it that's not working? And what kind of cardgame is it/how do players play it?

Advertisement

Its a simple card game with 52 cards both players, my questions is how i can do make a turn/ switch for each player can play

You store a variable that holds which player is currently active/having his turn. Then you either only display that players cards and only let those be editable, or show all cards but still only have the active player's cards editable - depending on the kind of game. When a player is done and presses the "end my turn"-button, you set the variable to the other/next player and repeat.


turn = (turn+1)&1;

Your question and description are still far too vague to be useful, but here's what I'd suggest... have a boolean value that holds whether or not the game has been won, then perform a gameloop until this is true. You can have a second variable that stores which player's turn it is, then at the end of each loop, you switch the current player.

Something like this (in C++):


bool game_won = false;
int player = 1;
while (!game_won) {

  // game logic
  if (player == 1)
    player = 2;
  else
    player = 1;
  // player = (player == 1) ? 2 : 1;
}
 
Advertisement

It's always a little worrying when conditional logic is thrown at simple "number problems". While I agree it's easier to understand in "for beginners", it also teaches bad habits (yes, I consider needless branching as bad style).

Plus, in a more generic game with 2-8 players, you'd be completely screwed with any kind of "if it was player x's turn, it is now player y's turn" logic.

List<Player> players;
int activePlayer = 0;
 
while (playing)
{
    //do stuff for players[activePlayer]
 
    //next player, modulo (%) will ensure wrapping back to 0 after the last player
    activePlayer = ++activePlayer % players.Count();
}

And if you prefer more readable code, keep in mind that pretty much every time you feel the need to comment a piece of code, it should probably be made a function named very much like what your comment is saying:

int nextPlayer()
{
     activePlayer = ++activePlayer % players.Count();
}
 
...
 
while (...)
{
    ...
 
    activePlayer = nextPlayer();
}
f@dzhttp://festini.device-zero.de

You store a variable that holds which player is currently active/having his turn. Then you either only display that players cards and only let those be editable, or show all cards but still only have the active player's cards editable - depending on the kind of game. When a player is done and presses the "end my turn"-button, you set the variable to the other/next player and repeat.

^ This. At least in the most basic sense.

But then again,a basic technique is all you need, really. Unless you want us to give you the full code in a specific language?

This topic is closed to new replies.

Advertisement