Card Deck Game

Assignment

Card.java

// add your own banner here

public class Card implements Comparable<Card>{

privateint suit; // use integers 1-4 to encode the suit

privateint rank; // use integers 1-13 to encode the rank

public Card(int s, int r){

//make a card with suit s and value v

}

publicintcompareTo(Card c){

// use this method to compare cards so they

// may be easily sorted

}

public String toString(){

// use this method to easily print a Card object

}

// add some more methods here if needed

}

Deck.java

 // add your own banner here

public class Deck {

private Card[] cards;

privateint top; // the index of the top of the deck

// add more instance variables if needed

public Deck(){

// make a 52 card deck here

}

public void shuffle(){

// shuffle the deck here

}

public Card deal(){

// deal the top card in the deck

}

// add more methods here if needed

} 

Game.java 

// add your own banner here

public class Game {

private Player p;

private Deck cards;

// you’ll probably need some more here

public Game(String[] testHand){

// This constructor is to help test your code

// use the contents of testHand to

// make a hand for the player

// use the following encoding for cards

// c = clubs

// d = diamonds

// h = hearts

// s = spades

// 1-13 correspond to ace – king

// example: s1 = ace of spades

// example: testhand = {s1, s13, s12, s11, s10} = royal flush

}

public Game(){

// This no-argument constructor is to actually play a normal game

}

public void play(){

// this method should play the game

}

public String checkHand(ArrayList<Card> hand){

// this method should take an ArrayList of cards

// as input and then determine what evaluates to and

// return that as a String

}

// you will likely want many more methods here

// per discussion in class

} 

Player.java

// add your own banner here

public class Player {

privateArrayList<Card> hand; // the player’s cards

private double bankroll;

private double bet;

// you may choose to use more instance variables

public Player(){

// create a player here

}

public void addCard(Card c){

// add the card c to the player’s hand

}

public void removeCard(Card c){

// remove the card c from the player’s hand

}

public void bets(double amt){

// player makes a bet

}

public void winnings(double odds){

// adjust bankroll if player wins

}

public double getBankroll(){

// return current balance of bankroll

}

// you may wish to use more methods here

} 

PokerTest.java

 public class PokerTest

//this class must remain unchanged

//your code must work with this test class

public static void main(String[] args){

if (args.length<1){

Game g = new Game();

g.play();

}

else{

Game g = new Game(args);

g.play();

}

}

} 

Solution 

Card.java

// add your own banner here

public class Card implements Comparable<Card>{

privateint suit; // use integers 1-4 to encode the suit

privateint rank; // use integers 1-13 to encode the rank

public Card(int s, int r){

//make a card with suit s and value v

this.suit = s;

this.rank = r;

}

publicintcompareTo(Card c){

// use this method to compare cards so they

// may be easily sorted

if (this.rank == 1) { // ‘A’ will be the larger rank

if (c.rank == 1) {

returnthis.suit – c.suit;

}

return 1;

}

if (c.rank == 1) {

return -1;

}

if (this.rank != c.rank) // cards with same rank are together

returnthis.rank – c.rank;

returnthis.suit – c.suit;

}

public String toString(){

// use this method to easily print a Card object

if (suit == 1)

return “c” + this.rank;

if (suit == 2)

return “d” + this.rank;

if (suit == 3)

return “h” + this.rank;

if (suit == 4)

return “s” + this.rank;

return “u” + this.rank; // should not happen

}

// add some more methods here if needed

// construct a card from the given string value (for the test game).

public Card(String value) {

char c = value.charAt(0);

this.rank = Integer.parseInt(value.substring(1));

if (c == ‘c’)

this.suit = 1;

if (c == ‘d’)

this.suit = 2;

if (c == ‘h’)

this.suit = 3;

if (c == ‘s’)

this.suit = 4;

}

publicintgetRank() {

returnthis.rank;

}

publicintgetSuit() {

returnthis.suit;

}

} 

Deck.java

// add your own banner here

importjava.util.Random;

public class Deck {

private Card[] cards;

privateint top; // the index of the top of the deck

// add more instance variables if needed

public Deck(){

// make a 52 card deck here

this.cards = new Card[52];

this.top = 0;

int k = 0;

for (int suit = 1; suit <= 4; suit++) {

for (int rank = 1; rank <= 13; rank++) {

this.cards[k] = new Card(suit, rank);

k ++;

}

}

}

public void shuffle(){

// shuffle the deck here

Random rand = new Random();

for (int i = this.cards.length – 1; i > 0; i–) {

// generate a random location to put the current card

// and swap the two cards

int j = rand.nextInt(i + 1);

Card c = this.cards[i];

this.cards[i] = this.cards[j];

this.cards[j] = c;

}

}

public Card deal(){

// deal the top card in the deck

if (this.top<this.cards.length) {

Card c = this.cards[this.top];

this.top ++;

return c;

}

return null; // no more cards left on the deck

}

// add more methods here if needed

publicint size() {

returnthis.cards.length – this.top; // number of cards left

}

} 

Game.java

// add your own banner here

importjava.util.ArrayList;

importjava.util.Scanner;

importjava.util.Collections;

public class Game {

private Player p;

private Deck cards;

// you’ll probably need some more here

privateboolean special; // is this the special game (for test)

public Game(String[] testHand){

// This constructor is to help test your code

// use the contents of testHand to

// make a hand for the player

// use the following encoding for cards

// c = clubs

// d = diamonds

// h = hearts

// s = spades

// 1-13 correspond to ace – king

// example: s1 = ace of spades

// example: testhand = {s1, s13, s12, s11, s10} = royal flush

// convert the parameter and give it to an array list

ArrayList<Card> hand = new ArrayList<>();

for (String value : testHand) {

hand.add(new Card(value));

}

// display the result of the hand

System.out.println(checkHand(hand));

this.special = true;

}

public Game(){

// This no-argument constructor is to actually play a normal game

this.cards = new Deck();

this.p = new Player();

this.special = false;

}

public void play(){

if (this.special) // no game play when testing

return;

// this method should play the game

// shuffle the deck and continue playing the game

this.cards.shuffle();

Scanner user = new Scanner(System.in);

while (true) {

if (this.cards.size() < 5) {

System.out.printf(“Only %d card(s) left on the deck…\n”, this.cards.size());

break;

}

if (this.p.getBankroll() < 1) { // make sure the player has enough

System.out.printf(“Only %.2f token(s) left…\n”, this.p.getBankroll());

break;

}

System.out.println(“[new turn]”);

System.out.printf(“– You have %.2f token(s)..\n”, p.getBankroll());

// ask the user to enter the bet

double bet = 0;

while (bet < 1 || bet >this.p.getBankroll()) {

System.out.printf(“– Enter the amount you want to bet (1-%.2f) (0 to quit): “, this.p.getBankroll());

bet = user.nextDouble();

if (bet == 0) {

break;

}

}

if (bet == 0) break; // stop the game

this.p.bets(bet);

System.out.println();

// now deal 5 cards to the player

for (int i = 0; i < 5; i++) {

Card c = this.cards.deal();

this.p.addCard(c);

}

// display the hand

ArrayList<Card> hand = this.p.getHand();

System.out.println(“– dealing cards…”);

display(hand);

System.out.println();

// now ask the user if he wants to replace card

int count = Math.min(5, this.cards.size());

System.out.printf(“– You can replace up to %d card(s)\n”, count);

for (int i = 0; i <hand.size() && count > 0; i++) {

System.out.printf(“– Enter 0 to keep or 1 to replace card #%d: “, i + 1);

int choice = user.nextInt();

if (choice == 1) { // replace this card

Card c = this.cards.deal();

hand.set(i, c);

count –;

System.out.println(“– Now you have…”);

display(hand);

System.out.println();

}

}

System.out.println();

// check the hand and calculate its odds

String value = checkHand(hand);

System.out.println(“You got [” + value + “].\n\n”);

// accumulate the odds to the user

int odds = checkOdds(hand);

if (odds > 0) {

this.p.winnings(odds);

}

// clear the hand of the player

hand.clear();

}

System.out.println(“\n\nEnd of Game…”);

}

public String checkHand(ArrayList<Card> hand){

// this method should take an ArrayList of cards

// as input and then determine what evaluates to and

// return that as a String

Collections.sort(hand);

// must check from highest payout to lowest

if (isRoyalFlush(hand))

return “Royal Flush”;

if (isStraightFlush(hand))

return “Straight Flush”;

if (isFourOfAKind(hand))

return “Four of a Kind”;

if (isFullHouse(hand))

return “Full House”;

if (isFlush(hand))

return “Flush”;

if (isStraight(hand))

return “Straight”;

if (isThreeOfAKind(hand))

return “Three of a Kind”;

if (isTwoPairs(hand))

return “Two pairs”;

if (isOnePair(hand))

return “One pair”;

return “No pair”;

}

// you will likely want many more methods here

// per discussion in class

private static intcheckOdds(ArrayList<Card> hand) {

// return the odds according to the hand

if (isRoyalFlush(hand))

return 250;

if (isStraightFlush(hand))

return 50;

if (isFourOfAKind(hand))

return 25;

if (isFullHouse(hand))

return 6;

if (isFlush(hand))

return 5;

if (isStraight(hand))

return 4;

if (isThreeOfAKind(hand))

return 3;

if (isTwoPairs(hand))

return 2;

if (isOnePair(hand))

return 1;

return 0;

}

private static void display(ArrayList<Card> hand) {

for (int i = 0; i <hand.size(); i++) {

System.out.printf(“#%d %s\n”, i + 1, hand.get(i));

}

}

// the following functions should be checked from the highest payout

// to the lowest payout

private static booleanisOnePair(ArrayList<Card> hand) {

returnisXOfAKind(hand, 2, 0) >= 0;

}

private static booleanisTwoPairs(ArrayList<Card> hand) {

int pair1 = isXOfAKind(hand, 2, 0);

if (pair1 >= 0) {

int pair2 = isXOfAKind(hand, 2, pair1 + 2);

return pair2 >= 0;

}

return false;

}

private static booleanisThreeOfAKind(ArrayList<Card> hand) {

returnisXOfAKind(hand, 3, 0) >= 0;

}

private static booleanisStraight(ArrayList<Card> hand) {

// five cards with consecutive values (cards in hand is already sorted)

for (int i = 0; i <hand.size() – 2; i++) {

Card curr = hand.get(i); // compare two cards

Card next = hand.get(i + 1);

if (curr.getRank() + 1 != next.getRank()) {

return false;

}

}

// the last two must be K A, or other continue

int s = hand.size();

if (s > 2) {

int r1 = hand.get(s – 2).getRank();

int r2 = hand.get(s – 1).getRank();

if (r2 == 1) { // K A

return r1 == 13;

}

return r2 + 1 == r1; // otherwise, consecutive

}

return true;

}

private static booleanisFlush(ArrayList<Card> hand) {

// five cards with the same suit

for (int i = 1; i <hand.size(); i++) {

if (hand.get(i).getSuit() != hand.get(0).getSuit()) {

return false;

}

}

return true;

}

private static booleanisFullHouse(ArrayList<Card> hand) {

int three = isXOfAKind(hand, 3, 0);

if (three == 0) { // first three are the same rank

returnisXOfAKind(hand, 2, 3) >= 3; // also a pair

}

if (three >= 2) { // last 3 are the same rank

returnisXOfAKind(hand, 2, 0) == 0;

}

return false;

}

private static booleanisFourOfAKind(ArrayList<Card> hand) {

returnisXOfAKind(hand, 4, 0) >= 0;

}

private static intisXOfAKind(ArrayList<Card> hand, int x, int i) {

// try to find x cards with the same rank

// searching from index i.

// if found, return the index of the first card of these x cards.

while (i + x <= hand.size()) {

// check if the next x cards have the same rank

boolean same = true;

for (int j = 1; j < x; j++) {

if (hand.get(i + j).getRank() != hand.get(i).getRank()) { // not same rank

same = false;

break;

}

}

if (same)

return i;

i ++;

}

return -1; // not found

}

private static booleanisStraightFlush(ArrayList<Card> hand) {

returnisStraight(hand) &&isFlush(hand);

}

private static booleanisRoyalFlush(ArrayList<Card> hand) {

returnisStraightFlush(hand) &&hand.get(0).getRank() == 10; // starts with 10.

}

} 

Player.java

// add your own banner here

importjava.util.ArrayList;

public class Player {

privateArrayList<Card> hand; // the player’s cards

private double bankroll;

private double bet;

// you may choose to use more instance variables

public Player(){

// create a player here

this.hand = new ArrayList<>();

this.bankroll = 5.00; // intitialize 5 tokens.

}

public void addCard(Card c){

// add the card c to the player’s hand

this.hand.add(c);

}

public void removeCard(Card c){

// remove the card c from the player’s hand

this.hand.remove(c);

}

public void bets(double amt){

// player makes a bet

this.bet = amt;

this.bankroll -= amt; // decrease the bankroll

}

public void winnings(double odds){

// adjust bankroll if player wins

this.bankroll += odds * this.bet;

}

public double getBankroll(){

// return current balance of bankroll

returnthis.bankroll;

}

// you may wish to use more methods here

publicArrayList<Card>getHand() {

returnthis.hand;

}

} 

PokerTest.java

public class PokerTest {

//this class must remain unchanged

//your code must work with this test class

public static void main(String[] args){

if (args.length<1){

Game g = new Game();

g.play();

}

else{

Game g = new Game(args);

g.play();

}

}

}