Java, need help with this gameOver method //------------------------------------
ID: 3741725 • Letter: J
Question
Java, need help with this gameOver method
//---------------------------------------------------------------------------------------
if either player's current round points + accumulated game points is greater or equal to the winning score,
sets the value of game over to true (false otherwise) and returns the result.
//---------------------------------------------------------------------------------------
public boolean gameOver()
{
if(roundPoints + gamePoints >= WINNING_SCORE)
{
gameOver return true;
}
return gameOver;
}
Full code:
import java.util.Scanner;
//******************************************************************************************
//
//
//******************************************************************************************
public class Game
{
private static final String WINNER_SCORE = null;
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
private Player p1, p2, current;
private PigDice d;
private boolean gameOver;
private final int WINNING_SCORE = 100;
//---------------------------------------------------------------------------------------
//creates the two players and a pigDice, initializes the current player to start the game
//and sets game over condition to false.
//---------------------------------------------------------------------------------------
public Game()
{
Scanner scan = new Scanner(System.in);
p1 = new Player("Player")
{
@Override
public boolean beAPig(boolean isPig)
{
return false;
}
};
p2 = new Player("Computer")
{
@Override
public boolean beAPig(boolean isPig)
{
return false;
}
};
current = p1;
}
//---------------------------------------------------------------------------------------
// if the game is not over, has the current player take its turn.
// If the current players pigness value is false after the turn, switches the
// current player.
//---------------------------------------------------------------------------------------
public boolean playGame()
{
if(!gameOver)
{
current.takeTurn(d);
if(!current.getPigness())
{
if(current == p1)
{
current = p2;
return true;
}
else
{
current = p1;
return true;
}
}
}
return false;
}
//---------------------------------------------------------------------------------------
// if either player's current round points + accumulated game points is
// greater or equal to the winning score,
// sets the value of game over to true (false otherwise) and returns the result.
//---------------------------------------------------------------------------------------
public boolean gameOver()
{
if(roundPoints + gamePoints >= WINNING_SCORE)
{
gameOver return true;
}
return gameOver;
}
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
public Player getCurrentPlayer()
{
return p1;
}
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
public Player getHuman()
{
return p2;
}
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
public Player getComputer()
{
return getComputer();
}
//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
public PigDice getPigDice()
{
return d;
}
//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
public String toString()
{
return "Game{" +
"p1=" + p1 +
",p2=" + p2 +
",current=" + current +
", d=" + d +
",gameOver=" + gameOver +
",WINNER_SCORE=" + WINNER_SCORE + "}";
}
}
Explanation / Answer
import java.util.Scanner; //****************************************************************************************** // // //****************************************************************************************** public class Game { private static final String WINNER_SCORE = null; //--------------------------------------------------------------------------------------- // //--------------------------------------------------------------------------------------- private Player p1, p2, current; private PigDice d; private boolean gameOver; private final int WINNING_SCORE = 100; //--------------------------------------------------------------------------------------- //creates the two players and a pigDice, initializes the current player to start the game //and sets game over condition to false. //--------------------------------------------------------------------------------------- public Game() { Scanner scan = new Scanner(System.in); p1 = new Player("Player") { @Override public boolean beAPig(boolean isPig) { return false; } }; p2 = new Player("Computer") { @Override public boolean beAPig(boolean isPig) { return false; } }; current = p1; } //--------------------------------------------------------------------------------------- // if the game is not over, has the current player take its turn. // If the current players pigness value is false after the turn, switches the // current player. //--------------------------------------------------------------------------------------- public boolean playGame() { if(!gameOver) { current.takeTurn(d); if(!current.getPigness()) { if(current == p1) { current = p2; return true; } else { current = p1; return true; } } } return false; } //--------------------------------------------------------------------------------------- // if either player's current round points + accumulated game points is // greater or equal to the winning score, // sets the value of game over to true (false otherwise) and returns the result. //--------------------------------------------------------------------------------------- public boolean gameOver() { if(current.getGamePoints() + current.getRoundPoints() >= WINNING_SCORE) { gameOver = true; } return gameOver; } //--------------------------------------------------------------------------------------- // //--------------------------------------------------------------------------------------- public Player getCurrentPlayer() { return p1; } //--------------------------------------------------------------------------------------- // //--------------------------------------------------------------------------------------- public Player getHuman() { return p2; } //--------------------------------------------------------------------------------------- // //--------------------------------------------------------------------------------------- public Player getComputer() { return getComputer(); } //-------------------------------------------------------------------------------------- // //-------------------------------------------------------------------------------------- public PigDice getPigDice() { return d; } //-------------------------------------------------------------------------------------- // //-------------------------------------------------------------------------------------- public String toString() { return "Game{" + "p1=" + p1 + ",p2=" + p2 + ",current=" + current + ", d=" + d + ",gameOver=" + gameOver + ",WINNER_SCORE=" + WINNER_SCORE + "}"; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.