package hobknob; /** * Game is the logic for a Hobknob game. * User draws a card and the card value is added to a total score. * When an ace is drawn the game is over. */ public class Game extends java.util.Observable implements I_Game { I_Deck deck; // the deck for this game I_CardPoints pointTable; // the point table for this game Counter undoCount; // count # of undo's Card lastCardDrawn; // save last card drawn int score; // score for last card int total; // total of all turns public Game(I_Deck aDeck, I_CardPoints aTable) { deck = aDeck; pointTable = aTable; } /** Reset the game state */ public void start() { score = 0; total = 0; lastCardDrawn = null; undoCount = new Counter(3); } /** Draw a card and get its score and add it to total. */ public void draw() { lastCardDrawn = deck.dealCard(); score = pointTable.getValue(lastCardDrawn); total = total + score; setChanged(); notifyObservers(); } /** Undo the last move (limit 3). (User can't do 2 undo's consecutively). * Undo removes points for last drawn card from total and returns * the card to the deck. */ public void undo() { if (undoCount.notDone() && lastCardDrawn != null) { deck.restock(lastCardDrawn); score = 0; total = total - pointTable.getValue(lastCardDrawn); lastCardDrawn = null; undoCount.inc(); } } /** Determine if the game is over. * @return true if last card drawn was an ace. */ public boolean gameOver() { if (lastCardDrawn != null) { return lastCardDrawn.rank() == Rank.ace; } else { return false; } } public Card getLastDraw() { return lastCardDrawn; } public int getScore() { return score; } public int getTotal() { return total; } }