Reputation: 555
OK, so I'm writing a game of Tic Tac Toe. I've got the whole program written, but I can't understand why it's not displaying correctly?
import java.util.Scanner;
public class TicTacToeGame {
//constants for game options
static final int STANDARD_GAME = 0;
static final int QUIT = -1;
//constants for the number of rows and columns in a standard TicTacToe grid
static final int STANDARD_GRID_ROWS = 3;
static final int STANDARD_GRID_COLUMNS = 3;
//two dimensional array to represent a TicTacToe grid
static char[][]grid = new char[STANDARD_GRID_ROWS][STANDARD_GRID_COLUMNS];
//Scanner for input
static Scanner keyboard = new Scanner(System.in);
//***RUN STANDARD GAME******************************************************
static void runStandardGame() {
int position = QUIT;
char playerChar = 'X';
boolean isGameOver = false;
fillPosition();
System.out.println("Choose a position to play.\n");
displayGrid();
do {
System.out.print("Player " + playerChar +
" (Enter a position or " + QUIT + " to resign): ");
position = keyboard.nextInt();
System.out.println("Choose a position to play.\n");
displayGrid();
if(isWin()) {
System.out.print("\nPlayer " + playerChar +"WINS!!!\n");
isGameOver = true;
}
else if (isTie()) {
System.out.print("\nTIE.\n");
isGameOver = true;
}
else {
//switch players because one of the players has just played
//and not won;, so, it is the other player's turn
if (playerChar == 'X') {
playerChar = 'O';
}
else{
playerChar = 'X';
}
}
}while(!isGameOver && position != QUIT);
}//end of runStandardGame
//***PLAY*******************************************************************
static void play(int cell, char playerChar) {
//the player has entered a number 1 through 9 for the position they want
//to play, so, figure out which row and which column of the array this is
//For example, if the player wants to play 'X' in position 7, this means
//the 'X' must go into row 2, column 0 of the array
int row = 0;
int column = 0;
if (cell > 0 && cell <= (STANDARD_GRID_ROWS * STANDARD_GRID_COLUMNS)){
row = (cell - 1) / STANDARD_GRID_ROWS;
column = (cell - 1) % STANDARD_GRID_COLUMNS;
grid[row][column] = playerChar;
}
}
//***IS WIN*****************************************************************
static boolean isWin() {
boolean isWin = false;
if (isRowWin() || isColumnWin() || isDiagonalWin()){
isWin = true;
}
return isWin;
}
//***IS TIE*****************************************************************
static boolean isTie() {
boolean isTie = false;
if(!isRowWin() || !isColumnWin() || !isDiagonalWin()){
int count = 9;
for (int row = 0; row < 3; row ++){
for (int column = 0; column < 3; column ++){
if (grid[row][column] == 0)
count --;
}
}
if (count == 0)
{
System.out.println("\nSorry, this game is tied. Better luck next time!");
return true;
}
}
return isTie;
}
//***IS ROW WIN**********************************************************
static boolean isRowWin() {
boolean isRowWin = false;
int column = 0 ;
for (int row = 0;row < STANDARD_GRID_ROWS && !isRowWin; row++) {
//if the three array elements on the same row are equal,
//then one of the players has either three 'X's on that
//row, or three 'O's, and therefore, has won
if (grid[row][column] == grid[row][column+1] &&
grid[row][column+1] == grid[row][column+2]){
isRowWin = true;
}
}
return isRowWin;
}
//***IS COLUMN WIN**********************************************************
static boolean isColumnWin() {
boolean isColumnWin = false;
int row = 0 ;
for (int column = 0;column < STANDARD_GRID_COLUMNS && !isColumnWin; column++) {
//if the three array elements on the same row are equal,
//then one of the players has either three 'X's on that
//row, or three 'O's, and therefore, has won
if (grid[row][column] == grid[row+1][column] &&
grid[row+1][column] == grid[row+2][column]){
isColumnWin = true;
}
}
return isColumnWin;
}
//***IS DIAGONAL WIN********************************************************
static boolean isDiagonalWin() {
boolean isDiagonalWin = false;
int row = 0;
int column = 0;
if(
(grid[row][column] == grid[row+1][column+1] &&
grid[row+1][column+1] == grid[row+2][column+2])||
(grid[row][column+2] == grid[row+1][column+1] &&
grid[row+1][column+1] == grid[row+2][column])){
isDiagonalWin = true;
}
return isDiagonalWin;
}
//***GET USER CHOICE********************************************************
static int getUserChoice() {
int choice;
System.out.print("Please enter your choice: ");
choice = keyboard.nextInt();
return choice;
}
//***FILL POSITIONS*********************************************************
static void fillPosition(){
char position = '1';
for (int row = 0; row < STANDARD_GRID_ROWS; row++) {
for (int column = 0; column < STANDARD_GRID_COLUMNS; column++) {
grid[row][column] = (char) ('1' + row*STANDARD_GRID_COLUMNS + column);
}
}
}
//***DISPLAY GRID***********************************************************
static void displayGrid() {
System.out.println((grid[0][0]) + " | " + (grid[0][1]) + " | " + (grid[0][2]));
System.out.println("---------");
System.out.println((grid[1][0]) + " | " + (grid[1][1]) + " | " + (grid[1][2]));
System.out.println("---------");
System.out.println((grid[2][0]) + " | " + (grid[2][1]) + " | " + (grid[2][2]));
}
//***END********************************************************************
}//end of class
In the game, I want it to display the board as:
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9
Regardless of what I choose, or where, it repeatedly ask me to make a choice...Even though I will purposely make one win, or make them tie to get it to end, it's neverending...is that a logical error?
ALSO, I deleted some of the code that I know is correct so it will be easier to read and understand.
Upvotes: 0
Views: 684
Reputation: 129507
The do-while loop in fillPosition
only gets executed once. In the condition row<4 && column<4 && position<10
, the boolean position<10
will initially be false
. Because position
is a char
,
position < 10
is interpreted as
(int)position < 10
(int)position = (int)'2' = 49
(position
is '2'
after the first loop iteration), which is not less than 10
, so this condition is false
, meaning the entire loop-condition is also false
- and hence the loop ends.
So, the '1'
gets placed correctly but nothing else does (it's a do-while loop, so it executes at least once regardless of its condition).
Upvotes: 3
Reputation: 347204
You're fillPosition
do-loop
's exit condition is wrong...
I'd also advise against using character arithmetic, better to use an int and Interger.toString(int)
, but that's just me.
Take a loop at using compound for
loops to fill the grid
array, the outer begin used for the rows and the inner for the columns, for example
Upvotes: 1
Reputation: 3257
You are trying to compare a char (column) with 10 which is a number, the ascii number for the character '1' is bigger than then, so the loop is breaking
Upvotes: 2
Reputation: 4640
Your fillPosition()
method is not correct. It should look sth like this:
static void fillPosition(){
for (int row = 0; row < STANDARD_GRID_ROWS; row++) {
for (int column = 0; column < STANDARD_GRID_COLUMNS; column++) {
grid[row][column] = (char) ('1' + row*STANDARD_GRID_COLUMNS + column);
}
}
}
Upvotes: 1