Infinite loop problem... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Infinite loop problem...

natureboynatureboy Posts: 1Member
I keep getting an infinite loop when i run my program. I've narrowed it down to the main class, i believe, can anyone give me some advice on where the problem is? I know it is a lot of code--and I apologize.

[code]public class TicTacToe
{


public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

//welcome user
MessagePrinter.printWelcomeMessage();


boolean continuePlaying = true;

//keep game running while...

while (continuePlaying)
{
//prompt for one or two player game
MessagePrinter.printPromptForOneOrTwoPlayerGame();

int oneOrTwoPlayerGame = Integer.parseInt(input.nextLine());


if (oneOrTwoPlayerGame == 3)
{
continuePlaying = false;
}else

{
Player player1 = null;
Player player2 = null;

Board board = new Board();

if (oneOrTwoPlayerGame == 1)
{
//player one is selected

MessagePrinter.printPromptForPlayerName(1);
String humanPlayerName = input.nextLine();

//prompt first player to play
MessagePrinter.printPromptForUserOrComputerFirst();
int firstToPlay = Integer.parseInt(input.nextLine());


if (firstToPlay == 1)
{
player1 = new HumanPlayer(humanPlayerName, Move.X);

player2 = new ComputerPlayer(board, Move.O);
}else

{
player1 = new ComputerPlayer(board, Move.X);

player2 = new HumanPlayer(humanPlayerName, Move.O);
}

}else

{
MessagePrinter.printPromptForPlayerName(1);
String player1Name = input.nextLine();


MessagePrinter.printPromptForPlayerName(2);
String player2Name = input.nextLine();


player1 = new HumanPlayer(player1Name, Move.X);

player2 = new HumanPlayer(player2Name, Move.O);
}
//store player
ArrayList players = new ArrayList();
players.add(player1);
players.add(player2);

MessagePrinter.printBoardMessage(board);

boolean wonGame = false;
Player winnerOfGame = null;
while ((continuePlaying) && (wonGame==false))
{
for (Player player : players)
{
boolean isMoveValid = false;


while ((continuePlaying) && (wonGame) && (isMoveValid=false))
{
MessagePrinter.printWhichPlayersMoveItIsMessage(player);

int row = player.getRow();

if (row == -1) {
continuePlaying = false;
}else

{
int column = player.getColumn();
if (column == -1) {
continuePlaying = false;
}else

{
isMoveValid = board.isMoveValid(row, column);

if (isMoveValid)
{
board.move(row, column, player.getMove());

MessagePrinter.printBoardMessage(board);


if (board.isWinner(player.getMove()))
{
wonGame = true;

winnerOfGame = player;
}else

{
if (!board.isDraw())
continue;

wonGame = true;

winnerOfGame = null;
}
}
}
} }
}
if (wonGame==true)
{
continue;
}
if (!(winnerOfGame == null)) {
MessagePrinter.printGameEndedWithWinnerMessage(winnerOfGame.getName());
}
else
{
MessagePrinter.printGameEndedInDrawMessage(player1.getName(), player2.getName());
}

MessagePrinter.printPromptToPlayAgainMessage();

int playAgain = Integer.parseInt(input.nextLine());

continuePlaying = playAgain == 1;
}
}


}
MessagePrinter.printGoodByeMessage();
}
}[/code]

Comments

Sign In or Register to comment.