INIT: game playable, but code messy, contains boilerplate, terminal cooked mode is anoying and should be set to raw
This commit is contained in:
commit
2746a14af9
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.class
|
460
Cubez.java
Normal file
460
Cubez.java
Normal file
@ -0,0 +1,460 @@
|
||||
public class Cubez {
|
||||
|
||||
private static boolean done;
|
||||
private ScoreSheet sSheet = new ScoreSheet();
|
||||
private int[] cubeCache = new int[5];
|
||||
private int[] cubeStorage = new int[5];
|
||||
private int storedCubez = 0;
|
||||
private int rollsLeft = 3;
|
||||
private int rounds = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(" ___ _ ____");
|
||||
System.out.println("| _| _ _ | |_ ___ |_ /");
|
||||
System.out.println("| <__| | || . \\/ ._> / /");
|
||||
System.out.println("`___/`___||___/\\___./___|");
|
||||
System.out.print("press return key to start... good luck!");
|
||||
System.console().readLine();
|
||||
|
||||
Cubez oCubez = new Cubez();
|
||||
while(!done) {
|
||||
if(oCubez.sSheet.bonus == 0) oCubez.sSheet.checkBonus();
|
||||
|
||||
oCubez.outputUI();
|
||||
|
||||
System.out.println("score: " + oCubez.sSheet.total + ", turns left: " + (13 - oCubez.rounds) + ", rolls left: " + oCubez.rollsLeft + "\n\n");
|
||||
System.out.println("[s]tore cube [c]ache cube [r]oll cubez, ");
|
||||
System.out.println("[m]egastore [o]mnicache ");
|
||||
System.out.println("[v]iew scores [e]nd turn [q]uit Cubez\n");
|
||||
System.out.print("Pick action: ");
|
||||
|
||||
oCubez.handleUserInput();
|
||||
oCubez.checkRounds();
|
||||
}
|
||||
|
||||
oCubez.sSheet.printScores();
|
||||
System.out.println("\nBetter luck next time!\n\n");
|
||||
}
|
||||
|
||||
public void clearScreen() {
|
||||
System.out.print("\033[H\033[2J");
|
||||
System.out.flush();
|
||||
}
|
||||
|
||||
public int rollD6() {
|
||||
int tmp = ((int)(Math.random() * 6)) + 1;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void rollCache() {
|
||||
int rolls = 5 - storedCubez;
|
||||
for(int i=0; i<5; i++) {
|
||||
cubeCache[i] = rollD6();
|
||||
if(i>(rolls-1)) cubeCache[i] = 0;
|
||||
}
|
||||
rollsLeft = rollsLeft-1;
|
||||
}
|
||||
|
||||
public void outputCache() {
|
||||
for(int i=0; i<cubeCache.length; i++) {
|
||||
System.out.print("[" + cubeCache[i] + "] ");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public void outputStorage() {
|
||||
for(int i=0; i<cubeStorage.length; i++) {
|
||||
System.out.print("{" + cubeStorage[i] + "} ");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public void outputUI () {
|
||||
clearScreen();
|
||||
System.out.println("(1) (2) (3) (4) (5) \n");
|
||||
outputCache();
|
||||
System.out.println("\n");
|
||||
outputStorage();
|
||||
System.out.println("\n");
|
||||
}
|
||||
|
||||
public void storeCube() {
|
||||
outputUI();
|
||||
System.out.print("Pick cube to store: ");
|
||||
String tmpIn = System.console().readLine();
|
||||
int pos = Integer.parseInt(tmpIn);
|
||||
if(pos<1 || pos>5) return;
|
||||
if(cubeCache[pos-1] == 0) return;
|
||||
|
||||
int freePos = -1;
|
||||
for(int i=0; i<cubeStorage.length; i++) {
|
||||
if(cubeStorage[i] == 0) {
|
||||
freePos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cubeStorage[freePos] = cubeCache[pos-1];
|
||||
cubeCache[pos-1] = 0;
|
||||
storedCubez = storedCubez+1;
|
||||
}
|
||||
|
||||
public void cacheCube() {
|
||||
outputUI();
|
||||
System.out.print("Pick cube to cache: ");
|
||||
String tmpIn = System.console().readLine();
|
||||
int pos = Integer.parseInt(tmpIn);
|
||||
if(pos<1 || pos>5) return;
|
||||
if(cubeStorage[pos-1] == 0) return;
|
||||
|
||||
int freePos = -1;
|
||||
for(int i=0; i<cubeCache.length; i++) {
|
||||
if(cubeCache[i] == 0) {
|
||||
freePos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cubeCache[freePos] = cubeStorage[pos-1];
|
||||
cubeStorage[pos-1] = 0;
|
||||
storedCubez = storedCubez-1;
|
||||
}
|
||||
|
||||
public void endTurn() {
|
||||
sSheet.setScore(cubeStorage);
|
||||
|
||||
for(int i=0;i<cubeStorage.length;i++) {
|
||||
cubeStorage[i] = 0;
|
||||
cubeCache[i] = 0;
|
||||
}
|
||||
rollsLeft = 3;
|
||||
storedCubez = 0;
|
||||
}
|
||||
|
||||
public void quit() {
|
||||
clearScreen();
|
||||
sSheet.printScores();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public void handleUserInput() {
|
||||
String uInput = System.console().readLine();
|
||||
switch(uInput) {
|
||||
case "s": storeCube(); break;
|
||||
case "c": cacheCube(); break;
|
||||
case "r": if(rollsLeft>0) rollCache(); break;
|
||||
case "v": sSheet.printScores(); System.console().readLine(); break;
|
||||
case "e": endTurn(); break;
|
||||
case "q": quit(); break;
|
||||
case "o": transferCubez(cubeStorage, cubeCache); break;
|
||||
case "m": transferCubez(cubeCache, cubeStorage); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
public void checkRounds() {
|
||||
if(rounds == 13) done = true;
|
||||
}
|
||||
|
||||
private int sum(int[] arr) {
|
||||
int s=0;
|
||||
for(int i=0;i<arr.length;i++) { s = s + arr[i]; }
|
||||
return s;
|
||||
}
|
||||
|
||||
public void transferCubez(int[] arrA, int[] arrB) {
|
||||
while(sum(arrA) > 0) {
|
||||
for(int i=0;i<arrA.length;i++) {
|
||||
if(arrA[i] != 0) {
|
||||
for(int j=0;j<arrB.length;j++) {
|
||||
if(arrB[j] == 0) {
|
||||
arrB[j] = arrA[i];
|
||||
arrA[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
storedCubez = 0;
|
||||
}
|
||||
|
||||
|
||||
private class ScoreSheet {
|
||||
private int aces=0;
|
||||
private int twos=0;
|
||||
private int threes=0;
|
||||
private int fours=0;
|
||||
private int fives=0;
|
||||
private int sixes=0;
|
||||
private int bonus=0;
|
||||
private int threeOAK=0;
|
||||
private int fourOAK=0;
|
||||
private int fullHouse=0;
|
||||
private int smallStraight=0;
|
||||
private int largeStraight=0;
|
||||
private int chance=0;
|
||||
private int pentaCube=0;
|
||||
private int total=0;
|
||||
|
||||
public void printScores() {
|
||||
System.out.println("Aces: " + this.aces);
|
||||
System.out.println("Twos: " + this.twos);
|
||||
System.out.println("Threes: " + this.threes);
|
||||
System.out.println("Fours: " + this.fours);
|
||||
System.out.println("Fives: " + this.fives);
|
||||
System.out.println("Sixes: " + this.sixes);
|
||||
System.out.println("Bonus: " + this.bonus);
|
||||
System.out.println("---");
|
||||
System.out.println("Triple: " + this.threeOAK);
|
||||
System.out.println("Quadruple: " + this.fourOAK);
|
||||
System.out.println("Fulltime: " + this.fullHouse);
|
||||
System.out.println("Escalator: " + this.smallStraight);
|
||||
System.out.println("Elevator: " + this.largeStraight);
|
||||
System.out.println("Chance: " + this.chance);
|
||||
System.out.println("PENTACUBE: " + this.pentaCube);
|
||||
System.out.println("---");
|
||||
System.out.println("Total: " + this.total);
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
public void checkBonus() {
|
||||
int[] numArr = { this.aces, this.twos, this.threes, this.fours, this.fives, this.sixes };
|
||||
int numSum = 0;
|
||||
|
||||
for(int i=0;i<numArr.length;i++) {
|
||||
numSum = numSum + numArr[i];
|
||||
}
|
||||
|
||||
if(numSum >= 63) this.bonus = 35;
|
||||
}
|
||||
|
||||
public void setScore(int[] store) {
|
||||
clearScreen();
|
||||
sSheet.printScores();
|
||||
outputStorage();
|
||||
System.out.print("\nWhat score to set? [type name] ");
|
||||
String choice = System.console().readLine();
|
||||
|
||||
switch (choice) {
|
||||
case "aces": setNumbers(store, 1); break;
|
||||
case "twos": setNumbers(store, 2); break;
|
||||
case "threes": setNumbers(store, 3); break;
|
||||
case "fours": setNumbers(store, 4); break;
|
||||
case "fives": setNumbers(store, 5); break;
|
||||
case "sixes": setNumbers(store, 6); break;
|
||||
case "triple": setTuple(store, 3); break;
|
||||
case "quadruple": setTuple(store, 4); break;
|
||||
case "fulltime": setFulltime(store); break;
|
||||
case "escalator": setEscalator(store); break;
|
||||
case "elevator": setElevator(store); break;
|
||||
case "pentacube": setPENTACUBE(store); break;
|
||||
case "chance": setChance(store); break;
|
||||
default: setScore(store); break;
|
||||
}
|
||||
}
|
||||
|
||||
public void setNumbers(int[] store, int number) {
|
||||
int scoreTS = 0;
|
||||
String yN = "n";
|
||||
|
||||
for (int i=0; i<store.length; i++) {
|
||||
if (store[i] == number) scoreTS = scoreTS + store[i];
|
||||
}
|
||||
|
||||
switch (number) {
|
||||
case 1:
|
||||
System.out.print("Set " + scoreTS + " for \"Aces\"? [y/n] ");
|
||||
yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.aces = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
break;
|
||||
case 2:
|
||||
System.out.print("Set " + scoreTS + " for \"Twos\"? [y/n] ");
|
||||
yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.twos = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
break;
|
||||
case 3:
|
||||
System.out.print("Set " + scoreTS + " for \"Threes\"? [y/n] ");
|
||||
yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.threes = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
break;
|
||||
case 4:
|
||||
System.out.print("Set " + scoreTS + " for \"Fours\"? [y/n] ");
|
||||
yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.fours = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
break;
|
||||
case 5:
|
||||
System.out.print("Set " + scoreTS + " for \"Fives\"? [y/n] ");
|
||||
yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.fives = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
break;
|
||||
case 6:
|
||||
System.out.print("Set " + scoreTS + " for \"Sixes\"? [y/n] ");
|
||||
yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.sixes = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void setTuple(int[] store, int kind) {
|
||||
int scoreTS = 0;
|
||||
int[] freq = new int[7];
|
||||
String kindText = ( (kind == 3) ? "Triple" : "Quadruple" );
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
freq[store[i]] = freq[store[i]] + 1;
|
||||
}
|
||||
|
||||
for(int i=0;i<freq.length;i++) {
|
||||
if(freq[i] == kind) scoreTS = kind * i;
|
||||
}
|
||||
|
||||
if(scoreTS > 0) {
|
||||
scoreTS = 0;
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
scoreTS = scoreTS + store[i];
|
||||
}
|
||||
}
|
||||
|
||||
System.out.print("Set " + scoreTS + " for \"" + kindText + "\"? [y/n] ");
|
||||
String yN = System.console().readLine();
|
||||
if(yN.contains("y")) {
|
||||
if(kind == 3) {
|
||||
this.threeOAK = scoreTS;
|
||||
}
|
||||
else {
|
||||
this.fourOAK = scoreTS;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.setScore(store); return;
|
||||
}
|
||||
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
}
|
||||
|
||||
public void setFulltime(int[] store) {
|
||||
int[] freq = new int[7];
|
||||
boolean triplet = false;
|
||||
boolean tuplet = false;
|
||||
int scoreTS = 0;
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
freq[store[i]] = freq[store[i]] + 1;
|
||||
}
|
||||
|
||||
for(int i=0;i<freq.length;i++) {
|
||||
if(freq[i] == 3) triplet = true;
|
||||
if(freq[i] == 2) tuplet = true;
|
||||
}
|
||||
|
||||
if (triplet && tuplet) {
|
||||
scoreTS = 25;
|
||||
}
|
||||
|
||||
System.out.print("Set " + scoreTS + " for \"Fulltime\"? [y/n] ");
|
||||
String yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.fullHouse = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
}
|
||||
|
||||
public void setEscalator(int[] store) {
|
||||
int[] freq = new int[7];
|
||||
int streak = 0;
|
||||
int scoreTS = 0;
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
freq[store[i]] = freq[store[i]] + 1;
|
||||
}
|
||||
|
||||
for(int i=1;i<freq.length;i++) {
|
||||
if(freq[i] == 1) streak = streak + 1;
|
||||
}
|
||||
|
||||
if (streak >= 4) {
|
||||
scoreTS = 30;
|
||||
}
|
||||
|
||||
System.out.print("Set " + scoreTS + " for \"Escalator\"? [y/n] ");
|
||||
String yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.smallStraight = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
}
|
||||
|
||||
public void setElevator(int[] store) {
|
||||
int[] freq = new int[7];
|
||||
int streak = 0;
|
||||
int scoreTS = 0;
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
freq[store[i]] = freq[store[i]] + 1;
|
||||
}
|
||||
|
||||
for(int i=1;i<freq.length;i++) {
|
||||
if(freq[i] == 1) streak = streak + 1;
|
||||
}
|
||||
|
||||
if (streak >= 5) {
|
||||
scoreTS = 40;
|
||||
}
|
||||
|
||||
System.out.print("Set " + scoreTS + " for \"Elevator\"? [y/n] ");
|
||||
String yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.largeStraight = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
}
|
||||
|
||||
public void setPENTACUBE(int[] store) {
|
||||
int[] freq = new int[7];
|
||||
int scoreTS = 0;
|
||||
boolean penta = false;
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
freq[store[i]] = freq[store[i]] + 1;
|
||||
}
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
if(freq[store[i]] == 5) penta = true;
|
||||
}
|
||||
|
||||
if(penta) scoreTS = 50;
|
||||
|
||||
System.out.print("Set " + scoreTS + " for \"PENTACUBE\"? [y/n] ");
|
||||
String yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.pentaCube = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
}
|
||||
|
||||
public void setChance(int[] store) {
|
||||
int scoreTS = 0;
|
||||
|
||||
for(int i=0;i<store.length;i++) {
|
||||
scoreTS = scoreTS + store[i];
|
||||
}
|
||||
|
||||
System.out.print("Set " + scoreTS + " for \"Chance\"? [y/n] ");
|
||||
String yN = System.console().readLine();
|
||||
if(yN.contains("y")) { this.chance = scoreTS; } else { this.setScore(store); return; }
|
||||
this.total = this.total + scoreTS;
|
||||
rounds = rounds + 1;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user