annotate it line by line with an explanation of what each line does. Do NOT comm
ID: 3761286 • 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 PigPlayer { public final static int ASK = -1; // prompt for round termination private int total; // total points accumulated in game private int round; // points accumulated in current round private int limit; // pass tolerance //----------------------------------------------------------------- // Initializes the point accumulators to zero, and the round // limit as specified. //----------------------------------------------------------------- public PigPlayer(int max) { total = 0; round = 0; limit = max; } //----------------------------------------------------------------- // Rolls the dice once and deals with the results. Returns true // if the player should roll again and false otherwise. The // player will not roll again if he busts or wins, or if his // round limit is reached. //----------------------------------------------------------------- public boolean roll(PairOfDice dice, int goal) { boolean rollAgain = true; dice.roll(); System.out.println (dice); int die1 = dice.getDie1FaceValue(); int die2 = dice.getDie2FaceValue(); if (die1 == 1 || die2 == 1) { System.out.println("Busted!!!"); rollAgain = false; round = 0; if (die1 == 1 && die2 == 1) total = 0; } else { round += die1 + die2; System.out.println("Current Round: " + round); System.out.println("Potential Total: " + (total+round)); if ((total+round) >= goal) rollAgain = false; else if (limit == ASK) { System.out.print("Take another turn (y/n)? "); Scanner scan = new Scanner(System.in); String again = scan.nextLine(); rollAgain = again.equalsIgnoreCase("y"); } else if (round >= limit) rollAgain = false; if (! rollAgain) { total += round; round = 0; } } return rollAgain; } //----------------------------------------------------------------- // Returns the total number of points accumulated by this player. //----------------------------------------------------------------- public int getPoints() { return total; } }
Explanation / Answer
Answer:
Note: line by line annotated code
Code:
/*initial player class*/
public class PigPlayer
{
public final static int ASK = -1;
// prompt for round termination
private int total;
// total points accumulated in game
private int round;
// points accumulated in current round
private int limit;
/* pass tolerance Initializes the point accumulators to zero, and the round
limit as specified. */
public PigPlayer(int max)
{
total = 0;
round = 0;
limit = max;
}
/*
Rolls the dice once and deals with the results. Returns true if the player should roll again and false otherwise. The player will not roll again if he busts or wins, or if his round limit is reached. */
public boolean roll(PairOfDice dice, int goal)
{
boolean rollAgain = true;
dice.roll();
System.out.println (dice);
int die1 = dice.getDie1FaceValue();
int die2 = dice.getDie2FaceValue();
if (die1 == 1 || die2 == 1)
{
System.out.println("Busted!!!");
rollAgain = false;
round = 0;
if (die1 == 1 && die2 == 1)
total = 0;
}
else
{
round += die1 + die2;
System.out.println("Current Round: " + round);
System.out.println("Potential Total: " +
(total+round));
if ((total+round) >= goal)
rollAgain = false;
else if (limit == ASK)
{
/*getting user input to take a chance or not*/
System.out.print("Take another turn (y/n)? ");
Scanner scan = new Scanner(System.in);
String again = scan.nextLine();
rollAgain = again.equalsIgnoreCase("y");
}
else if (round >= limit)
rollAgain = false;
if (! rollAgain)
{
/*once the dice not rolls again it is calculating the total*/
total += round; round = 0;
}
}
return rollAgain;
}
/* Returns the total number of points accumulated by this player. */
public int getPoints()
{
return total;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.