My project has three classes: Card
, Deck
, and BridgeConsole
. The Card
class has two fields: int rank
and int suite
. It has three methods: toString
, getSuitAsString
, and compareTo
:
public class Card implements Comparable<Object>{
int suit; // 0-Hearts, 1-Clubs, 2-Diamonds, 3-Spades
int rank; // 2-10, 11-Jack, 12-Queen, 13-King, 14-Ace
public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
public String getSuitString() {
String str = null;
switch(suit){
case 0:
str = "Hearts";
break;
case 1:
str = "Clubs";
break;
case 2:
str = "Diamonds";
break;
case 3:
str = "Spades";
break;
default:
System.out.print("Fatal error!!!");
break;
}
return str;
}
@Override
public String toString() {
return rank + " of " + getSuitString() + ", ";
}
@Override
public int compareTo(Object o) {
Card c2 = (Card) o;
if( suit < c2.suit) {
return -1;
}
if( suit > c2.suit) {
return 1;
}
if( rank > c2.rank) {
return 1;
}
if( rank < c2.rank) {
return -1;
}
return 0;
}
public boolean compareTo(int firstSuit, Card c2) {
return suit == firstSuit && rank > c2.rank;
}
}
My Deck
class constructs the deck of cards and has a method to deal cards to the players:
public class Deck {
ArrayList<Card> deck = new ArrayList<>();
public Deck() {
for (int s = 0; s < 4; s++) {
for (int r = 2; r < 15; r++) {
deck.add(new Card(r, s));
}
}
Collections.shuffle(deck);
}
public void addCard(Card card) {
if (deck.size() < 52) {
deck.add(card);
}
}
public Card dealCard() {
Card card = null;
if (deck.size() > 0) {
card = deck.remove(deck.size() - 1);
}
return card;
}
}
Finally, the game logic and main method are contained in the BridgeConsole
class. I used modulo arithmetic to keep track of the previous rounds winner so as to start with that player in the subsequent round.
public class BridgeConsole {
static Scanner stdin = new Scanner(System.in);
static Deck deck = new Deck();
static int playerCard;
static ArrayList<Card> player = new ArrayList<>();
static ArrayList<Card> ai1 = new ArrayList<>();
static ArrayList<Card> ai2 = new ArrayList<>();
static ArrayList<Card> ai3 = new ArrayList<>();
static ArrayList<Card> center = new ArrayList<>();
static int winner = 0;
static int team1 = 0;
static int team2 = 0;
static String whoWon = "";
public static int getPlyerCard() {
do {
System.out.print("\n player hands contains " + player.size() + " cards."
+ "\n Choose card [index]");
playerCard = stdin.nextInt();
while (playerCard > player.size()) {
System.out.print("Please enter a valid card index. [0-13]");
playerCard = stdin.nextInt();
}
} while (playerCard > player.size());
return playerCard;
}
public static void trackScore(ArrayList<Card> p_1, ArrayList<Card> p_2, ArrayList<Card> p_3, ArrayList<Card> p_4, ArrayList<Card> center, int winner) {
switch (winner) {
case 0:
team1++;
break;
case 1:
team2++;
break;
case 2:
team1++;
break;
case 3:
team2++;
break;
}
}
public static void playRound() {
System.out.println("PLAYER Hand:");
int index = 0;
for (Card c : player) {
System.out.print("[" + index + "] " + c.toString());
index++;
}
System.out.println("\n AI 1 hand");
displayCards(ai1);
System.out.println("\n AI 2 hand");
displayCards(ai2);
System.out.println("\n AI 3 hand");
displayCards(ai3);
switch (winner) {
case 0:
playerCard = getPlyerCard();
center.add(player.get(playerCard));
center.add(aiSelect(center.get(0), ai1));
center.add(aiSelect(center.get(0), ai2));
center.add(aiSelect(center.get(0), ai3));
ai1.remove(aiSelect(center.get(0), ai1));
ai2.remove(aiSelect(center.get(0), ai2));
ai3.remove(aiSelect(center.get(0), ai3));
player.remove(playerCard);
break;
case 1:
center.add(aiSelect(ai1));
center.add(aiSelect(center.get(0), ai2));
center.add(aiSelect(center.get(0), ai3));
System.out.print("\nCenter row of cards\n");
displayCards(center);
playerCard = getPlyerCard();
center.add(player.get(playerCard));
ai1.remove(aiSelect(ai1));
ai2.remove(aiSelect(center.get(0), ai2));
ai3.remove(aiSelect(center.get(0), ai3));
player.remove(playerCard);
break;
case 2:
center.add(aiSelect(ai2));
center.add(aiSelect(center.get(0), ai3));
System.out.print("\nCenter row of cards\n");
displayCards(center);
playerCard = getPlyerCard();
center.add(player.get(playerCard));
center.add(aiSelect(center.get(0), ai1));
ai2.remove(aiSelect(ai2));
ai3.remove(aiSelect(center.get(0), ai3));
player.remove(playerCard);
ai1.remove(aiSelect(center.get(0), ai1));
break;
case 3:
center.add(aiSelect(ai3));
System.out.print("\nCenter row of cards\n");
displayCards(center);
playerCard = getPlyerCard();
center.add(player.get(playerCard));
center.add(aiSelect(center.get(0), ai1));
center.add(aiSelect(center.get(0), ai2));
ai3.remove(aiSelect(ai3));
player.remove(playerCard);
ai1.remove(aiSelect(center.get(0), ai1));
ai2.remove(aiSelect(center.get(0), ai2));
break;
default:
break;
}
System.out.print("\nCenter row of cards\n");
displayCards(center);
System.out.print("\n Winning Card \n");
System.out.println(selectMax(center.get(0), center.get(1), center.get(2), center.get(3)).toString());
System.out.print("\n Player who won round \n");
winner = (winner + getWinningCard(center)) % 4;
System.out.println("At position [" + getPlayerPosition(center.get(getWinningCard(center)), center) + "]");
trackScore(player, ai2, ai3, ai1, center, winner);
center.clear();
}
public static int getPlayerPosition(Card playerCard, ArrayList<Card> center) {
int index = 0;
for (int i = 0; i < center.size(); i++) {
if (playerCard == center.get(i)) {
index = i;
}
}
return index;
}
public static int getWinningCard(ArrayList<Card> center) {
Card maxCard = selectMax(center.get(0), center.get(1), center.get(2), center.get(3));
return center.indexOf(maxCard);
}
public static Card aiSelect(Card current, ArrayList<Card> aiHand) {
Collections.sort(aiHand);
// var max will track highest card in AIs hand
Card max;
// contains all posible cards of the same sui as the current card
ArrayList<Card> followSuit = new ArrayList<>();
for (Card c : aiHand) {
if (c.suit == current.suit) {
followSuit.add(c);
}
}
if (!followSuit.isEmpty()) {
Collections.sort(aiHand);
max = followSuit.get(followSuit.size() - 1);
return max;
} else {
return aiHand.get(aiHand.size() - 1);
}
}
public static Card aiSelect(ArrayList<Card> aiHand) {
Collections.sort(aiHand);
// var max will track highest card in AIs hand
return aiHand.get(aiHand.size() - 1);
}
public static void displayCards(ArrayList<Card> hand) {
for (Card c : hand) {
System.out.print(c.toString());
}
}
public static void dealCards(Deck deck, ArrayList<Card> p1, ArrayList<Card> p2,
ArrayList<Card> p3, ArrayList<Card> p4) {
for (int i = 0; i < 13; i++) {
p1.add(deck.dealCard());
p2.add(deck.dealCard());
p3.add(deck.dealCard());
p4.add(deck.dealCard());
}
}
public static Card selectMax(Card c1, Card c2, Card c3, Card c4) {
// suit of card 1
int firsSuit = c1.suit;
if (c1.compareTo(firsSuit, c2) && c1.compareTo(firsSuit, c3)
&& c1.compareTo(firsSuit, c4)) {
return c1;
} else if (c2.compareTo(firsSuit, c1) && c2.compareTo(firsSuit, c3)
&& c2.compareTo(firsSuit, c4)) {
return c2;
} else if (c3.compareTo(firsSuit, c1) && c3.compareTo(firsSuit, c2)
&& c3.compareTo(firsSuit, c4)) {
return c3;
} else {
return c4;
}
}
public static void main(String[] args) {
System.out.println("Contract Bridge - Console");
System.out.println("Creating deck...");
System.out.println("Dealing Cards...");
dealCards(deck, player, ai1, ai2, ai3);
while (!player.isEmpty()) {
playRound();
}
System.out.print("Team1: " + team1 + " team2: " + team2);
stdin.close();
System.exit(0);
}
}
I would appreciate any feedback on design, programming style, etc.