# TicTacToe - How to Find a Winner

Page 1 of 1

## 8 Replies - 6826 Views - Last Post: 13 March 2012 - 08:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=270641&amp;s=478723beea0b8eeb90e60fe98ef0e0ec&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 vBoss

Reputation: 0
• Posts: 7
• Joined: 12-March 12

# TicTacToe - How to Find a Winner

Posted 12 March 2012 - 09:08 PM

My program is designed to allow 2 human players to play Tic Tac Toe in a command line Tic Tac Toe game.

I have successfully got the game working. The only last feature I am having problems with is determining a winner to the game. Thus far, I have used a separate method called isaWinner() to check for winners. It uses the board [x][y] array that is 0-2 (a 3x3 tic tac toe board).

So, I have all of the winning combinations in isaWinner that checks every single possible combo, to check for all X's and all Y's. As the game goes, the player (X or Y) is kept track. Within the if statement to change players (as you will see in the code), I have placed a nested if statement.

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"))
{
if(game.isaWinner(true))
{
System.out.print("Player X wins!");
}
player = "o";}
else
{
if(game.isaWinner(true))
{
System.out.print("Player Y wins!");
}
player = "x"; }
}

Point is, in front of me I have code that I believe should work but I don't know where to go from here.

If anyone can steer me in the right direction I'd appreciate the help.

My TicTacToe() class:

/**
A 3 x 3 tic-tac-toe board.
*/
public class TicTacToe
{
private String[][] board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
private static int turn = 0;
//private static String winner;
/**
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] = " ";
}

/**
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, String player)
{
if (board[i][j].equals(" "))
board[i][j] = player;

}
/**
Creates a string representation of the board, such as
|x  o|
|  x |
|   o|
@return the string representation
*/
@Override
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for (int j = 0; j < COLUMNS; j++)
r = r + board[i][j];
r = r + "|\n";
}
return r;
}

public boolean  isaWinner(boolean isTrue)
{
isTrue = false;
if (board[0][0] == "x" && board[0][1] == "x" && board[0][2] == "x"
| board[1][0] == "x" && board[1][1] == "x" && board[1][2] == "x"
| board[2][0] == "x" && board[2][1] == "x" && board[2][2] == "x"
| board[0][0] == "x" && board[1][0] == "x" && board[2][0] == "x"
| board[0][1] == "x" && board[1][1] == "x" && board[2][1] == "x"
| board[0][2] == "x" && board[1][2] == "x" && board[2][2] == "x")
{
isTrue = true;
}
if (board[0][0] == "y" && board[0][1] == "y" && board[0][2] == "y"
| board[1][0] == "y" && board[1][1] == "y" && board[1][2] == "y"
| board[2][0] == "y" && board[2][1] == "y" && board[2][2] == "y"
| board[0][0] == "y" && board[1][0] == "y" && board[2][0] == "y"
| board[0][1] == "y" && board[1][1] == "y" && board[2][1] == "y"
| board[0][2] == "y" && board[1][2] == "y" && board[2][2] == "y")
{
isTrue= true;
}
return isTrue;
}

}

My TicTacToeRunner class:

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);

String player = "x";
TicTacToe game = new TicTacToe();

//System.out.print(game.isaWinner(true, "X"));
boolean done = false;
//int row = 0;

while (!done)
{
//if (game.isaWinner(true, " "))
//System.out.print(game.isaWinner(true));
//done = true;

System.out.print(game.toString());
System.out.print(
"Row for " + player + " (-1 to exit): ");
// String playerOne = JOptionPane.showInputDialog(frame, "What's your name?");
//int row = 0
//int row = Integer.parseInt(rowNum)
//if (row > 2){
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"))
{
if(game.isaWinner(true))
{
System.out.print("Player X wins!");
}
player = "o";}
else
{
if(game.isaWinner(true))
{
System.out.print("Player Y wins!");
}
player = "x"; }
}

}

}

}

Is This A Good Question/Topic? 0

## Replies To: TicTacToe - How to Find a Winner

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: TicTacToe - How to Find a Winner

Posted 12 March 2012 - 09:14 PM

private String[][] board;

also have a int[][] countBoard of the same dimension

when the user put a X in board[i][j] put also 100 in countBoard[i][j]
when the user put a O in board[i][j] put also 10 in countBoard[i][j]

