# Thread: Infinite Loop

1. Member
Join Date
Oct 2009
Location
Oregon
Posts
22
Rep Power
0

## 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. 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. Member
Join Date
Oct 2009
Location
Oregon
Posts
22
Rep Power
0
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!

#### Posting Permissions

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