Jump to content

Pig the dice game/Player: Difference between revisions

(Added Java support. Plays the game (implemented from scratch) and includes 4 bots with easy extensibility.)
Line 1,575:
 
This is the main file, Pigdice.java
<lang Java>import java.util.Scanner;
import java.util.Scanner;
import java.util.ArrayList;
 
public class Pigdice {
Line 1,584 ⟶ 1,582:
Scanner scan = new Scanner(System.in);
int players = 0;
boolean flag = false;
//Validate the input
while(flag == falsetrue) {
//Get the number of players
System.out.println("Hello, welcome to Pig Dice the game! How many players? ");
Line 1,596 ⟶ 1,593:
if(nextInt > 0) {
players = nextInt;
flag = truebreak;
}
}
Line 1,620 ⟶ 1,617:
//Create an array of players and initialize them to defaults.
ArrayList<Player>[] players = new ArrayList<Player>()[group];
for(int count = 0; count < group; count++) {
players.add([count] = new Player(count));
System.out.println("Player " + players.get([count)].getNumber() + " is alive! ");
}
Line 1,635 ⟶ 1,632:
//Get the strategy for each player
for(intPlayer countplayer = 0; count <: players.size(); count++) {
System.out.println("\nWhat strategy would you like player " + players.get(count)player.getNumber() + " to use? ");
 
//Validate the strategy is a real strategy.
while(!flagtrue) {
boolean flag = false;
while(flag == false) {
if(scan.hasNextInt()) {
int nextInt = scan.nextInt();
if (nextInt < Strategy.STRATEGIES.length) {
playersplayer.getsetStrategy(count)Strategy.setStrategy(STRATEGIES[nextInt]);
flag = truebreak;
}
}
Line 1,660 ⟶ 1,656:
//Begin the round
for(intPlayer countplayer = 0; count <: players.size(); count++) {
System.out.println(">> Beginning Player " + players.get(count)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
players.get(count)player.setTurnPoints(0);
//Determine whether the player chooses to roll or hold.
players.get(count)player.setMax(max);
while(choice != "h"true) {
Move choice = players.get(count)player.choose();
if(choice == "r"Move.ROLL) {
int roll = dice.roll();
System.out.println(" A " + roll + " was rolled. ");
players.get(count)player.setTurnPoints(players.get(count)player.getTurnPoints() + roll);
//Increment the player's built in iterator.
players.get(count)player.incIter();
//If the player rolls a 1, their turn is over and they gain 0 points this round.
if(roll == 1) {
players.get(count)player.setTurnPoints(0);
break;
}
}
//Check if the player held or not.
}
else {
//Check System.out.println(" if the The player heldhas or notheld. ");
break;
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)player.addPoints(players.get(count)player.getTurnPoints());
System.out.println(" Player " + players.get(count)player.getNumber() + "'s turn is now over. Their total is " + players.get(count)player.getPoints() + ". \n");
//Reset the player's built in iterator.
players.get(count)player.resetIter();
//Update the max score if necessary.
if(max < players.get(count)player.getPoints()) {
max = players.get(count)player.getPoints();
}
//If someone won, stop the game and announce the winner.
if(max >= 100) {
System.out.println("Player " + players.get(count)player.getNumber() + " wins with " + max + " points! End scores: ");
//Announce the final scores.
for(intPlayer iterp =: 0; iter < group; iter++players) {
System.out.println("Player " + players.get(iter)p.getNumber() + " had " + players.get(iter)p.getPoints() + " points. ");
}
break;
Line 1,719 ⟶ 1,714:
}
}</lang>
</lang>
 
This is the Player.java class file.
<lang Java>public class Player {
import java.util.Scanner;
 
private int turnPointspoints = 0;
public class Player {
private int turnPoints = 0;
 
private intStrategy strategy = null;
Scanner str = new Scanner(System.in);
private int pointsmax = 0;
private int turnPoints;
private int strategy;
private int max;
private int number;
private int iter = 0;
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;
}
Line 1,769 ⟶ 1,751:
turnPoints = val;
}
public void setStrategy(intStrategy strat) {
strategy = strat;
}
Line 1,785 ⟶ 1,767:
}
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() + ". ");
}
public classMove Playerchoose() {
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*****/
//Determine whether to roll or hold based on the strategy for this player.
public Stringstatic choose()final Strategy[] STRATEGIES = {
String choice = null;
switch (strategy) {
//Strategy 0 is a user-defined strategy
caseplayer 0:-> {
System.out.println(" Your turn points are " + thisplayer.getTurnPoints() + ". Your total is " + thisplayer.getPoints() + ". ");
System.out.println(" The max points any player currently has is " + thisplayer.getMax() + ". (H)old or (R)oll?");
System.out.println(" Enter 'h' to hold and 'r' to roll. ");
while(flag == falsetrue) {
boolean flag = false;
String choiceinput = null;
while(!flag) {
if(this.str.hasNextLine()) {
choiceinput = this.str.nextLine();
}
if(choiceinput.contains("r")) {
choicereturn = "r"Move.ROLL;
flag = true;
}
else if(choiceinput.contains("h")) {
choicereturn = "h"Move.HOLD;
flag = true;
}
else {
System.out.println(" Enter an h or an r. \n");
System.out.println(choiceinput);
}
}
break;},
//Strategy 1 is a basic strategy where the AI rolls until 20+ points and holds unless the current max is 75+.
caseplayer 1:-> {
thisplayer.aiIntro();
if(thisplayer.getTurnPoints() < FLOOR || thisplayer.getMax() >= ROOF) {
if(thisplayer.getTurnPoints() >= (100 - thisplayer.getPoints())) {
choicereturn = "h"Move.HOLD;
}
else {
choicereturn = "r"Move.ROLL;
}
}
else {
choicereturn = "h"Move.HOLD;
}
break;},
//Strategy 2 is a basic strategy where the AI, after 3 successful rolls, will randomly decide to roll or hold.
caseplayer 2:-> {
thisplayer.aiIntro();
if(thisplayer.getPoints() == 0 && thisplayer.getTurnPoints() >= (BASEMENT / 2)) {
choicereturn = "h"Move.HOLD;
}
if(thisplayer.getIter() > 3) {
Dice die = new Dice(2);
int roll = die.roll();
if(roll == 1) {
choicereturn = "h"Move.HOLD;
}
else if(roll == 2) {
choicereturn = "r"Move.ROLL;
}
}
else {
choicereturn = "r"Move.ROLL;
}
break;},
//Strategy 3 is similar to strategy 2, except it's a little gutsier and will attempt 5 successful rolls.
caseplayer 3:-> {
thisplayer.aiIntro();
if(thisplayer.getIter() > 5) {
Dice die = new Dice(2);
int roll = die.roll();
if(roll == 1) {
choicereturn = "h"Move.HOLD;
}
else if(roll == 2) {
choicereturn = "r"Move.ROLL;
}
}
else if(thisplayer.getPoints() < BASEMENT && thisplayer.getTurnPoints() > BASEMENT) {
choicereturn = "h"Move.HOLD;
}
else {
choicereturn = "r"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.
Unless their total is zero, in which case they'll hold at 10 points. */
caseplayer 4:-> {
thisplayer.aiIntro();
if(thisplayer.getPoints() == 0 && thisplayer.getTurnPoints() >= (BASEMENT / 2)) {
choicereturn = "h"Move.HOLD;
}
else if(thisplayer.getTurnPoints() < FLOOR || thisplayer.getMax() >= ROOF) {
if(thisplayer.getTurnPoints() >= (100 - thisplayer.getPoints())) {
choicereturn = "h"Move.HOLD;
}
else {
choicereturn = "r"Move.ROLL;
}
}
else if(thisplayer.getTurnPoints() > FLOOR && thisplayer.getMax() <= ROOF) {
Dice die = new Dice(2);
int roll = die.roll();
if(roll == 1) {
choicereturn = "h"Move.HOLD;
}
else if(roll == 2) {
choicereturn = "r"Move.ROLL;
}
}
else {
choicereturn = "h"Move.HOLD;
}
break;
}
};
 
}</lang>
return choice;
}
 
}
</lang>
 
And finally, this is the Dice.java class file. It's pretty self-explanatory.
<lang Java>import java.util.Random;
import java.util.Random;
 
public class Dice {
Line 1,936 ⟶ 1,925:
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.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.