Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Infinite loop problem... Posted by natureboy on 15 Dec 2010 at 8:50 AM
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.

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<Player> players = new ArrayList<Player>();
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();
}
}

Report
Re: Infinite loop problem... Posted by silveredge52 on 15 Dec 2010 at 12:55 PM
Hey,
Not sure anyone gonna want to look at part of a program, that can't be independently run.
Suggest you zip all files and add as an attachment to your question.
Report
accidental repeated post Posted by MiggityMike on 15 Dec 2010 at 3:00 PM
sorry........
Report
Re: Infinite loop problem... Posted by MiggityMike on 15 Dec 2010 at 3:03 PM
I agree^. Post all the class files so we can get a look see at it thoroughly.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.