Infinite Loop

• 01-23-2010, 10:38 PM
rclausing
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?

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):

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!
• 01-23-2010, 10:50 PM
Fubarable
Here
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.
• 01-23-2010, 11:11 PM
rclausing
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!