Java, Need help writing this method, plus any tips on how you put it together wo
ID: 3741344 • Letter: J
Question
Java, Need help writing this method, plus any tips on how you put it together would be great. Below is the section I need help with. Also, the full Game and Player classes
//---------------------------------------------------------------------------------------
// algorithm: 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. Returns true if the currentPlayer changes, false otherwise.
//---------------------------------------------------------------------------------------
public boolean playGame()
{
if(gameOver == false)
do
{
currentPlayer = takeTurn;
if(currentPlayer == )
}
return gameOver;
}
*****************************************************************************************************
Game Class
****************************************************************************************************
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;
}
//---------------------------------------------------------------------------------------
// algorithm: 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. Returns true if the currentPlayer changes, false otherwise.
//---------------------------------------------------------------------------------------
public boolean playGame()
{
if(gameOver == false)
do
{
currentPlayer = takeTurn;
if(currentPlayer == )
}
return gameOver;
}
//---------------------------------------------------------------------------------------
// 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()
{
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 + "}";
}
}
*********************************************************************************************************************
Player Class
**********************************************************************************************************************
//*************************************************************************************
// Player.java Author: 3-31-18
//
//*************************************************************************************
public abstract class Player
{
//---------------------------------------------------------------------------------
// declaring protected variables
//---------------------------------------------------------------------------------
protected String name;
protected int roundPoints, gamePoints;
protected boolean pigness;
//---------------------------------------------------------------------------------
// constructor for name
//---------------------------------------------------------------------------------
public Player(String name)
{
this.name = name;
}
//---------------------------------------------------------------------------------
// getter name
//---------------------------------------------------------------------------------
public String getName()
{
return name;
}
//---------------------------------------------------------------------------------
// getter RoundPoints
//---------------------------------------------------------------------------------
public int getRoundPoints()
{
return roundPoints;
}
//---------------------------------------------------------------------------------
// getter GamePoints
//---------------------------------------------------------------------------------
public int getGamePoints()
{
return gamePoints;
}
//---------------------------------------------------------------------------------
// getter boolean Pigness
//---------------------------------------------------------------------------------
public boolean getPigness()
{
return pigness;
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
public void takeTurn(PigDice p)
{
{
if(this.pigness)
{
this.roundPoints += p.roll();
this.pigness = false;
}
else
{
this.gamePoints += this.roundPoints;
this.roundPoints = 0;
}
}
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
public abstract boolean beAPig(boolean isPig);
@Override
public String toString()
{
return "Player{" +
"name = " + name +
", roundPoints =" + roundPoints +
", gamePoints =" + gamePoints +
", pigness =" + pigness + "}";
}
}
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; } //--------------------------------------------------------------------------------------- // algorithm: 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. Returns true if the currentPlayer changes, false otherwise. //--------------------------------------------------------------------------------------- public boolean playGame() { if(!gameOver) { current.takeTurn(d); if(!current.isPigness()) { 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() { 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.