Question: In JAVA, Write a PigDice class that extends the PairOfDice class and also build a GUI around your project so that the user can repeatedly
In JAVA, Write a PigDice class that extends the PairOfDice class and also build a GUI around your project so that the user can repeatedly roll the dice and receive a dice visualization and "Pigger" result of each roll.
I have tried to write the code for the GUI as follow :
//***************************************************************************
//GUIDriver.java Author: Sam
//
//Represents two dice that inherit the methods of PairofDice.java.
//Represents the rule that the sum is 3 if only one of the dice lands on one.
//***************************************************************************
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import javafx.scene.image.Image;
public class GUIDriver extends Application{
private Text sumText, humScore, compScore;
private Game g;
private ImageView die1View, die2View;
private Button pig, pug, porg;
//--------------------------------------------------------------------------------------
// Constructor: creates the Stage, ImageView, FlowPane, etc. as
// well as initializing p, sumText, & the images pig1 and pig2.
//--------------------------------------------------------------------------------------
public void start(Stage primaryStage){
g = new Game();
sumText = new Text("Sum is: 0 ");
humScore = new Text (g.getHuman().toString());
compScore = new Text (g.getComputer().toString());
Image pig1 = g.getPigDice().getDie1Image();
Image pig2 = g.getPigDice().getDie2Image();
die1View = new ImageView(pig1);
die2View = new ImageView(pig2);
//----------------------------------------------------------------------------------
// Creates the Button pig, and tells pig to do an action if the button is pressed.
//----------------------------------------------------------------------------------
pig = new Button ("Roll/ Be a Pig");
pig.setOnAction(this::processButtonPress);
//----------------------------------------------------------------------------------
// Creates the Button pug, and tells pug to do an action if the button is pressed.
//----------------------------------------------------------------------------------
pug = new Button ("Pass Turn");
pug.setOnAction(this::processButtonPress);
//----------------------------------------------------------------------------------
// Creates the Button porg, and tells porg to do an action if the button is pressed.
//----------------------------------------------------------------------------------
porg = new Button ("Continue");
porg.setOnAction(this::processButtonPress);
//----------------------------------------------------------------------------------
// Creates the FlowPane pane and customizes its position, ImageViews, colors, etc.
//----------------------------------------------------------------------------------
FlowPane pane = new FlowPane(die1View, die2View, pig, pug, porg, sumText, humScore, compScore);
pane.setPrefWrapLength(100);
pane.setAlignment(Pos.CENTER);
pane.setHgap(10);
pane.setStyle("-fx-background-color: white");
//----------------------------------------------------------------------------------
// Creates the Scene scene and sets the title & scene, then shows it.
//----------------------------------------------------------------------------------
Scene scene = new Scene(pane, 400, 300);
primaryStage.setTitle("Pigger");
primaryStage.setScene(scene);
primaryStage.show();
}
//--------------------------------------------------------------------------------------
// Tells the Button to print sumText and the die1View/die2Views when it is pressed.
//--------------------------------------------------------------------------------------
public void processButtonPress(ActionEvent event){
if(!g.gameOver()){
if(pig == event.getSource()){
g.getCurrentPlayer().beAPig(true);
}else{
g.getCurrentPlayer().beAPig(false);
}
if(g.playGame() == true){
}
die1View.setImage(g.getPigDice().getDie1Image());
die2View.setImage(g.getPigDice().getDie2Image());
sumText.setText("Sum is: "+g.getPigDice().toString());
humScore.setText(g.getHuman().toString());
compScore.setText(g.getComputer().toString());
}
}
public static void main(String[] args){
launch(args);
}
}
Depended JAVA files are below:
Player.java
//*************************************************************************************
// 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;
roundPoints = 0;
gamePoints = 0;
pigness = true;
}
//---------------------------------------------------------------------------------
// 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(pigness)
{
int points = p.roll();
switch (points)
{
case 2:
{
gamePoints = 0;
}
case 3:
{
roundPoints = 0;
pigness = false;
break;
}
default:
{
roundPoints += points;
pigness = true;
}
}
}
else
{
gamePoints = gamePoints + roundPoints;
roundPoints = 0;
pigness = false;
}
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
public abstract boolean beAPig(boolean isPig);
@Override
public String toString()
{
return "Player{" +
"name = " + name +
", roundPoints =" + roundPoints +
", gamePoints =" + gamePoints +
", pigness =" + pigness + "}";
}
}
Comp.java :
//*********************************************************************
// Computer class
//
//*********************************************************************
public class Computer extends Player
{
//-----------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------
public Computer(String name)
{
super(name);
}
//-----------------------------------------------------------------
// rule of 20
//-----------------------------------------------------------------
public boolean beAPig(boolean isPig)
{
if(roundPoints < 20)
{
pigness = true;
}
else
{
gamePoints = gamePoints + roundPoints;
roundPoints = 0;
pigness = false;
}
return pigness;
}
}
Game.java
import java.util.Scanner;
//******************************************************************************************
//
//
//******************************************************************************************
public class Game
{
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
private Player p1, p2, current;
private PigDice d;
private boolean gameOver;
private final int WINNING_SCORE;
//---------------------------------------------------------------------------------------
//creates the two players and a pigDice, initializes the current player to start the game
//and sets game over condition to false.
//---------------------------------------------------------------------------------------
public Game()
{
p1 = new Human("Player");
p2 = new Computer("Computer");
d = new PigDice();
WINNING_SCORE = 100;
gameOver = 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()
{
boolean result = false;
if(!gameOver)
{
current.takeTurn(d);
if(!current.getPigness())
{
if(current == p1)
{
current = p2;
result = true;
}
else
{
current = p1;
result = true;
}
}
}
return result;
}
//---------------------------------------------------------------------------------------
// 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)
{
current.gamePoints = (current.getGamePoints() + current.getRoundPoints());
gameOver = true;
}
return gameOver;
}
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
public Player getCurrentPlayer()
{
return current;
}
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
public Player getHuman()
{
return p1;
}
//---------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
public Player getComputer()
{
return p2;
}
//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
public PigDice getPigDice()
{
return d;
}
//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
public String toString()
{
return "";
}
}
Human.java
//*********************************************************************
//
//
//*********************************************************************
public class Human extends Player
{
//-----------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------
public Human(String name)
{
super(name);
}
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
public boolean beAPig(boolean isPig)
{
pigness = isPig;
return isPig;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
