-1

there are no errors in any of my classes.i need help with semantics of my main class. i run the app and am able to get through entering whatever players i want before nothing ever shows up again. it doesnt exit either. where is my loop error?

package Final;

//William Patterson
//Semester 1 Final
import java.util.*;

public class Program
{
private static Scanner scan;

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"};
  scan = new Scanner(System.in);
  String something = "yes", something2 = "yes", success = "yes"; //Use with while loops
  String turn = "one";
  String temp2, temp3; //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");

  //Welcome
  System.out.println("Welcome to my program: Go Fish!");
  System.out.println("When playing, don't forget players' names!");
  System.out.println("Also, this version of Go Fish! will secure a set of four similar ranks at each turn's end.");
  System.out.println();

  //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!");
                          }
                      }
                  }
              }
          //Player two's turn
          if (turn.equalsIgnoreCase("two") && something.equalsIgnoreCase("yes"))
              {
                  System.out.println("Player two: " + two.getName() + "'s turn!");
                  System.out.println();
                  //Output hand
                  System.out.println("You have: " + Card.toString(two.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(two.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 one
                      if (temp2.equalsIgnoreCase(one.getName()))
                      {
                          if (one.searchHand(one.getHand(), temp3) != null)
                          {
                              while (one.searchHand(one.getHand(), temp3) != null)
                              {
                                  two.addCard(one.searchHand(one.getHand(), temp3));
                                  one.subCard(one.searchHand(one.getHand(), temp3));
                                  two.subSets(two.getHand(), temp3);
                              }
                          }
                          else if (two.searchHand(two.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(two.getName() + " drew a card.");
                              System.out.println();
                              two.addCard(Card.draw(deck));
                              two.subSets(two.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount >= 3)
                                  turn = "three";
                              else turn = "one";
                              success = "no";
                              something = two.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(two.getName() + " won!");
                          }
                      }
                      //Check player three
                      if (temp2.equalsIgnoreCase(three.getName()))
                      {
                          if (three.searchHand(three.getHand(), temp3) != null)
                          {
                              while (three.searchHand(three.getHand(), temp3) != null)
                              {
                                  two.addCard(three.searchHand(three.getHand(), temp3));
                                  three.subCard(three.searchHand(three.getHand(), temp3));
                                  two.subSets(two.getHand(), temp3);
                              }
                          }
                          else if (three.searchHand(three.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(two.getName() + " drew a card.");
                              System.out.println();
                              two.addCard(Card.draw(deck));
                              two.subSets(two.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount >= 3)
                                  turn = "three";
                              else turn = "one";
                              success = "no";
                              something = two.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(two.getName() + " won!");
                          }
                      }
                      //Check player four
                      if (temp2.equalsIgnoreCase(four.getName()))
                      {
                          if (four.searchHand(four.getHand(), temp3) != null)
                          {
                              while (four.searchHand(four.getHand(), temp3) != null)
                              {
                                  two.addCard(four.searchHand(four.getHand(), temp3));
                                  four.subCard(four.searchHand(four.getHand(), temp3));
                                  two.subSets(two.getHand(), temp3);
                              }
                          }
                          else if (four.searchHand(four.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(two.getName() + " drew a card.");
                              System.out.println();
                              two.addCard(Card.draw(deck));
                              two.subSets(two.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount >= 3)
                                  turn = "three";
                              else turn = "one";
                              success = "no";
                              something = two.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(two.getName() + " won!");
                          }
                      }
                  }
              }
          //Player three's turn
          if (turn.equalsIgnoreCase("three") && something.equalsIgnoreCase("yes"))
              {
                  System.out.println("Player three: " + three.getName() + "'s turn!");
                  System.out.println();
                  //Output hand
                  System.out.println("You have: " + Card.toString(two.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(two.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 one
                      if (temp2.equalsIgnoreCase(one.getName()))
                      {
                          if (one.searchHand(one.getHand(), temp3) != null)
                          {
                              while (one.searchHand(one.getHand(), temp3) != null)
                              {
                                  three.addCard(one.searchHand(one.getHand(), temp3));
                                  one.subCard(one.searchHand(one.getHand(), temp3));
                                  three.subSets(three.getHand(), temp3);
                              }
                          }
                          else if (one.searchHand(one.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(three.getName() + " drew a card.");
                              System.out.println();
                              three.addCard(Card.draw(deck));
                              three.subSets(three.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount > 3)
                                  turn = "four";
                              else turn = "one";
                              success = "no";
                              something = three.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(three.getName() + " won!");
                          }
                      }
                      //Check player four
                      if (temp2.equalsIgnoreCase(four.getName()))
                      {
                          if (four.searchHand(four.getHand(), temp3) != null)
                          {
                              while (four.searchHand(four.getHand(), temp3) != null)
                              {
                                  three.addCard(four.searchHand(four.getHand(), temp3));
                                  four.subCard(four.searchHand(four.getHand(), temp3));
                                  three.subSets(three.getHand(), temp3);
                              }
                          }
                          else if (four.searchHand(four.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(three.getName() + " drew a card.");
                              System.out.println();
                              three.addCard(Card.draw(deck));
                              three.subSets(three.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount > 3)
                                  turn = "four";
                              else turn = "one";
                              success = "no";
                              something = three.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(three.getName() + " won!");
                          }
                      }
                      //Check player two
                      if (temp2.equalsIgnoreCase(two.getName()))
                      {
                          if (two.searchHand(two.getHand(), temp3) != null)
                          {
                              while (two.searchHand(two.getHand(), temp3) != null)
                              {
                                  three.addCard(two.searchHand(two.getHand(), temp3));
                                  two.subCard(two.searchHand(two.getHand(), temp3));
                                  three.subSets(three.getHand(), temp3);
                              }
                          }
                          else if (two.searchHand(two.getHand(), temp3) == null)
                          {
                              System.out.println();
                              System.out.println("Go fish!");
                              System.out.println(three.getName() + " drew a card.");
                              System.out.println();
                              three.addCard(Card.draw(deck));
                              three.subSets(three.getHand(), temp3);
                              deck = Card.getDeck();
                              if (playercount > 3)
                                  turn = "four";
                              else turn = "one";
                              success = "no";
                              something = three.checkWin();
                              if (something.equalsIgnoreCase("no"))
                                  System.out.println(three.getName() + " won!");
                          }
                      }
                  }
              }
          //player 4's turn code here, but had to abbreviate code to post this question
      }
      //Replay
      System.out.println("Would you like to play again?");
      something = scan.nextLine();
  }
}

}
William
  • 23
  • 1
  • 5
  • @ElliottFrisch, I need to finish this project tonight! and I wouldn't ask the same question twice! – William Dec 19 '13 at 04:30
  • 1
    I think you would be better asking how to debug this type of thing yourself, rather than asking for the specific problem. Also perhaps, ask about how to break up your code. – WW. Dec 19 '13 at 04:31
  • Put lots of System.out.println() statements all over the code to see where you are going. And at every important condition, print the variable you are evaluating (to see if it is what you think it should be). Or step through with a debugger. – Floris Dec 19 '13 at 04:31
  • @ElliottFrisch, I just debugged it in Eclipse.. was something new supposed to happen? – William Dec 19 '13 at 04:32
  • like @Floris said put lots of print statements everywhere, if u have a infinite loop you will get something printing forever. – Pita Dec 19 '13 at 04:35
  • @William - [Java Debugging with Eclipse](http://www.vogella.com/articles/EclipseDebugging/article.html#usedebug). – Elliott Frisch Dec 19 '13 at 04:36
  • How did you reach a Final, and just learn about variable scope yesterday? – Elliott Frisch Dec 19 '13 at 04:38
  • @ElliottFrisch, first year Java is at my school,and the teacher has never taught it before. – William Dec 19 '13 at 04:41
  • 1
    You will get better results on this site is you narrow down this problem and ask specific questions – DeathByTensors Dec 19 '13 at 04:43

2 Answers2

2

This line in your source might cause it to loop infinitely.

  //Take turns
  while (something.equalsIgnoreCase("yes"));

A semicolon at the end of while loop is a big NO

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • 1
    For future reference, this is the kind of thing that line-by-line in a debugger will show you immediately. It will just stop right there. – Radiodef Dec 19 '13 at 04:42
1

Your code structure is simply screaming for bugs. Having each player as a named variable, instead of an array players[4], is frankly incomprehensible. Thus you end up with lots of near-duplicates of code, and more opportunities to break things.

That may be more radical than you want to do with a deadline looming; so start by simplifying your code to a two player game only - make a copy of your program, then cut mercilessly. Lots of ifs will disappear, and the structure will become clearer. Then reduce it to the skeleton - just the conditions of the loops, with no code inside. At this point it should be obvious what is going on.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • I'm trying to figure out my mistakes right now, and can you see any reason for my one.getName() to output two.getName()'s name instead? thats what it is doing – William Dec 19 '13 at 05:40
  • I would change each `System.out.println("What is your name: ");` to `System.out.println("What is your name player 1: "); etc - see that you are in the part if the program where you think you are when you import the names. And please: learn to use arrays... – Floris Dec 19 '13 at 11:29