If the total of a row or a column or a diagonal in countBoard is 30, the O won
if that total is 300, the X won

Quite easy to make this totals using for() loops

Happy coding

### #3 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

## Re: TicTacToe - How to Find a Winner

Posted 13 March 2012 - 01:49 AM

Also, | is not a comparison operator. (Single line) It is a bit-wise operator. || is what you want to compare two conditions. Of course, with pbl's solution, you might not need that either.

### #4 vBoss

Reputation: 0
• Posts: 7
• Joined: 12-March 12

## Re: TicTacToe - How to Find a Winner

Posted 13 March 2012 - 01:45 PM

Thanks for the replies. I tried implementing pbl's idea and I thought it was bulletproof but I am getting "nullpointerexception" error. Any ideas?

TicTacToeRunner

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);

String player = "x";
TicTacToe game = new TicTacToe();

//System.out.print(game.isaWinner(true, "X"));
boolean done = false;

while (!done)
{
if (game.isaWinner(true))
System.out.print(player + "is the winner!");
done = true;

System.out.print(game.toString());
System.out.print(
"Row for " + player + " (-1 to exit): ");
// String playerOne = JOptionPane.showInputDialog(frame, "What's your name?");
//int row = 0
//int row = Integer.parseInt(rowNum)
//if (row > 2){
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);
{
System.out.print("Player X has won the game!"); }

if (player.equals("x"))
{
player = "o";}
else
{
player = "x"; }
}

}
}
}

TicTacToe

/**
A 3 x 3 tic-tac-toe board.
*/
public class TicTacToe
{
private String[][] board;
private int[][] wboard;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
private static int turn = 0;
private static int o = 0;
private static int l = 0;
private static int a = 0;
//private static String winner;
/**
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] = " ";
}

/**
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, String player)
{
turn++;

if (board[i][j].equals(" "))
board[i][j] = player;

if (player.equals("x"))
o = (i * 10);
l = (j * 10);
if (player.equals("y"))
o = (i * 100);
l = (j * 100);

}
/**
Creates a string representation of the board, such as
|x  o|
|  x |
|   o|
@return the string representation
*/
@Override
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for (int j = 0; j < COLUMNS; j++)
r = r + board[i][j];
r = r + "|\n";
}
return r;
}

public boolean isaWinner(boolean aWin)
{
if (wboard[0][0] +  wboard[0][1] +  wboard[0][2] == 30
| wboard[1][0] + wboard[1][1] + wboard[1][2] == 30
| wboard[2][0] + wboard[2][1] + wboard[2][2] == 30
| wboard[0][0] + wboard[1][0] + wboard[2][0] == 30
| wboard[0][1] + wboard[1][1] + wboard[2][1] == 30
| wboard[0][2] + wboard[1][2] + wboard[2][2] == 30)
aWin = true;

if (wboard[0][0] +  wboard[0][1] +  wboard[0][2] == 300
| wboard[1][0] + wboard[1][1] + wboard[1][2] == 300
| wboard[2][0] + wboard[2][1] + wboard[2][2] == 300
| wboard[0][0] + wboard[1][0] + wboard[2][0] == 300
| wboard[0][1] + wboard[1][1] + wboard[2][1] == 300
| wboard[0][2] + wboard[1][2] + wboard[2][2] == 300)
aWin = true;
return aWin = false;
}
}

### #5 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: TicTacToe - How to Find a Winner

Posted 13 March 2012 - 02:55 PM

wboard[][] has not been initialized as you did with board[][] in lines 22 - 26. wboard contains "null" and that's why you're getting the error.

### #6 vBoss

Reputation: 0
• Posts: 7
• Joined: 12-March 12

## Re: TicTacToe - How to Find a Winner

Posted 13 March 2012 - 06:40 PM

I made some changes and initialized wboard. Code is still not working I just don't know where to go from here Any suggestions?

TicTacToeRunner()
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);

String player = "x";
TicTacToe game = new TicTacToe();

//System.out.print(game.isaWinner(true, "X"));
boolean done = false;

while (!done)
{
if (game.isaWinner(true))
System.out.print(player + "is the winner!");
done = true;

System.out.print(game.toString());
System.out.print(
"Row for " + player + " (-1 to exit): ");
// String playerOne = JOptionPane.showInputDialog(frame, "What's your name?");
//int row = 0
//int row = Integer.parseInt(rowNum)
//if (row > 2){
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);
{
System.out.print("Player X has won the game!"); }

if (player.equals("x"))
{
player = "o";}
else
{
player = "x"; }
}

}
}
}

TicTacToe()
/**
A 3 x 3 tic-tac-toe board.
*/
public class TicTacToe
{
private String[][] board;
private int[][] wboard;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
private static final int WROWS;
private static final int WCOLUMNS;
private static int turn = 0;
private static int o = 0;
private static int l = 0;
private static int a = 0;
//private static String winner;
/**
Constructs an empty board.
*/
public TicTacToe()
{

board = new String[ROWS][COLUMNS];
wboard = new int[WROWS][WCOLUMNS];
// 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, String player)
{
turn++;

if (board[i][j].equals(" "))
board[i][j] = player;

if (player.equals("x"))
o = (i * 10);
l = (j * 10);
if (player.equals("y"))
o = (i * 100);
l = (j * 100);

}
/**
Creates a string representation of the board, such as
|x  o|
|  x |
|   o|
@return the string representation
*/
@Override
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
r = r + "|";
for (int j = 0; j < COLUMNS; j++)
r = r + board[i][j];
r = r + "|\n";
}
return r;
}

public boolean isaWinner(boolean aWin)
{
if (wboard[0][0] +  wboard[0][1] +  wboard[0][2] == 30
| wboard[1][0] + wboard[1][1] + wboard[1][2] == 30
| wboard[2][0] + wboard[2][1] + wboard[2][2] == 30
| wboard[0][0] + wboard[1][0] + wboard[2][0] == 30
| wboard[0][1] + wboard[1][1] + wboard[2][1] == 30
| wboard[0][2] + wboard[1][2] + wboard[2][2] == 30)
aWin = true;

if (wboard[0][0] +  wboard[0][1] +  wboard[0][2] == 300
| wboard[1][0] + wboard[1][1] + wboard[1][2] == 300
| wboard[2][0] + wboard[2][1] + wboard[2][2] == 300
| wboard[0][0] + wboard[1][0] + wboard[2][0] == 300
| wboard[0][1] + wboard[1][1] + wboard[2][1] == 300
| wboard[0][2] + wboard[1][2] + wboard[2][2] == 300)
aWin = true;
return aWin = false;
}
}

### #7 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

## Re: TicTacToe - How to Find a Winner

Posted 13 March 2012 - 07:05 PM

Where do you set the values for wboard to 10 when player o gets it and 100 when player x gets it? Did you listen to me about my || operator?

### #8 vBoss

Reputation: 0
• Posts: 7
• Joined: 12-March 12

## Re: TicTacToe - How to Find a Winner

Posted 13 March 2012 - 07:49 PM

Right after I posted that I actually changed that. Changed all the | to ||

In this section of the code, I set the variables "o" and "l" which are the variables in each array of wboard[][].

board = new String[ROWS][COLUMNS];
wboard = new int[o][l];
// 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, String player)
{
turn++;

if (board[i][j].equals(" "))
board[i][j] = player;

if (player.equals("x"))
o = (i * 10);
l = (j * 10);
if (player.equals("y"))
o = (i * 100);
l = (j * 100);

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: TicTacToe - How to Find a Winner

Posted 13 March 2012 - 08:04 PM

Why are all those horrible static variables ? What is o and l ?
/**
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, String player)
{
turn++;

if (board[i][j].equals(" ")) {
board[i][j] = player;
if (player.equals("x"))
wboard[i][j] = 10;
else
wboard[i][j] = 100;
}

for() loops were not invented for nothing

for(int i = 0; i < 3; ++i) {
int total = 0;
for(int j = 0; j < 3; ++j) {
total += wboard[i][j];
}
if(total == 30)...X won on a row
if(total == 300) ... O won on a row
}

for(int i = 0; i < 3; ++i) {
int total = 0;
for(int j = 0; j < 3; ++j) {
total += wboard[j][i];
}
if(total == 30)...X won on a column
if(total == 300) ... O won on a column
}
// I'll let you do the diagonals :)/>