0

So I'm building a version of Go Fish. I am done with all of the coding, but it won't run because in the Player class, I can't use a Card.rank parameter in methods. I understand that, but I still need to be able to input a rank somehow.

Look at my Card constructor and any instance of me trying to use a Player method where I need a rank input (Player.subSets and Player.searchHand). (I am also only showing one player's turn code to be concise.)

Program (main) class:

import java.util.*;

public class Program
{
public static void main(String args[]) 
{
    String[] rank = {"two", "three", "four", "five", "six", "seven", "eight",
                 "nine", "ten", "jack", "queen", "king", "ace"};
    String[] suit = {"hearts", "diamonds", "spades", "clubs"};
    Scanner scan = new Scanner(System.in);
    String something = "yes", something2 = "yes", success = "yes"; //Use with while loops
    String turn = "one";
    String temp; //Use with setting names
    String temp2, temp3; //For player being searched
    Card temp4 = new Card(); //For player being searched
    Card[] deck = new Card[52]; //Deck array
    int playercount = 0;
    Player one = new Player("temp");
    Player two = new Player("temp");
    Player three = new Player("temp");
    Player four = new Player("temp");

    //Start game
    while (something.equalsIgnoreCase("yes"))
    {
        //Prepare game
        Card.makeDeck(deck, rank, suit);
        deck = Card.getDeck();
        Card.shuffle(deck);

        while (something2.equalsIgnoreCase("yes") || playercount < 2) //Add players to game
        {
            System.out.println("Would a(nother) player like to join?");
            something2 = scan.nextLine();
            System.out.println();
            if (something2.equalsIgnoreCase("yes"))
            {
                if (playercount <= 4)
                {
                    if (playercount == 0)
                    {
                        System.out.println("What is your name: ");
                        Player one1 = new Player(scan.nextLine());
                        one = one1;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 1)
                    {
                        System.out.println("What is your name: ");
                        Player two2 = new Player(scan.nextLine());
                        two = two2;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 2)
                    {
                        System.out.println("What is your name: ");
                        Player three3 = new Player(scan.nextLine());
                        three = three3;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 3)
                    {
                        System.out.println("What is your name: ");
                        Player four4 = new Player(scan.nextLine());
                        four = four4;
                        playercount++;
                        System.out.println();
                    }
                    else {System.out.println("Only four players are allowed.");
                          something2 = "no";}
                }
            }
            else if (playercount < 2)
            {
                System.out.println("You need at least two players...");
                System.out.println();
            }
        }
        //Deal cards
        if (playercount == 2) 
        {
            for (int i = 1; i < 8; i++)
            {
                one.addCard(Card.draw(deck));
                deck = Card.getDeck();
                two.addCard(Card.draw(deck));
                deck = Card.getDeck();
            }
        }
        else if (playercount == 3) 
        {
            for (int i = 1; i < 8; i++) 
            {
                one.addCard(Card.draw(deck));
                deck = Card.getDeck();
                two.addCard(Card.draw(deck));
                deck = Card.getDeck();
                three.addCard(Card.draw(deck));
                deck = Card.getDeck();
            }
        }
        else
        {
            for (int i = 1; i < 6; i++)
            {
                one.addCard(Card.draw(deck));
                deck = Card.getDeck();
                two.addCard(Card.draw(deck));
                deck = Card.getDeck();
                three.addCard(Card.draw(deck));
                deck = Card.getDeck();
                four.addCard(Card.draw(deck));
                deck = Card.getDeck();
            }
        }
        //Take turns
        while (something.equalsIgnoreCase("yes"));
        {
            success = "yes";
            if (turn.equalsIgnoreCase("one") && something.equalsIgnoreCase("yes"))
                {
                    System.out.println("Player one: " + one.getName() + "'s turn!");
                    System.out.println();
                    //Output hand
                    System.out.println("You have: " + Card.toString(one.getHand()));
                    System.out.println();
                    //Ask what who to search
                    System.out.println("Whose hand would you like to check?");
                    System.out.println("(Enter a player's name.)");
                    temp2 = scan.nextLine(); //Set player name to temp2
                    System.out.println();
                    //Ask what to search
                    while (success.equalsIgnoreCase("yes"))
                    {
                        System.out.println("Remember, you have: " + Card.toString(one.getHand()));
                        System.out.println();
                        System.out.println("Would you like to search for a two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace?");
                        temp3 = scan.nextLine(); //Set desired rank to temp3
                        //Check player two
                        if (temp2.equalsIgnoreCase(two.getName()))
                        {
                            if (two.searchHand(two.getHand(), temp3) != null)
                            {
                                while (two.searchHand(two.getHand(), temp3) != null)
                                {
                                    one.addCard(two.searchHand(two.getHand(), temp3));
                                    two.subCard(two.searchHand(two.getHand(), temp3));
                                    one.subSets(one.getHand(), temp3);
                                }
                            }
                            else if (two.searchHand(two.getHand(), temp3) == null)
                            {
                                System.out.println();
                                System.out.println("Go fish!");
                                System.out.println(one.getName() + " drew a card.");
                                System.out.println();
                                one.addCard(Card.draw(deck));
                                one.subSets(one.getHand(), temp3);
                                deck = Card.getDeck();
                                turn = "two";
                                success = "no";
                                something = one.checkWin();
                                if (something.equalsIgnoreCase("no"))
                                    System.out.println(one.getName() + " won!");
                            }
                        }
                        //Check player three
                        if (temp2.equalsIgnoreCase(three.getName()))
                        {
                            if (three.searchHand(three.getHand(), temp3) != null)
                            {
                                while (three.searchHand(three.getHand(), temp3) != null)
                                {
                                    one.addCard(three.searchHand(three.getHand(), temp3));
                                    three.subCard(three.searchHand(three.getHand(), temp3));
                                    one.subSets(one.getHand(), temp3);
                                }
                            }
                            else if (three.searchHand(three.getHand(), temp3) == null)
                            {
                                System.out.println();
                                System.out.println("Go fish!");
                                System.out.println(one.getName() + " drew a card.");
                                System.out.println();
                                one.addCard(Card.draw(deck));
                                one.subSets(one.getHand(), temp3);
                                deck = Card.getDeck();
                                turn = "two";
                                success = "no";
                                something = one.checkWin();
                                if (something.equalsIgnoreCase("no"))
                                    System.out.println(one.getName() + " won!");
                            }
                        }
                        //Check player four
                        if (temp2.equalsIgnoreCase(four.getName()))
                        {
                            if (four.searchHand(four.getHand(), temp3) != null)
                            {
                                while (four.searchHand(four.getHand(), temp3) != null)
                                {
                                    one.addCard(four.searchHand(four.getHand(), temp3));
                                    four.subCard(four.searchHand(four.getHand(), temp3));
                                    one.subSets(one.getHand(), temp3);
                                }
                            }
                            else if (four.searchHand(four.getHand(), temp3) == null)
                            {
                                System.out.println();
                                System.out.println("Go fish!");
                                System.out.println(one.getName() + " drew a card.");
                                System.out.println();
                                one.addCard(Card.draw(deck));
                                one.subSets(one.getHand(), temp3);
                                deck = Card.getDeck();
                                turn = "two";
                                success = "no";
                                something = one.checkWin();
                                if (something.equalsIgnoreCase("no"))
                                    System.out.println(one.getName() + " won!");
                            }
                        }
                    }
                }
           //ALL THE CODE FOR OTHER TURNS HERE
        }
        //Replay
        System.out.println("Would you like to play again?");
        something = scan.nextLine();
    }
}

}

Player class:

import java.util.*;

public class Player
{
private static String name;
private static Card[] hand = new Card[52];
private static Card[] sets = new Card[52];
private static int handsize = 0, nullcount = 0;
private static String win = "no"; 
private static int temp = 1; //For subCard
private static int temp2 = 0, temp3 = 0; //For subSets

//Constructor
public Player(String n)
{
    name = n;
}

//Mutators
//Add new card to hand
public static void addCard(Card c)
{
    hand[handsize] = c;
    handsize++;
}
//Lose card that was searched for
public static void subCard(Card c)
{
    for (int i = 0; i < hand.length; i++)
    {
        if (c == hand[i])
        {
            temp = i;
            if (temp < 51)
                temp++;
             hand[i] = hand[temp];
        }
    }
    hand[(hand.length - 1)] = null;
    handsize--;
}
//Lose sets of cards
public static void subSets(Card[] h, Card.rank r)
{
    //See if there are four of a kind
    for (int i = 0; i < h.length; i++)
    {
        if (h[i].rank == r)
        {
            temp2++;
        }
    }
    //Drop set of four
    if (temp2 == 4)
    {
        for (int i = 0; i < h.length; i++)
        {
            if (h[i].rank == r)
                h[i] = null;
            for (int ii = (i + 1); ii < (h.length - 1); ii++)
            {
                temp3 = i;
                h[temp3] = h[ii];
                temp3++;
            }
            h[(h.length - 1)] = null;
        }
    }
    hand = h;
}

//Accessors
public static String getName()
{
    return name;   
}
public Card[] getHand()
{
    return hand;   
}
public Card searchHand(Card[] h, Card.rank r)
{
    for (int i = 0; i < h.length; i++)
    {
        if (h[i].rank == r)
            return h[i];
        else if (i == h.length - 1)
            return null;
    }
}
public String checkWin()
{
    for (int i = 0; i < hand.length; i++)
    {
        if (hand[i] == null)
            nullcount++;
    }
    if (nullcount == (hand.length - 1))
        return "no"; //Set equal to something in Program
    else
    {
        nullcount = 0;
        return "yes"; //Set equal to something in Program
    }
}
}

Card class:

import java.util.*;

public class Card
{   
public String suit;
public String rank;
private static int temp = 0, temp2 = 0; //Use for reseting rank and suit
private static Card temp3; //Use for draw method
private static int temp4 = 1; //Use for draw and shuffle, always back to 1
private static Card[] deck = new Card[52];
private static Random random = new Random();

//Constructors
public Card()
{
    this.rank = "two";
    this.suit = "hearts";
}
public Card(String r, String s)
{
    this.rank = r;
    this.suit = s;
}

//Mutators
//Make deck
public static void makeDeck(Card[] c, String[] r, String[] s)
{
    for (int i = 0; i < c.length; i++)
    {
        c[i] = new Card(r[temp], s[temp2]);
        temp++; temp2++;
        //Reset rank and suit
        if (temp > 12)
            temp = 0;
        if (temp2 > 3)
            temp2 = 0;
    }
    deck = c;
}

//Accessors
//Return deck
public static Card[] getDeck()
{
    return deck;   
}
//Shuffle
public static Card[] shuffle(Card[] c) 
{
    for (int i = 0; i < c.length; i++)
    {
        int rand = (int)(random.nextInt(52)*(i + 1));
        //Don't let anything be in a slot that doesn't exist
        while (rand > c.length)
        {
            temp4 = random.nextInt(52);
            rand -= temp4;
        }
        if (rand < 0)
        {
            rand += temp4;
            Card temp = c[i];
            c[i] = c[rand];
            c[rand] = temp;
        }
    }
    deck = c;
    temp4 = 1;
    return deck;  
}
//Draw
public static Card draw(Card[] c)
{
    temp3 = c[0];
    for (int i = 0; i < c.length - 1; i++)
    {
        c[i] = c[temp4];
        if (temp4 < 51)
            temp4++;
    }
    c[(c.length - 1)] = null;
    temp4 = 1;
    deck = c;
    return temp3;
}
//Return string
public static String toString(Card[] h) 
{
    String temp5 = "yes";
    while (temp5.equals("yes"))
    {
        for (int i; i < h.length; i++)
        {
            if (h[i] != null)
                return h[i].rank + " of " + h[i].suit +", ";
            else temp5 = "no";
        }
    }
}
}
William
  • 23
  • 1
  • 5
  • Dare I ask - what is the problem? – Scary Wombat Dec 19 '13 at 02:21
  • Player.java 43 null error: cannot find symbol symbol null null class rank location null null class Card Player.java 81 null error: cannot find symbol symbol null null class rank location null null class Card @user2310289. it is only with Player.subSets and Player.searchHand – William Dec 19 '13 at 02:22
  • possible duplicate of [Java Array Index Out of Bounds somehow? \[code shown\]](http://stackoverflow.com/questions/20648171/java-array-index-out-of-bounds-somehow-code-shown) – Elliott Frisch Dec 19 '13 at 02:24
  • @ElliottFrisch, that was for a different problem i had last night. now my game is done, but Player.subSets and Player.searchHand dont work with rank parameters – William Dec 19 '13 at 02:25
  • How many times has this code been posted in the last couple of days? – Scary Wombat Dec 19 '13 at 02:25
  • It's the same program. – Elliott Frisch Dec 19 '13 at 02:27
  • @ElliottFrisch, well I learned what scope was last night. this is the same type of error but doesnt have to do with scope – William Dec 19 '13 at 02:27

1 Answers1

1

Card.rank is a string

public class Card
{   
public String rank;

So the method declaration needs

public static void subSets(Card[] h, String r)

As For

 public Card searchHand(Card[] h, Card.rank r)
 {
    for (int i = 0; i < h.length; i++)
    {
        if (h[i].rank == r)
            return h[i];
        else if (i == h.length - 1)
            return null;
    }
 }

If h.length is zero what are you going to return?

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • thank you. that fixed it, but now the only error is 'missing return statement' for Player.searchHand. i think it is because the return statements i have are protected. i think i can fix this one on my own. EDIT: h will never be zero in my game – William Dec 19 '13 at 02:29
  • Updated my answer for you. – Scary Wombat Dec 19 '13 at 02:32