Cubez/Cubez.java
Daniel Schneider 40a19f0401 another test
2025-03-12 11:42:30 +01:00

490 lines
15 KiB
Java

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.clearScreen();
System.out.println("\nAll turns used up! Now for the result...\n\n");
oCubez.sSheet.printScores();
if(oCubez.sSheet.total<100) System.out.println("\nMy brother in christ, did you even try?\n\n");
if(oCubez.sSheet.total<200 && oCubez.sSheet.total >= 100) System.out.println("\nMaybe try that again?\n\n");
if(oCubez.sSheet.total<300 && oCubez.sSheet.total >= 200) System.out.println("\nBetter luck next time!\n\n");
if(oCubez.sSheet.total >= 300) System.out.println("\nHoly cow, that is a number!\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(z) to store: ");
String tmpIn = System.console().readLine();
int operations = tmpIn.length();
if(operations>5 || operations<=0) return;
for(int i=0;i<operations;i++) {
int pos = tmpIn.charAt(i) - '0';
if(pos<1 || pos>5) return;
if(cubeCache[pos-1] == 0) continue;
int freePos = -1;
for(int j=0; j<cubeStorage.length; j++) {
if(cubeStorage[j] == 0) {
freePos = j;
break;
}
}
cubeStorage[freePos] = cubeCache[pos-1];
cubeCache[pos-1] = 0;
storedCubez = storedCubez+1;
}
}
public void cacheCube() {
outputUI();
System.out.print("Pick cube(z) to cache: ");
String tmpIn = System.console().readLine();
int operations = tmpIn.length();
if(operations>5 || operations<=0) return;
for(int i=0;i<operations;i++) {
int pos = tmpIn.charAt(i) - '0';
if(pos<1 || pos>5) return;
if(cubeStorage[pos-1] == 0) return;
int freePos = -1;
for(int j=0; j<cubeCache.length; j++) {
if(cubeCache[j] == 0) {
freePos = j;
break;
}
}
cubeCache[freePos] = cubeStorage[pos-1];
cubeStorage[pos-1] = 0;
storedCubez = storedCubez-1;
}
}
public void endTurn() {
boolean setScoreSuccessful = sSheet.setScore(cubeStorage);
if(setScoreSuccessful) {
for(int i=0;i<cubeStorage.length;i++) {
cubeStorage[i] = 0;
cubeCache[i] = 0;
}
rollsLeft = 3;
storedCubez = 0;
rounds = rounds + 1;
}
}
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); storedCubez = 0; break;
case "m": transferCubez(cubeCache, cubeStorage); storedCubez = 5; 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;
}
}
}
}
}
}
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 boolean setScore(int[] store) {
clearScreen();
sSheet.printScores();
outputStorage();
System.out.print("\nWhat score to set? [type name] ");
String choice = System.console().readLine();
boolean sSuccess = false;
switch (choice) {
case "aces": sSuccess = setNumbers(store, 1); return sSuccess;
case "twos": sSuccess = setNumbers(store, 2); return sSuccess;
case "threes": sSuccess = setNumbers(store, 3); return sSuccess;
case "fours": sSuccess = setNumbers(store, 4); return sSuccess;
case "fives": sSuccess = setNumbers(store, 5); return sSuccess;
case "sixes": sSuccess = setNumbers(store, 6); return sSuccess;
case "triple": sSuccess = setTuple(store, 3); return sSuccess;
case "quadruple": sSuccess = setTuple(store, 4); return sSuccess;
case "fulltime": sSuccess = setFulltime(store); return sSuccess;
case "escalator": sSuccess = setEscalator(store); return sSuccess;
case "elevator": sSuccess = setElevator(store); return sSuccess;
case "pentacube": sSuccess = setPENTACUBE(store); return sSuccess;
case "chance": sSuccess = setChance(store); return sSuccess;
case "": return false;
default: setScore(store); break;
}
return false;
}
public boolean setNumbers(int[] store, int number) {
int scoreTS = 0;
String yN = "n";
boolean sSuccess = false;
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 { sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
case 2:
System.out.print("Set " + scoreTS + " for \"Twos\"? [y/n] ");
yN = System.console().readLine();
if(yN.contains("y")) { this.twos = scoreTS; } else { sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
case 3:
System.out.print("Set " + scoreTS + " for \"Threes\"? [y/n] ");
yN = System.console().readLine();
if(yN.contains("y")) { this.threes = scoreTS; } else { sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
case 4:
System.out.print("Set " + scoreTS + " for \"Fours\"? [y/n] ");
yN = System.console().readLine();
if(yN.contains("y")) { this.fours = scoreTS; } else { sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
case 5:
System.out.print("Set " + scoreTS + " for \"Fives\"? [y/n] ");
yN = System.console().readLine();
if(yN.contains("y")) { this.fives = scoreTS; } else { sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
case 6:
System.out.print("Set " + scoreTS + " for \"Sixes\"? [y/n] ");
yN = System.console().readLine();
if(yN.contains("y")) { this.sixes = scoreTS; } else { sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
default:
return false;
}
}
public boolean 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(kind == 3) {
if(freq[i] >= 3) scoreTS = kind * i;
}
else {
if(freq[i] >= 4) 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 >= 4) {
this.fourOAK = scoreTS;
}
else {
this.threeOAK = scoreTS;
}
}
else {
boolean sSuccess = this.setScore(store); return sSuccess;
}
this.total = this.total + scoreTS;
return true;
}
public boolean 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 { boolean sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
}
public boolean 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) break;
if(freq[i] == 0) streak = 0;
}
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 { boolean sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
}
public boolean 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) break;
if(freq[i] == 0) streak = 0;
}
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 { boolean sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
}
public boolean 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 { boolean sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
}
public boolean 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 { boolean sSuccess = this.setScore(store); return sSuccess; }
this.total = this.total + scoreTS;
return true;
}
}
}