// JP opened flex table

Click to See Complete Forum and Search --> : Help with Tic Tac Toe Game (Begginer)


zero2008
January 10th, 2008, 07:17 AM
My problem is that I am writing a tic tac toe game and I don't know how to check the 2d array for who has won or if it is a draw.
I have included two java files which are fully working I just need to create a function to check for the winner or draw.

TicTacToeTest.java

import java.util.Scanner;

public class TicTacToeTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String player = "x";
TicTacToe game = new TicTacToe();
boolean done = false;
while (!done)
{
System.out.println(game.toString());
System.out.println("");
System.out.print(
"Row for " + player + " (-1 to exit): ");
int row = in.nextInt();
if (row < 0) done = true;
else
{
System.out.print("Column for " + player + ": ");
int column = in.nextInt();
game.set(row, column, player);
if (player.equals("x"))
player = "o";
else
player = "x";
}
}
}
}

TicTatToe.java

public class TicTacToe
{
/**
Constructs an empty board.
*/
public TicTacToe()
{
board = new String[ROWS][COLUMNS];
// Fill with spaces
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board[i][j] = " ";
}


public void set(int i, int j, String player)
{
if (board[i][j].equals(" "))
board[i][j] = player;
}

/**
Creates a string representation of the board, such as return the string representation
*/
public String toString()
{
System.out.println("");
System.out.println("Valid inputs range from 0,0 to 2,2!");
System.out.println("");
String r = "";
String str = "";
str = str + "-------\n";
for (int i = 0; i < ROWS; i++)
{


r = "|";
for (int j = 0; j < COLUMNS; j++)
str = str + r + board[i][j];
str = str + "|\n";
str = str + "-------\n";


}

return str;
}



private String[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;

}

P.S.
I have also tried this but it just wont work.

private boolean backDiagonalWinner() {
char test = board[0][boardSize - 1];
if (test == EMPTY)
return false;
for (int i = 1; i < boardSize; i++)
if (test != board[i][boardSize - 1 - i])
return false;
winner = test;
return true;
}

private boolean columnWinner(int col) {
char test = board[0][col];
if (test == EMPTY)
return false;
for (int i = 1; i < boardSize; i++)
if (test != board[i][col])
return false;
winner = test;
return true;
}

private boolean forwardDiagonalWinner() {
char test = board[0][0];
if (test == EMPTY)
return false;
for (int i = 1; i < boardSize; i++)
if (test != board[i][i])
return false;
winner = test;
return true;
}

private boolean rowWinner(int row) {
char test = board[row][0];
if (test == EMPTY)
return false;
for (int i = 1; i < boardSize; i++)
if (test != board[row][i])
return false;
winner = test;
return true;
}

private void setWinner() {
if (winner != NOWINNER)
return;
if (forwardDiagonalWinner() || backDiagonalWinner())
return;
for (int i = 0; i < boardSize; i++)
if (rowWinner(i) || columnWinner(i))
return;
if (numberMoves == boardSize*boardSize)
winner = DRAW;
}

Any help would be appreciated. Thanks

dlorde
January 10th, 2008, 10:37 AM
If I'm thinking of the right game, there is a winner if there are three of the same entries in any row, column, or diagonal, so, for example, if you have a [row][col] array, for the first row you have to check that each column entry is the same:if (array[0][0] == 'X' && array[0][1] == 'X' && array[0][2] == 'X')
// or
if (array[0][0] == array[0][1] && array[0][1] == array[0][2])

//for the second row:
if (array[1][0] == 'X' && array[1][1] == 'X' && array[1][2] == 'X')

// for the first column, you have to check each row entry:
if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
// or
if (array[0][0] == array[1][0] && array[1][0] == array[2][0])

// for the forward diagonal:
if (array[0][0] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
//or
if (array[0][0] == array[1][1] && array[1][1] == array[2][2])and so-on. There are lots of ways to do the checks, you should try a few out on paper (use diagrams) and see which seem most suitable - you can use nested loops to run through the rows and columns to save repeating almost identical code each time. Figure out the loop to check a single row (or column), then put it inside a loop that checks all the rows (or columns). Once you get started, you may find a short-cut that allows you shorten it a lot more, but start with the simple stuff first. The diagonals can be done separately.

Fancy algorithms are slow when N is small, and N is usually small...
R. Pike

//JP added flex table