1

This is my first Android game App and I'm a bit stuck at the moment.

So, basically what I have is a card game, which consists of a total of 6 turns. 1 st turn=player 1, 2nd turn=player 2, 3rd turn = player 1 and so on.

What I thought at first, was to handle each players turn inside a while loop which stated: WHILE (round<=6), and initialize variable round at 0.

Inside that WHILE loop I had an if(turn=1){ display player 1s available actions and cards on screen and set onclicklisteners and increment variable round by 1 upon a click}, else if (turn=2){ do the same but for player 2}.

This, clearly doesn't work, considering the while loop doesn't wait for the player to click a button or a card, and I find myself in a diabolical infinite loop :(.

So, I've been searching for a solution for a couple of days now, and I can't seem to find something very enlightening.

I would really appreciate if anyone could recommend some sort of solution or any source of information I could read on to get a possible solution.

Thank you.

3 Answers3

0

Your not going to want to do this with a loop. Your best bet is to look into the State Design Pattern.

Using this pattern you will have different states for a "turn". Then when the user clicks a button or a card, the state of the turn changes. Then the "turn" object has listeners to transact the changes, like whose turn it is.

Without seeing some actual code, I cannot give you a code example.

wesleywmd
  • 605
  • 1
  • 6
  • 20
0

You could use a For loop, that with the number of turns, and then if check if the number it's even, if it's even you could set a turn to player 1 or 2, and if not the other player turns

public static void startGame() {
        for (int i = 0; i <= 6; i++) {
            if(i%2==0){
            System.out.print("player 1 turns"+i+"\n");
            }else{
            System.out.print("player 2 turns"+i+"\n");
            }
        }

    }
Abstract
  • 664
  • 1
  • 5
  • 15
0

If you use a button for the user to click when they are finished with their turn, this is very simple. Just make a global turns variable that keeps track of the number of turns and keep a whoseTurn variable that contains "1" or "2", so you know whose turn it is currently.

Start the game as player 1's turn with turns = 1 and whoseTurn = 1. When the "button" is clicked then increment your turns by 1 and change whoseTurn to 2. Then you will want to change your interface from player 1 to player 2. Once the "button" is clicked again do the opposite so it is back to player 1 with the turn counter now at 3. Continue this until turns reaches 6.

Pseudocode:

int turns = 1;
int whoseTurn = 1;

on button click {
    //Check for end of game
    if(turns >= 6) {
        //game over
    }
    else {
        turns++;
        if(whoseTurn == 1) {
            whoseTurn = 2;
            player2();
        } else {
            whoseTurn = 1;
            player1();
    }
}

player1() {
    //code for player 1's turn
}

player2() {
    //code for player 2's turn
}

I know you are doing this in Java, but this little demo in Javascript might be useful.

Demo

Tricky12
  • 6,752
  • 1
  • 27
  • 35