Question: My 2 nd take trying to get help with this, the first answer I got said to add PairOfDice class, which I had I just
My 2 nd take trying to get help with this, the first answer I got said to add PairOfDice class, which I had I just forgot to add it to the listed code the first time around. So, I have added that class and my question remains the same, Please help. GUI is not compiling without error, and my other classes have been working properly and follow my UML. Is it a import or inherritance problem?? I am not sure what I am missing, I think it is something basic, I just am too green to find it.
JAVA, I keep getting errors in my GUIDriver? Not sure what I am doing wrong but compile errors keep happening. I am using eclipse IDE. I will list all my other class code. Everything but my GUI has been tested and is working correct. I also will put my gameTester at bottom of pertinent code. I think maybe there is a problem between my player class to GUI or maybe my Game class to GUI ?
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
//**********************************************************************
// GUIDriver.java Author: 3-15-18
//
// Demonstrates JavaFX pushButton and Die Images
//**********************************************************************
public class GUIDriver extends Application
{
private Text sumText;
private PigDice p;
private ImageView die1View, die2View;
private Game g;
private Button rollButton, passButton, continueButton, closeAndProceed;
private Text humanGameboard, compGameboard, currentPlayerIs, pigGreed;
private Scene scene, scene2;
private Stage primaryStage;
//------------------------------------------------------------------
// Presents GUI containing two dice images, a roll button, and label
// that displays the results of Die roll
//------------------------------------------------------------------
public void start(Stage primaryStage)
{
p = new PigDice();
g = new Game();
sumText = new Text ("Sum is 2");
Image pig1 = p.getDie1Image();
Image pig2 = p.getDie2Image();
die1View = new ImageView(pig1);
die2View = new ImageView(pig2);
rollButton = new Button("Be a Pig, roll dice!");
rollButton.setOnAction(this::processButtonPress);
passButton = new Button ("Complete Turn");
passButton.setOnAction(this::processButtonPress);
continueButton = new Button ("Continue rolling");
continueButton.setOnAction(this::processButtonPress);
continueButton.setDisable(g.getCurrentPlayer() == g.getHuman());
closeAndProceed = new Button ("Close window and proceed with game");
closeAndProceed.setOnAction(this::processButtonPress);
humanGameboard = new Text(g.getHuman().toString());
compGameboard = new Text(g.getComputer().toString());
currentPlayerIs = new Text(g.getComputer().getName());
pigGreed = new Text("You have broken the 8th Cardinal sin of Pig Greed! ");
FlowPane pane = FlowPane(humanGameboard, compGameboard, currentPlayerIs);
pane.setStyle("-fx-background-color: blue");
pane.setAlignment(javafx.geometry.Pos.CENTER);
pane.setHgap(20);
pane.setVgap(40);
FlowPane pane2 = new FlowPane(die1View, die2View, sumText);
pane2.setStyle("-fx-background-color: green");
pane2.setAlignment(javafx.geometry.Pos.CENTER);
pane2.setHgap(20);
pane2.setVgap(40);
//---------------------------------------------------------------
// Setting scene for Pigger Game
//---------------------------------------------------------------
Scene scene = new Scene(pane, 1250, 700);
//Scene scene2 = new Scene(pane2, 1250, 700);
primaryStage.setTitle("Pigger Game");
primaryStage.setScene(scene);
primaryStage.show();
}
//private FlowPane FlowPane(Text humanGameboard2, Text compGameboard2, Text currentPlayerIs2) {
// TODO Auto-generated method stub
//return null;
private FlowPane FlowPane(Text humanGameboard2, Text compGameboard2, Text currentPlayerIs2) {
// TODO Auto-generated method stub
return null;
}
//--------------------------------------------------------------------
// Updates the Images and Die1, Die2, and Sum totals
//--------------------------------------------------------------------
public void processButtonPress(ActionEvent event)
{
p.roll();
sumText.setText(" Results are: " + p.toString());
die1View.setImage(p.getDie1Image());
die2View.setImage(p.getDie2Image());
}
//-------------------------------------------------------------------
// optional depending on IDE
//-------------------------------------------------------------------
public void processButtonPress1(ActionEvent event)
{
if(!g.gameOver())
{
if(rollButton == event.getSource())
{
g.getCurrentPlayer().beAPig(true);
}
else
{
g.getCurrentPlayer().beAPig(false);
}
}
else
{
if(closeAndProceed != event.getSource())
{
primaryStage.setScene(scene2);
}
if(closeAndProceed == event.getSource())
{
primaryStage.setScene(scene);
}
}
g.playGame();
currentPlayerIs.setText(g.getCurrentPlayer().getName());
humanGameboard.setText(g.getHuman().toString());
compGameboard.setText(g.getComputer().toString());
sumText.setText((g.getPigDice().toString()));
die1View.setImage((g.getPigDice().getDie1Image()));
die2View.setImage((g.getPigDice().getDie2Image()));
rollButton.setDisable(g.getCurrentPlayer() == g.getComputer());
passButton.setDisable(g.getCurrentPlayer() == g.getComputer());
continueButton.setDisable(g.getCurrentPlayer() == g.getHuman());
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
PlayerClass
---------------------------------------------------------------------------------------------------------------------------------------------------
//*************************************************************************************
// 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 + "}";
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GameClass
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 "";
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HumanClass
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//*********************************************************************
//
//
//*********************************************************************
public class Human extends Player
{
//-----------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------
public Human(String name)
{
super(name);
}
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
public boolean beAPig(boolean isPig)
{
pigness = isPig;
return isPig;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
ComputerClass
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/*********************************************************************
// 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;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------
PigDice Class
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
//*******************************************************************************
// PigDice.java Author: 3-14-18
//
//*******************************************************************************
public class PigDice extends PairOfDice
{
//---------------------------------------------------------------------------
// Constructor PigDice
//---------------------------------------------------------------------------
public PigDice()
{
super();
}
//--------------------------------------------------------------------------
// Inheritance from PairOfDice
// Adjust the roll to output 3 everytime a 1 is rolled with any
// other number except one. Snake eyes produces sum: 2
//--------------------------------------------------------------------------
public int roll()
{
int sum = super.roll();
if (((getDie1()==1) && (getDie2()!=1)) || ((getDie2()==1) && (getDie1()!=1)))
sum = 3;
return sum;
}
//----------------------------------------------------------------------------
//
//
//----------------------------------------------------------------------------
public String toString()
{
int sum = getDie1() + getDie2();
if (((getDie1()==1) && (getDie2()!=1)) || ((getDie2()==1) && (getDie1()!=1)))
sum = 3;
return "\tDie 1: " + getDie1()
+ " \tDie 2: " + getDie2()
+ " \tSum: " + sum;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
Die Class
--------------------------------------------------------------------------------------------------------------------------------------------------------
import javafx.scene.image.Image;
//********************************************************************
// Die.java 3/13/2018
//
// Represents one die (singular of dice) with faces showing values
// between 1 and 6.
//********************************************************************
public class Die
{
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
//-----------------------------------------------------------------
// Constructor: Sets the initial face value.
//-----------------------------------------------------------------
public Die()
{
faceValue = 1;
}
//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Face value mutator.
//-----------------------------------------------------------------
public void setFaceValue (int value)
{
faceValue = value;
}
//-----------------------------------------------------------------
// Face value accessor.
//-----------------------------------------------------------------
public int getFaceValue()
{
return faceValue;
}
//-----------------------------------------------------------------
// Returns a string representation of this die.
//-----------------------------------------------------------------
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
//----------------------------------------------------------------
// getter of 6 die images from file
// PairOfDice program, aggregation to get images form Die Program
//----------------------------------------------------------------
public Image getDieImage()
{
Image pic = null;
switch (faceValue)
{
case 1:
pic = new Image("die1.png");
break;
case 2:
pic = new Image("die2.png");
break;
case 3:
pic = new Image("die3.png");
break;
case 4:
pic = new Image("die4.png");
break;
case 5:
pic = new Image("die5.png");
break;
case 6:
pic = new Image("die6.png");
break;
}
return pic ;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
GameTester
----------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class GameTester
{
public static void main(String[] args)
{
Game g = new Game();
Scanner s = new Scanner(System.in);
while (!g.gameOver())
{
System.out.println(g.getCurrentPlayer().getName() + "'s turn");
System.out.print("\tBe a Pig (Y/N)? ");
g.getCurrentPlayer().beAPig(s.nextLine().toUpperCase().equals("Y"));
g.playGame();
System.out.println("Dice: " + g.getPigDice().toString());
System.out.println(g.getHuman().toString());
System.out.println(g.getComputer().toString());
}
System.out.println("The winner is "
+ (g.getHuman().getGamePoints() > g.getComputer().getGamePoints() ? g.getHuman().getName() : g.getComputer().getName()));
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
PairOfDice
------------------------------------------------------------------------------------------------------------------------------------------
import javafx.scene.image.Image;
//********************************************************************
// PairOfDice.java Author: 3/14/2018
//
// Represents a Pair of Dice that aggregates two Die objects
//********************************************************************
public class PairOfDice
{
//Class level variables
private Die die1, die2;
//-----------------------------------------------------------------
// Constructor: Instantiates the two Die objects and stores them
// as class level variables
//-----------------------------------------------------------------
public PairOfDice()
{
die1 = new Die();
die2 = new Die();
}
//-----------------------------------------------------------------
// die 1 value accessor (getter)
//-----------------------------------------------------------------
public int getDie1()
{
return die1.getFaceValue();
}
//-----------------------------------------------------------------
// die 2 value accessor (getter)
//-----------------------------------------------------------------
public int getDie2()
{
return die2.getFaceValue();
}
//-----------------------------------------------------------------
// die 1 value mutator (setter)
//-----------------------------------------------------------------
public void setDie1(int val)
{
die1.setFaceValue(val);
}
//-----------------------------------------------------------------
// die 2 value mutator (setter)
//-----------------------------------------------------------------
public void setDie2(int val)
{
die2.setFaceValue(val);
}
//-----------------------------------------------------------------
// Rolls both of the die objects and returns the sum of the roll
//-----------------------------------------------------------------
public int roll()
{
return die1.roll() + die2.roll();
}
//-----------------------------------------------------------------
// Returns a string representation of this PairOfDice object with
// the values labeled appropriately
// Overrides the toString method inherited from Object
//-----------------------------------------------------------------
public String toString()
{
return "Die 1: " + die1.toString()
+ " Die 2: " + die2.toString()
+ " \tSum: " + (die1.getFaceValue() + die2.getFaceValue());
}
//-----------------------------------------------------------------
// Image die1 (getter), from Die program aggregation
// PigDice program will use inheritance from PairOfDice program
//-----------------------------------------------------------------
public Image getDie1Image()
{
return die1.getDieImage();
}
//-----------------------------------------------------------------
// Image die2 (getter), from Die program aggregation
// PigDice program will use inheritance from PairOfDice program
//-----------------------------------------------------------------
public Image getDie2Image()
{
return die2.getDieImage();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
