Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Is This Looping Forever?

loop

  • Please log in to reply
17 replies to this topic

#1 ultimate99

ultimate99

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 27 April 2012 - 02:34 AM

Hello, great people around here!

I have this code but it seems to hang in loop? Any pionters?

Much appreciated!
  • 0

#2 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 27 April 2012 - 03:06 AM

Please post the code here so we can see it.
  • 0

#3 ultimate99

ultimate99

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 27 April 2012 - 03:07 AM

Post updated.
  • 0

#4 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 27 April 2012 - 03:27 AM

Do you know where the code is looping? Add some println statements to all the loops to see where it is looping. When you find the loop then add println statements to print out the values of the variables that control the looping. The output will show you what the computer sees and help you understand the problem and fix it.

Where do you get any input from the players?
  • 1

#5 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts
  • Programming Language:C, Java, C++, Python, Ruby, Scheme, Bash, Haskell, Others
  • Learning:Others

Posted 27 April 2012 - 03:32 AM

One problem I see: In the while(playersTurn)-loop if the condition "!Character.isDigit(board[row][col])" is true, playersTurn will never be set to false and you will loop forever.
  • 0

#6 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 27 April 2012 - 03:58 AM

I think you missed to input the value of num in here

System.out.println("Enter the position where you want to play" + boardString);


you give an output to the user and ask him to enter the position he/she wants to play but you forget to enter the value of num as
as you will use this variable into your control statement IF. Yes you declared the variable num

int num = 0;

but the value stays at 0 and I can't find a condition for value 0




 if(num <= 3)
                         row = 0;
                  else
                    if(num <= 6)
                          row = 1;
                    else
                          row = 2;
                  if(num % 3 == 1)
                    col = 0;
                  else
                    if(num % 3 == 2)
                          col = 1;
                    else
                          col = 2;
                  if(!Character.isDigit(board[row][col]))
                         msg = "That position is already taken\n";
                  else
                  {
                         board[row][col] = 'X';
                         msg = "";
                         playersTurn = false;
                  }



therefore, the program will not go into the


else
                  {
                         board[row][col] = 'X';
                         msg = "";
                         playersTurn = false;
                  }

resulting an infinite loop
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#7 ultimate99

ultimate99

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 27 April 2012 - 04:05 AM

Thank you guys for your prompt reply! I look into these.

Btw, what if I want use John's code here? It looks really good and organized, but I don't want buttons, only console. hehe :D
  • 0

#8 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 27 April 2012 - 04:19 AM

Thank you guys for your prompt reply! I look into these.

Btw, what if I want use John's code here? It looks really good and organized, but I don't want buttons, only console. hehe :D


I would suggest you try to create your own to develop your programming skills :)
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#9 ultimate99

ultimate99

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 27 April 2012 - 04:22 AM

I would suggest you try to create your own to develop your programming skills :)


You're right. But seriously though, what if I want to use John's code but without the buttons, how do I do that?
  • 0

#10 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 27 April 2012 - 04:47 AM

try to get his idea and the logic on how he does it.. you can convert it into a console base application :)
good luck
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#11 ultimate99

ultimate99

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 28 April 2012 - 08:34 AM

I've changed my code totally.

The board appears to display properly, but how can I have each cell numbered and ask the player to to choose a numbered cell to play? Instead of having the player choose "row number" and "column number"?

I've thought of this code, but how can I implement that?
char[][] board = new char[3][3];
  board[0][0] = '1';
		 board[0][1] = '2';
		 board[0][2] = '3';
		 board[1][0] = '4';
		 board[1][1] = '5';
		 board[1][2] = '6';
		 board[2][0] = '7';
		 board[2][1] = '8';
		 board[2][2] = '9';

Original code:
import java.util.Scanner;
public class TicTacToe{

static Scanner object = new Scanner(System.in);

public static void main(String[] args){
 
  //Game board size
  final int SIZE = 3;
  char[][] board = new char[SIZE][SIZE];
 
  //Initialize board
  resetBoard(board);
 
  //Display welcome message and display board
  System.out.print("Welcome to TicTacToe");
  showBoard(board);
 
  //Let the player choose which symbol he wants
  System.out.print("\nChoose a symbol: X or O");
  char playerSymbol = object.next().toLowerCase().charAt(0);
  char computerSymbol = (playerSymbol == 'x') ? 'o' : 'x';
 
  //Ask player to go first or not.
  System.out.println();
  System.out.print("Do you want to go first? (y/n)");
  char answer = object.next().toLowerCase().charAt(0);
 
  //Turns: player 0, computer 1
  int turn;
  //Empty positions
  int remainPosition = SIZE * SIZE;
 
  //First move
  if(answer == 'y'){
   turn = 0;
   //player chooses the position on the board
   playerTurn(board, playerSymbol);
  }
  else{
   turn = 1;
   //computer chooses its position on the board
   computerTurn(board, computerSymbol);
  }
  //Show board
  showBoard(board);
  //decrement remaining positions on board
  remainPosition--;
 
  boolean done = false;
  int winner = -1;
 
  while(!done && remainPosition > 0){
   done = isWon(board, turn, playerSymbol, computerSymbol);
   if(done)
    winner = turn;
   else{
    turn = (turn + 1) % 2;
    if(turn == 0)
	 playerTurn(board, playerSymbol);
    else
	 computerTurn(board, computerSymbol);
   
    showBoard(board);
    remainPosition--;
   
   }
  }
 
  //Check for winner
  //If winner found, declare the winner
  if(winner == 0)
   System.out.println("You won!");
  else if(winner == 1)
   System.out.println("You lost!");
  else
   System.out.println("Tie!");
 
}
private static void resetBoard(char[][] board) {
  for(int i = 0; i < board.length; i++)
   for(int j = 0; j < board[0].length; j++)
    board[i][j] = ' ';
 
}
private static void showBoard(char[][] board) {
  int numRow = board.length;
  int numColumn = board[0].length;
 
  System.out.println();
 
  //Column header
  System.out.print(" ");
  for(int i = 0; i < numColumn; i++);
   System.out.print(i + " ");
  System.out.print('\n');
 
  //Blank after header
  System.out.println();
 
  //Table
  for(int i = 0; i < numRow; i++){
   System.out.print(i + " ");
   for(int j = 0; j < numColumn; j++){
    if(j !=0)
	 System.out.print("|");
    System.out.print(" " + board[i][j] + " ");
   }
  
   System.out.println();
  
   if(i != (numRow - 1)){
    //Line separator
    System.out.print(" ");
    for(int j = 0; j < numColumn; j++){
	 if(j != 0)
	  System.out.print("+");
	 System.out.print("---");
    }
    System.out.println();
   }
  }
  System.out.println();
}

private static boolean isWon(char[][] board, int turn, char playerSymbol,
   char computerSymbol) {
  char symb;
  if(turn == 0)
   symb = playerSymbol;
  else
   symb = computerSymbol;
 
  int i, j = 0;
  boolean win = false;
 
  //Check win from row
  for(i = 0; i < board.length && !win; i++){
   for(j = 0; j < board[0].length; j++){
    if(board[i][j] != symb)
	 break;
   }
   if (j == board[0].length)
    win = true;
  }
 
  // Check win from column
	 for (j = 0; j < board[0].length && !win; j++) {
	   for (i = 0; i < board.length; i++) {
		 if (board[i][j] != symb)
		   break;
	   }
	   if (i == board.length)
		 win = true;
	 }
 
  //Check win diagonally (1)
  if(!win){
   for(i = 0; i < board.length; i++){
    if(board[i][i] != symb)
	 break;
   }
   if(i == board.length)
    win = true;
  }
 
  //Check win diagonally (2)
  if (!win){
   for(i = 0; i < board.length; i++){
    if(board[i][board.length - 1 - i] != symb)
	 break;
   }
   if(i == board.length)
    win = true;
  }
  //Retrun win
  return win;
}
private static void computerTurn(char[][] board, char computerSymbol) {
  for(int i = 0; i < board.length; i++){
   for(int j = 0; j < board[0].length; j++){
    if(board[i][j] == ' '){
	 board[i][j] = computerSymbol;
	 return;
    }
   }
  }
 
}
private static void playerTurn(char[][] board, char playerSymbol) {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter row #, hit enter, column #, hit enter again: ");
  int rowNumber = sc.nextInt();
  int columnNumber = sc.nextInt();
 
  while(board[rowNumber][columnNumber] != ' '){
   System.out.print("Position taken. \nEnter row and column numbers: ");
   rowNumber = sc.nextInt();
   columnNumber = sc.nextInt();
  }
  board[rowNumber][columnNumber] = playerSymbol;
 
}



}

  • 0

#12 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 28 April 2012 - 08:42 AM

have each cell numbered and ask the player to to choose a numbered cell

Are you asking how to map numbers 1-9 to row and column? Something like the following
subtract 1 and
% by number of columns to get column
/ by number of rows to get row
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download