annotate it line by line with an explanation of what each line does. Do NOT comm
ID: 3761289 • Letter: A
Question
annotate it line by line with an explanation of what each line does. Do NOT comment on the obvious lines in the program. public class PigGame { private int goal; private PairOfDice dice; private PigPlayer computer, human, currentPlayer; //----------------------------------------------------------------- // Sets up the game including the point goal for the game. //----------------------------------------------------------------- public PigGame(int target) { goal = target; dice = new PairOfDice(); computer = new PigPlayer(20); human = new PigPlayer(PigPlayer.ASK); } //----------------------------------------------------------------- // This method contains the primary game loop, which terminates // only once a player has won the game. //----------------------------------------------------------------- public void play() { boolean noWinnerYet = true; currentPlayer = computer; while (noWinnerYet) { takeTurn(); // Check for winner; otherwise switch players and continue if (currentPlayer.getPoints() >= goal) noWinnerYet = false; else if (currentPlayer == computer) currentPlayer = human; else currentPlayer = computer; } announceWinner(); } //----------------------------------------------------------------- // Plays one player's complete turn, allowing the player to // roll the dice multiple times. //----------------------------------------------------------------- private void takeTurn() { boolean stillRolling = true; System.out.println("****************************************"); System.out.println("Current Status:"); System.out.println(" Computer: " + computer.getPoints()); System.out.println(" You: " + human.getPoints()); while (stillRolling) stillRolling = currentPlayer.roll(dice, goal); } //----------------------------------------------------------------- // Announces the winner and prints the results. //----------------------------------------------------------------- private void announceWinner() { System.out.println(); if (currentPlayer == computer) System.out.println("The computer has won!"); else System.out.println("Congratulations, you have won!"); System.out.println(); System.out.println("Final Results:"); System.out.println(" Computer: " + computer.getPoints()); System.out.println(" You: " + human.getPoints()); } }
Explanation / Answer
Please find my comments for each functions and important functionalities
public class PigGame {
private int goal;
private PairOfDice dice;
private PigPlayer computer, human, currentPlayer;
//----------------------------------------------------------------- // Sets up the game including the point goal for the game.
//-----------------------------------------------------------------
//Initializes the target
public PigGame(int target) {
goal = target;
//initializes dice
dice = new PairOfDice();
//initializes computer player. 20 is hardcoded for computer while for human it is input
computer = new PigPlayer(20);
//initializes human player
human = new PigPlayer(PigPlayer.ASK);
}
//----------------------------------------------------------------- // This method contains the primary game loop, which terminates // only once a player has won the game. //-----------------------------------------------------------------
public void play() {
boolean noWinnerYet = true;
currentPlayer = computer;
while (noWinnerYet) {//if winner is not declared then keep playing
takeTurn(); //turn change between computer and human
// Check for winner; otherwise switch players and continue
if (currentPlayer .getPoints() >= goal) noWinnerYet = false;
else if (currentPlayer == computer) currentPlayer = human;
else currentPlayer = computer;
}
announceWinner();
}
//-----------------------------------------------------------------
// Plays one player's complete turn, allowing the player to // roll the dice multiple times.
//-----------------------------------------------------------------
private void takeTurn() {
boolean stillRolling = true;
System.out.println("****************************************");
System.out.println("Current Status:");
System.out.println(" Computer: " + computer.getPoints());
System.out.println(" You: " + human.getPoints());
while (stillRolling) stillRolling = currentPlayer.roll(dice, goal);
}
//----------------------------------------------------------------- // Announces the winner and prints the results. //-------------------------------------------//----------------------
//Displays the winner and points collected by each player.
private void announceWinner() {
System.out.println();
if (currentPlayer == computer) System.out.println("The computer has won!");
else System.out.println("Congratulations, you have won!");
System.out.println();
System.out.println("Final Results:");
System.out.println(" Computer: " + computer.getPoints());
System.out.println(" You: " + human.getPoints());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.