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.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( |
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; |
||
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. |
||
Player[] players = new Player[group]; |
|||
for(int count = 0; count < group; count++) { |
for(int count = 0; count < group; count++) { |
||
players |
players[count] = new Player(count); |
||
System.out.println("Player " + players |
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( |
for(Player player : players) { |
||
System.out.println("\nWhat strategy would you like player " + |
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. |
||
⚫ | |||
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) { |
||
player.setStrategy(Strategy.STRATEGIES[nextInt]); |
|||
break; |
|||
} |
} |
||
} |
} |
||
Line 1,660: | Line 1,656: | ||
//Begin the round |
//Begin the round |
||
for( |
for(Player player : players) { |
||
⚫ | |||
⚫ | |||
⚫ | |||
//Set the points for the turn to 0 |
//Set the points for the turn to 0 |
||
player.setTurnPoints(0); |
|||
//Determine whether the player chooses to roll or hold. |
//Determine whether the player chooses to roll or hold. |
||
player.setMax(max); |
|||
while( |
while(true) { |
||
choice = |
Move choice = player.choose(); |
||
if(choice == |
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. "); |
||
player.setTurnPoints(player.getTurnPoints() + roll); |
|||
//Increment the player's built in iterator. |
//Increment the player's built in iterator. |
||
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) { |
||
player.setTurnPoints(0); |
|||
break; |
break; |
||
} |
} |
||
} |
} |
||
//Check if the player held or not. |
|||
⚫ | |||
else { |
|||
System.out.println(" The player has held. "); |
|||
⚫ | |||
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. |
||
player.addPoints(player.getTurnPoints()); |
|||
System.out.println(" Player " + |
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. |
||
player.resetIter(); |
|||
//Update the max score if necessary. |
//Update the max score if necessary. |
||
if(max < |
if(max < player.getPoints()) { |
||
max = |
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 " + |
System.out.println("Player " + player.getNumber() + " wins with " + max + " points! End scores: "); |
||
//Announce the final scores. |
//Announce the final scores. |
||
for( |
for(Player p : players) { |
||
System.out.println("Player " + |
System.out.println("Player " + p.getNumber() + " had " + p.getPoints() + " points. "); |
||
} |
} |
||
break; |
break; |
||
Line 1,719: | Line 1,714: | ||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
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 |
private int max = 0; |
||
⚫ | |||
⚫ | |||
private int max; |
|||
private int number; |
private int number; |
||
private int iter; |
private int iter = 0; |
||
⚫ | |||
⚫ | |||
⚫ | |||
public Player(int val) { |
public Player(int val) { |
||
points = 0; |
|||
⚫ | |||
strategy = 0; |
|||
max = 0; |
|||
number = val; |
number = val; |
||
iter = 0; |
|||
} |
} |
||
Line 1,769: | Line 1,751: | ||
turnPoints = val; |
turnPoints = val; |
||
} |
} |
||
public void setStrategy( |
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 " + |
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 " + |
System.out.println(" The max points any player currently has is " + getMax() + ". "); |
||
} |
} |
||
⚫ | |||
return strategy.choose(this); |
|||
⚫ | |||
⚫ | |||
This is the Move.java class file. |
|||
<lang Java>public enum Move { ROLL, HOLD }</lang> |
|||
This is the Strategy.java class file. |
|||
⚫ | |||
public interface Strategy { |
|||
Move choose(Player player); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*****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 |
public static final Strategy[] STRATEGIES = { |
||
String choice = null; |
|||
switch (strategy) { |
|||
//Strategy 0 is a user-defined strategy |
//Strategy 0 is a user-defined strategy |
||
player -> { |
|||
System.out.println(" Your turn points are " + |
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 " + |
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. "); |
||
⚫ | |||
boolean flag = false; |
|||
⚫ | |||
⚫ | |||
if( |
if(str.hasNextLine()) { |
||
input = str.nextLine(); |
|||
} |
} |
||
if( |
if(input.contains("r")) { |
||
return Move.ROLL; |
|||
flag = true; |
|||
} |
} |
||
else if( |
else if(input.contains("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( |
System.out.println(input); |
||
} |
} |
||
} |
} |
||
}, |
|||
//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+. |
||
player -> { |
|||
player.aiIntro(); |
|||
if( |
if(player.getTurnPoints() < FLOOR || player.getMax() >= ROOF) { |
||
if( |
if(player.getTurnPoints() >= (100 - player.getPoints())) { |
||
return Move.HOLD; |
|||
} |
} |
||
else { |
else { |
||
return Move.ROLL; |
|||
} |
} |
||
} |
} |
||
else { |
else { |
||
return Move.HOLD; |
|||
} |
} |
||
}, |
|||
//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. |
||
player -> { |
|||
player.aiIntro(); |
|||
if( |
if(player.getPoints() == 0 && player.getTurnPoints() >= (BASEMENT / 2)) { |
||
return Move.HOLD; |
|||
} |
} |
||
if( |
if(player.getIter() > 3) { |
||
⚫ | |||
int roll = die.roll(); |
int roll = die.roll(); |
||
if(roll == 1) { |
if(roll == 1) { |
||
return Move.HOLD; |
|||
} |
} |
||
else |
else { |
||
return Move.ROLL; |
|||
} |
} |
||
} |
} |
||
else { |
else { |
||
return Move.ROLL; |
|||
} |
} |
||
}, |
|||
//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. |
||
player -> { |
|||
player.aiIntro(); |
|||
if( |
if(player.getIter() > 5) { |
||
Dice die = new Dice(2); |
|||
int roll = die.roll(); |
int roll = die.roll(); |
||
if(roll == 1) { |
if(roll == 1) { |
||
return Move.HOLD; |
|||
} |
} |
||
else |
else { |
||
return Move.ROLL; |
|||
} |
} |
||
} |
} |
||
else if( |
else if(player.getPoints() < BASEMENT && player.getTurnPoints() > BASEMENT) { |
||
return Move.HOLD; |
|||
} |
} |
||
else { |
else { |
||
return Move.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. |
/*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. */ |
||
player -> { |
|||
player.aiIntro(); |
|||
if( |
if(player.getPoints() == 0 && player.getTurnPoints() >= (BASEMENT / 2)) { |
||
return Move.HOLD; |
|||
} |
} |
||
else if( |
else if(player.getTurnPoints() < FLOOR || player.getMax() >= ROOF) { |
||
if( |
if(player.getTurnPoints() >= (100 - player.getPoints())) { |
||
return Move.HOLD; |
|||
} |
} |
||
else { |
else { |
||
return Move.ROLL; |
|||
} |
} |
||
} |
} |
||
else if( |
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) { |
||
return Move.HOLD; |
|||
} |
} |
||
else |
else { |
||
return Move.ROLL; |
|||
} |
} |
||
} |
} |
||
else { |
else { |
||
return Move.HOLD; |
|||
} |
} |
||
⚫ | |||
} |
} |
||
}; |
|||
⚫ | |||
return choice; |
|||
⚫ | |||
} |
|||
⚫ | |||
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> |
|||
} |
|||
⚫ | |||
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. |