Skip to content

Commit

Permalink
AI extends Bot & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmp33 committed Sep 3, 2021
1 parent 0aa1926 commit c37c439
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 114 deletions.
14 changes: 3 additions & 11 deletions src/com/labrisca/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,14 @@ public static void main(String[] args) {
System.out.print("> ");
String input = sc.nextLine().trim().toLowerCase();

// initialize game, players and run game
// initialize and run game
if (input.equals("catalan")) {
var game = new com.labrisca.catalan.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);
new com.labrisca.catalan.Game().run();
break;
} else if (input.equals("english")) {
var game = new com.labrisca.english.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);
new com.labrisca.english.Game().run();
break;
}
System.out.println("Type \"Catalan\" or \"English\"");
Expand Down
31 changes: 16 additions & 15 deletions src/com/labrisca/catalan/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.labrisca.catalan;

import com.labrisca.catalan.entity.AI;
import com.labrisca.catalan.entity.Bot;
import com.labrisca.catalan.entity.Human;
import com.labrisca.catalan.entity.Player;
Expand All @@ -16,7 +17,7 @@
public class Game {
// fields
private final List<Card> deck;
private final List<Card> thePlay; // cards list that participate in the play
private final List<Card> thePlay;
private final List<Player> players;
private int gameTime;
private int round;
Expand Down Expand Up @@ -92,20 +93,15 @@ public Game() {
}

// principal method to run the game
public void run(Human human, Bot bot) {
public void run() {
// welcome message
welcomePrint();

// set the game mode and AI bot mode
// set the game mode
setGameMode();
bot.setAi(UserInput.askAIBot());

// add players into game
players.add(human);
players.add(bot);

// add players into game
players.add(human);
players.add(bot);
// create and adds players into game
addPlayers();

// shuffle players list to choose who starts the game, set trump and deal 3 first cards to each player
Collections.shuffle(players);
Expand Down Expand Up @@ -140,6 +136,13 @@ private void setGameMode() {
gameMode = UserInput.askGameMode();
}

// create and adds players into game
private void addPlayers() {
players.add(new Human("@ericmp33", this));
if (UserInput.askAIBot()) players.add(new AI(this));
else players.add(new Bot(this));
}

// ask if print cards final information
private void printCardsInfo() {
if (UserInput.askPrintCardsInfo()) printAllCards();
Expand Down Expand Up @@ -169,8 +172,8 @@ private void setTrump() {
String trump = trumpCard.getType();

System.out.println("Ha aparegut -> " + Color.name(trumpCard.getName()));
if (trump.contains("bast") || trump.contains("cop")) System.out.println("Va de " + trump + ".\n");
else System.out.println("Va d'" + trump + ".\n");
if (trump.contains("bast") || trump.contains("cop")) System.out.println("Va de " + trump + "!\n");
else System.out.println("Va d'" + trump + "!\n");

// set trump to cards
for (Card card : deck) {
Expand Down Expand Up @@ -323,8 +326,6 @@ private void validateThePlay() {

// play's winner collects the cards
for (Card card : thePlay) playWinner.getWonCards().add(card);

// remove cards from the play
thePlay.clear();

// print who won the play
Expand Down
48 changes: 33 additions & 15 deletions src/com/labrisca/catalan/entity/AI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,34 @@
import com.labrisca.catalan.Card;
import com.labrisca.catalan.Game;

public record AI(Bot bot, Game game) {
public Card throwCard() {
public class AI extends Bot {
// constructor
public AI(Game game) {
super(game);
}

@Override
public void throwCard(int round) {
// if gamemode is hacker show bot's in-hand cards
if (getGame().getGameMode().equals("hacker")) printCardsInHand();

// try to change last card
changeLastCard();

// "think", and throw a proper card
commonThrowCard(theChosenOne(), round);
}

// returns which card has to be thrown
public Card theChosenOne() {
// if the play is empty, means bot throws first
if (game.getThePlay().isEmpty()) {
if (getGame().getThePlay().isEmpty()) {
// throw lessValuableCard
return lessValuableCard();
}

// else, means it throws after human player, save human's thrown card
Card hCard = game.getThePlay().get(0);
Card hCard = getGame().getThePlay().get(0);

// if human's card is trump
if (hCard.isTrump()) {
Expand Down Expand Up @@ -48,7 +66,7 @@ public Card throwCard() {
Card mostVal = mostValuableCardSameType(type);

// check if bot can win the play
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
// if cards have same type and if bot's card has points > 0 and if card is the most valuable
if (type.equals(card.getType()) && card.getPoints() > 0 && card.equals(mostVal)) {
// win the play
Expand All @@ -63,23 +81,23 @@ public Card throwCard() {

// returns true if has cards with trump
private boolean hasTrumpCards() {
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
if (card.isTrump()) return true;
}
return false;
}

// returns a card with the same type as the param
private Card sameCardType(String type) {
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
if (type.equals(card.getType())) return card;
}
return lessValuableCard();
}

// returns a card with trump
private Card trumpCard() {
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
if (card.isTrump()) return card;
}

Expand All @@ -91,19 +109,19 @@ private Card trumpCard() {
private Card lessValuableCard() {
// first, check if has 2 trump cards and other one isn't
int trumpCount = 0;
for (Card card : bot.getInHandCards()) if (card.isTrump()) trumpCount++;
for (Card card : getInHandCards()) if (card.isTrump()) trumpCount++;

if (trumpCount == 2) {
// if the possible card to be thrown has less than 10 points and is not trump
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
if (card.getPoints() < 10 && !card.isTrump()) return card;
}
return lessValuableCardTrump();
}

// if not, returns less valuable card
Card lessVal = bot.getInHandCards().get(0);
for (Card card : bot.getInHandCards()) {
Card lessVal = getInHandCards().get(0);
for (Card card : getInHandCards()) {
// if "lessValuable" card value is greater than "card" value
if (lessVal.getValue() > card.getValue()) {
// less valuable card is "card"
Expand All @@ -116,7 +134,7 @@ private Card lessValuableCard() {
// returns the less valuable card with trump
private Card lessValuableCardTrump() {
Card lessVal = trumpCard();
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
if (lessVal.getValue() > card.getValue() && card.isTrump()) {
lessVal = card;
}
Expand All @@ -127,7 +145,7 @@ private Card lessValuableCardTrump() {
// returns most valuable card with same card type
private Card mostValuableCardSameType(String type) {
Card mostVal = sameCardType(type);
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
// if card's value is greater than mostVal and both have same card type as parsed type
if (mostVal.getValue() < card.getValue() && mostVal.getType().equals(type) && card.getType().equals(type)) {
// most valuable card is "card"
Expand All @@ -139,7 +157,7 @@ private Card mostValuableCardSameType(String type) {

// returns true if has cards more value than the param card within same card type
private boolean hasMoreValuableCardsSameType(Card hCard) {
for (Card card : bot.getInHandCards()) {
for (Card card : getInHandCards()) {
// if card's value is greater than human's thrown card and have same card type
if (card.getValue() > hCard.getValue() && card.getType().equals(hCard.getType())) {
return true;
Expand Down
23 changes: 3 additions & 20 deletions src/com/labrisca/catalan/entity/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
import java.util.concurrent.ThreadLocalRandom;

public class Bot extends Player {
// fields
private boolean ai;

// setters
public void setAi(boolean ai) { this.ai = ai; }

// constructor
public Bot(Game game) {
super("Bot", game);
Expand Down Expand Up @@ -45,7 +39,6 @@ public void printCardsInHand() {

for (int i = 0; i < getInHandCards().size(); i++) {
String cardName = Str.upperFirstChar(getInHandCards().get(i).getName());

System.out.print(Color.ANSI_PURPLE);
System.out.print((i + 1) + ") " + Color.name(cardName));
System.out.println(Color.ANSI_RESET);
Expand All @@ -55,21 +48,11 @@ public void printCardsInHand() {

@Override
public void throwCard(int round) {
// if game is hacker show bot's in-hand cards
// if gamemode is hacker show bot's in-hand cards
if (getGame().getGameMode().equals("hacker")) printCardsInHand();

Card card;
// if bot's AI is on
if (ai) {
// try to change last card
changeLastCard();

// assign AI thought of which card has to be thrown
card = new AI(this, getGame()).throwCard();
} else {
// assign random card
card = getInHandCards().get(ThreadLocalRandom.current().nextInt(0, getInHandCards().size()));
}
// throw a random card
Card card = getInHandCards().get(ThreadLocalRandom.current().nextInt(0, getInHandCards().size()));
commonThrowCard(card, round);
}
}
6 changes: 2 additions & 4 deletions src/com/labrisca/catalan/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ Card topDeckCard() {
// throw a card
public abstract void throwCard(int round);

// common code for bot and human
// common code for child classes
void commonThrowCard(Card card, int round) {
System.out.println("\n[!] El jugador " + name + " ha tirat -> " + Color.name((card.getName())));

// throw the card
// modify card attributes
card.setThrownBy(this);

// set the round where the card is thrown, and throw the card
card.setRoundThrown(round);

// if the play is empty
Expand Down
25 changes: 15 additions & 10 deletions src/com/labrisca/english/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.labrisca.english;

import com.labrisca.english.entity.AI;
import com.labrisca.english.entity.Bot;
import com.labrisca.english.entity.Human;
import com.labrisca.english.entity.Player;
Expand All @@ -16,7 +17,7 @@
public class Game {
// fields
private final List<Card> deck;
private final List<Card> thePlay; // cards list that participate in the play
private final List<Card> thePlay;
private final List<Player> players;
private int gameTime;
private int round;
Expand Down Expand Up @@ -92,16 +93,15 @@ public Game() {
}

// principal method to run the game
public void run(Human human, Bot bot) {
public void run() {
// welcome message
welcomePrint();

// set the game mode and AI bot mode
// set the game mode
setGameMode();
bot.setAi(UserInput.askAIBot());

// add players into game
players.add(human);
players.add(bot);
// create and adds players into game
addPlayers();

// shuffle players list to choose who starts the game, set trump and deal 3 first cards to each player
Collections.shuffle(players);
Expand Down Expand Up @@ -136,6 +136,13 @@ private void setGameMode() {
gameMode = UserInput.askGameMode();
}

// create and adds players into game
private void addPlayers() {
players.add(new Human("@ericmp33", this));
if (UserInput.askAIBot()) players.add(new AI(this));
else players.add(new Bot(this));
}

// ask if print cards final information
private void printCardsInfo() {
if (UserInput.askPrintCardsInfo()) printAllCards();
Expand Down Expand Up @@ -165,7 +172,7 @@ private void setTrump() {
String trump = trumpCard.getType();

System.out.println(Color.name(Str.upperFirstChar(trumpCard.getName()), false) + " appeared");
System.out.println("So.. trump is " + Color.type(trump + "s") + "!");
System.out.println("Trump is " + Color.type(trump + "s") + "!\n");

// set trump to cards
for (Card card : deck) {
Expand Down Expand Up @@ -318,8 +325,6 @@ private void validateThePlay() {

// play's winner collects the cards
for (Card card : thePlay) playWinner.getWonCards().add(card);

// remove cards from the play
thePlay.clear();

// print who won the play
Expand Down
Loading

0 comments on commit c37c439

Please sign in to comment.