Click here to Skip to main content
15,354,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm working on a game of Go Fish for a computer science class and I'm stuck trying to create the scoring system. Basically if you get four of the same card (2 of hearts, spades, aces, and clubs) it adds to an integer counter and removes them from the player's hand. Any help would be greatly appreciated

Java
<pre>// Programming Challenge: Go Fish Part 4
// This program shuffles a deck of cards and then deals them 

import java.util.*;
import java.io.*;

public class GoFish1 {
    public static void main(final String[] args) {
         String[] arr = { "Ace of Spades", "2 of Spades", "3 of Spades", "4 of Spades", "5 of Spades", "6 of Spades", "7 of Spades", "8 of Spades", "9 of Spades", "10 of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Hearts", "2 of Hearts", "3 of Hearts", "4 of Hearts", "5 of Hearts", "6 of Hearts", "7 of Hearts", "8 of Hearts", "9 of Hearts", "10 of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", "Ace of Diamonds", "2 of Diamonds", "3 of Diamonds", "4 of Diamonds", "5 of Diamonds", "6 of Diamonds", "7 of Diamonds", "8 of Diamonds", "9 of Diamonds", "10 of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds", "Ace of Clubs", "2 of Clubs", "3 of Clubs", "4 of Clubs", "5 of Clubs", "6 of Clubs", "7 of Clubs", "8 of Clubs", "9 of Clubs", "10 of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs" };
         ArrayList<String> deck = new ArrayList<>(Arrays.asList(arr));
        ArrayList <String> CPU = new ArrayList<String>();
      ArrayList <String> Player = new ArrayList<String>();
       ArrayList <String> TwoCards = new ArrayList<String>();
      int PlayerBook = 0;
      int CPUbook = 0;
      int Twocount=0;
      boolean PlayerTurn = true;
       boolean Pbook = false;


        System.out.println("\nPlayer's cards:");
         Random rand = new Random();
        for (int i = 0; i < 7 && deck.size() > 0; i++) {
             int randomIndex = rand.nextInt(deck.size());
             String card = deck.get(randomIndex);
             Player.add(card);
            System.out.println(card);
            deck.remove(randomIndex);
            }
            
        System.out.println("\nCPU's cards:");
        for (int i = 0; i < 7 && deck.size() > 0; i++) {
             int randomIndex = rand.nextInt(deck.size());
             String card = deck.get(randomIndex);
             CPU.add(card);
            System.out.println(card);
            deck.remove(randomIndex);      
        }
      while (deck.size() != 0)
{
  
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nWhat card would you like to ask for? (Enter 2-9, King, Ace, Jack, or Queen)");
      String checkT = keyboard.nextLine();
      int i;
      int count;
      boolean hasCard = false;
      for (count = 0; count < CPU.size();
      count++) {
       
        if (CPU.get(count).toLowerCase().contains(checkT.toLowerCase())) {
           hasCard = true;
           System.out.println(CPU.get(count));
           Player.add(CPU.get(count));
           CPU.remove(count);
           count--;
        }
      }
     
      if (hasCard == true) {
          System.out.println("\nThe CPU has that card.");
          
        }
        else {  
            System.out.println("\nGo Fish.");
            int randomI = rand.nextInt(deck.size());
            Player.add(deck.get(randomI));
            deck.remove(randomI);
            PlayerTurn = false;
            
        }
        
        
        if (PlayerTurn == false && CPU.size() > 0)
        {
        //  System.out.println("\n" + CPU.size());
          int randomID = rand.nextInt(CPU.size());
          System.out.println("The dealer asks for " + CPU.get(randomID));
        }
        
        if (CPU.size()==0)
        {
           int randomIndex = rand.nextInt(deck.size());
             String card = deck.get(randomIndex);
             CPU.add(card);
             deck.remove(card);
        }
        
System.out.println("\n"+"Cards left in deck: "+ deck.size());
System.out.println("\nPlayer's cards:");
            System.out.println(Player);

            
        System.out.println("\nCPU's cards:");

 
           System.out.println(CPU);
   
      if (Player.contains("2"))
      {
        TwoCards.add("1");
      
      if(TwoCards.size() == 4)
      {
        Pbook=true;
      }
      }
      if (Pbook== true)
      {
        System.out.println("\nYou have a book!");
        
}

if (deck.size() == 0)
{
  System.out.println("\nGame Over!");
}
if (PlayerBook>CPUbook&& deck.size()==0 || PlayerBook == 13)
{
 System.out.println("You Win!"); 
 System.out.println("Player books: " + PlayerBook);
 System.out.println("CPU books: " + CPUbook);
}
if (PlayerBook<CPUbook&& deck.size()==0 || CPUbook == 13)
{
  System.out.println("You Lose");
 System.out.println("Player books: " + PlayerBook);
 System.out.println("CPU books: " + CPUbook);
}
if (PlayerBook==CPUbook&& deck.size()==0)
{
  System.out.println("\nIts a tie");
 System.out.println("Player books: " + PlayerBook);
 System.out.println("CPU books: " + CPUbook);
} 
          }
    }
}


What I have tried:

I've tried using a loop, searching for the characters in the player's hand, and making separate counters for each card but I'm stuck. Any help would be greatly appreciated
Posted
Updated 18-May-22 10:37am

1 solution

I hate to say it, but ... you are going about this all wrong.
Instead of having an array of strings to hold your cards, create a Card class, and give it three properties: a Value which holds Ace, 2, 3, 4 ... King, and a Suit which holds Hearts, Spades, Diamonds, Clubs, and a Colour - enum[^] is probably the way to go.

Then you can write a method that returns a string "Ace of Spades", "Jack of Diamonds", etc. very easily. And you can compare cards to see if you have four of a kind with trivial and easy to understand code.

Then create an array of Cards and fill it with the pack, shuffle it, and the whole job becomes cleaner, clearer, and a lot simpler to code!
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900