Question: There is a trick that greatly simplifies building a deck of cards. Create the deck by using a loop that counts from 0 to 51
There is a trick that greatly simplifies building a deck of cards. Create the deck by using a loop that counts from 0 to 51 (inclusive). The rank is determined by taking the card number % 13 + 1. This particular game does not require any understanding of suit, but if you wanted to find the suit you would use number % 4 + 1 (if the suits were unit indexed like the ranks were). A single while loop can create all 52 cards. This could also be done with a nested loop.
Your deck of cards should be an ArrayList
Shuffle the cards using a method in the Collections class.
The real challenge to this program is to handle multiple ArrayList objects. You will need one ArrayList for each hand, one for the pool, and one for each players pile. The cards (which are just Integers) should only be constructed once. They should be moved around between the various ArrayList objects as play progresses. The methods are designed to alternate between being used with the computer doing the choosing and the player doing the choosing. Learning to manipulate these methods properly is the main purpose of this laboratory.
Warning on ArrayList
There is one really tricky thing that happens in Java in this project. The remove method has two versions with one parmeter: one takes an int argument, and the other an Object. The one with the int argument removes an element with a given index. The one with the Object argument removes an element that is .equals() to the given object. Unfortunately, this confuses Java (with autoboxing). It will always think you are using an index unless you use an Integer object as a parameter. The fix is shown in the code below.
ArrayList
list.add(1); // 1 gets autoboxed into an Integer object
list.add(2); // 2 gets autoboxed into an Integer object
list.remove(0); // removes the value at index zero
If you want to remove the number 2, you have to make it an Integer object.
list.remove(new Integer(2)); // removes the first value 2 (starting from index 0)
// that it finds in the ArrayListthis value is at index 0
TODO in Eclipse
Eclipse has a nice feature that well use in this project. When you make a comment with TODO: (and you must have the colon), it will be put into the task list when the file is saved.
To display the Task List go to Window -> Show View -> Tasks. The Task window will show up at the bottom of the usual eclipse frames along with the Console window. Remember, it only updates when the file is saved, so dont expect immediate responses.
These tags are used to keep track of things that need to be done. Ive put them throughout the code to help you find the opportunities to meet the learning objectives.
This is a shortcut that every professional programmer uses. Youll notice that eclipse puts little blue squares in the right hand margin and blue check boxes in the left hand margin to show you where the TODO: tags are located.
HERE IS THE CODE :
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). * Were 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 Mayra may ask, "Computer, do you have any threes?" Mayra 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, * Mayra 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 players 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
// 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
/** 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
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
