Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.89 KB | None | 0 0
  1. package main;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Scanner;
  7. import java.util.Random;
  8.  
  9. /** Play a simple game like Go Fish!
  10. *
  11. * @author Cullen Guthrie
  12. * @version 1
  13. *
  14. */
  15. public class GoFishExample {
  16.    
  17.     private static final int STARTING_HAND_SIZE = 7;
  18.     /** Each player gets 7 cards initially
  19.     *
  20.     */
  21.    
  22.     /** Play a game of Go Fish!  The rules are below.
  23.     * A regular deck of cards consists of 52 cards.
  24.     * There are four suits and thirteen card ranks (Ace, 2, 3,…10, Jack, Queen, and King).
  25.     * We’re going to simplify our cards.  The cards will have ranks from 1 to 13,
  26.     * and each rank will have identical cards.  This removes suit from the game.
  27.     *
  28.     * The computer deals seven cards to the human and the computer from a shuffled deck. The
  29.     * remaining cards are shared in a pile.
  30.     *
  31.     * The human player should play first. The human asks the computer for all its card(s)
  32.     * of a particular rank that is already in his or her hand.
  33.     * For example Alice may ask, "Computer, do you have any threes?" Alice must have at
  34.     * least one card of the rank she requested in her hand. The computer must hand over
  35.     * all cards of that rank. If the computer has no cards of that rank,
  36.     * Alice is told to "Go fish," and she draws a card from the pool and places
  37.     * it in her own hand. When any player at any time has all four cards of one rank,
  38.     *  it forms a book, and the cards must be removed from the hand and placed face
  39.     *  up in front of that player.
  40.     *
  41.     *  If the player has no cards in their hand, they may not request cards form the other
  42.     *  player, they just draw a card.
  43.     *  When the pile is empty, no cards are drawn, but the player still gets to ask for cards
  44.     *
  45.     *  following the same rules.
  46.     *
  47.     *  The computer is not allowed to examine or deduce the human player’s cards while
  48.     *  playing the game. The computer should randomly pick one card from their hand to request.
  49.     *  This means that the computer is not being strategic at all and will
  50.     *  probably lose most of the time (unless the player really stinks at Go Fish!).
  51.     *
  52.     *  When all sets of cards have been laid down, the game ends. The player with the
  53.     *  most cards in piles wins.
  54.     *
  55.     *  The game is easier to play if the cards are printed out in sorted order.
  56.     *  This also uses a method in the Collections class, which meets a learning objective.
  57.    
  58.     * @param args There are no command line arguments.
  59.     */
  60.    
  61.     public static void main(String[] args)
  62.     {
  63.         ArrayList<Integer> pool = createDeck();
  64.         Scanner input = new Scanner(System.in);
  65.        
  66.         Collections.shuffle(pool);
  67.        
  68.         playOneGame(pool, input);
  69.     }
  70.    
  71.     /** Play one full game of Go Fish!.
  72.     *
  73.     * @param pool The deck of cards, already shuffled.
  74.     * @param input Attached to the keyboard to interact with the user.
  75.     */
  76.    
  77.     public static void playOneGame(ArrayList<Integer> pool, Scanner input)
  78.     {
  79.         ArrayList<Integer> computer = new ArrayList<Integer>();
  80.         ArrayList<Integer> person = new ArrayList<Integer>();
  81.         ArrayList<Integer> computerPile = new ArrayList<Integer> ();
  82.         ArrayList<Integer> personPile = new ArrayList<Integer>();
  83.        
  84.         // TODO: Deal cards
  85.         dealHands(pool, person, computer);
  86.        
  87.         // TODO: Show the person their starting hand
  88.         showGameState(person, computerPile, personPile);
  89.         Random random = new Random();
  90.         // Play the game
  91.         while (computerPile.size() + personPile.size() < 52 || !pool.isEmpty())
  92.         {
  93.             // Let the person play first
  94.             // show the person their cards
  95.             if (!person.isEmpty())
  96.             {
  97.                 System.out.println("What card do you want?");
  98.                 int card = input.nextInt();
  99.                
  100.                 //TODO: Play one turn with the person doing the choosing
  101.                
  102.                 playOneTurn(card, person, computer, personPile, computerPile, pool);
  103.             }
  104.             else
  105.             {
  106.                 int cardFromPile = random.nextInt(pool.size());
  107.                 person.add(pool.get(cardFromPile));
  108.                 pool.remove(cardFromPile);
  109.             }
  110.            
  111.            
  112.             // Now it is the computer's turn
  113.             // Randomly choose a card
  114.             if (!computer.isEmpty())
  115.             {
  116.                 int card = computer.get((int)(Math.random()*computer.size()));
  117.                 System.out.println("Do you have any "  + card + "'s ?");
  118.                
  119.                 //TODO: Play one turn with the computer doing the choosing
  120.                 playOneTurn(card, computer, person, computerPile, personPile, pool);
  121.             }
  122.             else if (!pool.isEmpty())
  123.             {
  124.                 //TODO: Let the computer draw from the deck
  125.                 int cardFromPile = random.nextInt(pool.size());
  126.                 person.add(pool.get(cardFromPile));
  127.                 pool.remove(cardFromPile);
  128.             }
  129.            
  130.             showGameState(person, computerPile, personPile);
  131.         }
  132.        
  133.         // TODO: Determine the winner and tell the user--remember ties are possible
  134.         if ( personPile.size() > computerPile.size())
  135.         {
  136.             System.out.println("You are the winner with " + personPile.size() + " books");
  137.         }
  138.         if ( personPile.size() < computerPile.size())
  139.         {
  140.             System.out.println("You lost with the computer have " + computerPile.size() + " books.");
  141.         }
  142.         if ( personPile.size() == computerPile.size())
  143.         {
  144.             System.out.println("The game is a tie with " + computerPile.size() + " books.");
  145.         }
  146.     }
  147.    
  148.     /** Show the user their cards and their pile and the computer's pile.
  149.     *
  150.     * @param person The cards in the person's hand.
  151.     * @param computerPile The pile of completed books for the computer.
  152.     * @param personPile The pile of completed books for the person.
  153.     */
  154.    
  155.     public static void showGameState(ArrayList<Integer> person, ArrayList<Integer> computerPile,
  156.     ArrayList<Integer> personPile)
  157.     {
  158.         System.out.println("Here are your cards");
  159.         showCards(person);
  160.         System.out.println("Your groups:");
  161.         showCards(personPile);
  162.         System.out.println("CPU's groups:");
  163.         showCards(computerPile);
  164.         System.out.println("-----------------------------------------------");
  165.     }
  166.    
  167.     /** Play one turn of Go Fish!. The chooser is the person who is selecting a card from the
  168.     * other person's hand.  This will alternate between the person and the computer.
  169.     * @param card The card that has been selected.
  170.     * @param chooser The hand for the player who is currently choosing.
  171.     * @param chosen The hand for the player who is being asked for cards.
  172.     * @param chooserPile The pile for the player who is currently choosing.
  173.     * @param chosenPile The pile for the player who is being asked for cards.
  174.     * @param pool The deck of cards that have not yet been distributed, already sorted.
  175.     */
  176.    
  177.     public static void playOneTurn(int card, ArrayList<Integer> chooser, ArrayList<Integer> chosen,
  178.     ArrayList<Integer> chooserPile, ArrayList<Integer> chosenPile, ArrayList<Integer> pool)
  179.     {
  180.         if (chosen.contains(card))
  181.         {
  182.             //TODO: Chosen gives cards to Chooser
  183.             transferCards(card, chooser, chosen);
  184.            
  185.             //TODO: If there is a set of four matching cards, put them up on the table
  186.             int Books = 0;
  187.            
  188.             for ( int x = 0; x < chooser.size(); x++)
  189.             {
  190.                 if (card == chooser.get(x))
  191.                 {
  192.                     Books ++;
  193.                 }
  194.                 if(Books == 4)
  195.                 {
  196.                     chooserPile.add(card);
  197.                     for(int r = 0; r < chooser.size() + 1; r++)
  198.                     {
  199.                         if (chooser.get(r) == card)
  200.                         {
  201.                             chooser.remove(r);
  202.                         }
  203.                     }
  204.                 }
  205.                 Books = 0;
  206.             }
  207.            
  208.            
  209.         }
  210.         else
  211.         {
  212.             System.out.println("Go fish!");
  213.            
  214.             //TODO: Draw a card by removing it from the pool and putting it in the chooser's hand
  215.             Random random = new Random();
  216.             int cardFromPile = random.nextInt(pool.size());
  217.             chooser.add(pool.get(cardFromPile));
  218.             pool.remove(cardFromPile);
  219.            
  220.             //TODO: If there is a set of four matching cards, put them on the table
  221.             int Books = 0;
  222.             for ( int x = 0; x < chooser.size(); x++)
  223.             {
  224.                 if (card == chooser.get(x))
  225.                 {
  226.                     Books++;
  227.                 }
  228.             }
  229.             if(Books == 4)
  230.             {
  231.                 chooserPile.add(card);
  232.                 for(int r = 0; r < chooser.size(); r++)
  233.                 {
  234.                     if (chooser.get(r) == card)
  235.                     {
  236.                         chooser.remove(r);
  237.                     }
  238.                 }
  239.             }
  240.             Books = 0;
  241.            
  242.         }
  243.     }
  244.    
  245.     /** Transfer all cards of rank card from the source to the destination.
  246.     *
  247.     * @param card The rank of the selected card.
  248.     * @param destination The hand that will receive the cards.
  249.     * @param source The hand that will lose the cards.
  250.     */
  251.    
  252.     public static void transferCards(int card, ArrayList<Integer> destination, ArrayList<Integer> source)
  253.     {
  254.         while (source.contains(card))
  255.         {
  256.             destination.add(card);
  257.             source.remove(new Integer(card));
  258.         }
  259.     }
  260.    
  261.     /** Deal two equal size hands, one to each player.
  262.     *
  263.     * @param deck The deck of cards that should be dealt. These cards should have been shuffled.
  264.     * @param hand1 The first player.
  265.     * @param hand2 The second player.
  266.     */
  267.    
  268.     public static void dealHands(ArrayList<Integer> deck, ArrayList<Integer> hand1, ArrayList<Integer> hand2)
  269.     {
  270.         //TODO: Deal the cards
  271.         Random randomNumber = new Random();
  272.         int count = 0;
  273.         while ( count <= STARTING_HAND_SIZE )
  274.         {
  275.             int randomIndex = randomNumber.nextInt(deck.size());
  276.             hand1.add(deck.get(randomIndex));
  277.             deck.remove(deck.get(randomIndex));
  278.             count++;
  279.         }
  280.        
  281.         count = 0;
  282.         while ( count <= STARTING_HAND_SIZE )
  283.         {
  284.             int randomIndex = randomNumber.nextInt(deck.size());
  285.             hand2.add(deck.get(randomIndex));
  286.             deck.remove(deck.get(randomIndex));
  287.             count++;
  288.         }
  289.        
  290.     }
  291.    
  292.     /** Build a deck of 52 cards, 4 of each rank from 1 to 13
  293.     *
  294.     * @return The deck of cards.
  295.     */
  296.    
  297.     public static ArrayList<Integer> createDeck()
  298.     {
  299.         //TODO: Create a deck of cards
  300.         ArrayList<Integer> buildDeck = new ArrayList<Integer>();
  301.         int i = 0;
  302.         while (i < 52)
  303.         {
  304.             int addRankings = i % 13 + 1;
  305.             buildDeck.add(addRankings);
  306.             i++;
  307.         }
  308.        
  309.         return buildDeck;// returns built deck
  310.     }
  311.    
  312.    
  313.     /** Show all of the cards is any given pack, hand, deck, or pile.
  314.     *
  315.     * @param cards The cards to be displayed
  316.     */
  317.    
  318.     public static void showCards(ArrayList<Integer> cards)
  319.     {
  320.         // TODO: Sort the cards to make it easier for the user to know what they have
  321.         Collections.sort(cards);
  322.        
  323.         for (Integer i: cards)
  324.         {
  325.             System.out.print(i + " ");
  326.         }
  327.         System.out.println();
  328.     }
  329.    
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement