Register and join over 40,000 other developers!
Recent Topics
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
-
Job Gig PHP Form Needed
PJohnson - Apr 18 2019 03:55 AM
-
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
-
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

17 replies to this topic
#1
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!
I have this code but it seems to hang in loop? Any pionters?
Much appreciated!
#2
Posted 27 April 2012 - 03:06 AM
Please post the code here so we can see it.
#3
Posted 27 April 2012 - 03:07 AM
Post updated.
#4
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?
Where do you get any input from the players?
#5
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.
#6
Posted 27 April 2012 - 03:58 AM
I think you missed to input the value of num in here
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
but the value stays at 0 and I can't find a condition for value 0
therefore, the program will not go into the
resulting an infinite loop
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
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped
Never Forget To HIT "LIKE" If I Helped
#8
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
I would suggest you try to create your own to develop your programming skills

Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped
Never Forget To HIT "LIKE" If I Helped
#9
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?
#10
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

good luck
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped
Never Forget To HIT "LIKE" If I Helped
#11
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?
Original code:
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; } }
#12
Posted 28 April 2012 - 08:42 AM
Are you asking how to map numbers 1-9 to row and column? Something like the followinghave each cell numbered and ask the player to to choose a numbered cell
subtract 1 and
% by number of columns to get column
/ by number of rows to get row
Also tagged with one or more of these keywords: loop
Language Forums →
PHP →
Content and title won't show on certain page in WordpressStarted by Mello, 05 Jan 2016 ![]() |
|
![]() |
||
Language Forums →
C# →
Create Staircase with Nested LoopsStarted by StainedSilva, 12 Mar 2015 ![]() |
|
![]() |
||
Language Forums →
Other Languages →
Bash / Shell Scripting →
Here is a task... Find Nothing :PStarted by ShaunPrawn, 07 Aug 2014 ![]() |
|
![]() |
||
General Forums →
General Programming →
Trying to do a working loopStarted by Error, 23 Jun 2014 ![]() |
|
![]() |
||
Language Forums →
PHP →
Problem in displaying data inside table formatStarted by newphpcoder, 25 Jul 2013 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download