www.webdeveloper.com
Results 1 to 2 of 2

Thread: compiling problem with looping

  1. #1
    Join Date
    Oct 2010
    Posts
    6

    compiling problem with looping

    I keep getting an infinite loop when I run this program (when it calls the method MessagePrinter.printWhichPlayersMoveItIsMessage(player); )
    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<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();
    	}
    	}
    Last edited by Fang; 12-15-2010 at 12:41 AM. Reason: added BBCode

  2. #2
    Join Date
    Nov 2010
    Location
    Los Angeles, CA
    Posts
    234
    in case you're wondering why your question has 80 views and no replies is because we see a wall of unreadable text and move on to another question. If you want us to read your code please use code tags as outlined in this thread:

    http://www.webdeveloper.com/forum/sh...ad.php?t=64277

    It's very easy you just highlight your code and click one button to make your code readable. Make sure you read through that thread and try again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles