Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1025985034682073088
Rollback to Revision 2
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

Here are some suggested changes-

package higher.or.lower.cards;

import java.util.Arrays; import java.util.Scanner;

public class higherOrLower {

int set[] = new int[8];
private int flips = 1;
public higherOrLower() {
 deck deck = new deck();
 for (int i = 0; i < 8; i++) {
 set[i] = deck.draw();
 }
}
public void playGame() {
 Scanner sc = new Scanner(System.in);
 paint();
 for (int i = 0; i < 7; i++) {
 System.out.println("Higher or Lower?");
 String an = sc.nextLine();
 if (an.equalsIgnoreCase("higher") || an.toLowerCase().startsWith("h")) {
 if (set[i] > set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else if (an.equalsIgnoreCase("lower") || an.toLowerCase().startsWith("l")) {
 if (set[i] < set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else {
 System.out.println("Please enter one of the following \nHigher\nhigher\nH\nh\nLower\nlower\nL\nl");
 i--;
 }
 flips++;
 paint();
 }
 System.out.println("You win!");
 endGame();
}
public static void playAgain() {
 higherOrLower HOL = new higherOrLower();
 HOL.playGame();
}
public void flip() {
 flips++;
}
void endGame() {
 Scanner sc = new Scanner(System.in);
 System.out.println("Play again?");
 boolean done = false;
 while (!done) {
 String ans = sc.nextLine();
 switch (ans) {
 case "Yes":
 case "yes":
 case "Y":
 case "y":
 done = true;
 playAgain();
 break;
 case "No":
 case "no":
 case "N":
 case "n":
 done = true;
 System.exit(0);
 break;
 default:
 System.out.println("Please enter one of the following \nYes\nyes\nY\ny\nNo\nno\nN\nn");
 break;
 }
 }
}
public void paint() {
 if (flips == 1) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 2) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 3) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 4) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 5) {
 for (int i = 0; i < 8; i++) {
 graphics(set[0], i);
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 6) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 7) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(-1, i));
 }
 }
 if (flips == 8) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(set[7], i));
 }
 }
}
 String graphics(int card, int row) {
 switch (card) {
 case 1:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|A.......|";
 case 3:
 return "|........|";
 case 4:
 return "|....֍...|";
 case 5:
 return "|........|";
 case 6:
 return "|.......A|";
 case 7:
 return "|________|";
 }
 
 case 2:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|2.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......2|";
 
 case 7:
 return "|________|";
 
 }
 
 case 3:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|3.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......3|";
 
 case 7:
 return "|________|";
 
 }
 
 case 4:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|4.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......4|";
 
 case 7:
 return "|________|";
 
 }
 
 case 5:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|5.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......5|";
 
 case 7:
 return "|________|";
 
 }
 
 case 6:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|6.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......6|";
 
 case 7:
 return "|________|";
 
 }
 
 case 7:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|7.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......7|";
 
 case 7:
 return "|________|";
 
 }
 
 case 8:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|8.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......8|";
 
 case 7:
 return "|________|";
 
 }
 
 case 9:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|9.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......9|";
 
 case 7:
 return "|________|";
 
 }
 
 case 10:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|10......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|......10|";
 
 case 7:
 return "|________|";
 
 }
 
 case 11:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|J.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......J|";
 
 case 7:
 return "|________|";
 
 }
 
 case 12:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|Q.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......Q|";
 
 case 7:
 return "|________|";
 
 }
 
 case 13:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|K.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......K|";
 
 case 7:
 return "|________|";
 
 }
 
 case -1:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "| |";
 
 case 3:
 return "|\\//\\/\\/\\|";
 
 case 4:
 return "|/\\/\\/\\/\\|";
 
 case 5:
 return "|\\//\\/\\/\\|";
 
 case 6:
 return "|/\\/\\/\\/\\|";
 
 case 7:
 return "|________|";
 
 }
 
 
}
 return "";

}

}

Here are some suggested changes-

package higher.or.lower.cards;

import java.util.Arrays; import java.util.Scanner;

public class higherOrLower {

int set[] = new int[8];
private int flips = 1;
public higherOrLower() {
 deck deck = new deck();
 for (int i = 0; i < 8; i++) {
 set[i] = deck.draw();
 }
}
public void playGame() {
 Scanner sc = new Scanner(System.in);
 paint();
 for (int i = 0; i < 7; i++) {
 System.out.println("Higher or Lower?");
 String an = sc.nextLine();
 if (an.equalsIgnoreCase("higher") || an.toLowerCase().startsWith("h")) {
 if (set[i] > set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else if (an.equalsIgnoreCase("lower") || an.toLowerCase().startsWith("l")) {
 if (set[i] < set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else {
 System.out.println("Please enter one of the following \nHigher\nhigher\nH\nh\nLower\nlower\nL\nl");
 i--;
 }
 flips++;
 paint();
 }
 System.out.println("You win!");
 endGame();
}
public static void playAgain() {
 higherOrLower HOL = new higherOrLower();
 HOL.playGame();
}
public void flip() {
 flips++;
}
void endGame() {
 Scanner sc = new Scanner(System.in);
 System.out.println("Play again?");
 boolean done = false;
 while (!done) {
 String ans = sc.nextLine();
 switch (ans) {
 case "Yes":
 case "yes":
 case "Y":
 case "y":
 done = true;
 playAgain();
 break;
 case "No":
 case "no":
 case "N":
 case "n":
 done = true;
 System.exit(0);
 break;
 default:
 System.out.println("Please enter one of the following \nYes\nyes\nY\ny\nNo\nno\nN\nn");
 break;
 }
 }
}
public void paint() {
 if (flips == 1) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 2) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 3) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 4) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 5) {
 for (int i = 0; i < 8; i++) {
 graphics(set[0], i);
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 6) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 7) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(-1, i));
 }
 }
 if (flips == 8) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(set[7], i));
 }
 }
}
 String graphics(int card, int row) {
 switch (card) {
 case 1:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|A.......|";
 case 3:
 return "|........|";
 case 4:
 return "|....֍...|";
 case 5:
 return "|........|";
 case 6:
 return "|.......A|";
 case 7:
 return "|________|";
 }
 
 case 2:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|2.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......2|";
 
 case 7:
 return "|________|";
 
 }
 
 case 3:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|3.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......3|";
 
 case 7:
 return "|________|";
 
 }
 
 case 4:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|4.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......4|";
 
 case 7:
 return "|________|";
 
 }
 
 case 5:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|5.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......5|";
 
 case 7:
 return "|________|";
 
 }
 
 case 6:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|6.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......6|";
 
 case 7:
 return "|________|";
 
 }
 
 case 7:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|7.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......7|";
 
 case 7:
 return "|________|";
 
 }
 
 case 8:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|8.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......8|";
 
 case 7:
 return "|________|";
 
 }
 
 case 9:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|9.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......9|";
 
 case 7:
 return "|________|";
 
 }
 
 case 10:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|10......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|......10|";
 
 case 7:
 return "|________|";
 
 }
 
 case 11:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|J.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......J|";
 
 case 7:
 return "|________|";
 
 }
 
 case 12:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|Q.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......Q|";
 
 case 7:
 return "|________|";
 
 }
 
 case 13:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|K.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......K|";
 
 case 7:
 return "|________|";
 
 }
 
 case -1:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "| |";
 
 case 3:
 return "|\\//\\/\\/\\|";
 
 case 4:
 return "|/\\/\\/\\/\\|";
 
 case 5:
 return "|\\//\\/\\/\\|";
 
 case 6:
 return "|/\\/\\/\\/\\|";
 
 case 7:
 return "|________|";
 
 }
 
 
}
 return "";

}

}

added 17263 characters in body
Source Link
Clint
  • 111
  • 5

Here are some suggested changes-

package higher.or.lower.cards;

import java.util.Arrays; import java.util.Scanner;

public class higherOrLower {

int set[] = new int[8];
private int flips = 1;
public higherOrLower() {
 deck deck = new deck();
 for (int i = 0; i < 8; i++) {
 set[i] = deck.draw();
 }
}
public void playGame() {
 Scanner sc = new Scanner(System.in);
 paint();
 for (int i = 0; i < 7; i++) {
 System.out.println("Higher or Lower?");
 String an = sc.nextLine();
 if (an.equalsIgnoreCase("higher") || an.toLowerCase().startsWith("h")) {
 if (set[i] > set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else if (an.equalsIgnoreCase("lower") || an.toLowerCase().startsWith("l")) {
 if (set[i] < set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else {
 System.out.println("Please enter one of the following \nHigher\nhigher\nH\nh\nLower\nlower\nL\nl");
 i--;
 }
 flips++;
 paint();
 }
 System.out.println("You win!");
 endGame();
}
public static void playAgain() {
 higherOrLower HOL = new higherOrLower();
 HOL.playGame();
}
public void flip() {
 flips++;
}
void endGame() {
 Scanner sc = new Scanner(System.in);
 System.out.println("Play again?");
 boolean done = false;
 while (!done) {
 String ans = sc.nextLine();
 switch (ans) {
 case "Yes":
 case "yes":
 case "Y":
 case "y":
 done = true;
 playAgain();
 break;
 case "No":
 case "no":
 case "N":
 case "n":
 done = true;
 System.exit(0);
 break;
 default:
 System.out.println("Please enter one of the following \nYes\nyes\nY\ny\nNo\nno\nN\nn");
 break;
 }
 }
}
public void paint() {
 if (flips == 1) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 2) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 3) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 4) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 5) {
 for (int i = 0; i < 8; i++) {
 graphics(set[0], i);
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 6) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 7) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(-1, i));
 }
 }
 if (flips == 8) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(set[7], i));
 }
 }
}
 String graphics(int card, int row) {
 switch (card) {
 case 1:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|A.......|";
 case 3:
 return "|........|";
 case 4:
 return "|....֍...|";
 case 5:
 return "|........|";
 case 6:
 return "|.......A|";
 case 7:
 return "|________|";
 }
 
 case 2:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|2.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......2|";
 
 case 7:
 return "|________|";
 
 }
 
 case 3:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|3.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......3|";
 
 case 7:
 return "|________|";
 
 }
 
 case 4:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|4.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......4|";
 
 case 7:
 return "|________|";
 
 }
 
 case 5:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|5.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......5|";
 
 case 7:
 return "|________|";
 
 }
 
 case 6:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|6.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......6|";
 
 case 7:
 return "|________|";
 
 }
 
 case 7:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|7.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......7|";
 
 case 7:
 return "|________|";
 
 }
 
 case 8:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|8.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......8|";
 
 case 7:
 return "|________|";
 
 }
 
 case 9:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|9.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......9|";
 
 case 7:
 return "|________|";
 
 }
 
 case 10:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|10......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|......10|";
 
 case 7:
 return "|________|";
 
 }
 
 case 11:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|J.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......J|";
 
 case 7:
 return "|________|";
 
 }
 
 case 12:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|Q.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......Q|";
 
 case 7:
 return "|________|";
 
 }
 
 case 13:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|K.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......K|";
 
 case 7:
 return "|________|";
 
 }
 
 case -1:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "| |";
 
 case 3:
 return "|\\//\\/\\/\\|";
 
 case 4:
 return "|/\\/\\/\\/\\|";
 
 case 5:
 return "|\\//\\/\\/\\|";
 
 case 6:
 return "|/\\/\\/\\/\\|";
 
 case 7:
 return "|________|";
 
 }
 
 
}
 return "";

}

}

Here are some suggested changes-

package higher.or.lower.cards;

import java.util.Arrays; import java.util.Scanner;

public class higherOrLower {

int set[] = new int[8];
private int flips = 1;
public higherOrLower() {
 deck deck = new deck();
 for (int i = 0; i < 8; i++) {
 set[i] = deck.draw();
 }
}
public void playGame() {
 Scanner sc = new Scanner(System.in);
 paint();
 for (int i = 0; i < 7; i++) {
 System.out.println("Higher or Lower?");
 String an = sc.nextLine();
 if (an.equalsIgnoreCase("higher") || an.toLowerCase().startsWith("h")) {
 if (set[i] > set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else if (an.equalsIgnoreCase("lower") || an.toLowerCase().startsWith("l")) {
 if (set[i] < set[i + 1]) {
 System.out.println("You Lose");
 endGame();
 }
 } else {
 System.out.println("Please enter one of the following \nHigher\nhigher\nH\nh\nLower\nlower\nL\nl");
 i--;
 }
 flips++;
 paint();
 }
 System.out.println("You win!");
 endGame();
}
public static void playAgain() {
 higherOrLower HOL = new higherOrLower();
 HOL.playGame();
}
public void flip() {
 flips++;
}
void endGame() {
 Scanner sc = new Scanner(System.in);
 System.out.println("Play again?");
 boolean done = false;
 while (!done) {
 String ans = sc.nextLine();
 switch (ans) {
 case "Yes":
 case "yes":
 case "Y":
 case "y":
 done = true;
 playAgain();
 break;
 case "No":
 case "no":
 case "N":
 case "n":
 done = true;
 System.exit(0);
 break;
 default:
 System.out.println("Please enter one of the following \nYes\nyes\nY\ny\nNo\nno\nN\nn");
 break;
 }
 }
}
public void paint() {
 if (flips == 1) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 2) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(-1, i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 3) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(-1, i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 4) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(-1, i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 5) {
 for (int i = 0; i < 8; i++) {
 graphics(set[0], i);
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(-1, i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 6) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(-1, i), graphics(-1, i));
 }
 }
 if (flips == 7) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(-1, i));
 }
 }
 if (flips == 8) {
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[0], i), graphics(set[1], i), graphics(set[2], i), graphics(set[3], i));
 }
 
 for (int i = 0; i < 8; i++) {
 System.out.printf("%s %s %s %s %n", graphics(set[4], i), graphics(set[5], i), graphics(set[6], i), graphics(set[7], i));
 }
 }
}
 String graphics(int card, int row) {
 switch (card) {
 case 1:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|A.......|";
 case 3:
 return "|........|";
 case 4:
 return "|....֍...|";
 case 5:
 return "|........|";
 case 6:
 return "|.......A|";
 case 7:
 return "|________|";
 }
 
 case 2:
 switch (row) {
 case 1:
 return " ________ ";
 case 2:
 return "|2.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......2|";
 
 case 7:
 return "|________|";
 
 }
 
 case 3:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|3.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......3|";
 
 case 7:
 return "|________|";
 
 }
 
 case 4:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|4.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......4|";
 
 case 7:
 return "|________|";
 
 }
 
 case 5:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|5.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......5|";
 
 case 7:
 return "|________|";
 
 }
 
 case 6:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|6.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......6|";
 
 case 7:
 return "|________|";
 
 }
 
 case 7:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|7.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......7|";
 
 case 7:
 return "|________|";
 
 }
 
 case 8:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|8.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......8|";
 
 case 7:
 return "|________|";
 
 }
 
 case 9:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|9.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......9|";
 
 case 7:
 return "|________|";
 
 }
 
 case 10:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|10......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|......10|";
 
 case 7:
 return "|________|";
 
 }
 
 case 11:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|J.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......J|";
 
 case 7:
 return "|________|";
 
 }
 
 case 12:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|Q.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......Q|";
 
 case 7:
 return "|________|";
 
 }
 
 case 13:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "|K.......|";
 
 case 3:
 return "|........|";
 
 case 4:
 return "|....֍...|";
 
 case 5:
 return "|........|";
 
 case 6:
 return "|.......K|";
 
 case 7:
 return "|________|";
 
 }
 
 case -1:
 switch (row) {
 case 1:
 return " ________ ";
 
 case 2:
 return "| |";
 
 case 3:
 return "|\\//\\/\\/\\|";
 
 case 4:
 return "|/\\/\\/\\/\\|";
 
 case 5:
 return "|\\//\\/\\/\\|";
 
 case 6:
 return "|/\\/\\/\\/\\|";
 
 case 7:
 return "|________|";
 
 }
 
 
}
 return "";

}

}

deleted 17 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Java version of card game "higher or lower". I was wondering if anyone saw any optimizations or just what you thought of the code

First of all, I have my basic main method which does pretty much nothing.:

Then I have the class and methods where all the action happens.:

And after that I have one other class, the deck.:

Java version of card game "higher or lower". I was wondering if anyone saw any optimizations or just what you thought of the code

First of all, I have my basic main method which does pretty much nothing.

Then I have the class and methods where all the action happens.

And after that I have one other class, the deck.

Java version of card game "higher or lower"

First of all, I have my basic main method which does pretty much nothing:

Then I have the class and methods where all the action happens:

And after that I have one other class, the deck:

Source Link
Clint
  • 111
  • 5
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /