Results 1 to 3 of 3
Thread: Infinite Loop
- 01-23-2010, 09:38 PM #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?
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: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.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; } }
Thanks for any help!
-
Here
When is this loop ever going to end? The only possible place you might have changed winner, you've commented it out.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; }
- 01-23-2010, 10:11 PM #3
Member
- Join Date
- Oct 2009
- Location
- Oregon
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
for loop help
By soc86 in forum New To JavaReplies: 9Last Post: 01-24-2011, 09:45 PM -
Infinite running loop problems
By BigDummy in forum New To JavaReplies: 5Last Post: 10-14-2009, 06:39 AM -
Infinite loop when reading text file
By MartinMorrison in forum New To JavaReplies: 2Last Post: 08-03-2009, 04:36 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
java recursion infinite loop
By tony404 in forum Advanced JavaReplies: 9Last Post: 10-03-2008, 01:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks