Jay_

Example boolean in Java

Jan 9th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /* It’s a dice game then when the button is clicked two dice roll and the textbox says “player 1 rolls” until the two dice match like 6-6 or 2-2 etc.
  2. from which the textbox then says “player 1 wins”.
  3.  
  4. that’s all working sound, but now I want it to be 2 player, so after each click of the button it says
  5.  
  6. click: player 1 rolls.
  7. Click: player 2 rolls.
  8. Click: player 1 rolls.
  9. Click player 2 rolls.
  10. Etc.
  11.  
  12. Until someone wins.
  13.  
  14.  
  15. Heres  the code snippet … any idea how to do it?*/
  16.  
  17. int dice1;
  18. int dice2;
  19. int player = 1;
  20.  
  21. TextView mText2;
  22.  
  23. // Create a boolean variable which determines whos turn it is.
  24. // If isPlayer1Turn = false, then its GOT to be players 2 turn, and vice versa.
  25. // Set the default value to true, so player 1 goes first.
  26. boolean:isPlayer1Turn = true;
  27.  
  28. // Player has won because dice1 and dice2 match.
  29. if (dice1 == dice2) {
  30.     mText2.setText("Player " + player + " wins");
  31. }else if (dice1 != dice2) { // Dice do not match, set it to player 2's turn.       
  32.     // It WAS players 1s turn, so now its player 2's turn
  33.     if(isPlayer1Turn == true) {
  34.         mText2.setText("Player 2 rolls!");
  35.         isPlayer1Turn = false;
  36.     }else{  // It WAS player 2s turn, but now its player 1s turn!
  37.         isPlayer1Turn = true;
  38.         mText2.setText("Player 1 rolls!");
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment