I need help with shortening my JAVA program, PIG. I feel like I can shorten this
ID: 640187 • Letter: I
Question
I need help with shortening my JAVA program, PIG.
I feel like I can shorten this somehow, but not sure how. Any assistance would help, thank you!!
import java.util.Scanner;
public class Pig
{
Boolean player1 = true;
Boolean computer1 = true;
int dice;
int player1points, computer1points;
int player1total = 0;
int computer1total = 0;
private Scanner keyboard;
public void roll()
{
dice = (int)(Math.random()*6) + 1;
}
public int player1score()
{
{
player1points = dice + player1points;
System.out.println("You threw: " + dice);
} return player1points;
}
public void player1scoreZero()
{
player1points = 0;
}
public int computer1score()
{
{
computer1points = dice + computer1points;
} return computer1points;
}
public void computer1scoreZero()
{
computer1points = 0;
}
public Pig()
{
humanGame();
if(!player1);
{
computerTurn();
}
}
public int humanGame()
{
System.out.println(" SCORE: " + player1total + "-" + computer1total);
System.out.println("PLAYER, it is your turn.");
do
{
roll();
if(dice == 1)
{
System.out.println("You rolled a 1 - turn over.");
player1points = 0;
computerTurn();
}
else if(dice != 1)
{
player1total += dice;
if(player1total >= 100)
{
System.out.println("You rolled: " + dice);
System.out.println("You have " + player1total + " points.");
System.exit(0);
}
player1score();
System.out.println("You have " + player1total + " points.");
System.out.println("Keep going? (y/n)");
keyboard = new Scanner(System.in);
String choice = keyboard.nextLine();
if(choice.equalsIgnoreCase("Y"))
{
roll();
if(!choice.equalsIgnoreCase("Y"))
{
System.out.println("You didn't press 'y'. To make sure the program is running correctly please press 'y' to roll or 'n' to hold.");
humanGame();
}
}
if(choice.equalsIgnoreCase("n"))
{
player1scoreZero();
computerTurn();
}
}
}while(player1);
return dice;
}
public int computerTurn()
{
System.out.println(" SCORE: " + player1total + "-" + computer1total);
System.out.println("COMPUTER, it is your turn.");
do
{
roll();
if(dice !=1)
{
computer1total += dice;
if(computer1total >=100)
{
System.out.println("You rolled a " + dice);
System.exit(0);
}
System.out.println("You rolled a " + dice);
computer1score();
roll();
}
if(dice == 1)
{
System.out.println("You rolled a 1 - turn over.");
computer1scoreZero();
humanGame();
}
}while(computer1);
return dice;
}
public static void main(String[] args)
{
new Pig();
}
}
Explanation / Answer
Simple Java Program For PIG:
import java.util.Random;
import java.util.Scanner;
public class PIG
{
public static void main(final String[] args)
{
int playerScores = 0;
int playerTotal = 0;
int computerScores = 0;
int computerTotal = 0;
int dice;
boolean gameOver = false;
boolean turnOver = false;
char repeat;
String input;
final Scanner keyboard = new Scanner(System.in);
final Random rand = new Random();
System.out.println("Welcome to the game of Pig! ");
while (gameOver == false)
{
do
{
dice = rand.nextInt(6) + 1;
System.out.println("You rolled: " + dice);
if (dice == 1)
{
playerScores = 0;
System.out.print("You lose your turn! ");
System.out.println("Your total is " + playerTotal);
turnOver = true;
while (playerTotal < 100) //look at this
;
}
else
{
playerScores += dice;
System.out.print("Your turn score is " + playerScores);
System.out.println(" If you hold you will have "+ playerScores + " points.");
System.out.println("Enter 'r' to roll " + "again, 'h' to hold.");
input = keyboard.nextLine();
repeat = input.charAt(0);
if (repeat == 'h')
break;
}
}
while (turnOver == false || dice != 1);
playerTotal += playerScores;
System.out.println("Your score is " + playerTotal);
playerScores = 0;
if (playerTotal >= 100)
{
System.out.println("YOU WIN!");
gameOver = true;
while (playerTotal >= 100) [b]//and this[/b]
;
}
System.out.println();
System.out.println("It is the computer's turn.");
do
{
dice = rand.nextInt(6) + 1;
System.out.println("The computer rolled: " + dice);
if (dice == 1)
{
computerScores = 0;
System.out.print("The computer lost its turn!");
System.out.print(" Computer total is " + computerTotal)
turnOver = true;
while (computerTotal < 100) //and this
;
}
else
{
computerScores += dice;
if (computerScores >= 20
|| (computerTotal + computerScores) >= 100)
System.out.println("The computer holds");
turnOver = true;
}
}
while (dice != 1 || computerScores < 20);
computerTotal += computerScores;
System.out.println("The computer's score is " + computerTotal + " ");
computerScores = 0;
if (computerTotal >= 100)
{
System.out.println("THE COMPUTER WINS!");
gameOver = true;
while (computerTotal >= 100) //and this
;
}
if (keyboard != null)
keyboard.close();
}
}
}