Pig the dice game/Player: Difference between revisions

Content added Content deleted
(Added Java support. Plays the game (implemented from scratch) and includes 4 bots with easy extensibility.)
Line 1,575: Line 1,575:


This is the main file, Pigdice.java
This is the main file, Pigdice.java
<lang Java>
<lang Java>import java.util.Scanner;
import java.util.Scanner;
import java.util.ArrayList;


public class Pigdice {
public class Pigdice {
Line 1,584: Line 1,582:
Scanner scan = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
int players = 0;
int players = 0;
boolean flag = false;
//Validate the input
//Validate the input
while(flag == false) {
while(true) {
//Get the number of players
//Get the number of players
System.out.println("Hello, welcome to Pig Dice the game! How many players? ");
System.out.println("Hello, welcome to Pig Dice the game! How many players? ");
Line 1,596: Line 1,593:
if(nextInt > 0) {
if(nextInt > 0) {
players = nextInt;
players = nextInt;
flag = true;
break;
}
}
}
}
Line 1,620: Line 1,617:
//Create an array of players and initialize them to defaults.
//Create an array of players and initialize them to defaults.
ArrayList<Player> players = new ArrayList<Player>();
Player[] players = new Player[group];
for(int count = 0; count < group; count++) {
for(int count = 0; count < group; count++) {
players.add(new Player(count));
players[count] = new Player(count);
System.out.println("Player " + players.get(count).getNumber() + " is alive! ");
System.out.println("Player " + players[count].getNumber() + " is alive! ");
}
}
Line 1,635: Line 1,632:
//Get the strategy for each player
//Get the strategy for each player
for(int count = 0; count < players.size(); count++) {
for(Player player : players) {
System.out.println("\nWhat strategy would you like player " + players.get(count).getNumber() + " to use? ");
System.out.println("\nWhat strategy would you like player " + player.getNumber() + " to use? ");


//Validate the strategy is a real strategy.
//Validate the strategy is a real strategy.
while(true) {
boolean flag = false;
while(flag == false) {
if(scan.hasNextInt()) {
if(scan.hasNextInt()) {
int nextInt = scan.nextInt();
int nextInt = scan.nextInt();
if (nextInt < STRATEGIES) {
if (nextInt < Strategy.STRATEGIES.length) {
players.get(count).setStrategy(nextInt);
player.setStrategy(Strategy.STRATEGIES[nextInt]);
flag = true;
break;
}
}
}
}
Line 1,660: Line 1,656:
//Begin the round
//Begin the round
for(int count = 0; count < players.size(); count++) {
for(Player player : players) {
System.out.println(">> Beginning Player " + player.getNumber() + "'s turn. ");
String choice = null;
System.out.println(">> Beginning Player " + players.get(count).getNumber() + "'s turn. ");
//Set the points for the turn to 0
//Set the points for the turn to 0
players.get(count).setTurnPoints(0);
player.setTurnPoints(0);
//Determine whether the player chooses to roll or hold.
//Determine whether the player chooses to roll or hold.
players.get(count).setMax(max);
player.setMax(max);
while(choice != "h") {
while(true) {
choice = players.get(count).choose();
Move choice = player.choose();
if(choice == "r") {
if(choice == Move.ROLL) {
int roll = dice.roll();
int roll = dice.roll();
System.out.println(" A " + roll + " was rolled. ");
System.out.println(" A " + roll + " was rolled. ");
players.get(count).setTurnPoints(players.get(count).getTurnPoints() + roll);
player.setTurnPoints(player.getTurnPoints() + roll);
//Increment the player's built in iterator.
//Increment the player's built in iterator.
players.get(count).incIter();
player.incIter();
//If the player rolls a 1, their turn is over and they gain 0 points this round.
//If the player rolls a 1, their turn is over and they gain 0 points this round.
if(roll == 1) {
if(roll == 1) {
players.get(count).setTurnPoints(0);
player.setTurnPoints(0);
break;
break;
}
}
}
}
//Check if the player held or not.
}
else {
//Check if the player held or not.
System.out.println(" The player has held. ");
break;
if(choice == "h") {
}
System.out.println(" The player has held. ");
}
}
//End the turn and add any accumulated points to the player's pool.
//End the turn and add any accumulated points to the player's pool.
players.get(count).addPoints(players.get(count).getTurnPoints());
player.addPoints(player.getTurnPoints());
System.out.println(" Player " + players.get(count).getNumber() + "'s turn is now over. Their total is " + players.get(count).getPoints() + ". \n");
System.out.println(" Player " + player.getNumber() + "'s turn is now over. Their total is " + player.getPoints() + ". \n");
//Reset the player's built in iterator.
//Reset the player's built in iterator.
players.get(count).resetIter();
player.resetIter();
//Update the max score if necessary.
//Update the max score if necessary.
if(max < players.get(count).getPoints()) {
if(max < player.getPoints()) {
max = players.get(count).getPoints();
max = player.getPoints();
}
}
//If someone won, stop the game and announce the winner.
//If someone won, stop the game and announce the winner.
if(max >= 100) {
if(max >= 100) {
System.out.println("Player " + players.get(count).getNumber() + " wins with " + max + " points! End scores: ");
System.out.println("Player " + player.getNumber() + " wins with " + max + " points! End scores: ");
//Announce the final scores.
//Announce the final scores.
for(int iter = 0; iter < group; iter++) {
for(Player p : players) {
System.out.println("Player " + players.get(iter).getNumber() + " had " + players.get(iter).getPoints() + " points. ");
System.out.println("Player " + p.getNumber() + " had " + p.getPoints() + " points. ");
}
}
break;
break;
Line 1,719: Line 1,714:
}
}
}</lang>
}
</lang>


This is the Player.java class file.
This is the Player.java class file.
<lang Java>
<lang Java>public class Player {
import java.util.Scanner;


private int points = 0;
public class Player {
private int turnPoints = 0;

private Strategy strategy = null;
Scanner str = new Scanner(System.in);
private int points;
private int max = 0;
private int turnPoints;
private int strategy;
private int max;
private int number;
private int number;
private int iter;
private int iter = 0;
final int ROOF = 75;
final int FLOOR = 20;
final int BASEMENT = 10;
public Player(int val) {
public Player(int val) {
points = 0;
turnPoints = 0;
strategy = 0;
max = 0;
number = val;
number = val;
iter = 0;
}
}
Line 1,769: Line 1,751:
turnPoints = val;
turnPoints = val;
}
}
public void setStrategy(int strat) {
public void setStrategy(Strategy strat) {
strategy = strat;
strategy = strat;
}
}
Line 1,785: Line 1,767:
}
}
public void aiIntro() {
public void aiIntro() {
System.out.println(" Player " + this.getNumber() + "'s turn points are " + this.getTurnPoints() + ". Their total is " + this.getPoints() + ". ");
System.out.println(" Player " + getNumber() + "'s turn points are " + getTurnPoints() + ". Their total is " + getPoints() + ". ");
System.out.println(" The max points any player currently has is " + this.getMax() + ". ");
System.out.println(" The max points any player currently has is " + getMax() + ". ");
}
}
public Move choose() {
return strategy.choose(this);
}

}</lang>

This is the Move.java class file.
<lang Java>public enum Move { ROLL, HOLD }</lang>

This is the Strategy.java class file.
<lang Java>import java.util.Scanner;

public interface Strategy {

Move choose(Player player);
static final Scanner str = new Scanner(System.in);
static final Dice die = new Dice(2);
static final int ROOF = 75;
static final int FLOOR = 20;
static final int BASEMENT = 10;
/*****MODIFY THIS AREA TO MODIFY THE STRATEGIES*****/
/*****MODIFY THIS AREA TO MODIFY THE STRATEGIES*****/
//Determine whether to roll or hold based on the strategy for this player.
//Determine whether to roll or hold based on the strategy for this player.
public String choose() {
public static final Strategy[] STRATEGIES = {
String choice = null;
switch (strategy) {
//Strategy 0 is a user-defined strategy
//Strategy 0 is a user-defined strategy
case 0:
player -> {
System.out.println(" Your turn points are " + this.getTurnPoints() + ". Your total is " + this.getPoints() + ". ");
System.out.println(" Your turn points are " + player.getTurnPoints() + ". Your total is " + player.getPoints() + ". ");
System.out.println(" The max points any player currently has is " + this.getMax() + ". (H)old or (R)oll?");
System.out.println(" The max points any player currently has is " + player.getMax() + ". (H)old or (R)oll?");
System.out.println(" Enter 'h' to hold and 'r' to roll. ");
System.out.println(" Enter 'h' to hold and 'r' to roll. ");
while(true) {
boolean flag = false;
String input = null;
while(!flag) {
if(this.str.hasNextLine()) {
if(str.hasNextLine()) {
choice = this.str.nextLine();
input = str.nextLine();
}
}
if(choice.contains("r")) {
if(input.contains("r")) {
choice = "r";
return Move.ROLL;
flag = true;
}
}
else if(choice.contains("h")) {
else if(input.contains("h")) {
choice = "h";
return Move.HOLD;
flag = true;
}
}
else {
else {
System.out.println(" Enter an h or an r. \n");
System.out.println(" Enter an h or an r. \n");
System.out.println(choice);
System.out.println(input);
}
}
}
}
break;
},
//Strategy 1 is a basic strategy where the AI rolls until 20+ points and holds unless the current max is 75+.
//Strategy 1 is a basic strategy where the AI rolls until 20+ points and holds unless the current max is 75+.
case 1:
player -> {
this.aiIntro();
player.aiIntro();
if(this.getTurnPoints() < FLOOR || this.getMax() >= ROOF) {
if(player.getTurnPoints() < FLOOR || player.getMax() >= ROOF) {
if(this.getTurnPoints() >= (100 - this.getPoints())) {
if(player.getTurnPoints() >= (100 - player.getPoints())) {
choice = "h";
return Move.HOLD;
}
}
else {
else {
choice = "r";
return Move.ROLL;
}
}
}
}
else {
else {
choice = "h";
return Move.HOLD;
}
}
break;
},
//Strategy 2 is a basic strategy where the AI, after 3 successful rolls, will randomly decide to roll or hold.
//Strategy 2 is a basic strategy where the AI, after 3 successful rolls, will randomly decide to roll or hold.
case 2:
player -> {
this.aiIntro();
player.aiIntro();
if(this.getPoints() == 0 && this.getTurnPoints() >= (BASEMENT / 2)) {
if(player.getPoints() == 0 && player.getTurnPoints() >= (BASEMENT / 2)) {
choice = "h";
return Move.HOLD;
}
}
if(this.getIter() > 3) {
if(player.getIter() > 3) {
Dice die = new Dice(2);
int roll = die.roll();
int roll = die.roll();
if(roll == 1) {
if(roll == 1) {
choice = "h";
return Move.HOLD;
}
}
else if(roll == 2) {
else {
choice = "r";
return Move.ROLL;
}
}
}
}
else {
else {
choice = "r";
return Move.ROLL;
}
}
break;
},
//Strategy 3 is similar to strategy 2, except it's a little gutsier and will attempt 5 successful rolls.
//Strategy 3 is similar to strategy 2, except it's a little gutsier and will attempt 5 successful rolls.
case 3:
player -> {
this.aiIntro();
player.aiIntro();
if(this.getIter() > 5) {
if(player.getIter() > 5) {
Dice die = new Dice(2);
int roll = die.roll();
int roll = die.roll();
if(roll == 1) {
if(roll == 1) {
choice = "h";
return Move.HOLD;
}
}
else if(roll == 2) {
else {
choice = "r";
return Move.ROLL;
}
}
}
}
else if(this.getPoints() < BASEMENT && this.getTurnPoints() > BASEMENT) {
else if(player.getPoints() < BASEMENT && player.getTurnPoints() > BASEMENT) {
choice = "h";
return Move.HOLD;
}
}
else {
else {
choice = "r";
return Move.ROLL;
}
}
break;
},
/*Strategy 4 is like a mix between strategies 1 and 3. After turn points are >= 20 and while max points are still less than 75, it will randomly hold or roll.
/*Strategy 4 is like a mix between strategies 1 and 3. After turn points are >= 20 and while max points are still less than 75, it will randomly hold or roll.
Unless their total is zero, in which case they'll hold at 10 points. */
Unless their total is zero, in which case they'll hold at 10 points. */
case 4:
player -> {
this.aiIntro();
player.aiIntro();
if(this.getPoints() == 0 && this.getTurnPoints() >= (BASEMENT / 2)) {
if(player.getPoints() == 0 && player.getTurnPoints() >= (BASEMENT / 2)) {
choice = "h";
return Move.HOLD;
}
}
else if(this.getTurnPoints() < FLOOR || this.getMax() >= ROOF) {
else if(player.getTurnPoints() < FLOOR || player.getMax() >= ROOF) {
if(this.getTurnPoints() >= (100 - this.getPoints())) {
if(player.getTurnPoints() >= (100 - player.getPoints())) {
choice = "h";
return Move.HOLD;
}
}
else {
else {
choice = "r";
return Move.ROLL;
}
}
}
}
else if(this.getTurnPoints() > FLOOR && this.getMax() <= ROOF) {
else if(player.getTurnPoints() > FLOOR && player.getMax() <= ROOF) {
Dice die = new Dice(2);
int roll = die.roll();
int roll = die.roll();
if(roll == 1) {
if(roll == 1) {
choice = "h";
return Move.HOLD;
}
}
else if(roll == 2) {
else {
choice = "r";
return Move.ROLL;
}
}
}
}
else {
else {
choice = "h";
return Move.HOLD;
}
}
break;
}
}
};


}</lang>
return choice;
}

}
</lang>


And finally, this is the Dice.java class file. It's pretty self-explanatory.
And finally, this is the Dice.java class file. It's pretty self-explanatory.
<lang Java>
<lang Java>import java.util.Random;
import java.util.Random;


public class Dice {
public class Dice {
Line 1,936: Line 1,925:
return rand.nextInt(sides) + 1;
return rand.nextInt(sides) + 1;
}
}
}</lang>
}
</lang>


Here's a small sample output using only bots (even though it fully supports human players too). A full game simulation can obviously be MUCH longer.
Here's a small sample output using only bots (even though it fully supports human players too). A full game simulation can obviously be MUCH longer.