Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS lab Project(Go fish Game) import java.util.ArrayList; import java.util.Collec

ID: 3694801 • Letter: C

Question

CS lab Project(Go fish Game)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/** Play a simple game like Go Fish!
*
* @author Deborah A. Trytten
* @version 1.0
*
*/
public class GoFish {

   /** Each player gets 7 cards initially
   *
   */
   public static int STARTING_HAND_SIZE = 7;
  
   /** Play a game of Go Fish! The rules are below.
   * A regular deck of cards consists of 52 cards.
   * There are four suits and thirteen card ranks (Ace, 2, 3,?10, Jack, Queen, and King).
   * We?e going to simplify our cards. The cards will have ranks from 1 to 13,
   * and each rank will have identical cards. This removes suit from the game.
   *
   * The computer deals seven cards to the human and the computer from a shuffled deck. The
   * remaining cards are shared in a pile.
   *
   * The human player should play first. The human asks the computer for all its card(s)
   * of a particular rank that is already in his or her hand.
   * For example Alice may ask, "Computer, do you have any threes?" Alice must have at
   * least one card of the rank she requested in her hand. The computer must hand over
   * all cards of that rank. If the computer has no cards of that rank,
   * Alice is told to "Go fish," and she draws a card from the pool and places
   * it in her own hand. When any player at any time has all four cards of one rank,
   * it forms a book, and the cards must be removed from the hand and placed face
   * up in front of that player.
   *
   * If the player has no cards in their hand, they may not request cards form the other
   * player, they just draw a card.
   * When the pile is empty, no cards are drawn, but the player still gets to ask for cards
   * following the same rules.
   *
   * The computer is not allowed to examine or deduce the human player? cards while
   * playing the game. The computer should randomly pick one card from their hand to request.
   * This means that the computer is not being strategic at all and will
   * probably lose most of the time (unless the player really stinks at Go Fish!).
   *
   * When all sets of cards have been laid down, the game ends. The player with the
   * most cards in piles wins.
   *
   * The game is easier to play if the cards are printed out in sorted order.
   * This also uses a method in the Collections class, which meets a learning objective.

   * @param args There are no command line arguments.
   */
   public static void main(String[] args)
   {
       // TODO: Create deck of cards
       ArrayList<Integer> pool=null;
       Scanner input = new Scanner(System.in);
      
       // Shuffle cards
       //TODO: Shuffle Cards
      
       playOneGame(pool, input);
   }
  
   /** Play one full game of Go Fish!.
   *
   * @param pool The deck of cards, already shuffled.
   * @param input Attached to the keyboard to interact with the user.
   */
   public static void playOneGame(ArrayList<Integer> pool, Scanner input)
   {
       ArrayList<Integer> computer = new ArrayList<Integer>();
       ArrayList<Integer> person = new ArrayList<Integer>();
       ArrayList<Integer> computerPile = new ArrayList<Integer> ();
       ArrayList<Integer> personPile = new ArrayList<Integer>();

       // TODO: Deal cards
      
       // TODO: Show the person their starting hand
  
      
       // Play the game
       while (computerPile.size() + personPile.size() < 52 || !pool.isEmpty())
       {
           // Let the person play first
           // show the person their cards
           if (!person.isEmpty())
           {
               System.out.println("What card do you want?");
               int card = input.nextInt();
              
               //TODO: Play one turn with the person doing the choosing
              
           }
           else
           {
               //TODO: Let the player draw from the deck
           }
          
           showGameState(person, computerPile, personPile);
          
           // Now it is the computer's turn
           // Randomly choose a card
           if (!computer.isEmpty())
           {
               int card = computer.get((int)(Math.random()*computer.size()));  
               System.out.println("Do you have any " + card + "'s ?");
              
               //TODO: Play one turn with the computer doing the choosing
           }
           else if (!pool.isEmpty())
           {
               //TODO: Let the computer draw from the deck
           }
          
           showGameState(person, computerPile, personPile);
       }
      
       // TODO: Determine the winner and tell the user--remember ties are possible

          
   }
  
   /** Show the user their cards and their pile and the computer's pile.
   *
   * @param person The cards in the person's hand.
   * @param computerPile The pile of completed books for the computer.
   * @param personPile The pile of completed books for the person.
   */
   public static void showGameState(ArrayList<Integer> person, ArrayList<Integer> computerPile,
           ArrayList<Integer> personPile)
   {
       System.out.println("Here are your cards");
       showCards(person);
       System.out.println("Here is your pile");
       showCards(personPile);
       System.out.println("Here is my pile");
       showCards(computerPile);
   }
  
   /** Play one turn of Go Fish!. The chooser is the person who is selecting a card from the
   * other person's hand. This will alternate between the person and the computer.
   * @param card The card that has been selected.
   * @param chooser The hand for the player who is currently choosing.
   * @param chosen The hand for the player who is being asked for cards.
   * @param chooserPile The pile for the player who is currently choosing.
   * @param chosenPile The pile for the player who is being asked for cards.
   * @param pool The deck of cards that have not yet been distributed, already sorted.
   */
   public static void playOneTurn(int card, ArrayList<Integer> chooser, ArrayList<Integer> chosen,
           ArrayList<Integer> chooserPile, ArrayList<Integer> chosenPile, ArrayList<Integer> pool)
   {
       if (chosen.contains(card))
       {
           //TODO: Chosen gives cards to Chooser
           //TODO: If there is a set of four matching cards, put them up on the table
          
       }
       else
       {
           System.out.println("Go fish!");
          
           //TODO: Draw a card by removing it from the pool and putting it in the chooser's hand
           //TODO: If there is a set of four matching cards, put them on the table
          
       }
   }
  
   /** Transfer all cards of rank card from the source to the destination.
   *
   * @param card The rank of the selected card.
   * @param destination The hand that will receive the cards.
   * @param source The hand that will lose the cards.
   */
   public static void transferCards(int card, ArrayList<Integer> destination, ArrayList<Integer> source)
   {
       while (source.contains(card))
       {
           destination.add(card);
           source.remove(new Integer(card)); // this is that tricky thing from the handout
       }
   }
  
   /** Deal two equal size hands, one to each player.
   *
   * @param deck The deck of cards that should be dealt. These cards should have been shuffled.
   * @param hand1 The first player.
   * @param hand2 The second player.
   */
   public static void dealHands(ArrayList<Integer> deck, ArrayList<Integer> hand1, ArrayList<Integer> hand2)
   {
       //TODO: Deal the cards
   }
  
   /** Build a deck of 52 cards, 4 of each rank from 1 to 13
   *
   * @return The deck of cards.
   */
   public static ArrayList<Integer> createDeck()
   {
       //TODO: Create a deck of cards
       return null;// keep the compiler happy and quiet
   }
  

   /** Show all of the cards is any given pack, hand, deck, or pile.
   *
   * @param cards The cards to be displayed
   */
   public static void showCards(ArrayList<Integer> cards)
   {
       // TODO: Sort the cards to make it easier for the user to know what they have
      
       for (Integer i: cards)
       {
           System.out.print(i + " ");
       }
       System.out.println();
   }

}

Learning Objectives 1. Use at least four distinct ArrayList objects in a single program. (10 points)- 2. Use at least two methods from the Collections class. (10 points) 3. Pass different ArrayList objects to the same parameter in a method. (20 points) 4. Write a method that can be used repeatedly to alternate turns in a game by changing arguments. (20 points) 5. Use TODO: tags in eclipse to track progress. (10 points) 6. Implement the logic of the card game Go Fish! correctly. (20 points) 10 points will be awarded for the documentation of your program. That means using good names for variables, proper and consistent indentation of code, sufficient comments and meaningful use of whitespace. When your program is completed and running, have the teaching assistants check it to get credit for the lab. If you do not complete the laboratory during the allotted time, you may submit it on Janux before Wednesday, April 27 at 11:59 p.m. Only people who attend the whole laboratory will be permitted to submit assignments on Janux +' The deadline is after the midterm examination. It will be highly beneficial for you to complete the laboratory before the examination I have provided sample code to get you off to a good start and help you pick methods in such a way that you can meet the learning objectives of the project.v Description of Game we'll implement the card game “Go Fish." with a human playing against a computer. The rules are below, taken from http://en.wikipedia.org/wiki/Go Fish but edited significantly. Please pay attention to the rules I have here-the real game gets fairly sticky to program and this one is more simple. A regular deck of cards consists of 52 cards. There are four suits and thirteen card ranks (Ace, 2, 310 Jack, Queen, and King). We're going to simplify our cards. The cards will have ranks from 1 to 13, and each rank will have identical cards. This removes suit from the game.+ The computer deals seven cards to the human and the computer from a shuffled deck. The remaining cards are shared in a pile.+

Explanation / Answer

GoFish.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class GoFish {

   public static int STARTING_HAND_SIZE = 7;

   public static void main(String[] args)
   {
       // TODO: Create deck of cards
       ArrayList<Integer> pool = createDeck();
       Scanner input = new Scanner(System.in);

       // Shuffle cards
       //TODO: Shuffle Cards
       Collections.shuffle(pool);
       playOneGame(pool, input);
   }

   public static void playOneGame(ArrayList<Integer> pool, Scanner input)
   {
       ArrayList<Integer> computer = new ArrayList<Integer>();
       ArrayList<Integer> person = new ArrayList<Integer>();
       ArrayList<Integer> computerPile = new ArrayList<Integer> ();
       ArrayList<Integer> personPile = new ArrayList<Integer>();

       // TODO: Deal cards
       dealHands(person,pool,computer);

       // TODO: Show the person their starting hand
       showGameState(person, computerPile, personPile);
       Random random = new Random();

       // Play the game
       while (computerPile.size() + personPile.size() < 52 || !pool.isEmpty())
       {
           // Let the person play first
           // show the person their cards
           if (!person.isEmpty())
           {
               System.out.println("What card do you want?");
               int card = input.nextInt();

               //TODO: Play one turn with the person doing the choosing
               playOneTurn(card, person, computer, personPile, computerPile, pool);
           }
           else
           {
               //TODO: Let the player draw from the deck
               int cardFromPile = random.nextInt(pool.size());
               person.add(pool.get(cardFromPile));
               pool.remove(cardFromPile);
           }

           showGameState(person, computerPile, personPile);

           // Now it is the computer's turn
           // Randomly choose a card
           if (!computer.isEmpty())
           {
               int card = computer.get((int)(Math.random()*computer.size()));  
               System.out.println("Do you have any " + card + "'s ?");

               //TODO: Play one turn with the computer doing the choosing
               playOneTurn(card, person, computer, personPile, computerPile, pool);
           }
           else if (!pool.isEmpty())
           {
               //TODO: Let the computer draw from the deck
               int cardFromPile = random.nextInt(pool.size());
               person.add(pool.get(cardFromPile));
               pool.remove(cardFromPile);
           }

           showGameState(person, computerPile, personPile);
       }

       // TODO: Determine the winner and tell the user--remember ties are possible
       if ( personPile.size() > computerPile.size())
       {
           System.out.println("You are the winner with " + personPile.size() + " books");
       }
       if ( personPile.size() < computerPile.size())
       {
           System.out.println("You lost with the computer have " + computerPile.size() + " books.");
       }
       if ( personPile.size() == computerPile.size())
       {
           System.out.println("The game is a tie with " + computerPile.size() + " books.");
       }        
   }

   public static void showGameState(ArrayList<Integer> person, ArrayList<Integer> computerPile,
           ArrayList<Integer> personPile)
   {
       System.out.println("Here are your cards");
       showCards(person);
       System.out.println("Here is your pile");
       showCards(personPile);
       System.out.println("Here is my pile");
       showCards(computerPile);
   }

   public static void playOneTurn(int card, ArrayList<Integer> chooser, ArrayList<Integer> chosen,
           ArrayList<Integer> chooserPile, ArrayList<Integer> chosenPile, ArrayList<Integer> pool)
   {
       if (chosen.contains(card))
       {
           //TODO: Chosen gives cards to Chooser
           transferCards(card, chooser, chosen);
           //TODO: If there is a set of four matching cards, put them up on the table
           int x = 0;
           int deck = 0;
           for (x = 0; x < chooser.size(); x++)
           {
               if (card == chooser.get(x))
               {
                   deck = deck + 1;
               }
               if(deck == 4)
               {
                   chooserPile.add(card);
                   int y = 0;
                   for(y = 0; y < chooser.size() + 1; y++)
                   {
                       if (chooser.get(y) == card)
                       {
                           chooser.remove(y);
                       }
                   }
               }
           }
       }
       else
       {
           System.out.println("Go fish!");

           //TODO: Draw a card by removing it from the pool and putting it in the chooser's hand
           Random random = new Random();
           if(pool.size() > 0)
           {
               int cardFromPile = random.nextInt(pool.size());
               chooser.add(pool.get(cardFromPile));
               pool.remove(cardFromPile);
           }

           //TODO: If there is a set of four matching cards, put them on the table
           int x = 0;
           for (x = 0; x < chooser.size(); x++)
           {
               int frequency = Collections.frequency(chooser, x);
               if (frequency == 4)
               {
                   for (int i = 0; i < 4; i++)
                   {
                       chooserPile.add(x);
                       chooser.remove(new Integer(x));
                   }
               }
               if(pool.isEmpty() && chooser.get(0) == chooser.get(3))
               {
                   int y = chooser.get(0);
                   for (int i = 0; i < 4; i++)
                   {
                   chooserPile.add(new Integer(y));
                   chooser.remove(new Integer(y));
                   }
               }
           }
       }
   }

   public static void transferCards(int card, ArrayList<Integer> destination, ArrayList<Integer> source)
   {
       while (source.contains(card))
       {
           destination.add(card);
           source.remove(new Integer(card));
       }
   }

   public static void dealHands(ArrayList<Integer> deck, ArrayList<Integer> hand1, ArrayList<Integer> hand2)
   {
       //TODO: Deal the cards
       Random randomNumber = new Random();
       if (deck.size() > 0)
       {
           int count = 0;
           while (count <= STARTING_HAND_SIZE)
           {
               int randomIndex = randomNumber.nextInt(deck.size());
               hand1.add(deck.get(randomIndex));
               deck.remove(deck.get(randomIndex));
               count++;
           }
           count = 0;
           while (count <= STARTING_HAND_SIZE)
           {
               int randomIndex = randomNumber.nextInt(deck.size());
               hand2.add(deck.get(randomIndex));
               deck.remove(deck.get(randomIndex));
               count++;
           }
       }
       else
       {
           return;  
       }
   }


   public static ArrayList<Integer> createDeck()
   {
       //TODO: Create a deck of cards
       ArrayList<Integer> createDeck = new ArrayList<Integer>();
       int i = 0;
       while (i < 52)
       {
           int addRankings = (i % 13) + 1;
           createDeck.add(addRankings);
           i++;
       }
       return createDeck;
   }

   public static void showCards(ArrayList<Integer> cards)
   {
       // TODO: Sort the cards to make it easier for the user to know what they have
       Collections.sort(cards);
       for (Integer i: cards)
       {
           System.out.print(i + " ");
       }
       System.out.println();
   }

}

sample output

Here are your cards                                                                                                                                         
                                                                                                                                                            
Here is your pile                                                                                                                                           
                                                                                                                                                            
Here is my pile                                                                                                                                             
                                                                                                                                                            
Here are your cards                                                                                                                                         
13                                                                                                                                                          
Here is your pile                                                                                                                                           
                                                                                                                                                            
Here is my pile                                                                                                                                             
                                                                                                                                                            
Here are your cards                                                                                                                                         
5 13                                                                                                                                                        
Here is your pile                                                                                                                                           
Here are your cards                                                                                                                                         
5 13                                                                                                                                                        
Here is your pile                                                                                                                                           
                                                                                                                                                            
Here is my pile                                                                                                                                             
                                                                                                                                                            
What card do you want?                                                                                                                                      
13                                                                                                                                                          
Go fish!                                                                                                                                                    
Here are your cards                                                                                                                                         
5 8 13                                                                                                                                                      
Here is your pile                                                                                                                                           
                                                                                                                                                            
Here is my pile                                                                                                                                             
                                                                                                                                                            
Here are your cards                                                                                                                                         
5 6 8 13                                                                                                                                                    
Here is your pile                                                                                                                                           
                                                                                                                                                            
Here is my pile                                                                                                                                             
                                                                                                                                                            
What card do you want?                                                                                                                                      
5                                                                                                                                                           
Go fish!                                                                                                                                                    
Here are your cards                                                                                                                                         
5 6 8 8 13                                                                                                                                                  
Here is your pile                                                                                                                                           
                                                                                                                                                            
Here is my pile                                                                                                                                             
                                                                                                                                                            
Here are your cards