Skip to content

Commit

Permalink
sc: no line found exception solved
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmp33 committed Sep 4, 2021
1 parent 7793fd7 commit ac3e725
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 28 deletions.
Binary file modified out/artifacts/la_brisca_cli_jar/la-brisca-cli.jar
Binary file not shown.
10 changes: 4 additions & 6 deletions src/com/labrisca/App.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.labrisca;

import java.util.Scanner;
import com.labrisca.english.io.UserInput;

public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose language: Catalan or English");
while (true) {
System.out.print("> ");
String input = sc.nextLine().trim().toLowerCase();
String input = UserInput.getInput();

// initialize and run game
if (input.equals("catalan")) {
if (input.equals("catalan") || input.equals("cat")) {
System.out.println();
new com.labrisca.catalan.Game().run();
break;
} else if (input.equals("english")) {
} else if (input.equals("english") || input.equals("en")) {
System.out.println();
new com.labrisca.english.Game().run();
break;
Expand Down
32 changes: 21 additions & 11 deletions src/com/labrisca/catalan/io/UserInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@ public class UserInput {

UserInput() {}

// get user input
public static String getInput() {
String input = "";
System.out.print("> " + Color.ANSI_CYAN);

// check if program is forced to close
if (SC.hasNextLine()) {
input = SC.nextLine().trim().toLowerCase();
System.out.print(Color.ANSI_RESET);
} else {
System.out.print(Color.ANSI_RESET);
System.exit(0);
}
return input;
}

// ask the game mode to the user
public static String askGameMode() {
System.out.println("[?] Activar el mode hacker?");
while (true) {
System.out.print("> ");
String input = SC.nextLine().trim().toLowerCase();
String input = getInput();
if (input.equals("si") || input.equals("1")) {
System.out.println("Mode hacker activat!");
System.out.print("Text" + Color.ANSI_PURPLE + " lila " + Color.ANSI_RESET);
Expand All @@ -35,8 +50,7 @@ public static String askGameMode() {
public static boolean askAIBot() {
System.out.println("[?] Fer que el bot sigui intel·ligent?");
while (true) {
System.out.print("> ");
String input = SC.nextLine().trim().toLowerCase();
String input = getInput();
if (input.equals("si") || input.equals("1")) {
System.out.println("Perdràs :P!");
System.out.println();
Expand All @@ -54,8 +68,7 @@ public static boolean askAIBot() {
public static boolean askPrintCardsInfo() {
System.out.println("\n[?] Veure informació final de les cartes?");
while (true) {
System.out.print("> ");
String input = SC.nextLine().trim().toLowerCase();
String input = getInput();
if (input.equals("si") || input.equals("1")) return true;
else if (input.equals("no") || input.equals("0")) return false;
System.out.println("Introdueix \"si\" o \"no\"...");
Expand All @@ -64,12 +77,9 @@ public static boolean askPrintCardsInfo() {

// ask which card must be thrown or / and change latest card
public static String askThrowCard(Player p) {
String input;
while (true) {
System.out.print("> ");
input = SC.nextLine().trim();

// check if player wants and can change latest card
String input = getInput();
// if player wants and can change latest card
if (input.equals("7") && p.canChangeLastCard()) {
p.changeLastCard();
p.printCardsInHand();
Expand Down
1 change: 1 addition & 0 deletions src/com/labrisca/catalan/util/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Color {
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";

Color() {}

Expand Down
32 changes: 21 additions & 11 deletions src/com/labrisca/english/io/UserInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@ public class UserInput {

UserInput() {}

// get user input
public static String getInput() {
String input = "";
System.out.print("> " + Color.ANSI_CYAN);

// check if program is forced to close
if (SC.hasNextLine()) {
input = SC.nextLine().trim().toLowerCase();
System.out.print(Color.ANSI_RESET);
} else {
System.out.print(Color.ANSI_RESET);
System.exit(0);
}
return input;
}

// ask the game mode to the user
public static String askGameMode() {
System.out.println("[?] Enable hacker mode?");
while (true) {
System.out.print("> ");
String input = SC.nextLine().trim().toLowerCase();
String input = getInput();
if (input.equals("yes") || input.equals("1")) {
System.out.println("Hacker mode on!");
System.out.print(Color.ANSI_PURPLE + "Purple " + Color.ANSI_RESET);
Expand All @@ -35,8 +50,7 @@ public static String askGameMode() {
public static boolean askAIBot() {
System.out.println("[?] Enable bot's AI?");
while (true) {
System.out.print("> ");
String input = SC.nextLine().trim().toLowerCase();
String input = getInput();
if (input.equals("yes") || input.equals("1")) {
System.out.println("You'll lose :P!");
System.out.println();
Expand All @@ -54,8 +68,7 @@ public static boolean askAIBot() {
public static boolean askPrintCardsInfo() {
System.out.println("\n[?] See cards final information?");
while (true) {
System.out.print("> ");
String input = SC.nextLine().trim().toLowerCase();
String input = getInput();
if (input.equals("yes") || input.equals("1")) return true;
else if (input.equals("no") || input.equals("0")) return false;
System.out.println("Input \"yes\" or \"no\"...");
Expand All @@ -64,12 +77,9 @@ public static boolean askPrintCardsInfo() {

// ask which card must be thrown or / and change latest card
public static String askThrowCard(Player p) {
String input;
while (true) {
System.out.print("> ");
input = SC.nextLine().trim();

// check if player wants and can change latest card
String input = getInput();
// if player wants and can change latest card
if (input.equals("7") && p.canChangeLastCard()) {
p.changeLastCard();
p.printCardsInHand();
Expand Down
1 change: 1 addition & 0 deletions src/com/labrisca/english/util/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Color {
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";

Color() {}

Expand Down

0 comments on commit ac3e725

Please sign in to comment.