Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We are making a Pigdice game without Loops and Arrays . I am stuck on how to get everything working. Im not worried about the

We are making a Pigdice game without Loops and Arrays. I am stuck on how to get everything working. Im not worried about the GUIDriver but please help on the other classes.

These are the classes I have to use:

image text in transcribed

This is the algorithm of what each class has to do:

GUIDriver Class:

processButtonPress method

input: ActionEvent

output: none

algorithm: if the game is not over, calls the beAPig method for the current player with the value of true if the human rollbutton is the ActionEvent, or fals otherwise. Next, calls the playGame method and updates the GUI components accordingly.

Game Class:

Game constructor method

Input: none

Output: none

Algorithm:creates the two players and a pigDice, initializes the current player to start the game and sets game over conditions to false.

playGame method

input: none

output: boolean: true if currentPlayer changes, false otherwise

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. Return true if the currentPlayer changes, false otherwise.

gameOver method

input: none

output: boolean: true if the game is over, false otherwise

algorithm: if either players current round points + accumulated game points is greater or equal to the winning score, sets the value of a game over to true ( false otherwise) and returns the result.

Player Class:

takeTurn method

input: PigDice

output: none

Algorithm: if the players pigness is true rolls the PigDice and sets the Players roundPoints, gamePoints and pigness according to the roll results, otherwise adds the roundPoints to the gamePoints and zeros out the roundPoints.

Human Class:

beAPig method

input: Boolean pigness

output: Boolean value of pigness

algorithm: sets the value of the pigness variable to the input parameter and returns the same value

Computer Class:

beAPig method

input: Boolean pigness

output: Boolean value of pigness

sets the value of the pigness variable according to the AI rules for a computer player and ignores the input value

This is what I have done so far:

Die.java:

//*********************************************************************

// Die.java Author: Lewis/Loftus/DuVall-Early Updated 3/21/16

//

// Represents one die (singular of dice) with faces showing values

// between 1 and 6.

//*********************************************************************

import javafx.scene.image.Image;

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;

}

//*********************************************************************

// Image accessor

//*********************************************************************

public Image getDieImage()

{

Image dieImage = null; //image assigned from DiceImages files based on faceValue of Die

switch (faceValue)

{

case 1: dieImage = new Image("1.png");

break;

case 2: dieImage = new Image("2.png");

break;

case 3: dieImage = new Image("3.png");

break;

case 4: dieImage = new Image("4.png");

break;

case 5: dieImage = new Image("5.png");

break;

case 6: dieImage = new Image("6.png");

break;

}

return dieImage;

}

//*********************************************************************

// Returns a string representation of this die.

//*********************************************************************

public String toString()

{

String result = Integer.toString(faceValue);

return result;

}

}

Computer.java:

public class Computer extends Player { public Computer(String name) { super(name); } }

Game.java:

public class Game

{

private Player p1, p2, current;

private PigDice d;

private boolean GameOver= true;

private final int WINNING_SCORE=100;

public void Game()

{

current = p1;

}

public void playGame()

{

}

public void gameOver()

{

}

public void getCurrentPlayer()

{

return;

}

public void getHuman()

{

return;

}

public void getComputer()

{

return;

}

public void getPigDice()

{

}

public String toString()

{

return null;

}

}

GameTester.java:

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()));

}

}

Human.java:

import java.util.Scanner;

public class Human extends Player

{

public Human(String n)

{

super(n);

System.out.println("Would you like to roll again [y]? ");

}

public boolean beAPig()

{

return true;

}

}

PairOfDice.java:

//****************************************************************************** // PairOfDice.java Author: DuVall-Early 3/21/16 // // Aggregates 2 Die objects to represent a pair of dice //****************************************************************************** import javafx.scene.image.Image;

public class PairOfDice { private Die die1; private Die die2;

//****************************************************************************** //Create a new pair of dice //****************************************************************************** public PairOfDice() { die1 = new Die(); die2 = new Die(); }

//****************************************************************************** //Get faceValue for die1 //****************************************************************************** public int getDie1() { return die1.getFaceValue(); }

//****************************************************************************** //Get faceValue for die2 //****************************************************************************** public int getDie2() { return die2.getFaceValue(); }

//****************************************************************************** //Set faceValue for die1 //****************************************************************************** public void setDie1(int value) { die1.setFaceValue(value); }

//****************************************************************************** //Set faceValue for die2 //****************************************************************************** public void setDie2(int value) { die2.setFaceValue(value); }

//****************************************************************************** // Returns the image for the first Die object //****************************************************************************** public Image getDie1Image() { return die1.getDieImage(); }

//****************************************************************************** // Returns the image for the Second Die object //****************************************************************************** public Image getDie2Image() { return die2.getDieImage(); }

//****************************************************************************** //Roll the pair of dice //****************************************************************************** public int roll() { int val1 = die1.roll(); int val2 = die2.roll();

return val1 + val2; } //****************************************************************************** // Returns a string representation //****************************************************************************** public String toString() { return "Die 1: " + die1.toString() + " Die 2: " + die2.toString() + " Total: " + (die1.getFaceValue() + die2.getFaceValue()); } }

PigDice.java:

//******************************************************************** // PigDice.java Author: DuVall-Early 3/21/16 // // Extends PairOfDice class to customize roll and toString methods for Pig Game //******************************************************************** public class PigDice extends PairOfDice {

//******************************************************************** //Overrides the PairOfDice roll method to provide a pig sum //******************************************************************** public int roll() { int result = super.roll(); if ((result != 2) && (super.getDie1() == 1 || super.getDie2() == 1)) result = 3; return result; }

//******************************************************************** //Overrides the toString method to use the adjusted pig sum //******************************************************************** public String toString() { int sum = super.getDie1() + super.getDie2(); sum = (sum!=2 && (super.getDie1() == 1 || super.getDie2() == 1)? 3 : sum); String state = "Die 1: " + super.getDie1() + " Die 2: " + super.getDie2() + " Sum: " + sum; return state; } }

Player.java:

import java.lang.String;

public abstract class Player

{

protected int roundPts;

protected int gamePts;

protected String name;

protected boolean pigness;

public Player(String n)

{

name = n;

}

public String getName()

{

return name;

}

public int getRoundPts()

{

return roundPts;

}

public int getGamePts()

{

return gamePts;

}

public boolean getPigness()

{

return pigness;

}

public void setPigness()

{

}

public void takeTurn(PigDice p)

{

if (pigness == true)

{

p.roll();

getGamePts

return roundPts;

else

{

gamePts = gamePts + roundPts;

}

}

}

@Override

public String toString()

{

return ("Points earned this round " + roundPts) +

("Total is" + (roundPts + gamePts));

}

public void setGamePts(int Val)

{

gamePts += Val;

}

public void setRoundPts(int Val)

{

roundPts += Val;

}

public abstract boolean beAPig();

}

Project! C src ? (default package) v Computerjava Diejava Gamejava GameTester.java GuiDriver.java Human.java PairOfDice,java PigDice.java Player java E PK1 ?1.png ?2.png 3.png 4.png ?5.png 6.png Project! C src ? (default package) v Computerjava Diejava Gamejava GameTester.java GuiDriver.java Human.java PairOfDice,java PigDice.java Player java E PK1 ?1.png ?2.png 3.png 4.png ?5.png 6.png

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions