1

This is code for my Tic Tac Toe game. My professor doesn't want us to use while(true) and breaks. I don't know how to change it. Can someone please help me.

public void playerMakeMove()
{
    while(true)
    {
        System.out.println("It is "+username+"'s move");
        System.out.println("Give me your best move!");
        int move = input.nextInt();
        if (validatePlayerMove(move))
        {
            if (checkPositionAvailability(move))
            {
                board[move] = 'H'; //'H' for player move
                break;
            }
            else
            {
                System.out.println("Position not available.\nMake a different choice.");
                continue;
            }
        }
        if (move >= 9 || move <= -1)
        {
            System.out.println("Invalid entry!");
            continue;
        }
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Can you describe the problem in more detail (like what does it do that is not what you expected) and what you’ve tried and make a bit more effort than cutting and pasting your homework here? – Missy Dec 15 '19 at 22:57
  • When your professor said they didn't want you to use `while(true)` and `break`, did they say what they _did_ want you to use? – Dawood ibn Kareem Dec 15 '19 at 22:58

2 Answers2

0

I highly recommend using a boolean that does the job instead of while (true) { code }

Useful link for this : Are "while(true)" loops so bad?

N_Craft
  • 70
  • 7
0

You could write something like this:

while ( gameInLimbo() ) {
   makeAnotherMove();
}

where gameInLimbo() is a method that returns true if there is no current winner and there are still open square(s), and false otherwise.

FredK
  • 4,094
  • 1
  • 9
  • 11