From b6dd1951ecd453d5e15af5f18166a330f4a53d6f Mon Sep 17 00:00:00 2001 From: ericmp Date: Thu, 2 Sep 2021 10:46:39 +0200 Subject: [PATCH] refactoring classes and optimizing --- src/com/labrisca/App.java | 10 +- src/com/labrisca/catalan/Card.java | 3 +- src/com/labrisca/catalan/Game.java | 150 ++++++------------ src/com/labrisca/catalan/entities/Human.java | 74 --------- .../catalan/{entities => entity}/AI.java | 16 +- .../catalan/{entities => entity}/Bot.java | 8 +- src/com/labrisca/catalan/entity/Human.java | 46 ++++++ .../catalan/{entities => entity}/Player.java | 8 +- src/com/labrisca/catalan/io/UserInput.java | 90 +++++++++++ .../labrisca/catalan/{ => util}/Color.java | 16 +- src/com/labrisca/catalan/util/Str.java | 15 ++ src/com/labrisca/english/Card.java | 3 +- src/com/labrisca/english/Game.java | 150 ++++++------------ src/com/labrisca/english/entities/Human.java | 74 --------- .../english/{entities => entity}/AI.java | 16 +- .../english/{entities => entity}/Bot.java | 8 +- src/com/labrisca/english/entity/Human.java | 46 ++++++ .../english/{entities => entity}/Player.java | 8 +- src/com/labrisca/english/io/UserInput.java | 90 +++++++++++ .../labrisca/english/{ => util}/Color.java | 16 +- src/com/labrisca/english/util/Str.java | 15 ++ 21 files changed, 446 insertions(+), 416 deletions(-) delete mode 100644 src/com/labrisca/catalan/entities/Human.java rename src/com/labrisca/catalan/{entities => entity}/AI.java (93%) rename src/com/labrisca/catalan/{entities => entity}/Bot.java (91%) create mode 100644 src/com/labrisca/catalan/entity/Human.java rename src/com/labrisca/catalan/{entities => entity}/Player.java (95%) create mode 100644 src/com/labrisca/catalan/io/UserInput.java rename src/com/labrisca/catalan/{ => util}/Color.java (89%) create mode 100644 src/com/labrisca/catalan/util/Str.java delete mode 100644 src/com/labrisca/english/entities/Human.java rename src/com/labrisca/english/{entities => entity}/AI.java (93%) rename src/com/labrisca/english/{entities => entity}/Bot.java (91%) create mode 100644 src/com/labrisca/english/entity/Human.java rename src/com/labrisca/english/{entities => entity}/Player.java (95%) create mode 100644 src/com/labrisca/english/io/UserInput.java rename src/com/labrisca/english/{ => util}/Color.java (90%) create mode 100644 src/com/labrisca/english/util/Str.java diff --git a/src/com/labrisca/App.java b/src/com/labrisca/App.java index fa148ea..1d00501 100644 --- a/src/com/labrisca/App.java +++ b/src/com/labrisca/App.java @@ -13,16 +13,16 @@ public static void main(String[] args) { // initialize game, players and run game if (input.equals("catalan")) { var game = new com.labrisca.catalan.Game(); - var jug = new com.labrisca.catalan.entities.Human("@ericmp33", game); - var bot = new com.labrisca.catalan.entities.Bot(game); + var jug = new com.labrisca.catalan.entity.Human("@ericmp33", game); + var bot = new com.labrisca.catalan.entity.Bot(game); System.out.println(); game.run(jug, bot); break; } else if (input.equals("english")) { var game = new com.labrisca.english.Game(); - var jug = new com.labrisca.english.entities.Human("@ericmp33", game); - var bot = new com.labrisca.english.entities.Bot(game); + var jug = new com.labrisca.english.entity.Human("@ericmp33", game); + var bot = new com.labrisca.english.entity.Bot(game); System.out.println(); game.run(jug, bot); @@ -32,3 +32,5 @@ public static void main(String[] args) { } } } + +// todo: change -> game is ai? nope, bot is ai \ No newline at end of file diff --git a/src/com/labrisca/catalan/Card.java b/src/com/labrisca/catalan/Card.java index 9ed8b0e..da54632 100644 --- a/src/com/labrisca/catalan/Card.java +++ b/src/com/labrisca/catalan/Card.java @@ -1,6 +1,7 @@ package com.labrisca.catalan; -import com.labrisca.catalan.entities.Player; +import com.labrisca.catalan.entity.Player; +import com.labrisca.catalan.util.Color; public class Card { // variables diff --git a/src/com/labrisca/catalan/Game.java b/src/com/labrisca/catalan/Game.java index 74b58b9..d4f0cf4 100644 --- a/src/com/labrisca/catalan/Game.java +++ b/src/com/labrisca/catalan/Game.java @@ -1,13 +1,15 @@ package com.labrisca.catalan; -import com.labrisca.catalan.entities.Player; +import com.labrisca.catalan.entity.Player; +import com.labrisca.catalan.util.Color; +import com.labrisca.catalan.util.Str; +import com.labrisca.catalan.io.UserInput; -import java.util.ArrayList; -import java.util.Collections; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.Scanner; public class Game { // variables @@ -18,7 +20,6 @@ public class Game { private int round; private String gameMode; private boolean ai; // if turn on AI's bot - private static final Scanner SC = new Scanner(System.in); // getters public List getDeck() { return deck; } @@ -26,7 +27,6 @@ public class Game { public int getRound() { return round; } public String getGameMode() { return gameMode; } public boolean isAi() { return ai; } - public static Scanner getSc() { return SC; } // constructor public Game() { @@ -85,7 +85,7 @@ public Game() { new Card("rei d'oro", "oros", 10, 4) )); - // shuffle the deck and initialize the play and list of players + // shuffle deck and initialize the play and list of players Collections.shuffle(deck); thePlay = new ArrayList<>(); players = new ArrayList<>(); @@ -99,61 +99,52 @@ public void welcomePrint() { System.out.println("Canvia l'última carta introduïnt \"7\"\n"); } + // principal method to run the game + public void run(Player human, Player bot) { + // add players into game + players.add(human); + players.add(bot); + + // welcome message + welcomePrint(); + + // set the game mode and AI bot mode + setGameMode(); + setAIBot(); + + // shuffle players list to choose who starts the game, set trump and deal 3 first cards to each player + Collections.shuffle(players); + setTrump(); + deal3FirstCards(); + + // game's main loop + mainLoop(); + + // once game is over, print total points and cards won, game's winner and game time + printPointsAndCards(); + setAndPrintWinner(); + printGameTime(); + + // ask if print all the cards final information + printCardsInfo(); + + // print game's author + printAuthor(); + } + // set the game mode private void setGameMode() { - System.out.println("[?] Activar el mode hacker?"); - while (true) { - System.out.print("> "); - String input = SC.nextLine().trim().toLowerCase(); - if (input.equals("si")) { - System.out.println("Mode hacker activat!"); - System.out.print("Text" + Color.ANSI_PURPLE + " lila " + Color.ANSI_RESET); - System.out.println("= text que no veuries ;)"); - gameMode = "hacker"; - break; - } else if (input.equals("no")) { - System.out.println("Mode normal activat!"); - gameMode = "normal"; - break; - } - System.out.println("Introdueix \"si\" o \"no\"..."); - } - System.out.println(); + gameMode = UserInput.askGameMode(); } // set AI bot mode private void setAIBot() { - System.out.println("[?] Fer que el bot sigui intel·ligent?"); - while (true) { - System.out.print("> "); - String input = SC.nextLine().trim().toLowerCase(); - if (input.equals("si")) { - System.out.println("Perdràs :P!"); - ai = true; - break; - } else if (input.equals("no")) { - System.out.println("Beep beep..."); - ai = false; - break; - } - System.out.println("Introdueix \"si\" o \"no\"..."); - } - System.out.println(); + ai = UserInput.askAIBot(); } // ask if print cards final information - private void askPrintCardsInfo() { - System.out.println("\n[?] Veure informació final de les cartes?"); - while (true) { - System.out.print("> "); - String input = SC.nextLine().trim().toLowerCase(); - if (input.equals("si")) { - System.out.println(); - printAllCards(); - break; - } else if (input.equals("no")) break; - System.out.println("Introdueix \"si\" o \"no\"..."); - } + private void printCardsInfo() { + if (UserInput.askPrintCardsInfo()) printAllCards(); } // deal 3 first cards to each player @@ -162,7 +153,6 @@ private void deal3FirstCards() { // for each player, take 3 cards for (Player p : players) { for (int i = 0; i < 3; i++) p.takeCard(); - // if hacker mode is on, make a print (more friendly look) if (gameMode.equals("hacker")) System.out.println(); } @@ -171,7 +161,7 @@ private void deal3FirstCards() { if (!gameMode.equals("hacker")) System.out.println(); } - // set trump + // set game trump private void setTrump() { Card trumpCard = deck.get(6); deck.remove(6); @@ -195,10 +185,6 @@ private void setTrump() { } } - public static String capitalizeStr(String s) { - return s.substring(0, 1).toUpperCase() + s.substring(1); - } - // print all cards and their attributes private void printAllCards() { System.out.println("Informació de totes les cartes:"); @@ -301,14 +287,9 @@ private void setAndPrintWinner() { if (draw) s = "Empat! Els dos jugadors han fet 60 pedres!"; else s = "El jugador " + winner.getName() + " ha guanyat la partida!!! Ha fet " + winner.getPoints() + " pedres!"; + s = Str.centerStr(ln.length(), s); - System.out.println(Color.colorizeRand(ln) + "\n" + centerStr(ln.length(), s) + "\n" + Color.colorizeRand(ln)); - } - - // returns centered String inside lines - private String centerStr(int lnLength, String s) { - int i = (lnLength - s.length()) / 2; - return " ".repeat(i) + s; + System.out.println(Color.colorizeRand(ln) + "\n" + s + "\n" + Color.colorizeRand(ln)); } // print how much the game took to finish @@ -343,9 +324,7 @@ private void validateThePlay() { Player playWinner = validateThePlayWinner(thePlay.get(0), thePlay.get(1)); // play's winner collects the cards - for (Card card : thePlay) { - playWinner.getWonCards().add(card); - } + for (Card card : thePlay) playWinner.getWonCards().add(card); // remove cards from the play thePlay.clear(); @@ -388,37 +367,4 @@ private Card validateThePlayWinnerCard(Card card0, Card card1) { return card0.isThrownFirst() ? card0 : card1; } } - - // principal method to run the game - public void run(Player human, Player bot) { - // add players into game - players.add(human); - players.add(bot); - - // welcome message - welcomePrint(); - - // set the game mode and AI bot mode - setGameMode(); - setAIBot(); - - // shuffle players list to choose who starts the game, set trump and deal 3 first cards to each player - Collections.shuffle(players); - setTrump(); - deal3FirstCards(); - - // game's main loop - mainLoop(); - - // once game is over, print total points and cards won, game's winner and game time - printPointsAndCards(); - setAndPrintWinner(); - printGameTime(); - - // ask if print all the cards final information - askPrintCardsInfo(); - - // print game's author - printAuthor(); - } } diff --git a/src/com/labrisca/catalan/entities/Human.java b/src/com/labrisca/catalan/entities/Human.java deleted file mode 100644 index e2aaa8d..0000000 --- a/src/com/labrisca/catalan/entities/Human.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.labrisca.catalan.entities; - -import com.labrisca.catalan.Card; -import com.labrisca.catalan.Color; -import com.labrisca.catalan.Game; - -public class Human extends Player { - // constructor - public Human(String name, Game game) { - super(name, game); - } - - @Override - public void takeCard() { - // exit method if there aren't cards in the deck - if (getGame().deckIsEmpty()) return; - - // else, take a random card - Card card = topDeckCard(); - getInHandCards().add(card); - - System.out.println("Has agafat: " + Color.name(card.getName())); - } - - @Override - public void printCardsInHand() { - System.out.println("\n[?] Et toca tirar. Les teves cartes:"); - - for (int i = 0; i < getInHandCards().size(); i++) { - String cardName = Game.capitalizeStr(getInHandCards().get(i).getName()); - System.out.println((i + 1) + ") " + Color.name(cardName)); - } - } - - @Override - public void throwCard(int round) { - // print available in-hand cards to be thrown - printCardsInHand(); - - // ask which card must be thrown or / and change latest card - String input; - label: - while (true) { - System.out.print("> "); - input = Game.getSc().nextLine().trim(); - - // check if player wants and can change latest card - if (input.equals("7") && canChangeLastCard()) { - changeLastCard(); - printCardsInHand(); - } else if (input.equals("7") && !canChangeLastCard()) { - System.out.println("No pots canviar l'última carta..."); - } else { - switch (getInHandCards().size()) { - case 3: - if (input.equals("1") || input.equals("2") || input.equals("3")) break label; - break; - case 2: - if (input.equals("1") || input.equals("2")) break label; - break; - case 1: - if (input.equals("1")) break label; - break; - default: - throw new IllegalStateException("Unexpected inHandCards size"); - } - System.out.println("Introdueix un número vàlid..."); - } - } - - // call common method used by Human and Bot classes with the user's chosen card - commonThrowCard(getInHandCards().get(Integer.parseInt(input) - 1), round); - } -} diff --git a/src/com/labrisca/catalan/entities/AI.java b/src/com/labrisca/catalan/entity/AI.java similarity index 93% rename from src/com/labrisca/catalan/entities/AI.java rename to src/com/labrisca/catalan/entity/AI.java index 40d4fe2..3dbde52 100644 --- a/src/com/labrisca/catalan/entities/AI.java +++ b/src/com/labrisca/catalan/entity/AI.java @@ -1,19 +1,9 @@ -package com.labrisca.catalan.entities; +package com.labrisca.catalan.entity; import com.labrisca.catalan.Card; import com.labrisca.catalan.Game; -public class AI { - // variables - private final Bot bot; - private final Game game; - - // constructor - public AI(Bot bot, Game game) { - this.bot = bot; - this.game = game; - } - +public record AI(Bot bot, Game game) { public Card throwCard() { // if the play is empty, means bot throws first if (game.getThePlay().isEmpty()) { @@ -26,7 +16,7 @@ public Card throwCard() { // if human's card is trump if (hCard.isTrump()) { - // if points > 3, bot has trump and more valuable cards with same type + // if points > 3, bot has trump cards and more valuable cards with same type if (hCard.getPoints() > 3 && hasTrumpCards() && hasMoreValuableCardsSameType(hCard)) { // win the play return lessValuableCardTrump(); diff --git a/src/com/labrisca/catalan/entities/Bot.java b/src/com/labrisca/catalan/entity/Bot.java similarity index 91% rename from src/com/labrisca/catalan/entities/Bot.java rename to src/com/labrisca/catalan/entity/Bot.java index 9e6fbe2..1833962 100644 --- a/src/com/labrisca/catalan/entities/Bot.java +++ b/src/com/labrisca/catalan/entity/Bot.java @@ -1,8 +1,9 @@ -package com.labrisca.catalan.entities; +package com.labrisca.catalan.entity; import com.labrisca.catalan.Card; -import com.labrisca.catalan.Color; import com.labrisca.catalan.Game; +import com.labrisca.catalan.util.Color; +import com.labrisca.catalan.util.Str; import java.util.concurrent.ThreadLocalRandom; @@ -37,7 +38,7 @@ public void printCardsInHand() { System.out.println("\n[?] Cartes a la mà del " + getName() + ":"); for (int i = 0; i < getInHandCards().size(); i++) { - String cardName = Game.capitalizeStr(getInHandCards().get(i).getName()); + String cardName = Str.upperFirstChar(getInHandCards().get(i).getName()); System.out.print(Color.ANSI_PURPLE); System.out.print((i + 1) + ") " + Color.name(cardName)); @@ -63,7 +64,6 @@ public void throwCard(int round) { // assign random card card = getInHandCards().get(ThreadLocalRandom.current().nextInt(0, getInHandCards().size())); } - commonThrowCard(card, round); } } diff --git a/src/com/labrisca/catalan/entity/Human.java b/src/com/labrisca/catalan/entity/Human.java new file mode 100644 index 0000000..bde0187 --- /dev/null +++ b/src/com/labrisca/catalan/entity/Human.java @@ -0,0 +1,46 @@ +package com.labrisca.catalan.entity; + +import com.labrisca.catalan.Card; +import com.labrisca.catalan.util.Color; +import com.labrisca.catalan.Game; +import com.labrisca.catalan.io.UserInput; +import com.labrisca.catalan.util.Str; + +public class Human extends Player { + // constructor + public Human(String name, Game game) { + super(name, game); + } + + @Override + public void takeCard() { + // exit method if there aren't cards in the deck + if (getGame().deckIsEmpty()) return; + + // else, take top deck card + Card card = topDeckCard(); + getInHandCards().add(card); + System.out.println("Has agafat: " + Color.name(card.getName())); + } + + @Override + public void printCardsInHand() { + System.out.println("\n[?] Et toca tirar. Les teves cartes:"); + for (int i = 0; i < getInHandCards().size(); i++) { + String cardName = Str.upperFirstChar(getInHandCards().get(i).getName()); + System.out.println((i + 1) + ") " + Color.name(cardName)); + } + } + + @Override + public void throwCard(int round) { + // print available in-hand cards to be thrown + printCardsInHand(); + + // ask which card must be thrown or / and change latest card + String input = UserInput.askThrowCard(this); + + // call common method used by Human and Bot classes with user's chosen card + commonThrowCard(getInHandCards().get(Integer.parseInt(input) - 1), round); + } +} diff --git a/src/com/labrisca/catalan/entities/Player.java b/src/com/labrisca/catalan/entity/Player.java similarity index 95% rename from src/com/labrisca/catalan/entities/Player.java rename to src/com/labrisca/catalan/entity/Player.java index ba9e041..61c0928 100644 --- a/src/com/labrisca/catalan/entities/Player.java +++ b/src/com/labrisca/catalan/entity/Player.java @@ -1,7 +1,7 @@ -package com.labrisca.catalan.entities; +package com.labrisca.catalan.entity; import com.labrisca.catalan.Card; -import com.labrisca.catalan.Color; +import com.labrisca.catalan.util.Color; import com.labrisca.catalan.Game; import java.util.ArrayList; @@ -70,7 +70,7 @@ public int getPoints() { } // changes the last card for 7 of trump - void changeLastCard() { + public void changeLastCard() { // if it can, change it if (canChangeLastCard()) { // unset the latest card and save it in the hand of the player @@ -90,7 +90,7 @@ void changeLastCard() { } // returns if player can change latest card - boolean canChangeLastCard() { + public boolean canChangeLastCard() { // true if player has 7 of trump, its value is less than latest and round is less than 21 if (getGame().deckIsEmpty()) return false; return has7Trump() && getSevenTrump().getValue() < game.latestCard().getValue() && game.getRound() < 21; diff --git a/src/com/labrisca/catalan/io/UserInput.java b/src/com/labrisca/catalan/io/UserInput.java new file mode 100644 index 0000000..ab760ad --- /dev/null +++ b/src/com/labrisca/catalan/io/UserInput.java @@ -0,0 +1,90 @@ +package com.labrisca.catalan.io; + +import com.labrisca.catalan.entity.Player; +import com.labrisca.catalan.util.Color; + +import java.util.Scanner; + +public class UserInput { + private static final Scanner SC = new Scanner(System.in); + + UserInput() {} + + // 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(); + if (input.equals("si")) { + System.out.println("Mode hacker activat!"); + System.out.print("Text" + Color.ANSI_PURPLE + " lila " + Color.ANSI_RESET); + System.out.println("= text que no veuries ;)"); + System.out.println(); + return "hacker"; + } else if (input.equals("no")) { + System.out.println("Mode normal activat!"); + System.out.println(); + return "default"; + } + System.out.println("Introdueix \"si\" o \"no\"..."); + } + } + + // ask if enable bot's AI + 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(); + if (input.equals("si")) { + System.out.println("Perdràs :P!"); + System.out.println(); + return true; + } else if (input.equals("no")) { + System.out.println("Beep beep..."); + System.out.println(); + return false; + } + System.out.println("Introdueix \"si\" o \"no\"..."); + } + } + + // ask if print cards final information + 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(); + if (input.equals("si")) return true; + else if (input.equals("no")) return false; + System.out.println("Introdueix \"si\" o \"no\"..."); + } + } + + // 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 + if (input.equals("7") && p.canChangeLastCard()) { + p.changeLastCard(); + p.printCardsInHand(); + } else if (input.equals("7") && !p.canChangeLastCard()) { + System.out.println("No pots canviar l'última carta..."); + } + + // check if player can throw specified card + else if (canThrowCard(input, p.getInHandCards().size())) return input; + else System.out.println("Introdueix un número vàlid..."); + } + } + private static boolean canThrowCard(String input, int inHandCardsSize) { + if (inHandCardsSize == 3 && (input.equals("3") || input.equals("2") || input.equals("1"))) return true; + if (inHandCardsSize == 2 && (input.equals("2") || input.equals("1"))) return true; + return inHandCardsSize == 1 && input.equals("1"); + } +} diff --git a/src/com/labrisca/catalan/Color.java b/src/com/labrisca/catalan/util/Color.java similarity index 89% rename from src/com/labrisca/catalan/Color.java rename to src/com/labrisca/catalan/util/Color.java index 928e24c..1753933 100644 --- a/src/com/labrisca/catalan/Color.java +++ b/src/com/labrisca/catalan/util/Color.java @@ -1,6 +1,6 @@ -package com.labrisca.catalan; +package com.labrisca.catalan.util; -import com.labrisca.catalan.entities.Player; +import com.labrisca.catalan.entity.Player; import java.util.concurrent.ThreadLocalRandom; @@ -22,7 +22,7 @@ private static String randomColor() { } // returns given String colorized randomly - static String colorizeRand(String s) { + public static String colorizeRand(String s) { String oldRandom = randomColor(); StringBuilder colorize = new StringBuilder(oldRandom); @@ -39,7 +39,7 @@ static String colorizeRand(String s) { } // returns colorized card's type - static String type(String s) { + public static String type(String s) { if (s.toLowerCase().contains("bast")) return ANSI_GREEN + s + ANSI_RESET; if (s.toLowerCase().contains("cop")) return ANSI_RED + s + ANSI_RESET; if (s.toLowerCase().contains("espas")) return ANSI_BLUE + s + ANSI_RESET; @@ -48,7 +48,7 @@ static String type(String s) { } // returns colorized card's num - static String num(int n) { + public static String num(int n) { return ANSI_BLUE + n + ANSI_RESET; } @@ -64,19 +64,19 @@ private static String aux(String s, String target, String tipus) { } // returns colorized card's boolean - static String bool(boolean b) { + public static String bool(boolean b) { if (b) return ANSI_GREEN + true + ANSI_RESET; return ANSI_RED + false + ANSI_RESET; } // returns colorized latest card - static String bool(String s) { + public static String bool(String s) { if (s.equals("false")) return ANSI_RED + s + ANSI_RESET; return ANSI_GREEN + s + ANSI_RESET; } // returns green text if player is not bot, else red - static String playWinner(Player winner) { + public static String playWinner(Player winner) { String color; if (winner.getName().equalsIgnoreCase("bot")) color = ANSI_RED; else color = ANSI_GREEN; diff --git a/src/com/labrisca/catalan/util/Str.java b/src/com/labrisca/catalan/util/Str.java new file mode 100644 index 0000000..13ba6e6 --- /dev/null +++ b/src/com/labrisca/catalan/util/Str.java @@ -0,0 +1,15 @@ +package com.labrisca.catalan.util; + +public class Str { + Str() {} + + public static String upperFirstChar(String s) { + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + + // returns centered String inside lines + public static String centerStr(int lnLength, String s) { + int i = (lnLength - s.length()) / 2; + return " ".repeat(i) + s; + } +} diff --git a/src/com/labrisca/english/Card.java b/src/com/labrisca/english/Card.java index c479279..0ab258d 100644 --- a/src/com/labrisca/english/Card.java +++ b/src/com/labrisca/english/Card.java @@ -1,6 +1,7 @@ package com.labrisca.english; -import com.labrisca.english.entities.Player; +import com.labrisca.english.entity.Player; +import com.labrisca.english.util.Color; public class Card { // variables diff --git a/src/com/labrisca/english/Game.java b/src/com/labrisca/english/Game.java index f0cc396..7a89657 100644 --- a/src/com/labrisca/english/Game.java +++ b/src/com/labrisca/english/Game.java @@ -1,13 +1,15 @@ package com.labrisca.english; -import com.labrisca.english.entities.Player; +import com.labrisca.english.entity.Player; +import com.labrisca.english.util.Color; +import com.labrisca.english.util.Str; +import com.labrisca.english.io.UserInput; -import java.util.ArrayList; -import java.util.Collections; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.Scanner; public class Game { // variables @@ -18,7 +20,6 @@ public class Game { private int round; private String gameMode; private boolean ai; // if turn on AI's bot - private static final Scanner SC = new Scanner(System.in); // getters public List getDeck() { return deck; } @@ -26,7 +27,6 @@ public class Game { public int getRound() { return round; } public String getGameMode() { return gameMode; } public boolean isAi() { return ai; } - public static Scanner getSc() { return SC; } // constructor public Game() { @@ -85,12 +85,44 @@ public Game() { new Card("coin's king", "coin", 10, 4) )); - // shuffle the deck and initialize the play and list of players + // shuffle deck and initialize the play and list of players Collections.shuffle(deck); thePlay = new ArrayList<>(); players = new ArrayList<>(); } + // principal method to run the game + public void run(Player human, Player bot) { + // add players into game + players.add(human); + players.add(bot); + + welcomePrint(); + + // set the game mode and AI bot mode + setGameMode(); + setAIBot(); + + // shuffle players list to choose who starts the game, set trump and deal 3 first cards to each player + Collections.shuffle(players); + setTrump(); + deal3FirstCards(); + + // game's main loop + mainLoop(); + + // once game is over, print total points and cards won, game's winner and game time + printPointsAndCards(); + setAndPrintWinner(); + printGameTime(); + + // ask if print all the cards final information + printCardsInfo(); + + // print game's author + printAuthor(); + } + // welcome message public void welcomePrint() { System.out.println("Welcome to \"La Brisca\" CLI cards game"); @@ -101,59 +133,17 @@ public void welcomePrint() { // set the game mode private void setGameMode() { - System.out.println("[?] Enable hacker mode?"); - while (true) { - System.out.print("> "); - String input = SC.nextLine().trim().toLowerCase(); - if (input.equals("yes")) { - System.out.println("Hacker mode on!"); - System.out.print(Color.ANSI_PURPLE + "Purple " + Color.ANSI_RESET); - System.out.println("text = text you wouldn't see ;)"); - gameMode = "hacker"; - break; - } else if (input.equals("no")) { - System.out.println("Default mode on!"); - gameMode = "default"; - break; - } - System.out.println("Input \"yes\" or \"no\"..."); - } - System.out.println(); + gameMode = UserInput.askGameMode(); } // set AI bot mode private void setAIBot() { - System.out.println("[?] Enable bot's AI?"); - while (true) { - System.out.print("> "); - String input = SC.nextLine().trim().toLowerCase(); - if (input.equals("yes")) { - System.out.println("You'll lose :P!"); - ai = true; - break; - } else if (input.equals("no")) { - System.out.println("Beep beep..."); - ai = false; - break; - } - System.out.println("Input \"yes\" or \"no\"..."); - } - System.out.println(); + ai = UserInput.askAIBot(); } // ask if print cards final information - private void askPrintCardsInfo() { - System.out.println("\n[?] See cards final information?"); - while (true) { - System.out.print("> "); - String input = SC.nextLine().trim().toLowerCase(); - if (input.equals("yes")) { - System.out.println(); - printAllCards(); - break; - } else if (input.equals("no")) break; - System.out.println("Input \"yes\" or \"no\"..."); - } + private void printCardsInfo() { + if (UserInput.askPrintCardsInfo()) printAllCards(); } // deal 3 first cards to each player @@ -162,7 +152,6 @@ private void deal3FirstCards() { // for each player, take 3 cards for (Player p : players) { for (int i = 0; i < 3; i++) p.takeCard(); - // if hacker mode is on, make a print (more friendly look) if (gameMode.equals("hacker")) System.out.println(); } @@ -171,7 +160,7 @@ private void deal3FirstCards() { if (!gameMode.equals("hacker")) System.out.println(); } - // set trump + // set game trump private void setTrump() { Card trumpCard = deck.get(6); deck.remove(6); @@ -180,7 +169,7 @@ private void setTrump() { String trump = trumpCard.getType(); - System.out.println(Color.name(capitalizeStr(trumpCard.getName()), false) + " appeared"); + System.out.println(Color.name(Str.upperFirstChar(trumpCard.getName()), false) + " appeared"); System.out.println("So.. trump is " + Color.type(trump + "s") + "!"); // set trump to cards @@ -194,10 +183,6 @@ private void setTrump() { } } - public static String capitalizeStr(String s) { - return s.substring(0, 1).toUpperCase() + s.substring(1); - } - // print all cards and their attributes private void printAllCards() { System.out.println("Information of all the cards:"); @@ -300,14 +285,9 @@ private void setAndPrintWinner() { if (draw) s = "Draw! Both players got 60 points!"; else s = winner.getName() + " won the game with " + winner.getPoints() + " points!!!"; + s = Str.centerStr(ln.length(), s); - System.out.println(Color.colorizeRand(ln) + "\n" + centerStr(ln.length(), s) + "\n" + Color.colorizeRand(ln)); - } - - // returns centered String inside lines - private String centerStr(int lnLength, String s) { - int i = (lnLength - s.length()) / 2; - return " ".repeat(i) + s; + System.out.println(Color.colorizeRand(ln) + "\n" + s + "\n" + Color.colorizeRand(ln)); } // print how much the game took to finish @@ -342,9 +322,7 @@ private void validateThePlay() { Player playWinner = validateThePlayWinner(thePlay.get(0), thePlay.get(1)); // play's winner collects the cards - for (Card card : thePlay) { - playWinner.getWonCards().add(card); - } + for (Card card : thePlay) playWinner.getWonCards().add(card); // remove cards from the play thePlay.clear(); @@ -387,36 +365,4 @@ private Card validateThePlayWinnerCard(Card card0, Card card1) { return card0.isThrownFirst() ? card0 : card1; } } - - // principal method to run the game - public void run(Player human, Player bot) { - // add players into game - players.add(human); - players.add(bot); - - welcomePrint(); - - // set the game mode and AI bot mode - setGameMode(); - setAIBot(); - - // shuffle players list to choose who starts the game, set trump and deal 3 first cards to each player - Collections.shuffle(players); - setTrump(); - deal3FirstCards(); - - // game's main loop - mainLoop(); - - // once game is over, print total points and cards won, game's winner and game time - printPointsAndCards(); - setAndPrintWinner(); - printGameTime(); - - // ask if print all the cards final information - askPrintCardsInfo(); - - // print game's author - printAuthor(); - } } diff --git a/src/com/labrisca/english/entities/Human.java b/src/com/labrisca/english/entities/Human.java deleted file mode 100644 index c41b5df..0000000 --- a/src/com/labrisca/english/entities/Human.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.labrisca.english.entities; - -import com.labrisca.english.Card; -import com.labrisca.english.Color; -import com.labrisca.english.Game; - -public class Human extends Player { - // constructor - public Human(String name, Game game) { - super(name, game); - } - - @Override - public void takeCard() { - // exit method if there aren't cards in the deck - if (getGame().deckIsEmpty()) return; - - // else, take a random card - Card card = topDeckCard(); - getInHandCards().add(card); - - System.out.println(getName() + " took -> " + Color.name(card.getName(), false)); - } - - @Override - public void printCardsInHand() { - System.out.println("\n[?] " + getName() + "'s turn. In-hand cards:"); - - for (int i = 0; i < getInHandCards().size(); i++) { - String cardName = Game.capitalizeStr(getInHandCards().get(i).getName()); - System.out.println((i + 1) + ") " + Color.name(cardName, false)); - } - } - - @Override - public void throwCard(int round) { - // print available in-hand cards to be thrown - printCardsInHand(); - - // ask which card must be thrown or / and change latest card - String input; - label: - while (true) { - System.out.print("> "); - input = Game.getSc().nextLine().trim(); - - // check if player wants and can change latest card - if (input.equals("7") && canChangeLastCard()) { - changeLastCard(); - printCardsInHand(); - } else if (input.equals("7") && !canChangeLastCard()) { - System.out.println("You can't change latest card..."); - } else { - switch (getInHandCards().size()) { - case 3: - if (input.equals("1") || input.equals("2") || input.equals("3")) break label; - break; - case 2: - if (input.equals("1") || input.equals("2")) break label; - break; - case 1: - if (input.equals("1")) break label; - break; - default: - throw new IllegalStateException("Unexpected inHandCards size"); - } - System.out.println("Input a valid card number..."); - } - } - - // call common method used by Human and Bot classes with the user's chosen card - commonThrowCard(getInHandCards().get(Integer.parseInt(input) - 1), round); - } -} diff --git a/src/com/labrisca/english/entities/AI.java b/src/com/labrisca/english/entity/AI.java similarity index 93% rename from src/com/labrisca/english/entities/AI.java rename to src/com/labrisca/english/entity/AI.java index 5fca47e..b2b6194 100644 --- a/src/com/labrisca/english/entities/AI.java +++ b/src/com/labrisca/english/entity/AI.java @@ -1,19 +1,9 @@ -package com.labrisca.english.entities; +package com.labrisca.english.entity; import com.labrisca.english.Card; import com.labrisca.english.Game; -public class AI { - // variables - private final Bot bot; - private final Game game; - - // constructor - public AI(Bot bot, Game game) { - this.bot = bot; - this.game = game; - } - +public record AI(Bot bot, Game game) { public Card throwCard() { // if the play is empty, means bot throws first if (game.getThePlay().isEmpty()) { @@ -26,7 +16,7 @@ public Card throwCard() { // if human's card is trump if (hCard.isTrump()) { - // if points > 3, bot has trump and more valuable cards with same type + // if points > 3, bot has trump cards and more valuable cards with same type if (hCard.getPoints() > 3 && hasTrumpCards() && hasMoreValuableCardsSameType(hCard)) { // win the play return lessValuableCardTrump(); diff --git a/src/com/labrisca/english/entities/Bot.java b/src/com/labrisca/english/entity/Bot.java similarity index 91% rename from src/com/labrisca/english/entities/Bot.java rename to src/com/labrisca/english/entity/Bot.java index 66c9b8b..d81db49 100644 --- a/src/com/labrisca/english/entities/Bot.java +++ b/src/com/labrisca/english/entity/Bot.java @@ -1,8 +1,9 @@ -package com.labrisca.english.entities; +package com.labrisca.english.entity; import com.labrisca.english.Card; -import com.labrisca.english.Color; import com.labrisca.english.Game; +import com.labrisca.english.util.Color; +import com.labrisca.english.util.Str; import java.util.concurrent.ThreadLocalRandom; @@ -37,7 +38,7 @@ public void printCardsInHand() { System.out.println("\n[?] " + getName() + "'s turn. In-hand cards:"); for (int i = 0; i < getInHandCards().size(); i++) { - String cardName = Game.capitalizeStr(getInHandCards().get(i).getName()); + String cardName = Str.upperFirstChar(getInHandCards().get(i).getName()); System.out.print(Color.ANSI_PURPLE); System.out.print((i + 1) + ") " + Color.name(cardName, true)); @@ -63,7 +64,6 @@ public void throwCard(int round) { // assign random card card = getInHandCards().get(ThreadLocalRandom.current().nextInt(0, getInHandCards().size())); } - commonThrowCard(card, round); } } diff --git a/src/com/labrisca/english/entity/Human.java b/src/com/labrisca/english/entity/Human.java new file mode 100644 index 0000000..d6d2fe8 --- /dev/null +++ b/src/com/labrisca/english/entity/Human.java @@ -0,0 +1,46 @@ +package com.labrisca.english.entity; + +import com.labrisca.english.io.UserInput; +import com.labrisca.english.Card; +import com.labrisca.english.util.Color; +import com.labrisca.english.Game; +import com.labrisca.english.util.Str; + +public class Human extends Player { + // constructor + public Human(String name, Game game) { + super(name, game); + } + + @Override + public void takeCard() { + // exit method if there aren't cards in the deck + if (getGame().deckIsEmpty()) return; + + // else, take top deck card + Card card = topDeckCard(); + getInHandCards().add(card); + System.out.println(getName() + " took -> " + Color.name(card.getName(), false)); + } + + @Override + public void printCardsInHand() { + System.out.println("\n[?] " + getName() + "'s turn. In-hand cards:"); + for (int i = 0; i < getInHandCards().size(); i++) { + String cardName = Str.upperFirstChar(getInHandCards().get(i).getName()); + System.out.println((i + 1) + ") " + Color.name(cardName, false)); + } + } + + @Override + public void throwCard(int round) { + // print available in-hand cards to be thrown + printCardsInHand(); + + // ask which card must be thrown or / and change latest card + String input = UserInput.askThrowCard(this); + + // call common method used by Human and Bot classes with user's chosen card + commonThrowCard(getInHandCards().get(Integer.parseInt(input) - 1), round); + } +} diff --git a/src/com/labrisca/english/entities/Player.java b/src/com/labrisca/english/entity/Player.java similarity index 95% rename from src/com/labrisca/english/entities/Player.java rename to src/com/labrisca/english/entity/Player.java index 37108d8..a07e700 100644 --- a/src/com/labrisca/english/entities/Player.java +++ b/src/com/labrisca/english/entity/Player.java @@ -1,7 +1,7 @@ -package com.labrisca.english.entities; +package com.labrisca.english.entity; import com.labrisca.english.Card; -import com.labrisca.english.Color; +import com.labrisca.english.util.Color; import com.labrisca.english.Game; import java.util.ArrayList; @@ -70,7 +70,7 @@ public int getPoints() { } // changes the last card for 7 of trump - void changeLastCard() { + public void changeLastCard() { // if it can, change it if (canChangeLastCard()) { // unset the latest card and save it in the hand of the player @@ -90,7 +90,7 @@ void changeLastCard() { } // returns if player can change the latest card - boolean canChangeLastCard() { + public boolean canChangeLastCard() { // true if player has 7 of trump, its value is less than latest and round is less than 21 if (getGame().deckIsEmpty()) return false; return has7Trump() && getSevenTrump().getValue() < game.latestCard().getValue() && game.getRound() < 21; diff --git a/src/com/labrisca/english/io/UserInput.java b/src/com/labrisca/english/io/UserInput.java new file mode 100644 index 0000000..e4eca31 --- /dev/null +++ b/src/com/labrisca/english/io/UserInput.java @@ -0,0 +1,90 @@ +package com.labrisca.english.io; + +import com.labrisca.english.entity.Player; +import com.labrisca.english.util.Color; + +import java.util.Scanner; + +public class UserInput { + private static final Scanner SC = new Scanner(System.in); + + UserInput() {} + + // 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(); + if (input.equals("yes")) { + System.out.println("Hacker mode on!"); + System.out.print(Color.ANSI_PURPLE + "Purple " + Color.ANSI_RESET); + System.out.println("text = text you wouldn't see ;)"); + System.out.println(); + return "hacker"; + } else if (input.equals("no")) { + System.out.println("Default mode on!"); + System.out.println(); + return "default"; + } + System.out.println("Input \"yes\" or \"no\"..."); + } + } + + // ask if enable bot's AI + public static boolean askAIBot() { + System.out.println("[?] Enable bot's AI?"); + while (true) { + System.out.print("> "); + String input = SC.nextLine().trim().toLowerCase(); + if (input.equals("yes")) { + System.out.println("You'll lose :P!"); + System.out.println(); + return true; + } else if (input.equals("no")) { + System.out.println("Beep beep..."); + System.out.println(); + return false; + } + System.out.println("Input \"yes\" or \"no\"..."); + } + } + + // ask if print cards final information + public static boolean askPrintCardsInfo() { + System.out.println("\n[?] See cards final information?"); + while (true) { + System.out.print("> "); + String input = SC.nextLine().trim().toLowerCase(); + if (input.equals("yes")) return true; + else if (input.equals("no")) return false; + System.out.println("Input \"yes\" or \"no\"..."); + } + } + + // 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 + if (input.equals("7") && p.canChangeLastCard()) { + p.changeLastCard(); + p.printCardsInHand(); + } else if (input.equals("7") && !p.canChangeLastCard()) { + System.out.println("You can't change latest card..."); + } + + // check if player can throw specified card + else if (canThrowCard(input, p.getInHandCards().size())) return input; + else System.out.println("Input a valid number..."); + } + } + private static boolean canThrowCard(String input, int inHandCardsSize) { + if (inHandCardsSize == 3 && (input.equals("3") || input.equals("2") || input.equals("1"))) return true; + if (inHandCardsSize == 2 && (input.equals("2") || input.equals("1"))) return true; + return inHandCardsSize == 1 && input.equals("1"); + } +} diff --git a/src/com/labrisca/english/Color.java b/src/com/labrisca/english/util/Color.java similarity index 90% rename from src/com/labrisca/english/Color.java rename to src/com/labrisca/english/util/Color.java index f7c1213..9938950 100644 --- a/src/com/labrisca/english/Color.java +++ b/src/com/labrisca/english/util/Color.java @@ -1,6 +1,6 @@ -package com.labrisca.english; +package com.labrisca.english.util; -import com.labrisca.english.entities.Player; +import com.labrisca.english.entity.Player; import java.util.concurrent.ThreadLocalRandom; @@ -22,7 +22,7 @@ private static String randomColor() { } // returns given String colorized randomly - static String colorizeRand(String s) { + public static String colorizeRand(String s) { String oldRandom = randomColor(); StringBuilder colorize = new StringBuilder(oldRandom); @@ -39,7 +39,7 @@ static String colorizeRand(String s) { } // returns colorized card's type - static String type(String s) { + public static String type(String s) { if (s.toLowerCase().contains("club")) return ANSI_GREEN + s + ANSI_RESET; if (s.toLowerCase().contains("cup")) return ANSI_RED + s + ANSI_RESET; if (s.toLowerCase().contains("sword")) return ANSI_BLUE + s + ANSI_RESET; @@ -48,7 +48,7 @@ static String type(String s) { } // returns colorized card's num - static String num(int n) { + public static String num(int n) { return ANSI_BLUE + n + ANSI_RESET; } @@ -69,19 +69,19 @@ public static String name(String cardName, Boolean hacker) { } // returns colorized card's boolean - static String bool(boolean b) { + public static String bool(boolean b) { if (b) return ANSI_GREEN + true + ANSI_RESET; return ANSI_RED + false + ANSI_RESET; } // returns colorized latest card - static String bool(String s) { + public static String bool(String s) { if (s.equals("false")) return ANSI_RED + s + ANSI_RESET; return ANSI_GREEN + s + ANSI_RESET; } // returns green text if player is not bot, else red - static String playWinner(Player winner) { + public static String playWinner(Player winner) { String color; if (winner.getName().equalsIgnoreCase("bot")) color = ANSI_RED; else color = ANSI_GREEN; diff --git a/src/com/labrisca/english/util/Str.java b/src/com/labrisca/english/util/Str.java new file mode 100644 index 0000000..24a9c44 --- /dev/null +++ b/src/com/labrisca/english/util/Str.java @@ -0,0 +1,15 @@ +package com.labrisca.english.util; + +public class Str { + Str() {} + + public static String upperFirstChar(String s) { + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + + // returns centered String inside lines + public static String centerStr(int lnLength, String s) { + int i = (lnLength - s.length()) / 2; + return " ".repeat(i) + s; + } +}