added old projects
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> Die
|
||||
* <p> Project 3
|
||||
* <p> This class simulates a die
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
//
|
||||
// class to manage the value of a single simulated die
|
||||
//
|
||||
public class Die
|
||||
{
|
||||
private int _pips = 1;
|
||||
private final int _MAX_PIPS = 6;
|
||||
private Random _randNum;
|
||||
|
||||
// constructor that will create a Random class and generate a random start value.
|
||||
public Die()
|
||||
{
|
||||
_randNum = new Random();
|
||||
}
|
||||
|
||||
//
|
||||
// constructor that will create a Random class, set the seed of the RNG,
|
||||
// and generate a random start value.
|
||||
//
|
||||
public Die(int seed)
|
||||
{
|
||||
_randNum = new Random(seed);
|
||||
}
|
||||
|
||||
//
|
||||
// accessor to return the current value of the die.
|
||||
//
|
||||
public int faceValue()
|
||||
{
|
||||
return _pips;
|
||||
}
|
||||
|
||||
//
|
||||
// mutator to randomly change the value of the die.
|
||||
//
|
||||
public int roll()
|
||||
{
|
||||
_pips = _randNum.nextInt(_MAX_PIPS) + 1; // gets the random number for the side of the die
|
||||
return _pips;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> EntryPoint
|
||||
* <p> Project 3
|
||||
* <p> This code holds the main function which starts up the game controller class
|
||||
*/
|
||||
public class EntryPoint
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
//
|
||||
// start up
|
||||
//
|
||||
GameController gc = new GameController();
|
||||
gc.play();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> GameController
|
||||
* <p> Project 3
|
||||
* <p> This class is in charge of running the game.
|
||||
*/
|
||||
|
||||
/*
|
||||
// The rules to the dice game Pig
|
||||
//
|
||||
// Number of Players: 2 +
|
||||
// Game Duration: 30 mins
|
||||
// Players Aged: 6 +
|
||||
//
|
||||
// You will need: 2 dice and paper to score on.
|
||||
//
|
||||
// To Play: The players take turns to roll both dice,
|
||||
// they can roll as many times as they want in one turn.
|
||||
//
|
||||
// A player scores the sum of the two dice thrown and
|
||||
// gradually reaches a higher score as they continue to roll.
|
||||
//
|
||||
// If a single number 1 is thrown on either die, the score
|
||||
// for that whole turn is lost. However a double 1 counts as 25.
|
||||
// The first player to 100 wins unless a player scores more
|
||||
// subsequently in the same round. This means that everyone in
|
||||
// the game must have the same number of turns.
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class GameController {
|
||||
// central method to start and manage game play
|
||||
public void play() {
|
||||
//initialize variables
|
||||
Scanner kb = new Scanner(System.in);
|
||||
PigDice pd1;
|
||||
PigDice pd2;
|
||||
int maxScore = getInitialMax(kb);
|
||||
|
||||
do { //the main loop of the game
|
||||
pd1 = new PigDice(); //makes new PigDice so that the score resets after each game
|
||||
pd2 = new PigDice();
|
||||
while (true) { //loops until there is a winner
|
||||
System.out.println("\nPLAYER 1"); //Player 1's turn
|
||||
takeTurn(kb, pd1);
|
||||
|
||||
System.out.println("\nPLAYER 2"); //Player 2's turn
|
||||
takeTurn(kb, pd2);
|
||||
|
||||
if (pd2.currentTotal() >= maxScore) //checks for a winner
|
||||
break;
|
||||
if (pd1.currentTotal() >= maxScore)
|
||||
break;
|
||||
|
||||
System.out.println("\n\nPLAYER 1: " + pd1.currentTotal() + " -- Player 2: " + pd2.currentTotal() + "\n\n"); // prints totals
|
||||
}
|
||||
System.out.println("\n\nPLAYER 1: " + pd1.currentTotal() + " -- Player 2: " + pd2.currentTotal() + "\n\n");
|
||||
System.out.println("Do you want to play again? (Y/N)");
|
||||
} while (yesResponse(kb)); //gets response from user
|
||||
}
|
||||
|
||||
//
|
||||
// Returns the initial max score (loops until a value between 1 <= score <= 100
|
||||
// is entered)
|
||||
//
|
||||
private int getInitialMax(Scanner kb)
|
||||
{
|
||||
while (true) {
|
||||
System.out.println("What score would you like to play to? (100 max)");
|
||||
int maxScore = Integer.parseInt(kb.nextLine());
|
||||
if (maxScore >= 1 && maxScore <= 100) //checks the input
|
||||
return maxScore;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// method for managing a single session of rolling dice
|
||||
//
|
||||
private void takeTurn(Scanner kb, PigDice pd) {
|
||||
String response;
|
||||
boolean keepRolling = true;
|
||||
|
||||
do {
|
||||
// Roll the dice
|
||||
pd.rollDice();
|
||||
|
||||
// Report the result
|
||||
System.out.println(pd.lastRoll() + " scored " + pd.evaluate() + " points.");
|
||||
|
||||
// Did the player pig out?
|
||||
if (pd.piggedOut()) {
|
||||
System.out.println("You pigged out this turn.");
|
||||
} else {
|
||||
//
|
||||
// Roll again; see if the user wants to roll again to add to total or pass and
|
||||
// keep current points
|
||||
//
|
||||
System.out.println(
|
||||
"Your current roll is " + pd.currentRound() + " points. Keep rolling? Respond (Y/N) only.");
|
||||
|
||||
if (!yesResponse(kb)) {
|
||||
keepRolling = false;
|
||||
int roundScore = pd.save();
|
||||
System.out.printf("Your total for the round was %d and your total score is %d.\r\n", roundScore,
|
||||
pd.currentTotal());
|
||||
}
|
||||
}
|
||||
|
||||
} while (!pd.piggedOut() && keepRolling);
|
||||
}
|
||||
|
||||
//
|
||||
// Returns true if the user enters a 'y' or 'Y'
|
||||
//
|
||||
final String _YES = "Y";
|
||||
public boolean yesResponse(Scanner kb) {
|
||||
return kb.nextLine().substring(0, 1).toUpperCase().equals(_YES);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import javax.lang.model.util.ElementScanner6;
|
||||
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> PigDice
|
||||
* <p> Project 3
|
||||
* <p> This class handles the nitty gritty details of the game.
|
||||
*/
|
||||
|
||||
//
|
||||
// The rules to the dice game Pig
|
||||
//
|
||||
// Number of Players: 2 +
|
||||
// Game Duration: 30 mins
|
||||
// Players Aged: 6 +
|
||||
//
|
||||
// You will need: 2 dice and paper to score on.
|
||||
//
|
||||
// To Play: The players take turns to roll both dice,
|
||||
// they can roll as many times as they want in one turn.
|
||||
//
|
||||
// A player scores the sum of the two dice thrown and
|
||||
// gradually reaches a higher score as they continue to roll.
|
||||
//
|
||||
// If a single number 1 is thrown on either die, the score
|
||||
// for that whole turn is lost. However a double 1 counts as 25.
|
||||
// The first player to 100 wins unless a player scores more
|
||||
// subsequently in the same round. This means that everyone in
|
||||
// the game must have the same number of turns.
|
||||
//
|
||||
|
||||
// this class manages the state of the dice and the scoring
|
||||
public class PigDice
|
||||
{
|
||||
// keep track of total and round scores as well as the two dice.
|
||||
private int _totalScore = 0;
|
||||
private int _roundScore = 0;
|
||||
private Die _die1;
|
||||
private Die _die2;
|
||||
|
||||
public PigDice()
|
||||
{
|
||||
_die1 = new Die(); // makes the new die
|
||||
_die2 = new Die();
|
||||
|
||||
}
|
||||
|
||||
// accessor for total score
|
||||
public int currentTotal()
|
||||
{
|
||||
return _totalScore;
|
||||
}
|
||||
|
||||
// accessor for this round score
|
||||
public int currentRound()
|
||||
{
|
||||
return _roundScore;
|
||||
}
|
||||
|
||||
// accessor to see if the user has rolled a single "1" and loses turn
|
||||
public boolean piggedOut()
|
||||
{
|
||||
if(singleOneRolled()) {
|
||||
_roundScore = 0;
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
// mutator that simulates rolling two dice and evaluating the resulting score
|
||||
public void rollDice()
|
||||
{
|
||||
// Roll the die
|
||||
_die1.roll();
|
||||
_die2.roll();
|
||||
|
||||
// Evalulate
|
||||
_roundScore += evaluate();
|
||||
}
|
||||
|
||||
// accessor for a formatted string of what the last roll looked like
|
||||
public String lastRoll()
|
||||
{
|
||||
return "D1 (" + _die1.faceValue() + "), D2 (" + _die2.faceValue() + ")";
|
||||
}
|
||||
|
||||
public int evaluate()
|
||||
{
|
||||
if(doubleOnesRolled()) //checks if there has been a double 1
|
||||
return 25;
|
||||
else
|
||||
return _die1.faceValue() + _die2.faceValue();
|
||||
}
|
||||
|
||||
private boolean singleOneRolled() // checks if there has been a single 1 rolled
|
||||
{
|
||||
if (_die1.faceValue() == 1 && _die2.faceValue() != 1 || _die2.faceValue() == 1 && _die1.faceValue() != 1) {
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean doubleOnesRolled() // checks if there has been a double 1 rolled
|
||||
{
|
||||
if (_die1.faceValue() == 1 && _die2.faceValue() == 1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// mutator to end a round and keep the add this round to the total
|
||||
// also returns the total value of the round and resets the round total for next time
|
||||
//
|
||||
public int save()
|
||||
{
|
||||
_totalScore += _roundScore;
|
||||
int x = _roundScore;
|
||||
_roundScore = 0;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> Die
|
||||
* <p> Project 3
|
||||
* <p> This class simulates a die
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
//
|
||||
// class to manage the value of a single simulated die
|
||||
//
|
||||
public class Die
|
||||
{
|
||||
private int _pips = 1;
|
||||
private final int _MAX_PIPS = 6;
|
||||
private Random _randNum;
|
||||
|
||||
// constructor that will create a Random class and generate a random start value.
|
||||
public Die()
|
||||
{
|
||||
_randNum = new Random();
|
||||
}
|
||||
|
||||
//
|
||||
// constructor that will create a Random class, set the seed of the RNG,
|
||||
// and generate a random start value.
|
||||
//
|
||||
public Die(int seed)
|
||||
{
|
||||
_randNum = new Random(seed);
|
||||
}
|
||||
|
||||
//
|
||||
// accessor to return the current value of the die.
|
||||
//
|
||||
public int faceValue()
|
||||
{
|
||||
return _pips;
|
||||
}
|
||||
|
||||
//
|
||||
// mutator to randomly change the value of the die.
|
||||
//
|
||||
public int roll()
|
||||
{
|
||||
_pips = _randNum.nextInt(_MAX_PIPS) + 1; // gets the random number for the side of the die
|
||||
return _pips;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> EntryPoint
|
||||
* <p> Project 3
|
||||
* <p> This code holds the main function which starts up the game controller class
|
||||
*/
|
||||
public class EntryPoint
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
//
|
||||
// start up
|
||||
//
|
||||
GameController gc = new GameController();
|
||||
gc.play();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> GameController
|
||||
* <p> Project 3
|
||||
* <p> This class is in charge of running the game.
|
||||
*/
|
||||
|
||||
/*
|
||||
// The rules to the dice game Pig
|
||||
//
|
||||
// Number of Players: 2 +
|
||||
// Game Duration: 30 mins
|
||||
// Players Aged: 6 +
|
||||
//
|
||||
// You will need: 2 dice and paper to score on.
|
||||
//
|
||||
// To Play: The players take turns to roll both dice,
|
||||
// they can roll as many times as they want in one turn.
|
||||
//
|
||||
// A player scores the sum of the two dice thrown and
|
||||
// gradually reaches a higher score as they continue to roll.
|
||||
//
|
||||
// If a single number 1 is thrown on either die, the score
|
||||
// for that whole turn is lost. However a double 1 counts as 25.
|
||||
// The first player to 100 wins unless a player scores more
|
||||
// subsequently in the same round. This means that everyone in
|
||||
// the game must have the same number of turns.
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class GameController {
|
||||
// central method to start and manage game play
|
||||
public void play() {
|
||||
//initialize variables
|
||||
Scanner kb = new Scanner(System.in);
|
||||
PigDice pd1;
|
||||
PigDice pd2;
|
||||
int maxScore = getInitialMax(kb);
|
||||
|
||||
do { //the main loop of the game
|
||||
pd1 = new PigDice(); //makes new PigDice so that the score resets after each game
|
||||
pd2 = new PigDice();
|
||||
while (true) { //loops until there is a winner
|
||||
System.out.println("\nPLAYER 1"); //Player 1's turn
|
||||
takeTurn(kb, pd1);
|
||||
|
||||
System.out.println("\nPLAYER 2"); //Player 2's turn
|
||||
takeTurn(kb, pd2);
|
||||
|
||||
if (pd2.currentTotal() >= maxScore) //checks for a winner
|
||||
break;
|
||||
if (pd1.currentTotal() >= maxScore)
|
||||
break;
|
||||
|
||||
System.out.println("\n\nPLAYER 1: " + pd1.currentTotal() + " -- Player 2: " + pd2.currentTotal() + "\n\n"); // prints totals
|
||||
}
|
||||
System.out.println("\n\nPLAYER 1: " + pd1.currentTotal() + " -- Player 2: " + pd2.currentTotal() + "\n\n");
|
||||
System.out.println("Do you want to play again? (Y/N)");
|
||||
} while (yesResponse(kb)); //gets response from user
|
||||
}
|
||||
|
||||
//
|
||||
// Returns the initial max score (loops until a value between 1 <= score <= 100
|
||||
// is entered)
|
||||
//
|
||||
private int getInitialMax(Scanner kb)
|
||||
{
|
||||
while (true) {
|
||||
System.out.println("What score would you like to play to? (100 max)");
|
||||
int maxScore = Integer.parseInt(kb.nextLine());
|
||||
if (maxScore >= 1 && maxScore <= 100) //checks the input
|
||||
return maxScore;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// method for managing a single session of rolling dice
|
||||
//
|
||||
private void takeTurn(Scanner kb, PigDice pd) {
|
||||
String response;
|
||||
boolean keepRolling = true;
|
||||
|
||||
do {
|
||||
// Roll the dice
|
||||
pd.rollDice();
|
||||
|
||||
// Report the result
|
||||
System.out.println(pd.lastRoll() + " scored " + pd.evaluate() + " points.");
|
||||
|
||||
// Did the player pig out?
|
||||
if (pd.piggedOut()) {
|
||||
System.out.println("You pigged out this turn.");
|
||||
} else {
|
||||
//
|
||||
// Roll again; see if the user wants to roll again to add to total or pass and
|
||||
// keep current points
|
||||
//
|
||||
System.out.println(
|
||||
"Your current roll is " + pd.currentRound() + " points. Keep rolling? Respond (Y/N) only.");
|
||||
|
||||
if (!yesResponse(kb)) {
|
||||
keepRolling = false;
|
||||
int roundScore = pd.save();
|
||||
System.out.printf("Your total for the round was %d and your total score is %d.\r\n", roundScore,
|
||||
pd.currentTotal());
|
||||
}
|
||||
}
|
||||
|
||||
} while (!pd.piggedOut() && keepRolling);
|
||||
}
|
||||
|
||||
//
|
||||
// Returns true if the user enters a 'y' or 'Y'
|
||||
//
|
||||
final String _YES = "Y";
|
||||
public boolean yesResponse(Scanner kb) {
|
||||
return kb.nextLine().substring(0, 1).toUpperCase().equals(_YES);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import javax.lang.model.util.ElementScanner6;
|
||||
|
||||
/*
|
||||
* @Austin Bennett
|
||||
* <p> PigDice
|
||||
* <p> Project 3
|
||||
* <p> This class handles the nitty gritty details of the game.
|
||||
*/
|
||||
|
||||
//
|
||||
// The rules to the dice game Pig
|
||||
//
|
||||
// Number of Players: 2 +
|
||||
// Game Duration: 30 mins
|
||||
// Players Aged: 6 +
|
||||
//
|
||||
// You will need: 2 dice and paper to score on.
|
||||
//
|
||||
// To Play: The players take turns to roll both dice,
|
||||
// they can roll as many times as they want in one turn.
|
||||
//
|
||||
// A player scores the sum of the two dice thrown and
|
||||
// gradually reaches a higher score as they continue to roll.
|
||||
//
|
||||
// If a single number 1 is thrown on either die, the score
|
||||
// for that whole turn is lost. However a double 1 counts as 25.
|
||||
// The first player to 100 wins unless a player scores more
|
||||
// subsequently in the same round. This means that everyone in
|
||||
// the game must have the same number of turns.
|
||||
//
|
||||
|
||||
// this class manages the state of the dice and the scoring
|
||||
public class PigDice
|
||||
{
|
||||
// keep track of total and round scores as well as the two dice.
|
||||
private int _totalScore = 0;
|
||||
private int _roundScore = 0;
|
||||
private Die _die1;
|
||||
private Die _die2;
|
||||
|
||||
public PigDice()
|
||||
{
|
||||
_die1 = new Die(); // makes the new die
|
||||
_die2 = new Die();
|
||||
|
||||
}
|
||||
|
||||
// accessor for total score
|
||||
public int currentTotal()
|
||||
{
|
||||
return _totalScore;
|
||||
}
|
||||
|
||||
// accessor for this round score
|
||||
public int currentRound()
|
||||
{
|
||||
return _roundScore;
|
||||
}
|
||||
|
||||
// accessor to see if the user has rolled a single "1" and loses turn
|
||||
public boolean piggedOut()
|
||||
{
|
||||
if(singleOneRolled()) {
|
||||
_roundScore = 0;
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
// mutator that simulates rolling two dice and evaluating the resulting score
|
||||
public void rollDice()
|
||||
{
|
||||
// Roll the die
|
||||
_die1.roll();
|
||||
_die2.roll();
|
||||
|
||||
// Evalulate
|
||||
_roundScore += evaluate();
|
||||
}
|
||||
|
||||
// accessor for a formatted string of what the last roll looked like
|
||||
public String lastRoll()
|
||||
{
|
||||
return "D1 (" + _die1.faceValue() + "), D2 (" + _die2.faceValue() + ")";
|
||||
}
|
||||
|
||||
public int evaluate()
|
||||
{
|
||||
if(doubleOnesRolled()) //checks if there has been a double 1
|
||||
return 25;
|
||||
else
|
||||
return _die1.faceValue() + _die2.faceValue();
|
||||
}
|
||||
|
||||
private boolean singleOneRolled() // checks if there has been a single 1 rolled
|
||||
{
|
||||
if (_die1.faceValue() == 1 && _die2.faceValue() != 1 || _die2.faceValue() == 1 && _die1.faceValue() != 1) {
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean doubleOnesRolled() // checks if there has been a double 1 rolled
|
||||
{
|
||||
if (_die1.faceValue() == 1 && _die2.faceValue() == 1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// mutator to end a round and keep the add this round to the total
|
||||
// also returns the total value of the round and resets the round total for next time
|
||||
//
|
||||
public int save()
|
||||
{
|
||||
_totalScore += _roundScore;
|
||||
int x = _roundScore;
|
||||
_roundScore = 0;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user