Skip to content

Commit

Permalink
refactoring classes and optimizing
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmp33 committed Sep 2, 2021
1 parent 0b4b69d commit b6dd195
Show file tree
Hide file tree
Showing 21 changed files with 446 additions and 416 deletions.
10 changes: 6 additions & 4 deletions src/com/labrisca/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -32,3 +32,5 @@ public static void main(String[] args) {
}
}
}

// todo: change -> game is ai? nope, bot is ai
3 changes: 2 additions & 1 deletion src/com/labrisca/catalan/Card.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
150 changes: 48 additions & 102 deletions src/com/labrisca/catalan/Game.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,15 +20,13 @@ 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<Card> getDeck() { return deck; }
public List<Card> getThePlay() { return thePlay; }
public int getRound() { return round; }
public String getGameMode() { return gameMode; }
public boolean isAi() { return ai; }
public static Scanner getSc() { return SC; }

// constructor
public Game() {
Expand Down Expand Up @@ -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<>();
Expand All @@ -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
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -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:");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
74 changes: 0 additions & 74 deletions src/com/labrisca/catalan/entities/Human.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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()) {
Expand All @@ -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();
Expand Down
Loading

0 comments on commit b6dd195

Please sign in to comment.