Tic Tac Toe Java help!
Hi, I have been trying to create a simple Tic Tac Toe game using Java. I'm currently having problems as to how to create the board using arrays (sorry I don't understand them very well). All I have so far is this,
public class ticTacToe {
private int [] [] theBoard = new int [3] [3];
// *'N' is no winner
//'X' is X won
// 'O' is O won
// 'C' is a cat's game
public static char didSomeoneWin(int [] [] currentBoard)
{
int winForX = 8000, winForO = 1,checkIfItsAcatsGame = 1, product;
char winner = 'N';
for(int column = 0; column < 3; column++) // Check the columns
{
product = currentBoard [0] [column] * currentBoard [1] [column] * currentBoard [2] [column];
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
}
for(int row = 0; row < 3; row++) // Check the rows
{
product = currentBoard [row] [0] * currentBoard [row] [1] * currentBoard [row] [2];
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
}
product = currentBoard [0] [0] * currentBoard [1] [1] * currentBoard [2] [2]; // Check one diagonal
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
product = currentBoard [0] [2] * currentBoard [1] [1] * currentBoard [2] [0]; // Check the other diagonal
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
if(winner == 'N') // If nobody's won, Check for a cat's game
{
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
checkIfItsAcatsGame *=currentBoard [row] [column];
}
if(checkIfItsAcatsGame != 0) winner = 'C'; // any empty space is a zero. So product is zero if there is space left.
}
return winner;
public void markX(int row, int column)
{
myBoard [row] [column] = 20;
}
public void markO(int row, int column)
{
myBoard [row] [column] = 1;
}
That is all so far, could someone help me print out the board? And help me out on the arrays? I don't think they are quite right. Thanks in Advance!
Message was edited by:
matthews1.7.2.0

