Created
November 26, 2014 09:16
-
-
Save seronis/dba14ebb2af6e4e54274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package seronis.reddit.prompts; | |
import seronis.reddit.Main; | |
import seronis.reddit.util.DieNumeric; | |
/** | |
* Created by seronis on 11/26/14. | |
*/ | |
public class Dice111 extends Main { | |
boolean finished; | |
private int numPlayers; | |
private int[] scores; | |
private int highScore; | |
private int forfeits; | |
private int winners; | |
private int curRound; | |
private DieNumeric roller; | |
public Dice111() { | |
super(); | |
roller = new DieNumeric(6); | |
} | |
private void init() { | |
finished = false; | |
numPlayers = 0; | |
curRound = 0; | |
} | |
@Override | |
public void run(String[] args) { | |
while(!finished) { | |
init(); | |
playGame(); | |
} | |
} | |
private void playGame() { | |
println("Welcome to Eleventy-One !!!"); | |
while(numPlayers == 0) { | |
print("How many numPlayers will be playing? (2-6 or 0 to quit) "); | |
if( cin.hasNext() ) { | |
if( cin.hasNextInt() ) { | |
numPlayers = cin.nextInt(); | |
if( numPlayers >= 2 && numPlayers <= 6 ) { | |
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 6."); | |
} | |
scores = new int[numPlayers]; | |
showRules(); | |
while( !checkGameOver() ) | |
playRound(); | |
} | |
private void playRound() { | |
println("Beginning round " + ++curRound ); | |
for( int player=0; player < scores.length; ++player ) { | |
if( scores[player] == -1 ) { | |
println("Skipping player " + (player+1) + "'s turn"); | |
continue; | |
} | |
if( highScore >= 111 && scores[player] != highScore ) { | |
println("Skipping player " + (player+1) + "'s turn"); | |
continue; | |
} | |
println("\nNow player " + (player+1) + "'s turn. Current score is " + scores[player]); | |
int tempScore = 0; | |
while( tempScore >= 0 ) { | |
println("\nYou have earned " + tempScore + " points this turn. Choose an option."); | |
if( tempScore > 0 ) | |
print(" 1: roll 2: pass 9: quit \n\nYour choice? "); | |
else | |
print(" 1: roll 9: quit \n\nYour choice? "); | |
String input = cin.nextLine(); | |
if( input.equalsIgnoreCase("1") || input.equalsIgnoreCase("roll") ) { | |
int roll = roller.Roll(); | |
if( roll == 1 ) { | |
println("You rolled a 1 ending your turn."); | |
tempScore = -1; | |
} else { | |
tempScore += roll; | |
println("You rolled a " + roll); | |
} | |
continue; | |
} | |
if( tempScore > 0 ) | |
if( input.equalsIgnoreCase("2") || input.equalsIgnoreCase("pass") ) { | |
scores[player] += tempScore; | |
tempScore = -1; | |
println("Your new total score is " + scores[player] + "\n"); | |
continue; | |
} | |
if( input.equalsIgnoreCase("9") || input.equalsIgnoreCase("quit") ) { | |
scores[player] = -1; | |
tempScore = -1; | |
println("You have forfeited the game."); | |
continue; | |
} | |
println("*" + input.isEmpty() + "*"); | |
} | |
} | |
} | |
private boolean checkGameOver() { | |
winners = 0; | |
forfeits = 0; | |
highScore = 0; | |
for( int score: scores ) { | |
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 >= 111 ) { | |
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(int player=0; player<scores.length; ++player) { | |
println("Player " + player + ": " + scores[player]); | |
} 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. " | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package seronis.reddit.util; | |
import java.util.Random; | |
/** | |
* Created by seronis on 11/26/14. | |
*/ | |
public abstract class Die { | |
protected int value; | |
protected int sides; | |
protected boolean froze; | |
protected Random rand = new Random(); | |
protected Die(int _sides) { | |
this.froze = false; | |
this.sides = _sides; | |
this.value = 0; | |
} | |
public int Roll() { | |
if( !froze ) { | |
value = rand.nextInt(sides); | |
} | |
return GetValue(); | |
} | |
public abstract int GetValue(); | |
public abstract String Display(); | |
public void Lock() { | |
froze = true; | |
} | |
public void Unlock() { | |
froze = false; | |
} | |
public boolean isLocked() { | |
return froze; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package seronis.reddit.util; | |
/** | |
* Created by seronis on 11/26/14. | |
*/ | |
public class DieNumeric extends Die { | |
public DieNumeric(int _sides) { | |
super(_sides); | |
} | |
@Override | |
public int GetValue() { | |
return value+1; | |
} | |
@Override | |
public String Display() { | |
return "" + GetValue(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package seronis.reddit; | |
import seronis.reddit.prompts.Dice111; | |
import java.util.Scanner; | |
public abstract class Main { | |
protected Scanner cin; | |
protected Main() { | |
cin = new Scanner(System.in); | |
} | |
public static void main(String[] args) { | |
Main theApp = new Dice111(); | |
theApp.run(args); | |
} | |
public abstract void run(String[] args); | |
public static void print(String msg) { | |
System.out.print(msg); | |
} | |
public static void println(String msg) { | |
System.out.println(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment