Anonymous user
Pig the dice game/Player: Difference between revisions
→{{header|Java}}
(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;▼
public class Pigdice {
Line 1,584 ⟶ 1,582:
Scanner scan = new Scanner(System.in);
int players = 0;
//Validate the input
while(
//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;
}
}
Line 1,620 ⟶ 1,617:
//Create an array of players and initialize them to defaults.
for(int count = 0; count < group; count++) {
players
System.out.println("Player " + players
}
Line 1,635 ⟶ 1,632:
//Get the strategy for each player
for(
System.out.println("\nWhat strategy would you like player " +
//Validate the strategy is a real strategy.
if(scan.hasNextInt()) {
int nextInt = scan.nextInt();
if (nextInt < Strategy.STRATEGIES.length) {
}
}
Line 1,660 ⟶ 1,656:
//Begin the round
for(
String choice = null;▼
▲ System.out.println(">> Beginning Player " + players.get(count).getNumber() + "'s turn. ");
//Set the points for the turn to 0
//Determine whether the player chooses to roll or hold.
while(
Move choice =
if(choice ==
int roll = dice.roll();
System.out.println(" A " + roll + " was rolled. ");
//Increment the player's built in iterator.
//If the player rolls a 1, their turn is over and they gain 0 points this round.
if(roll == 1) {
break;
}
}
//Check if the player held or not.
}▼
else {
break;▼
▲ }
}
//End the turn and add any accumulated points to the player's pool.
System.out.println(" Player " +
//Reset the player's built in iterator.
//Update the max score if necessary.
if(max <
max =
}
//If someone won, stop the game and announce the winner.
if(max >= 100) {
System.out.println("Player " +
//Announce the final scores.
for(
System.out.println("Player " +
}
break;
Line 1,719 ⟶ 1,714:
}
}</lang>▼
}▼
▲</lang>
This is the Player.java class file.
<lang Java>public class Player {
public class Player {▼
Scanner str = new Scanner(System.in);▼
private int
▲ private int turnPoints;
▲ private int strategy;
private int number;
private int iter = 0;
final int ROOF = 75;▼
final int FLOOR = 20;▼
final int BASEMENT = 10;▼
public Player(int val) {
▲ turnPoints = 0;
number = val;
}
Line 1,769 ⟶ 1,751:
turnPoints = val;
}
public void setStrategy(
strategy = strat;
}
Line 1,785 ⟶ 1,767:
}
public void aiIntro() {
System.out.println(" Player " +
System.out.println(" The max points any player currently has is " +
}
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 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
//Strategy 0 is a user-defined strategy
System.out.println(" Your turn points are " +
System.out.println(" The max points any player currently has is " +
System.out.println(" Enter 'h' to hold and 'r' to roll. ");
▲ while(!flag) {
if(
}
if(
}
else if(
}
else {
System.out.println(" Enter an h or an r. \n");
System.out.println(
}
}
//Strategy 1 is a basic strategy where the AI rolls until 20+ points and holds unless the current max is 75+.
if(
if(
}
else {
}
}
else {
}
//Strategy 2 is a basic strategy where the AI, after 3 successful rolls, will randomly decide to roll or hold.
if(
}
if(
▲ Dice die = new Dice(2);
int roll = die.roll();
if(roll == 1) {
}
else
}
}
else {
}
//Strategy 3 is similar to strategy 2, except it's a little gutsier and will attempt 5 successful rolls.
if(
int roll = die.roll();
if(roll == 1) {
}
else
}
}
else if(
}
else {
}
/*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. */
if(
}
else if(
if(
}
else {
}
}
else if(
int roll = die.roll();
if(roll == 1) {
}
else
}
}
else {
}
▲ break;
}
};
}</lang>▼
▲ }
▲</lang>
And finally, this is the Dice.java class file. It's pretty self-explanatory.
<lang Java>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.
|