Results 1 to 3 of 3

Thread: Infinite Loop

  1. #1
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default Infinite Loop

    I must have looked at this code for so long that I can't see where the problem is. The following is running in an infinite loop, can someone please tell me why?

    Java Code:
    public String boardPrint()
    		{
    			String boardLine = " 0 1 2 3 4 5 6 7 \n";
    			String newLine = " \n|";
    			String boardSpot = "";
    			String spacer = "|";
    			for (int i=0; i<gameboard.length; i ++)
    			{
    				for (int I=0; I < gameboard[i].length; I ++)
    				{
    					boardSpot = gameboard[i][I].PiecePrt();
    					boardLine = boardLine + boardSpot + spacer;
    					if (I > 0 && I%7 == 0 && i !=7)
    						boardLine = boardLine + newLine;
    
    				}
    			}
    			return boardLine;
    		}
    Just in case this part of the code is not the culprit, here is what I have so far (program is supposed to eventually play a game of connect 4):

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.*;
    
    class Connect4
    {
    	public final static int NOPLAYER = 0;
    	public final static int PLAYER1 = 1;
    	public final static int PLAYER2 =2;
    
    	///////////////////////////////////////////
    	//  main
    	//////////////////////////////////////////
    
    	public static void main (String[] args)
    	{
    		char again = 'Y';
    		int player = PLAYER2;
    		int winner = NOPLAYER;
    		int games = 0;
    		Scanner scan = new Scanner(System.in);
    		Board Myboard = new Board();
    		System.out.println (Myboard.boardPrint());
    
    		while (again == 'Y')
    		{
    			Myboard = new Board();
    			winner = NOPLAYER;
    			int moves = 0;
    			while (winner == NOPLAYER)
    			{
    				player = (player == PLAYER1) ? PLAYER2 : PLAYER1;
    				System.out.println (Myboard.boardPrint());
    				//winner = Myboard.check_win();
    				moves += 1;
    			}
    
    			System.out.println ("Congratulations Player " + winner + "!");
    			System.out.println ("Total moves this game: " + moves);
    			System.out.println ("Do you want to play again? (y/n): ");
    			String word = scan.next();
    			word = word.toUpperCase();
    			again = word.charAt(0);
    		}
    	}
    }
    
    //////////////////////////////////////////////////////
    // class piece
    ////////////////////////////////////////////////////
    
    class Piece
    {
    	private int owner;
    
    	//
    	// sets up a new piece
    	//
    	public Piece()
    	{
    		owner = Connect4.NOPLAYER;
    	}
    	public String PiecePrt()
    	{
    		String prtPiece = " ";
    		if (owner == Connect4.NOPLAYER)
    			prtPiece = " ";
    		else if (owner == Connect4.PLAYER1)
    			prtPiece = "X";
    		else
    			prtPiece = "O";
    		return prtPiece;
    	}
    
    }
    
    //////////////////////////////////////////////////////////
    // class Board
    //////////////////////////////////////////////////////////
    class Board
    {
    	Piece[][] gameboard = new Piece[8][8];
    
    	public Board()
    	{
    		for (int i =0; i < gameboard.length; i ++)
    		{
    			for (int I=0; I < gameboard[i].length; I ++)
    			{
    				gameboard[i][I] = new Piece();
    			}
    		}
    	}
    
    	public String boardPrint()
    		{
    			String boardLine = " 0 1 2 3 4 5 6 7 \n";
    			String newLine = " \n|";
    			String boardSpot = "";
    			String spacer = "|";
    			for (int i=0; i<gameboard.length; i ++)
    			{
    				for (int I=0; I < gameboard[i].length; I ++)
    				{
    					boardSpot = gameboard[i][I].PiecePrt();
    					boardLine = boardLine + boardSpot + spacer;
    					if (I > 0 && I%7 == 0 && i !=7)
    						boardLine = boardLine + newLine;
    
    				}
    			}
    			return boardLine;
    		}
    	}
    Right now I am just looking to get the board to print correctly.

    Thanks for any help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Here
    Java Code:
             winner = NOPLAYER;
             int moves = 0;
             while (winner == NOPLAYER) {
                player = (player == PLAYER1) ? PLAYER2 : PLAYER1;
                System.out.println(Myboard.boardPrint());
                // winner = Myboard.check_win();
                moves += 1;
             }
    When is this loop ever going to end? The only possible place you might have changed winner, you've commented it out.

  3. #3
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    lol... I see that now. I had been mussing with that code and blocked it off because there is an error there where I am trying to compare indicies to determine a winner. I knew it had to be simple.

    Thanks for the extra set of eyes! And the QUICK reply!

Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 09:45 PM
  2. Infinite running loop problems
    By BigDummy in forum New To Java
    Replies: 5
    Last Post: 10-14-2009, 06:39 AM
  3. Infinite loop when reading text file
    By MartinMorrison in forum New To Java
    Replies: 2
    Last Post: 08-03-2009, 04:36 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. java recursion infinite loop
    By tony404 in forum Advanced Java
    Replies: 9
    Last Post: 10-03-2008, 01:16 PM

Posting Permissions

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