# Thread: Tic-Tac-Toe "Computer Algorithm"

1. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## Tic-Tac-Toe "Computer Algorithm"

Hello there!

I'm coding a Tic-Tac-Toe game, and I have tried to create an algorithm so that the computer always chooses a tile which itself or the player can win by choosing in the next move. If there are no such tiles, the computer just chooses them in another order that I have set up, first the middle, then the corners and the remaining tiles.

Unfortunately, this didn't work out as expected, the computer just does the second part of the algorithm; the order that I had set up, first the middle, then the corners and the remaining tiles. Can you help me in figuring out why this does not work?

(I did not want to post the whole program because the part of significance is, as said, the computer algorithm, but if you need the whole program I could post it. Also if there is something you don't understand, please tell me and I will explain it the best I can.)

Java Code:
```// This class is used for everything that has got to do with the board, displaying it, changing it, etc.
class Board{
// This is the board itself.
char board[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
// This is the order of priority I talked about earlier.
int priorityOrder[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
// These are used to remember the tile which the computer must choose to win, or to prevent the user from winning.
int Xposition = -1, Yposition  = -1;
// This is a variable which tells us who the winner is, 1 for the player, 2 for the computer and 3 for a tie.
static int winner = 0;
// These are the possible ways of winning. The numbers are the tile numbers but in the way a computer would number them, from 0-8 instead of 1-9.
int possibilities[][] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 7}, {2, 4, 6}};
...

// In this method the computer chooses a tile.
public void turnComputer() throws IOException{

boolean test = computerAlgorithm('O', 'O');

if(!test){
test = computerAlgorithm('X', 'O');
if(!test){

for(int x=0;x<priorityOrder.length;x++)
if(board[priorityOrder[x]] != 'X' && board[priorityOrder[x]] != 'O'){
board[priorityOrder[x]] = 'O';
System.out.print("The computer chose tile number " + (priorityOrder[x]+1) + "! ");
break;
}
}
}
}
...
// This method is used for finding the tiles which the user or the computer can win by using. It takes one parameter, the "test" variable, which can either be an 'X' or an 'O'. The program will count
// these and see if there are two of them in a row on the board. If there are, it will choose the tile that hasn't been chosen in that row (because there are three tiles in a row),
// that is, a character that is not an X or an O.
public boolean computerAlgorithm(char test) throws IOException{
int tiles = 0;

for(int x=0;x<possibilities.length;x++){

for(int y=0;y<3;y++){
if(possibilities[x][y] == test)
tiles++;
else{
Yposition = y;
Xposition = x;
}
}

if(tiles == 2){
System.out.print("The computer chose tile number " + (possibilities[Xposition][Yposition]+1) + "! ");
board[possibilities[Xposition][Yposition]] = 'O';
return true;
}
}

return false;
}

...
}```
Last edited by Zelaine; 12-07-2013 at 12:48 PM.

2. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## Re: Tic-Tac-Toe "Computer Algorithm"

Could anyone please answer? I have been trying to solve this problem for days now :(

3. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## Re: Tic-Tac-Toe "Computer Algorithm"

No worries, I solved it by putting a lot of print statements everywhere.

4. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Tic-Tac-Toe "Computer Algorithm"

Good job. Nothing better than the good old peppering of System.outs :) In the future you will learn to use a debugger though which requires even less effort to see what's going on, but even when using Java for more than 10 years, I still apply the System.out trick sometimes to debug certain problems.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•