1

I am trying to figure out why my program is printing the same statement repeatedly. I am able to get this method to work fine when I am putting in good values, but I have an else statement set up to catch invalid selections. When my else statement runs, it prints infinitely and the while loop never commences. I can't figure it out. I am going to paste the entire method, but bold the problem area. I hope this is clear.

public static int[][] placeCheese(int [][] gameBoard, String Player1Name) 
{ 
    Scanner console = new Scanner(System.in);
    int turnTaker = 0;
    int validRow = 0;
    int validCol = 0;
    int RowIndex = -1;
    int ColIndex = -1;
    while(turnTaker == 0)
    {
        while(validRow == 0)
        {
            System.out.println( Player1Name + ", please place your cheese by choosing a row, or choose -1 to exit.");
            RowIndex = console.nextInt() -1; 
            if(RowIndex < gameBoard.length && RowIndex >=0)
            {
                validRow++;
            }
            else if(RowIndex == -2)
            {
                System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
                System.exit(0);
            }
        }
        while(validCol == 0){
            System.out.println( Player1Name + ", please place your cheese by choosing a column, or choose -1 to exit.");
            ColIndex = console.nextInt() -1; 
            if(ColIndex < gameBoard.length && ColIndex >=0)
            {
                validCol++;
            }
            else if(RowIndex == -2)
            {
                System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
                System.exit(0);
            }
        }
    if(gameBoard[RowIndex][ColIndex] == 0)
    {
        gameBoard[RowIndex][ColIndex] = 1;
        turnTaker++;
        numPieces1--;
    }
    else 
    {
        System.out.println("this space is already occupied, please choose again.");
    }
    return gameBoard;
}
7
  • 1
    Please try to format the code into something more readable.. Commented Sep 16, 2011 at 21:27
  • Gah I was in the middle of editing this and it didn't allow me to save it. Commented Sep 16, 2011 at 21:28
  • What do you see if you debug when it has gone into an infinite loop? This tools is likely to help you solve the problem as this is what it is for. Commented Sep 16, 2011 at 21:28
  • Not all codepaths change the value of turnTaker (eg: the else block), thus the infinite loop. Commented Sep 16, 2011 at 21:31
  • I don't know if I am unclear about what the problem is. I want the entire method to iterate repeatedly until turntaker is set = 1. Which will only happen if the piece is placed. Right now it gets stuck in the else statement, and never asks the player to try again. If they get to the else statement and turntaker is changed, the method wont try again to have them place a piece, right? Commented Sep 16, 2011 at 21:34

3 Answers 3

2

The first time this is called, it will force you to provide valid column and row numbers; the square will get set, turnTaker will be incremented, and the loop will terminate.

The second time this is called, if the row and column numbers chosen are the same, then turnTaker will not be incremented because the chosen spot in the array is not 0. Since validRow and validCol aren't 0, furthermore, it will never ask you for more numbers -- it will just go into an endless loop printing the message without ever prompting again!

Your "else" clause that prints the message could fix this by setting validRow and validCol to 0 again. As someone else has pointed out, it would be much better if these variables and turnTaker as well were booleans, not ints.

Sign up to request clarification or add additional context in comments.

2 Comments

I just noticed the same thing. Thank you!
Oh sorry not sure how to do that, will figure it out :)
1

Your code is illegible but if I had to guess I'd say you weren't changing turnTaker in your else statement. Infinite loop!

Comments

0

You're not changing the value of turnTaker, so it will always stay zero.

1 Comment

Oh whoops! I posted this before the edit, and now I see the rest of the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.