This game uses objects and enumerators to simulate cards, decks, and hands of cards to play go fish. It is entirely text based.
//Cards by Jack P. Oakley //This program makes a card and deck class then uses //them to play Go Fish import java.util.*; /****************************************************************************** * goFishv3 class: * * PURPOSE: Runs the main for the Go Fish game * ******************************************************************************/ class goFishv3 { /*************************************************************************** * main function: * * PURPOSE: Drives the program * * IN: default String[] args * * OUT: none * ***************************************************************************/ public static void main(String[] args) { Scanner kb = new Scanner(System.in); boolean gameOver = false, userTurn = true; int userBooks, compBooks, numMatches; Value guess; userBooks = compBooks = 0; Card temp; Card draw = new Card(Value.values()[0], Suit.values()[0]); System.out.println("The Game of Go Fish!\n"); DeckHand user = new DeckHand(); DeckHand comp = new DeckHand(); DeckHand deck = new DeckHand(); //Build Deck of 52 cards for(Suit suit : Suit.values()) { for(int i = 1; i <= 13; ++i) { deck.insert(new Card(Value.values()[i], suit)); } } //Deal 7 cards to each player for(int i = 0; i < 7; ++i) { user.insert(deck.deleteAny()); comp.insert(deck.deleteAny()); } //Play Go Fish! do { if(userTurn) { userTurn = false; System.out.println("Your turn!"); System.out.println("You have " + userBooks + " books."); System.out.println("Your opponent has " + compBooks + " books.\n"); System.out.println("Your hand consists of: "); System.out.println(user); do { System.out.println("What value would you like to ask for? (1-13) "); guess = Value.values()[kb.nextInt()]; if(user.count(guess) <= 0) { System.out.println("You don't have a card of that value. Try again."); } }while(user.count(guess) <= 0); if(comp.count(guess) > 0) { numMatches = comp.count(guess); System.out.println("Your opponent has " + numMatches + " matches."); for(int i = 0; i < numMatches; ++i) { user.insert(comp.delByVal(guess)); } userTurn = true; } else { System.out.println("Computer: Go Fish!"); draw = deck.deleteAny(); System.out.println("You drew the " + draw); user.insert(draw); if(draw.getValue() == guess) { System.out.println("Lucky you!"); userTurn = true; } } if(user.count(guess) == 4) { System.out.println("You got a book!"); for(int i = 0; i < 4; ++i) { user.delByVal(guess); } userBooks++; } else if(user.count(draw.getValue()) == 4) { System.out.println("You got a book!"); for(int i = 0; i < 4; ++i) { user.delByVal(draw.getValue()); } userBooks++; } holdScreen(kb); } else { userTurn = true; System.out.println("Computer's Turn\n"); temp = comp.deleteAny(); guess = temp.getValue(); comp.insert(temp); System.out.println("Computer: Do you have any " + guess + "s?"); if(user.count(guess) > 0) { userTurn = false; numMatches = user.count(guess); System.out.println("You do, so the computer takes them."); for(int i = 0; i < numMatches; ++i) { comp.insert(user.delByVal(guess)); } } else { System.out.println("You tell the computer \"Go Fish!\""); draw = deck.deleteAny(); System.out.println("The computer drew a card."); comp.insert(draw); if(draw.getValue() == guess) { System.out.println("The computer got its wish!"); userTurn = false; } } if(comp.count(guess) == 4) { System.out.println("The computer got a book!"); for(int i = 0; i < 4; ++i) { comp.delByVal(guess); } compBooks++; } else if(comp.count(draw.getValue()) == 4) { System.out.println("The computer got a book!"); for(int i = 0; i < 4; ++i) { comp.delByVal(draw.getValue()); } compBooks++; } holdScreen(kb); } if(user.getSize() <= 0 || comp.getSize() <= 0 || deck.getSize() <= 0) { gameOver = true; } }while(!gameOver); if(userBooks == compBooks) { System.out.println("Tie game!"); } else if(userBooks > compBooks) { System.out.println("You win!"); } else { System.out.println("You lose!"); } System.out.println(userBooks + " : " + compBooks); } //End main() public static void holdScreen(Scanner kb) { System.out.println("\nEnter any letter and press Enter to continue."); kb.next(); } //End holdScreen() } //End goFishv2 class /****************************************************************************** * Card class: * * PURPOSE: Object that represents a playing card * ******************************************************************************/ class Card { private Value _value; private Suit _suit; /*private String[] suitName = {Suit.SPADES, Suit.CLUBS, Suit.HEARTS, Suit.DIAMONDS}; private String[] valueName = {Value.JOKER, value.ACE, Value.TWO, Value.THREE, "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};*/ /*************************************************************************** * Constructor: * * IN: 2 ints for card value and suit * ***************************************************************************/ public Card(Value value, Suit suit) { _value = value; _suit = suit; } /*************************************************************************** * Accessor: * * IN: none * * OUT: int of value datamember * ***************************************************************************/ public Value getValue() { return _value; } /*************************************************************************** * Accessor: * * IN: none * * OUT: int of suit datamember * ***************************************************************************/ public Suit getSuit() { return _suit; } /*************************************************************************** * toString: * * IN: none * * OUT: formatted String of object datamember values * * ADTNL NOTE: "Value of Suit" * ***************************************************************************/ public String toString() { return _value + " of " + _suit; } }// End Card class /****************************************************************************** * Deckhand class: * * PURPOSE: Object that represents both a deck and a hand (collection of cards)* ******************************************************************************/ class DeckHand { private Card[] _hand; private int _size; private final int _MAXSIZE = 52; private static Random generator = new Random(); /*************************************************************************** * Constructor: * * IN: none * ***************************************************************************/ public DeckHand() { _hand = new Card[_MAXSIZE]; _size = 0; } /*************************************************************************** * Accessor: * * IN: none * * OUT: int of size of deckHand * ***************************************************************************/ public int getSize() { return _size; } /*************************************************************************** * insert function: * * PURPOSE: Allows the insertion of a card into the DeckHand * * IN: Card to be inserted * * OUT: none * ***************************************************************************/ public void insert(Card toInsert) { if(_size >= _hand.length) { Card[] temp = new Card[_hand.length + _MAXSIZE]; for(int i = 0; i < _size; ++i) { temp[i] = _hand[i]; } _hand = temp; } _hand[_size] = toInsert; _size++; } //End insert() /*************************************************************************** * delByVal function: * * PURPOSE: Remove the first card found with a matching value that is given * * IN: int value given * * OUT: returns card being removed * ***************************************************************************/ public Card delByVal(Value value) { for(int i = 0; i < _size; ++i) { if(_hand[i].getValue() == value) { Card toDelete = _hand[i]; _hand[i] = _hand[_size - 1]; _size--; return toDelete; } } return null; } //End delByVal() /*************************************************************************** * deleteAny function: * * PURPOSE: Remove a random card from the DeckHand * * IN: none * * OUT: returns a card chosen at random * ***************************************************************************/ public Card deleteAny() { int rand = generator.nextInt(_size); Card toDelete = _hand[rand]; _hand[rand] = _hand[_size - 1]; _size--; return toDelete; } //End deleteAny() /*************************************************************************** * count function: * * PURPOSE: Counts the number of cards with specified value * * IN: int of chosen value * * OUT: returns int of number of matching cards * ***************************************************************************/ public int count(Value value) { int count = 0; for(int i = 0; i < _size; ++i) { if(_hand[i].getValue() == value) { count++; } } return count; } //End count() /*************************************************************************** * toString: * * IN: none * * OUT: formatted String of object datamember values * * ADTNL NOTE: * ***************************************************************************/ public String toString() { String s = ""; for(int i = 0; i < _size; ++i) { s = s + _hand[i] + "\n"; } return s; } //End toString() } //End DeckHand class enum Suit { SPADES, CLUBS, HEARTS, DIAMONDS; } enum Value { JOKER, ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING; }