Tic Toe java code exists when we enter 0 row and 0 column
good afternoon everyone. i have been working on a tictactoe game using arrays and if statements and loops. my problem is the game exits when i type in row 0 and column 0. also i was wondering if you could help me change the input method so that the user could type in like 1 and it would place an x or an o in the box. also i just noticed it doesn't stop when you have three in a row
Code :
import java.util.Scanner;
/**
This program runs a TicTacToe game. It prompts the
user to set positions on the board and prints out the
result.
*/
public class TicTacToeRunner
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
char player = 'x';
TicTacToe game = new TicTacToe();
boolean done = false;
int Count=0;
do{
System.out.print(game.toString());
System.out.print(
"Row for " + player );
int row = in.nextInt();
System.out.print("Column for " + player + ": ");
int column = in.nextInt();
game.set(row, column, player);
if (player==('X'))
player = 'O';
else
player = 'X';
}while(!game.winner()&&Count<=9);
}
}
Code :
/**
A 3 x 3 tic-tac-toe board.
*/
public class TicTacToe
{
/**
Constructs an empty board.
*/
public TicTacToe()
{
board = new char [ROWS][COLUMNS];
// Fill with spaces
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] =' ';
}
/**
Sets a field in the board. The field must be unoccupied.
@param i the row index
@param j the column index
@param player the player ("x" or "o")
*/
public void set(int i, int j, char player)
{
if (board[i][j]== ' ')
board[i][j] = player;
}
/**
Creates a string representation of the board, such as
|x o|
| x |
| o|
@return the string representation
*/
public String toString()
{
String r = "";
r+=" | | \n";
r+=" "+board[0][0]+" |"+board[0][1]+" | "+board[0][2]+"\n";
r+=" | | \n";
r+="----------\n";
r+=" | | \n";
r+=" "+board[1][0]+" |"+board[1][1]+" | "+board[1][2]+"\n";
r+=" | | \n";
r+="----------\n";
r+=" | | \n";
r+=" "+board[2][0]+" |"+board[2][1]+" | "+board[2][2]+"\n";
r+=" | | \n";
return r;
}
public boolean winner()
{
boolean winner;
if(board[0][0] == board[0][1] && board[0][1] == board[0][2]&&board[0][0] !=' ')
return true;
if(board[1][0] == board[1][1] && board[1][1] == board[1][2]&&board[1][0] !=' ')
return true;
if(board[2][0] == board[2][1] && board[2][1] == board[2][2]&&board[2][0] !=' ')
return true;
if(board[0][0] == board[1][0] && board[1][0] == board[2][0]&&board[0][0] !=' ')
return true;
if(board[0][1] == board[1][1] && board[1][1] == board[2][1]&&board[0][0] !=' ')
return true;
if(board[0][2] == board[1][2] && board[1][2] == board[2][2]&&board[0][0] !=' ')
return true;
if(board[0][0] == board[1][2] && board[1][2] == board[2][2]&&board[0][0] !=' ')
return true;
if(board[2][0] == board[1][2] && board[1][2] == board[2][1]&&board[0][0] !=' ')
return true;
return false;
}
private char[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
}
thanks for all your help
Re: help with tic tac toe
hello again i just fixed the stopping when i put in 0,0 but it keeps going even if i have three x or o's it stops once i get all nine moves. i think i need an if statement in the tic tac toe runner? here is my updated class
Code :
/**
A 3 x 3 tic-tac-toe board.
*/
public class TicTacToe
{
/**
Constructs an empty board.
*/
public TicTacToe()
{
board = new char [ROWS][COLUMNS];
// Fill with spaces
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] =' ';
}
/**
Sets a field in the board. The field must be unoccupied.
@param i the row index
@param j the column index
@param player the player ("x" or "o")
*/
public void set(int i, int j, char player)
{
if (board[i][j]== ' ')
board[i][j] = player;
}
/**
Creates a string representation of the board, such as
|x o|
| x |
| o|
@return the string representation
*/
public String toString()
{
String r = "";
r+=" | | \n";
r+=" "+board[0][0]+" |"+board[0][1]+" | "+board[0][2]+"\n";
r+=" | | \n";
r+="----------\n";
r+=" | | \n";
r+=" "+board[1][0]+" |"+board[1][1]+" | "+board[1][2]+"\n";
r+=" | | \n";
r+="----------\n";
r+=" | | \n";
r+=" "+board[2][0]+" |"+board[2][1]+" | "+board[2][2]+"\n";
r+=" | | \n";
return r;
}
public boolean winner()
{
boolean winner;
if(board[0][0] == board[0][1] && board[0][1] == board[0][2]&&board[0][0] !=' ')
return true;
if(board[1][0] == board[1][1] && board[1][1] == board[1][2]&&board[1][0] !=' ')
return true;
if(board[2][0] == board[2][1] && board[2][1] == board[2][2]&&board[2][0] !=' ')
return true;
if(board[0][0] == board[1][0] && board[1][0] == board[2][0]&&board[0][1] !=' ')
return true;
if(board[0][1] == board[1][1] && board[1][1] == board[2][1]&&board[0][2] !=' ')
return true;
if(board[0][2] == board[1][2] && board[1][2] == board[2][2]&&board[1][1] !=' ')
return true;
if(board[0][0] == board[1][2] && board[1][2] == board[2][2]&&board[1][2] !=' ')
return true;
if(board[2][0] == board[1][2] && board[1][2] == board[2][1]&&board[2][1] !=' ')
return true;
return false;
}
private char[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
}
also i need help checking for a valid move.
Re: help with tic tac toe
Hello big_c, glad you solved this issue.
What was the problem in the end?