Skip to content

Instantly share code, notes, and snippets.

@seronis
Created November 26, 2014 12:24
Show Gist options
  • Save seronis/f79f49d6a7e51301c3a0 to your computer and use it in GitHub Desktop.
Save seronis/f79f49d6a7e51301c3a0 to your computer and use it in GitHub Desktop.
package seronis.reddit.prompts.dice111;
import seronis.reddit.Main;
import seronis.reddit.util.DieNumeric;
import java.util.ArrayList;
/**
* Created by seronis on 11/26/14.
*/
public class Dice111 extends Main {
boolean finished;
private int numPlayers;
private ArrayList<Dice111Player> players;
private int[] scores;
private int highScore;
private int forfeits;
private int winners;
private int curRound;
public DieNumeric roller;
private int MaxScore;
public Dice111() {
super();
roller = new DieNumeric(6);
players = new ArrayList<Dice111Player>();
}
private void init() {
MaxScore = 111;
finished = false;
numPlayers = 0;
curRound = 0;
players.clear();
Dice111Player.Reset();
}
@Override
public void run(String[] args) {
while(!finished) {
init();
playGame();
}
}
private void playGame() {
println("Welcome to Eleventy-One !!!");
while(numPlayers == 0) {
print("How many players will be playing? (2-8 or 0 to quit) ");
if( cin.hasNext() ) {
if( cin.hasNextInt() ) {
numPlayers = cin.nextInt();
if( numPlayers >= 2 && numPlayers <= 8 ) {
String purgeNewline = cin.nextLine();
continue;
}
if( numPlayers == 0 ) {
finished = true;
return;
}
} else {
String ignore = cin.next();
if( ignore.startsWith("Q") || ignore.startsWith("q") ) {
finished = true;
return;
}
}
}
numPlayers = 0;
println("You must enter a number between 2 and 8.");
}
for(int num=0; num<numPlayers; ++num) {
Dice111Player plr = new Dice111Player();
Dice111Strategy strat = SelectStrategy(plr);
plr.setStrategy(strat);
plr.ChooseName(players,this);
players.add(plr);
}
scores = new int[numPlayers];
showRules();
while( !checkGameOver() )
playRound();
}
private Dice111Strategy SelectStrategy(Dice111Player plr) {
while(true) {
println("\nSelect a control type for " + plr.name);
println(" 1. Human 2. NPC\n");
print("What is your choice? ");
String input = cin.nextLine();
if (input.equalsIgnoreCase("1") || input.equalsIgnoreCase("human")) {
return new StrategyHuman(plr);
}
if (input.equalsIgnoreCase("2") || input.equalsIgnoreCase("npc")) {
//TODO: add more options
return new StrategyForfeit(plr);
}
println("I dont understand that choice...");
}
}
private void playRound() {
println("\n**\n* Beginning round " + ++curRound +"\n**" );
for( Dice111Player plr: players ) {
if( plr.GetScore() == -1 ) {
println("Skipping Turn: Player " + plr.name + " (forfeited)");
continue;
}
if( highScore >= MaxScore && plr.GetScore() != highScore ) {
println("Skipping Turn: Player " + plr.name + " (sudden death)");
continue;
}
println("\nNow player "+ plr.name + "'s turn. Current score is " + plr.GetScore());
plr.TakeTurn(players,this);
}
}
private boolean checkGameOver() {
winners = 0;
forfeits = 0;
highScore = 0;
for( Dice111Player plr: players ) {
int score = plr.score;
if( score == -1 ) {
forfeits++;
continue;
}
if( score > highScore ) {
highScore = score;
winners = 1;
continue;
}
if( score == highScore ) {
winners++;
continue;
}
}
if( forfeits == scores.length ) {
println("No players remaining. Game over.");
finished = true;
return true;
}
if( highScore >= MaxScore ) {
if( winners > 1 ) {
println("Sudden Death mode !!!");
return false;
} else {
println("We have a winner !!\n");
showScores();
return true;
}
}
return false;
}
private void showScores() {
for(Dice111Player plr: players) {
println("Player " + plr.name + ": " + plr.GetScore());
} println("");
}
private void showRules() {
println( "\n" +
"The point of Elvenenty-One is to be the first player to get a score of 111 or more. You roll\n" +
"one die at a time. If you roll 2 or more the value of the die is added to your score for the\n" +
"round. If you roll a 1 you forfeit all points you've earned this round and your turn ends.\n" +
"You can stop rolling at any time and your points earned this round is added to your total \n" +
"score and the turn passes to the next player. If a player gets a score of 111 or more the \n" +
"players who have not yet rolled this round still get a chance to get a higher score. "
);
}
}
package seronis.reddit.prompts.dice111;
import java.util.ArrayList;
/**
* Created by seronis on 11/26/14.
*/
public class Dice111Player {
private static int counter = 1;
public final int id;
protected String name;
protected int score;
public void setStrategy(Dice111Strategy strategy) {
this.strategy = strategy;
}
private Dice111Strategy strategy;
public Dice111Player() {
id = counter++;
name = "Player " + id;
}
void ChooseName( ArrayList<Dice111Player> players, Dice111 game ) {
strategy.ChooseName(players,game);
}
int TakeTurn( ArrayList<Dice111Player> players, Dice111 game ) {
strategy.TakeTurn(players,game);
return GetScore();
}
public int GetScore() {
return score;
}
public static void Reset() {
counter = 1;
}
}
package seronis.reddit.prompts.dice111;
import java.util.ArrayList;
/**
* Created by seronis on 11/26/14.
*/
public abstract class Dice111Strategy {
protected Dice111Player player;
public Dice111Strategy( Dice111Player _p ) {
player = _p;
}
abstract void ChooseName(ArrayList<Dice111Player> players, Dice111 game);
abstract void TakeTurn( ArrayList<Dice111Player> players, Dice111 game );
}
package seronis.reddit.prompts.dice111;
import java.util.ArrayList;
/**
* Created by seronis on 11/26/14.
*/
public class StrategyForfeit extends Dice111Strategy {
static private String[] names = {"Napoleon", "Courage", "Cyrus", "Awol", "Jerry"};
static private int nameCounter;
public StrategyForfeit(Dice111Player _p) {
super(_p);
}
@Override
void ChooseName(ArrayList<Dice111Player> players, Dice111 game) {
player.name = names[nameCounter++];
nameCounter = nameCounter%names.length;
for( Dice111Player plr: players ) {
if( plr.id == player.id ) return;
if( plr.name.equalsIgnoreCase(player.name) ) {
player.name = "NPC_" + player.id;
return;
}
}
}
@Override
void TakeTurn( ArrayList<Dice111Player> players, Dice111 game ) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) { }
System.out.println(player.name + " forfeits the game.");
player.score = -1;
try {
Thread.sleep(1500);
} catch (InterruptedException e) { }
}
}
package seronis.reddit.prompts.dice111;
import java.util.ArrayList;
/**
* Created by seronis on 11/26/14.
*/
public class StrategyHuman extends Dice111Strategy {
public StrategyHuman(Dice111Player _p) {
super(_p);
}
@Override
void ChooseName(ArrayList<Dice111Player> players, Dice111 game) {
while(true) {
game.print( player.name + " what is your name? ");
String input = game.cin.nextLine();
//TODO: check that name isnt already used
if( input.length() < 1 || input.length() > 16 ) {
game.println("Please enter a name between 1 and 16 characters long");
continue;
}
player.name = input;
return;
}
}
@Override
void TakeTurn(ArrayList<Dice111Player> players, Dice111 game) {
int tempScore = 0;
while( tempScore >= 0 ) {
game.println("\nYou have earned " + tempScore + " points this turn. Choose an option.");
if( tempScore > 0 )
game.print(" 1: roll 2: pass 9: quit \n\nYour choice? ");
else
game.print(" 1: roll 9: quit \n\nYour choice? ");
String input = game.cin.nextLine();
if( input.equalsIgnoreCase("1") || input.equalsIgnoreCase("roll") ) {
int roll = game.roller.Roll();
if( roll == 1 ) {
game.println("You rolled a 1 ending your turn.");
tempScore = -1;
} else {
tempScore += roll;
game.println("You rolled a " + roll);
}
continue;
}
if( tempScore > 0 )
if( input.equalsIgnoreCase("2") || input.equalsIgnoreCase("pass") ) {
player.score += tempScore;
tempScore = -1;
game.println("Your new total score is " + player.score + "\n");
continue;
}
if( input.equalsIgnoreCase("9") || input.equalsIgnoreCase("quit") ) {
player.score = -1;
tempScore = -1;
game.println("You have forfeited the game.");
continue;
}
game.println("* ??? *");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment