Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
SoloSnap: A Simple Card Game Lecture 1: Case Study: SoloSnap CS2504/CS4092– Algorithms and Linear Data Structures Rules of SoloSnap Shuffle deck Deal cards one by one until either Dr Kieran T. Herley snap 1 occurs (player wins), or run out of cards (player loses) Department of Computer Science University College Cork Challenge Develop Java application to simulate SoloSnap 2014/15 1 KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 Two consecutive cards with same value e.g. two kings, two sevens, etc. 1/1 KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 2/1 2014/15 4/1 Class Card Towards a Java Implementation Class Card Requirements Each card to encode suit, value Operations Our application will employ three Java classes probe suit probe value print card Card abstraction of playing card DeckOfCards abstraction of deck container SoloSnap implementation of game using above classes Ideas Rep. suit, value by ints Getters for these toString method useful KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 3/1 KH (27/01/15) Lecture 1: Case Study: SoloSnap Class Card Class Card Class Card.java–skeleton 1 Class Card.java–skeleton 2 We need some getters and setters for suit and value, so . . . public class Card { public Card(int s , int v) { value = v; suit = s; } public int getSuit () { return suit ; } public int getValue() { return value ; } . . . private int suit ; private int value ; public class Card { public Card(int s , int v) { value = v; suit = s; } . . . . . . private int suit ; private int value ; } } KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 5/1 KH (27/01/15) Lecture 1: Case Study: SoloSnap Class Card 2014/15 6/1 2014/15 8/1 Class Card Using Class Card Using Class Constants Add named class constants to Card.java Card.java: a “blueprint” for card objects /∗ Suit encodings ∗/ public static final int public static final int public static final int public static final int // Create 2 of Diamonds Card c1 = new Card(2, 2); // Create ace of hearts Card c2 = new Card(4, 14); /∗ Value encodings ∗/ public static final int . . . public static final int public static final int public static final int public static final int public static final int // Create four of clubs Card c3 = new Card(3, 4); Awkward: Need to remember “encoding” of suits etc. HEARTS = 4; CLUBS = 3; DIAMONDS = 2; SPADES = 1; TWO = 2; TEN = 10; JACK = 11; QUEEN = 12; KING = 13; ACE = 14; 2 2 KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 7/1 Note: Using an enum would be far better for this. KH (27/01/15) Lecture 1: Case Study: SoloSnap Class Card Class Card Using Class Constants cont’d A Useful Method Would be nice to be able to display cards as text e.g. “Ace of Hearts” rather than cryptic “4 14”. // Create 2 of Diamonds Card c1 = new Card(Card.DIAMONDS, Card.TWO); private String suitAsString () { switch ( getSuit ()) { case Card.HEARTS: return ”Hearts”; case Card.CLUBS: return ”Clubs”; case Card.DIAMONDS: return ”Diamonds”; case Card.SPADES: return ”Spades”; } return ””; } // Create ace of hearts Card c2 = new Card(Card.HEARTS, Card.ACE); // Create four of clubs Card c3 = new Card(Card.CLUBS, Card.FOUR); NB Syntax Classname-dot-Constantname; also far-more readable public String toString () { return valueAsString()+” of ”+suitAsString(); } Note: Java also provides a mechanism for enumerated types that provide a clearer soloution to this problem but we ignore this possibility for simplicity. See Card2.java for details. KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 NB “helper” method private 9/1 KH (27/01/15) Class Card 2014/15 10 / 1 2014/15 12 / 1 Class DeckOfCards Class Card Class DeckOfCards public class Card { public Card(int s , int v) { value = v; suit = s; } public int getSuit () { return suit ; } public int getValue() { return value ; } Requirements /∗ Suit encodings ∗/ public static final int HEARTS = 4; . . . public void print () { System.out. println ( valueAsString()+” of ”+ suitAsString ()); } KH (27/01/15) Lecture 1: Case Study: SoloSnap /∗ Value encodings ∗/ public static final int TWO = 2; public static final int THREE = 3; public static final int FOUR = 4; . . . Represent collection of cards i.e. is a container Operations (add card) shuffle cards remove card probe emptiness Ideas Use array to hold cards Use random swaps for shuffling private int suit ; private int value ; } Lecture 1: Case Study: SoloSnap 2014/15 11 / 1 KH (27/01/15) Lecture 1: Case Study: SoloSnap Class DeckOfCards Class DeckOfCards Representation for DeckOfCards–Left-Justified Array Adding a Card array of (≤ 52) (references to) Card objects left-justified array numCards cards in deck occupy slots 0 to numCards − 1 inclusive, i.e. compacted in left side of array, no “gaps” Place new item in leftmost unoccupied slot (index numCards) Increment numCards slots n upwards are “empty” additions/removals preserve this property need array plus number-of-cards variable (here numCards) KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 13 / 1 KH (27/01/15) Class DeckOfCards Lecture 1: Case Study: SoloSnap 2014/15 14 / 1 2014/15 16 / 1 Class DeckOfCards Removing a Card Skeleton of DeckOfCards.java – 1 public class { . . . } Extract item from rightmost occupied slot (index numCards − 1) DeckOfCards Decrement numCards Return item KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 15 / 1 KH (27/01/15) Lecture 1: Case Study: SoloSnap Class DeckOfCards Class DeckOfCards Skeleton of DeckOfCards.java – 2 public class { . . . Skeleton of DeckOfCards.java – 3 public class DeckOfCards { public DeckOfCards() { theCards = new Card[FULLDECKSIZE]; numCards = 0; DeckOfCards addAllCards (); private static final int FULLDECKSIZE = 52; private Card[] theCards; private int numCards; } . . } private void addAllCards() { . . .} NB Declaration of theCards does not create an array; this variable is initially null. . . . private static final int FULLDECKSIZE = 52; private Card[] theCards; private int numCards; } KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 17 / 1 KH (27/01/15) Class DeckOfCards 2014/15 18 / 1 Class DeckOfCards Initialization–addAllCards cont’d Initialization–addAllCards cont’d private void addAllCards() { for (int s = Card.FIRSTSUIT; s <= Card.LASTSUIT; s++) { for (int v = Card.FIRSTVALUE; v <= Card.LASTVALUE; v++) { /∗ add card for suit s and value v } } } Card.FIRSTSUIT etc. better than “embedded constants” 1 etc. Iterates through all 52 suit-value combinations, creates the corresponding cards, and places each in a distinct slot of the array 3 Lecture 1: Case Study: SoloSnap private void addAllCards() { for (int s = Card.FIRSTSUIT; s <= Card.LASTSUIT; s++) { for (int v = Card.FIRSTVALUE; v <= Card.LASTVALUE; v++) { theCards[numCards] = new Card(s, v); numCards++; } } } 3 Again, this would be cleaner with enums. KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 19 / 1 KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 20 / 1 Class DeckOfCards Class DeckOfCards Simple Operations–removeCard Simple Operations–isEmpty Representation Representation removeCard public Card removeCard() { numCards−−; return theCards[numCards]; } isEmpty public boolean isEmpty() { return (numCards == 0); } KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 21 / 1 KH (27/01/15) Class DeckOfCards Lecture 1: Case Study: SoloSnap 2014/15 22 / 1 2014/15 24 / 1 Class DeckOfCards Shuffling Shuffling cont’d Idea import java. util .Random; . . . Random randomNumGen = new Random(); . . . int rand = randomNumGen.nextInt(N); Repeat following “many” times Pick two array positions at random Swap cards at those two positions Random Numbers Java class java.util.Random provides source of random ints etc. Quantifying “Many” Statistical calculation; we choose ten times number of cards KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 Details See code 23 / 1 KH (27/01/15) Lecture 1: Case Study: SoloSnap SoloSnap SoloSnap The Story So Far Warmup Exercise Exercise Shuffle deck, remove cards one by one and print them A lot done . . . Code Snippet Card.java playing card abstraction Operations: getSuit, getValue, print DeckOfCards.java DeckOfCards deck = new DeckOfCards(); deck. shuffle (); while (! deck.isEmpty()) { currentCard = deck.removeCard(); System.out. println (currentCard . toString ()); } deck of cards abstraction Operations: shuffle, removeCard, isEmpty More to do Implement SoloSnap using above classes KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 25 / 1 KH (27/01/15) Lecture 1: Case Study: SoloSnap SoloSnap 2014/15 26 / 1 2014/15 28 / 1 SoloSnap SoloSnap 1 SoloSnap public class SoloSnap { public static void main(String [] args) { DeckOfCards deck = new DeckOfCards(); deck. shuffle (); public class SoloSnap { public static void main(String [] args) { DeckOfCards deck = new DeckOfCards(); // create and shuffle deck deck. shuffle (); Card currentCard = null; Card prevCard = null; boolean snapFound = false; . . . } // Current and previous card // True if snap found . . . } if (snapFound){...} else {...} // Print outcome } } KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 27 / 1 KH (27/01/15) Lecture 1: Case Study: SoloSnap SoloSnap SoloSnap SoloSnap SoloSnap public class SoloSnap { public static void main(String [] args) { DeckOfCards deck = new DeckOfCards(); deck.shuffle(); public class SoloSnap { public static void main(String [] args) { DeckOfCards deck = new DeckOfCards(); deck.shuffle(); Card currentCard = null; Card prevCard = null; boolean snapFound = false; Card currentCard = null; Card prevCard = null; boolean snapFound = false; while (! deck.isEmpty() && ! snapFound) { . . . currentCard = deck.removeCard(); System.out. println (currentCard . toString ()); . . . } while (! deck.isEmpty() && ! snapFound) { prevCard = currentCard; // Remember prev. card currentCard = deck.removeCard(); System.out. println (currentCard . toString ()); if (prevCard != null) // Compare curent and prev. cards { snapFound = (currentCard.getValue() == prevCard.getValue()); } if (snapFound){...} else {...} } } } if (snapFound){...} else {...} KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 29 / 1 SoloSnap Points to Note Design: multiple classes, each encapsulating one coherent concept DeckOfCards as “container” KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 31 / 1 } } KH (27/01/15) Lecture 1: Case Study: SoloSnap 2014/15 30 / 1