Pig the dice game/Player: Difference between revisions
Content added Content deleted
m (Added the Sidef language) |
(Added Java support. Plays the game (implemented from scratch) and includes 4 bots with easy extensibility.) |
||
Line 1,569: | Line 1,569: | ||
out of 100 000 tests. |
out of 100 000 tests. |
||
</pre> |
|||
=={{header|Java}}== |
|||
This is a full implementation of pig dice with 4 built in bots. Their behavior can easily be modified and additional bots can be added as well. |
|||
This is the main file, Pigdice.java |
|||
<lang Java> |
|||
import java.util.Scanner; |
|||
import java.util.ArrayList; |
|||
public class Pigdice { |
|||
public static void main(String[] args) { |
|||
Scanner scan = new Scanner(System.in); |
|||
int players = 0; |
|||
boolean flag = false; |
|||
//Validate the input |
|||
while(flag == false) { |
|||
//Get the number of players |
|||
System.out.println("Hello, welcome to Pig Dice the game! How many players? "); |
|||
if(scan.hasNextInt()) { |
|||
//Gotta be more than 0 |
|||
int nextInt = scan.nextInt(); |
|||
if(nextInt > 0) { |
|||
players = nextInt; |
|||
flag = true; |
|||
} |
|||
} |
|||
else { |
|||
System.out.println("That wasn't an integer. Try again. \n"); |
|||
scan.next(); |
|||
} |
|||
} |
|||
System.out.println("Alright, starting with " + players + " players. \n"); |
|||
//Start the game |
|||
play(players, scan); |
|||
scan.close(); |
|||
} |
|||
public static void play(int group, Scanner scan) { |
|||
//Set the number of strategies available. |
|||
final int STRATEGIES = 5; |
|||
//Construct the dice- accepts an int as an arg for number of sides, but defaults to 6. |
|||
Dice dice = new Dice(); |
|||
//Create an array of players and initialize them to defaults. |
|||
ArrayList<Player> players = new ArrayList<Player>(); |
|||
for(int count = 0; count < group; count++) { |
|||
players.add(new Player(count)); |
|||
System.out.println("Player " + players.get(count).getNumber() + " is alive! "); |
|||
} |
|||
/*****Print strategy options here. Modify Player.java to add strategies. *****/ |
|||
System.out.println("Each strategy is numbered 0 - " + (STRATEGIES - 1) + ". They are as follows: "); |
|||
System.out.println(">> Enter '0' for a human player. "); |
|||
System.out.println(">> Strategy 1 is a basic strategy where the AI rolls until 20+ points and holds unless the current max is 75+."); |
|||
System.out.println(">> Strategy 2 is a basic strategy where the AI, after 3 successful rolls, will randomly decide to roll or hold. "); |
|||
System.out.println(">> Strategy 3 is similar to strategy 2, except it's a little gutsier and will attempt 5 successful rolls. "); |
|||
System.out.println(">> 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. "); |
|||
//Get the strategy for each player |
|||
for(int count = 0; count < players.size(); count++) { |
|||
System.out.println("\nWhat strategy would you like player " + players.get(count).getNumber() + " to use? "); |
|||
//Validate the strategy is a real strategy. |
|||
boolean flag = false; |
|||
while(flag == false) { |
|||
if(scan.hasNextInt()) { |
|||
int nextInt = scan.nextInt(); |
|||
if (nextInt < STRATEGIES) { |
|||
players.get(count).setStrategy(nextInt); |
|||
flag = true; |
|||
} |
|||
} |
|||
else { |
|||
System.out.println("That wasn't an option. Try again. "); |
|||
scan.next(); |
|||
} |
|||
} |
|||
} |
|||
//Here is where the rules for the game are programmatically defined. |
|||
int max = 0; |
|||
while(max < 100) { |
|||
//Begin the round |
|||
for(int count = 0; count < players.size(); count++) { |
|||
String choice = null; |
|||
System.out.println(">> Beginning Player " + players.get(count).getNumber() + "'s turn. "); |
|||
//Set the points for the turn to 0 |
|||
players.get(count).setTurnPoints(0); |
|||
//Determine whether the player chooses to roll or hold. |
|||
players.get(count).setMax(max); |
|||
while(choice != "h") { |
|||
choice = players.get(count).choose(); |
|||
if(choice == "r") { |
|||
int roll = dice.roll(); |
|||
System.out.println(" A " + roll + " was rolled. "); |
|||
players.get(count).setTurnPoints(players.get(count).getTurnPoints() + roll); |
|||
//Increment the player's built in iterator. |
|||
players.get(count).incIter(); |
|||
//If the player rolls a 1, their turn is over and they gain 0 points this round. |
|||
if(roll == 1) { |
|||
players.get(count).setTurnPoints(0); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
//Check if the player held or not. |
|||
if(choice == "h") { |
|||
System.out.println(" The player has held. "); |
|||
} |
|||
//End the turn and add any accumulated points to the player's pool. |
|||
players.get(count).addPoints(players.get(count).getTurnPoints()); |
|||
System.out.println(" Player " + players.get(count).getNumber() + "'s turn is now over. Their total is " + players.get(count).getPoints() + ". \n"); |
|||
//Reset the player's built in iterator. |
|||
players.get(count).resetIter(); |
|||
//Update the max score if necessary. |
|||
if(max < players.get(count).getPoints()) { |
|||
max = players.get(count).getPoints(); |
|||
} |
|||
//If someone won, stop the game and announce the winner. |
|||
if(max >= 100) { |
|||
System.out.println("Player " + players.get(count).getNumber() + " wins with " + max + " points! End scores: "); |
|||
//Announce the final scores. |
|||
for(int iter = 0; iter < group; iter++) { |
|||
System.out.println("Player " + players.get(iter).getNumber() + " had " + players.get(iter).getPoints() + " points. "); |
|||
} |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
This is the Player.java class file. |
|||
<lang Java> |
|||
import java.util.Scanner; |
|||
public class Player { |
|||
Scanner str = new Scanner(System.in); |
|||
private int points; |
|||
private int turnPoints; |
|||
private int strategy; |
|||
private int max; |
|||
private int number; |
|||
private int iter; |
|||
final int ROOF = 75; |
|||
final int FLOOR = 20; |
|||
final int BASEMENT = 10; |
|||
public Player(int val) { |
|||
points = 0; |
|||
turnPoints = 0; |
|||
strategy = 0; |
|||
max = 0; |
|||
number = val; |
|||
iter = 0; |
|||
} |
|||
public int getPoints() { |
|||
return points; |
|||
} |
|||
public int getTurnPoints() { |
|||
return turnPoints; |
|||
} |
|||
public int getMax() { |
|||
return max; |
|||
} |
|||
public int getNumber() { |
|||
return number; |
|||
} |
|||
public int getIter() { |
|||
return iter; |
|||
} |
|||
public void addPoints(int val) { |
|||
points += val; |
|||
} |
|||
public void setTurnPoints(int val) { |
|||
turnPoints = val; |
|||
} |
|||
public void setStrategy(int strat) { |
|||
strategy = strat; |
|||
} |
|||
public void setMax(int val) { |
|||
max = val; |
|||
} |
|||
public void setNumber(int val) { |
|||
number = val; |
|||
} |
|||
public void resetIter() { |
|||
iter = 0; |
|||
} |
|||
public void incIter() { |
|||
iter++; |
|||
} |
|||
public void aiIntro() { |
|||
System.out.println(" Player " + this.getNumber() + "'s turn points are " + this.getTurnPoints() + ". Their total is " + this.getPoints() + ". "); |
|||
System.out.println(" The max points any player currently has is " + this.getMax() + ". "); |
|||
} |
|||
/*****MODIFY THIS AREA TO MODIFY THE STRATEGIES*****/ |
|||
//Determine whether to roll or hold based on the strategy for this player. |
|||
public String choose() { |
|||
String choice = null; |
|||
switch (strategy) { |
|||
//Strategy 0 is a user-defined strategy |
|||
case 0: |
|||
System.out.println(" Your turn points are " + this.getTurnPoints() + ". Your total is " + this.getPoints() + ". "); |
|||
System.out.println(" The max points any player currently has is " + this.getMax() + ". (H)old or (R)oll?"); |
|||
System.out.println(" Enter 'h' to hold and 'r' to roll. "); |
|||
boolean flag = false; |
|||
while(!flag) { |
|||
if(this.str.hasNextLine()) { |
|||
choice = this.str.nextLine(); |
|||
} |
|||
if(choice.contains("r")) { |
|||
choice = "r"; |
|||
flag = true; |
|||
} |
|||
else if(choice.contains("h")) { |
|||
choice = "h"; |
|||
flag = true; |
|||
} |
|||
else { |
|||
System.out.println(" Enter an h or an r. \n"); |
|||
System.out.println(choice); |
|||
} |
|||
} |
|||
break; |
|||
//Strategy 1 is a basic strategy where the AI rolls until 20+ points and holds unless the current max is 75+. |
|||
case 1: |
|||
this.aiIntro(); |
|||
if(this.getTurnPoints() < FLOOR || this.getMax() >= ROOF) { |
|||
if(this.getTurnPoints() >= (100 - this.getPoints())) { |
|||
choice = "h"; |
|||
} |
|||
else { |
|||
choice = "r"; |
|||
} |
|||
} |
|||
else { |
|||
choice = "h"; |
|||
} |
|||
break; |
|||
//Strategy 2 is a basic strategy where the AI, after 3 successful rolls, will randomly decide to roll or hold. |
|||
case 2: |
|||
this.aiIntro(); |
|||
if(this.getPoints() == 0 && this.getTurnPoints() >= (BASEMENT / 2)) { |
|||
choice = "h"; |
|||
} |
|||
if(this.getIter() > 3) { |
|||
Dice die = new Dice(2); |
|||
int roll = die.roll(); |
|||
if(roll == 1) { |
|||
choice = "h"; |
|||
} |
|||
else if(roll == 2) { |
|||
choice = "r"; |
|||
} |
|||
} |
|||
else { |
|||
choice = "r"; |
|||
} |
|||
break; |
|||
//Strategy 3 is similar to strategy 2, except it's a little gutsier and will attempt 5 successful rolls. |
|||
case 3: |
|||
this.aiIntro(); |
|||
if(this.getIter() > 5) { |
|||
Dice die = new Dice(2); |
|||
int roll = die.roll(); |
|||
if(roll == 1) { |
|||
choice = "h"; |
|||
} |
|||
else if(roll == 2) { |
|||
choice = "r"; |
|||
} |
|||
} |
|||
else if(this.getPoints() < BASEMENT && this.getTurnPoints() > BASEMENT) { |
|||
choice = "h"; |
|||
} |
|||
else { |
|||
choice = "r"; |
|||
} |
|||
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. |
|||
Unless their total is zero, in which case they'll hold at 10 points. */ |
|||
case 4: |
|||
this.aiIntro(); |
|||
if(this.getPoints() == 0 && this.getTurnPoints() >= (BASEMENT / 2)) { |
|||
choice = "h"; |
|||
} |
|||
else if(this.getTurnPoints() < FLOOR || this.getMax() >= ROOF) { |
|||
if(this.getTurnPoints() >= (100 - this.getPoints())) { |
|||
choice = "h"; |
|||
} |
|||
else { |
|||
choice = "r"; |
|||
} |
|||
} |
|||
else if(this.getTurnPoints() > FLOOR && this.getMax() <= ROOF) { |
|||
Dice die = new Dice(2); |
|||
int roll = die.roll(); |
|||
if(roll == 1) { |
|||
choice = "h"; |
|||
} |
|||
else if(roll == 2) { |
|||
choice = "r"; |
|||
} |
|||
} |
|||
else { |
|||
choice = "h"; |
|||
} |
|||
break; |
|||
} |
|||
return choice; |
|||
} |
|||
} |
|||
</lang> |
|||
And finally, this is the Dice.java class file. It's pretty self-explanatory. |
|||
<lang Java> |
|||
import java.util.Random; |
|||
public class Dice { |
|||
Random rand = new Random(); |
|||
int sides; |
|||
Dice(int numSides) { |
|||
sides = numSides; |
|||
} |
|||
Dice() { |
|||
sides = 6; |
|||
} |
|||
int roll() { |
|||
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. |
|||
<pre> |
|||
Hello, welcome to Pig Dice the game! How many players? |
|||
4 |
|||
Alright, starting with 4 players. |
|||
Player 0 is alive! |
|||
Player 1 is alive! |
|||
Player 2 is alive! |
|||
Player 3 is alive! |
|||
Each strategy is numbered 0 - 4. They are as follows: |
|||
>> Enter '0' for a human player. |
|||
>> Strategy 1 is a basic strategy where the AI rolls until 20+ points and holds unless the current max is 75+. |
|||
>> Strategy 2 is a basic strategy where the AI, after 3 successful rolls, will randomly decide to roll or hold. |
|||
>> Strategy 3 is similar to strategy 2, except it's a little gutsier and will attempt 5 successful rolls. |
|||
>> 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. |
|||
What strategy would you like player 0 to use? |
|||
1 |
|||
What strategy would you like player 1 to use? |
|||
2 |
|||
What strategy would you like player 2 to use? |
|||
3 |
|||
What strategy would you like player 3 to use? |
|||
4 |
|||
>> Beginning Player 0's turn. |
|||
Player 0's turn points are 0. Their total is 0. |
|||
The max points any player currently has is 0. |
|||
A 4 was rolled. |
|||
Player 0's turn points are 4. Their total is 0. |
|||
The max points any player currently has is 0. |
|||
A 4 was rolled. |
|||
Player 0's turn points are 8. Their total is 0. |
|||
The max points any player currently has is 0. |
|||
A 1 was rolled. |
|||
Player 0's turn is now over. Their total is 0. |
|||
>> Beginning Player 1's turn. |
|||
... |
|||
... |
|||
*SNIP* |
|||
... |
|||
... |
|||
Player 3 wins with 102 points! End scores: |
|||
Player 0 had 20 points. |
|||
Player 1 had 73 points. |
|||
Player 2 had 66 points. |
|||
Player 3 had 102 points. |
|||
</pre> |
</pre> |
||