First of all, you'll need a
board.
private char[][] board = new char[3][3];
I would recommend making final chars to represent what is in each
sqaure.
private final char EMPTY = ' ';
private final char PLAYER1 = 'X';
private final char PLAYER2 = 'O';
you need to initialize the
board.
public void init() {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
board[x][y] = EMPTY;
}
}
}
to check the
winner, this gets a bit tedious.
public char winner() {
for (int i = 0; i < board.length; i++) {
/* check horizontally */
if (board[i][0] == board[i][1] &&
board[i][0] == board[i][2]) {
if (board[i][0] != EMPTY) {
return board[i][0];
}
}
/* check vertically */
if (board[0][i] == board[1][i] &&
board[0][i] == board[2][i]) {
if (board[0][i] != EMPTY) {
return board[0][i];
}
}
}
/* check diagonally */
if (board[0][0] == board[1][1] &&
board[0][0] == board[2][2]) {
if (board[0][0] != EMPTY) {
return board[0][0];
}
}
if (board[0][2] == board[1][1] &&
board[0][2] == board[2][0]) {
if (board[0][2] != EMPTY)) {
return board[0][2];
}
}
return EMPTY;
}
Greetings
Albert