start a new game
I need help with the level 3, 4 features mentioned in the picture. My code is attached below
int rounds = 0;
int playerScore = 0;
int computerScore = 0;
int playerNumber = 1;
int computerNumber = 1;
int playerLosses = 0;
int computerLosses = 0;
int playerWins = 0;
int computerWins = 0;
int playerTies = 0;
int computerTies = 0;
int playerTotalPoints = 0;
int computerTotalPoints = 0;
int playerRolls = 0;
int computerRolls = 0;
int menuChoice = 0;
PrintStream output = new PrintStream(new File("output.txt"));
PrintStream console = System.out;
System.setOut(output);
c.println("\t\t\t\tDice Game");
c.println("\t\t\t\t---------");
c.println("Please select your option:");
c.println("1- Play against computer.");
c.println("2- Exit the game, show summary and print to output.txt.");
c.println("******************************************************");
menuChoice = c.readInt();
c.clear();
while(menuChoice != 2)
{
if(menuChoice == 1)
{
c.println("How many rounds would you like to play?");
rounds = c.readInt();
c.println();
}
else if(menuChoice == 2)
{
System.exit(0)
}
else
{
c.println("Invalid option. Choose again.");
}
//for loop
for(int x = 0; x < rounds; x++)
{
playerNumber = (int) (Math.random()*6 + 1);
computerNumber = (int) (Math.random()*6 + 1);
if(playerNumber > computerNumber)
{
c.println("You win round " + (x+1) + ".");
playerWins++;
computerLosses++;
}
else if(computerNumber > playerNumber)
{
c.println("The computer wins round " + (x+1) + "." );
computerWins++;
playerLosses++;
}
else if(playerNumber == computerNumber)
{
c.println("Round " + (x+1) + " is a tie.");
playerTies++;
computerTies++;
}
playerScore = ((2*(playerWins)) + playerTies);
computerScore = ((2*(computerWins)) + computerTies);
System.out.println("[Player]");
System.out.println("Player Dice Number: " + playerNumber);
c.println("Player Dice Number: " + playerNumber);
System.out.println("Player Rolls: " + playerRolls++);
System.out.println("Points: " + playerScore);
c.println("Points: " + playerScore);
System.out.println("Wins: " + playerWins);
System.out.println("Losses: " + playerLosses);
System.out.println("Ties: " + playerTies);
System.out.println();
System.out.println("[Computer]");
System.out.println("Computer Dice Number: " + computerNumber);
c.println("Computer Dice Number: " + computerNumber);
System.out.println("Computer Rolls: " + computerRolls++);
System.out.println("Points: " + computerScore);
c.println("Points: " + computerScore);
System.out.println("Wins: " + computerWins);
System.out.println("Losses: " + computerLosses);
System.out.println("Ties: " + computerTies);
System.out.println("*****************************************");
System.out.println();
c.println("Press [Enter] to continue.");
c.getChar();
c.println();
}
c.println("Please select your option");
c.println("1- Play against computer.");
c.println("2- Exit the game, show summary and print to output.txt.");
c.println("******************************************************");
menuChoice = c.readInt();
c.clear();
if(playerScore > computerScore)
{
c.println("You win the game by " + (playerScore - computerScore) + " point(s)!");
c.println("--> Detailed rounds summary has been printed to output.txt");
System.out.println(">You won the game!");
System.out.println("------------------");
}
else if(computerScore > playerScore)
{
c.println("The computer wins the game by " + (computerScore - playerScore) + " point(s)!");
c.println("--> Detailed rounds summary has been printed to output.txt");
System.out.println(">The computer won the game!");
System.out.println("---------------------------");
}
else if(playerScore == computerScore)
{
c.println("Both the players have an equal score.");
c.println("Player score = " + playerScore);
c.println("Computer score = " + computerScore);
c.println("--> Detailed rounds summary has been printed to output.txt");
System.out.println(">Both players have an equal score. It is a tie");
System.out.println("----------------------------------------------");
}
output.close();
}
Step by step
Solved in 7 steps with 8 images