Rummy-card-game.docx

  • Uploaded by: Farooq Azam
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Rummy-card-game.docx as PDF for free.

More details

  • Words: 1,180
  • Pages: 11
Rummy Card Game CS351 – Artificial Intelligence Rida-e-Fatima

2012314

Zainub Wahid

2012420

Card package RummyProject; public class Card { private private private private private private

Rank rank; Suit suit; boolean inPlay = false; boolean inStraight = false; boolean inOfAKind = false; boolean inDiscard = false;

Card(){} Card(Rank rank, Suit suit){ this.rank = rank; this.suit = suit; } Card(int rankNum, int suitNum){ for (Rank r : Rank.values()){ if(r.ordinal() == rankNum) this.rank = r; } for (Suit s : Suit.values()){ if(s.ordinal() == suitNum) this.suit = s; } } public int getPoints(){ return this.rank.points; } public int getOrder(){ return this.suit.order; } public Rank getRank(){ return this.rank; } public Suit getSuit(){ return this.suit; } public boolean getInPlay(){ return inPlay; } public void setInPlay(boolean inPlay){ this.inPlay = inPlay; } public boolean getInStraight(){ return this.inStraight; }

public void setInStraight(boolean inStraight){ this.inStraight = inStraight; } public boolean getInOfAKind(){ return this.inOfAKind; } public void setInOfAKind(boolean inOfAKind){ this.inOfAKind = inOfAKind; } }

Deck package RummyProject; import java.util.Random; public class Deck { private Card[] deck = new Card[52]; public int cardsInPlay = 0; Deck(){ int i = 0; for(Rank r : Rank.values()){ for(Suit s : Suit.values()){ deck[i++] = (new Card(r,s)); } } } public Card[] getDeck(){ return this.deck; } public Card[] getStartingHand(){ Card[] startingHand = new Card[11]; for(int i=0; i<10; i++){ startingHand[i] = deck[i]; deck[i].setInPlay(true); } this.cardsInPlay = 10; return startingHand; } public Card getTopCard(){ this.deck[cardsInPlay].setInPlay(true); return this.deck[cardsInPlay++]; } public void shuffle(){ Random randomGenerator = new Random(); for(int i=0; i<300; i++){ int switch1 = randomGenerator.nextInt(25); int switch2 = randomGenerator.nextInt(25) + 26; Card placeHolder;

placeHolder = this.deck[switch1]; this.deck[switch1] = this.deck[switch2]; this.deck[switch2] = placeHolder; } } public void printDeck(){ String isInPlay = ""; for(int i=0; i < this.deck.length; i++){ if(deck[i].getInPlay()) {isInPlay = "in play";} else{isInPlay = "not in play";} System.out.println("Card: " + this.deck[i].getRank() + " of " + this.deck[i].getSuit() + " is " + isInPlay); } } }

Rank package RummyProject; public enum Rank { ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10); public final int points; Rank(int points){ this.points = points; } }

Suit package RummyProject; public enum Suit { CLUBS(1), DIAMONDS(2), HEARTS(3), SPADES(4); public final int order; Suit(int order){ this.order = order; }

}

Player package RummyProject; import java.io.*; public class Player { private String name = null; private Card[] myHand = new Card[11]; private int cardCount = 0; private Card[][] straights = new Card[3][4]; private Card[][] ofAKinds = new Card[3][4]; private int straightCount = 0; private int ofAKindCount = 0; Player(){}; Player(Card[] startingHand){ this.myHand = startingHand; this.cardCount = 10; } public void addCard(Card card){ if(cardCount<11){ myHand[cardCount++] = card; this.clearStraights(); this.organizeHand(); } } public Card dropCard(){ System.out.println("Please enter # of card you would like to drop: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { int drop = Integer.parseInt(br.readLine())-1; Card temp = myHand[drop]; myHand[drop] = myHand[10]; myHand[10] = null; this.cardCount--; this.clearStraights(); this.organizeHand(); return(temp); } catch (IOException ioe) { return(null); } } public Card getLastCard(){ return myHand[cardCount]; } public String getName(){ return this.name; }

public int getCardCount(){ return this.cardCount; } public void printHand(){ for(int i=0; i < this.cardCount; i++){ System.out.println((i+1) + ": " + myHand[i].getRank() + " of " + myHand[i].getSuit()); } } public void getPlayerName(){ // prompt the user to enter their name System.out.print("Enter your name: "); // open up standard input BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //

read the username from the command-line; need to use try/catch

with the // readLine() method try { this.name = br.readLine(); } catch (IOException ioe) { System.out.println("IO error trying to read your name!"); System.exit(1); } } public void organizeHand(){ Card temp = new Card(); for(int i=0; i < cardCount; i++){ for(int j = 0; j < cardCount; j++){ if(this.myHand[i].getRank().ordinal() < this.myHand[j].getRank().ordinal()){ temp = this.myHand[i]; this.myHand[i] = this.myHand[j]; this.myHand[j] = temp; } else if((this.myHand[i].getRank().ordinal() == this.myHand[j].getRank().ordinal()) && (this.myHand[i].getSuit().ordinal() < this.myHand[j].getSuit().ordinal())) { temp = this.myHand[i]; this.myHand[i] = this.myHand[j]; this.myHand[j] = temp; } } } findOfAKinds(); findStraights(); } public void findOfAKinds(){ for(int i=0; i < this.cardCount; i++){

} } public void findStraights(){ for(int i = 0; i < (this.cardCount - 1); i++){ this.myHand[i].setInStraight(false); int j = checkForNext(i+1, this.myHand[i].getSuit(), this.myHand[i].getRank()); if(j > 0){ int k = checkForNext(j+1, this.myHand[j].getSuit(), this.myHand[j].getRank()); if(k > 0){ int l = checkForNext(k+1, this.myHand[k].getSuit(), this.myHand[k].getRank()); if(l > 0){ addStraight(i, j, k ,l); } else{ addStraight(i, j, k); } } } } } private int checkForNext(int position, Suit s, Rank r){ int furthest = position + 4; for(; (position < furthest) && (position < this.cardCount); position++){ if(this.myHand[position].getSuit() == s){ if(this.myHand[position].getRank().ordinal() == (r.ordinal() + 1)){ return position; } } } return 0; } private void addStraight(int i, int j, int k){ this.straights[this.straightCount][0] = this.myHand[i]; this.myHand[i].setInStraight(true); this.straights[this.straightCount][1] = this.myHand[j]; this.myHand[j].setInStraight(true); this.straights[this.straightCount][2] = this.myHand[k]; this.myHand[k].setInStraight(true); this.straightCount++; } private void addStraight(int i, int j, int k, int l){ this.straights[this.straightCount][0] = this.myHand[i]; this.myHand[i].setInStraight(true); this.straights[this.straightCount][1] = this.myHand[j]; this.myHand[j].setInStraight(true); this.straights[this.straightCount][2] = this.myHand[k]; this.myHand[k].setInStraight(true); this.straights[this.straightCount][3] = this.myHand[l]; this.myHand[l].setInStraight(true);

this.straightCount++; } public void clearStraights(){ for(int i=0; i < this.cardCount; i++){ this.myHand[i].setInStraight(false); } for(int j=0; j < 3; j++){ for(int k=0; k < 4; k++){ this.straights[j][k] = null; } } this.straightCount = 0; } private void addThreeOfAKind(int i, int j, this.ofAKinds[this.ofAKindCount][0] this.myHand[i].setInOfAKind(true); this.ofAKinds[this.ofAKindCount][1] this.myHand[j].setInOfAKind(true); this.ofAKinds[this.ofAKindCount][2] this.myHand[k].setInOfAKind(true); this.ofAKindCount++; }

int k){ = this.myHand[i]; = this.myHand[j]; = this.myHand[k];

private void addFourOfAKind(int i, int j, int k, int l){ this.ofAKinds[this.ofAKindCount][0] = this.myHand[i]; this.myHand[i].setInOfAKind(true); this.ofAKinds[this.ofAKindCount][1] = this.myHand[j]; this.myHand[j].setInOfAKind(true); this.ofAKinds[this.ofAKindCount][2] = this.myHand[k]; this.myHand[k].setInOfAKind(true); this.ofAKinds[this.ofAKindCount][3] = this.myHand[l]; this.myHand[l].setInOfAKind(true); this.ofAKindCount++; } /*public int countPoints(){ }*/ public String deckOrDiscard(){ System.out.println("Please enter 1 to pick up from discard \n" + "2 to pick up from deck"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { int answer = Integer.parseInt(br.readLine()); if(answer==1){return "Discard";} else{return "Deck";} } catch (IOException ioe) { return("IO error trying to read your choice!"); } } }

Computer Player

package RummyProject; public class ComputerPlayer extends Player{ private int gameTurns = 0; public void setGameTurns(int gameTurns){ this.gameTurns = gameTurns; }

}

Discard package RummyProject; public class Discard { private Card topOfDiscard; public Card getTop(){ return this.topOfDiscard; } public void setTop(Card card){ this.topOfDiscard = card; } public Card takeTop(){ Card temp = topOfDiscard; topOfDiscard = null; return(temp); } }

Colors package RummyProject; public enum Colors { BLUE(1), YELLOW(2), GREEN(3); private final int value; Colors(int value){ this.value = value; } public int getValue(){ return value; } }

Counter

package RummyProject; public class Counter { Counter (){} private private private private private

Card[][] straights = new Card[3][4]; Card[][] ofAKinds = new Card[3][4]; int straightCount = 0; int ofAKindCount = 0; int points = 0;

public int getPoints(){ return points; } }

Play package RummyProject; import java.io.*; public class Play { public static int gameTurns = 0; public static boolean knock = false; public static void show(Card card, String display){ System.out.println(display + " " + card.getRank() + " of " + card.getSuit()); }

public static void main(String[] args) { Deck deck1 = new Deck(); deck1.shuffle();

// get a new deck // shuffle the deck

Discard discard1 = new Discard(); Player player1 = new Player(deck1.getStartingHand()); new player with a starting hand from the deck player1.getPlayerName(); // sets player name player1.organizeHand(); player1.printHand();

// create

// organize the player hand // show player hand

while(!knock){ discard1.setTop(deck1.getTopCard()); show(discard1.getTop(), "Top Of Discard:"); if(player1.deckOrDiscard() == "Discard"){ player1.addCard(discard1.takeTop()); } else{ player1.addCard(deck1.getTopCard()); } player1.printHand(); discard1.setTop(player1.dropCard());

//computer move } deck1.printDeck();

} }

More Documents from "Farooq Azam"

Proposal.docx
November 2019 18
Proposal Final.docx
November 2019 11
Android-based-rummy-game.pdf
November 2019 13
Rummy-card-game.docx
November 2019 10
Cover Heg.docx
December 2019 28
Pengesah Heg.docx
December 2019 36