# Thread: need help creating my Tic Tac Toe game!

1. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0

## need help creating my Tic Tac Toe game!

This is what i have so far for creating a simple tic tac toe game (user vs computer).
Im not sure how to determine the winner propely by using an if '3 x's or o's are in a row then that person/computer wins.

Java Code:
```// The "TicTacToe" class.
import java.awt.*;
import hsa.Console;
public class TicTacToe
{
static Console c;           // The output console
public static void main (String[] args)
{
c = new Console ();
// Place your program here.  'c' is the output console
String board[];
board = new String [9];
int playerMove = 0;
int compMove;
c.print ("Tic Tac Toe Game!");
c.println ();
c.println ();
//prompt user for their move
c.print ("Enter the location of your next move (0-8): ");
board [playerMove] = "X";
//the computer must now make a move choosing a random number 0-8 and printing an "O"
for (int counter = 0 ; counter < 1 ; counter++)
{
compMove = (int) (Math.random () * (8 - 0 + 1)) + 0;
board [compMove] = "O";
}
//make an if statement to determine if there is a winner
if (board[playerMove]= "XXX") //if player gets three X's in a row
{
c.println ("Player Wins!");
}
else if (board[compMove]= "OOO") //if player gets three O's in a row
{
c.println ("Computer Wins!");
}
//make a loop so the game goes on until the maximum moves (9) have occured
while (board[playerMove]+ [compMove]<9)
{

}
} // main method
} // TicTacToe class```

Bob... before you start committing grey matter to code I would suggest that you sit down and write down all the issues involved with this task. Write the tasks needed for a tic-tac-toe game... some come mind:
• Are you going to use a single dimension array ?

Java Code:
`gameBoard [] = {0,1,2,3,4,5,6,7,8,9}`
• or are you going to use a double dimension array?

Java Code:
`gameBoard [3][3]`
• What combinations within the array will make a win?
• How do you avoid overwriting an "X" with an "O" or visaversa?
• How are you going to show the game board status to the player?

Luck,
CJSL

3. Member
Join Date
Dec 2008
Location
North America
Posts
29
Rep Power
0

## Instructions, steps

Here are the steps giving to me in order to complete my program. I have showed where I am stuck, and this is what is expected of my tic tac toe program:
Tic-Tac-Toe

We’re going to make the game Tic-Tac-Toe. Our goal is to play against the computer.

My initial thoughts on this program are that:

We will need a 9 element array of type String called board that represents the playing board.

The user will make their move by entering a number between 0 and 8 inclusive (see the sample code). In the location that the user chose, the program will place an X.

The computer will choose its move be generating a random number between 0 and 8 inclusive. In the spot that the computer chose, the program will place an O.

The program will need if statements to determine if there is a winner (three X’s or three O’s in a row) --this where i am roughly

The program will need a loop so that the user and computer can continue playing each other until the game ends. (What is the maximum number of moves that can occur before all of the spaces have been used?)

I think that we should pre-load every element in the array with a “.” so that the user can see which squares are still available. Also, we need to show the user the 9 element array; we should display it in a grid so that I looks more like the real game. Pre-loaded and arranged in a grid, it should look like this:
Notes:

Your program will have to ensure that both the user and the computer can only choose spots that are still available. (How can you tell if an array element is still available?)

The following is a screen shot from a sample version of the program. Notice that you don't have to ask the computer to move; it should automatically move after the user enters their move.

4. We will need a 9 element array of type String called board that represents the playing board.
Hm, I would think that a 3 x 3 two-dimensional array would be a more natural fit here, but YMMV.

The user will make their move by entering a number between 0 and 8 inclusive (see the sample code). In the location that the user chose, the program will place an X.
Or some type of ordered pair.

The computer will choose its move be generating a random number between 0 and 8 inclusive. In the spot that the computer chose, the program will place an O.
ditto.

The program will need if statements to determine if there is a winner (three X’s or three O’s in a row) --this where i am roughly
Do the logic out on paper. In fact plan on going through lots of paper and lead til you find a decent solution. Then and only then start coding.

5. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
13
just draw it out and number the squares. this is one of those problems where the solution can be programmed similarly to how you solve the problem by hand.

6. There are 8 possible winning solutions. Once you get one if statement coded the other 7 are pretty much the same thing.

When in doubt pseudo-code something and show us and we can tell you if it's going to work or not.

P.S. Whats YMMV mean?

7. ## Ymmv

#### Posting Permissions

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