I've programmed a dice game for pig which utilizes a single die. The points are suppose to accumulate during your turn unless you hit a 1 and they are zeroed out and nothing added to your total or you can hold your turn points before a 1 is rolled and just add them to the total passing it to the next player. My turn total points and the accumlated points do not seem to hold up correctly. When I hit hold, it's only taking points from current turn. If I hit a 1 it adds it along with the rest of the turn and doesn't 0 out for that current turn. And the turn and total are remaining the same now. Everytime I change something I get a new problem. So I thought I would go ahead and post here before to much happened. Also for giggles, I need to use an array or arraylist somewhere, maybe to keep points? Is this possible, what would I need to do. Sorry so long winded, following is all my code.
import java.util.Scanner; //Imports scanner to be used for rolling / holding actions public class Pig // Begin Class Pig { // Declaring memory used in game int player1Total, player2Total; int dice; int player1TurnPoints, player2TurnPoints; Boolean player1Turn = true; Boolean player2Turn = true; // Sets roll method and face value for dice, calls random public void roll() { dice = (int)(Math.random()*6) + 1; } // Keeps track of points during current turn, dice value, plus turn points public int player1TurnScore() { { player1TurnPoints = dice + player1TurnPoints; System.out.println("Player 1 scored: " + player1TurnPoints + " in their turn."); } return player1TurnPoints; } //Keeps track of points during current turn, dice value, plus turn points public int player2TurnScore() { { player2TurnPoints = dice + player2TurnPoints; System.out.println("Player 2 scored: " + player2TurnPoints + " in their turn."); } return player2TurnPoints; } // Declares if points are "0" you lose turn public void player1TurnZero() { //player1TurnPoints = 0; System.out.println("Player 1 loses turn."); } //Declares if points are "0" you lose turn public void player2TurnZero() { //player2TurnPoints = 0; System.out.println("Player 2 loses turn."); } // Declares whose turn it should be public Pig() { player1(); //if(!player1Turn) //{ // player2(); // } } // Starts the game and rolls die for player 1 public int player1(){ System.out.println("Player 1 hit 'r' to roll your die."); Scanner key = new Scanner(System.in); String start = key.nextLine(); if(start.equalsIgnoreCase("r")){ { System.out.println("The die has been rolled."); roll(); } } do{ roll(); if(dice == 1) // Lose your turn when a 1 is rolled { System.out.println("1 has been rolled, you lose your turn."); player1TurnZero(); player2(); } else if(dice != 1) // Shows what you rolled on your turn { System.out.println(dice + " Has been rolled"); player1TurnScore(); System.out.println("Player 1 total score so far is: " + player1TurnPoints); System.out.println("Player 1 press 'r' to roll or 'h' to hold."); Scanner keyboard = new Scanner(System.in); String choice = keyboard.nextLine(); // What happens when you press r or h if(choice.equalsIgnoreCase("r")) { { System.out.println("Rolling again."); roll(); } } // What happens when you press r or h if(choice.equalsIgnoreCase("h")) { System.out.println("Player 1, your points thus far is " + player1Total); player2(); // Switches to Player 2 Turn } } player1Total += player1TurnPoints; // Adds turn points to Total if(player1Total >= 100) // You win once you beat 100 points { System.out.println("Player 1 wins, way to go champ!"); System.exit(0); } }while(player1Turn); return dice; } //Starts the game and rolls die for player 2 public int player2(){ System.out.println("Player 2 hit 'r' to roll your die."); Scanner key = new Scanner(System.in); String start = key.nextLine(); if(start.equalsIgnoreCase("r")){ { System.out.println("The die has been rolled."); roll(); } } do{ roll(); if(dice == 1) // Lose your turn when a 1 is rolled { System.out.println("1 has been rolled, you lose your turn."); player2TurnZero(); player1(); } else if(dice != 1) // Shows what you rolled on your turn { System.out.println(dice + " Has been rolled"); player2TurnScore(); System.out.println("Player 2 total score so far is: " + player2TurnPoints); System.out.println("Player 2 press 'r' to roll or 'h' to hold."); Scanner keyboard = new Scanner(System.in); String choice = keyboard.nextLine(); // What happens when you press r or h if(choice.equalsIgnoreCase("r")) { { System.out.println("Rolling again."); roll(); } } // What happens when you press r or h if(choice.equalsIgnoreCase("h")) { System.out.println("Holding, your points thus far is " + player2Total); player1(); // Switches to Player 1 Turn } } player2Total += player2TurnPoints; // Adds turn points to Total if(player2Total >= 100) // You win once you beat 100 points { System.out.println("Player 2 wins, way to go champ!"); System.exit(0); } }while(player2Turn); return dice; } public static void main(String[] args) { new Pig(); } }