View Single Post
  #1 (permalink)  
Old 01-10-2008, 05:42 PM
zero2008 zero2008 is offline
Member
 
Join Date: Jan 2008
Posts: 4
zero2008 is on a distinguished road
Tic Tac Toe Ggame Help (Begginer)
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

PHP Code:
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 0done true;
else
{
System.out.print("Column for " player ": ");
int column in.nextInt();
game.set(rowcolumnplayer);
if (
player.equals("x"))
player "o";
else
player "x";
}
}
}

TicTatToe.java

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


public 
void set(int iint jString 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 0ROWSi++)
{


"|";
for (
int j 0COLUMNSj++)
str str 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.

PHP Code:
private boolean backDiagonalWinner() {
char test board[0][boardSize 1];
if (
test == EMPTY)
return 
false;
for (
int i 1boardSizei++)
if (
test != board[i][boardSize 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 1boardSizei++)
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 1boardSizei++)
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 1boardSizei++)
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 0boardSizei++)
if (
rowWinner(i) || columnWinner(i))
return;
if (
numberMoves == boardSize*boardSize)
winner DRAW;

Any help would be appreciated. Thanks
Reply With Quote
Sponsored Links