# Turns in a tic-tac-toe simulation?

Page 1 of 1

## 2 Replies - 5550 Views - Last Post: 18 October 2012 - 10:22 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=296162&amp;s=bbe5f70b20ceadf5926f91127a796ef0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 SAlford6129

Reputation: 1
• Posts: 18
• Joined: 03-September 12

# Turns in a tic-tac-toe simulation?

Posted 18 October 2012 - 10:11 AM

The code below goes to my homework, and although the assignment does not request what I'm asking about, I'm a little curious about something. It just wants us to use an array to hold a random 0 or 1, and then 0 = O and 1 = X on the board. We had to use labels to create the game board, so I did, but in running the simulation multiple times, I find an odd result. 75% of the time, the board looks believable and legit. But every so often I get a weird one, like the following example.

```O O X      O X O
O O O |or| O O O
X O O      O X O
```

Clearly, you couldn't play Tic-Tac-Toe like that unless you're playing against someone who doesn't know the rules and cheating. My question is, is there any way besides using a variable for a "coin toss" to decide a starting player, and then alternating the turns that way, to get a more normal board? (By that I mean, X goes, then O goes) I'm using an Array to decide the random numbers, and then a series of if statements to set each label to the array values populate the board.

I'm not posting any code because this is homework, and I don't want to encourage any cheating off my work, but I don't mind sharing it to get some input if there is a better way than what I've done. This isn't required, and I'm 100% I'm going to get full points on this, since it didn't say to forbid this scenario, but I'm curious to see if there is another way to do it without setting up another loop or two to try and use a coin toss variable/turn-by-turn method, or nesting EVERY loop to check for a winner and not let the entire game be played. I'm probably just thinking into this too much, since I'm sure the way it asked us to create this simulator is inherently troublesome for turn by turn with random values in an array.

This post has been edited by tlhIn`toq: 18 October 2012 - 10:49 AM
Reason for edit:: Used [code] tags for the layout to maintain monospacing

Is This A Good Question/Topic? 0

## Replies To: Turns in a tic-tac-toe simulation?

### #2 SAlford6129

Reputation: 1
• Posts: 18
• Joined: 03-September 12

## Re: Turns in a tic-tac-toe simulation?

Posted 18 October 2012 - 10:16 AM

Okay so I guess I can't edit my post, and I didn't preview it. I'm uploading a couple of screen shots of what I was trying to show on the game board above. lol

### #3 SAlford6129

Reputation: 1
• Posts: 18
• Joined: 03-September 12

## Re: Turns in a tic-tac-toe simulation?

Posted 18 October 2012 - 10:22 AM

These are the screen shots

Example 1:

Example 2: