I was asked in my C# class to write a simulation of a Tic-Tac-Toe game, 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 examples.




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 or vice versa.) I'm using a 2D Array to store the random numbers, and then a series of if statements to set each label to the array values and populate the board.

I'm not posting any code because 1)this is homework, 2)it is complete, and 3)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% sure 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.

I'm not asking anyone to do my homework, I'm asking if anyone knows of a way to limit the number of X's and O's without doing too much work. If not, I'll just live with it.

Recommended Answers

All 6 Replies

What makes your boards so weird?

It seems like your algorithm for each turn is not only randomly choosing a position on the board, but is also randomly choosing which player's turn it is (X or O). What you really want to be doing is choosing a random position on the board only and alternating whose turn it is. Say you have a turn counter that starts at zero and increments after each turn. You could do playerToken = turn % 2 == 0 ? 'X' : 'O'; to determine which token to place.

@ddanbe - No way should a board have 8 O's and only 1 x, or 7 O's and 2 X's. Turn by turn indicates that the starting player should 1 more turn than player 2. (9 slots, 2 players, Player 2 can't make a move after Player 1's 9th Turn.) So player 1 should get 5 turns, Player 2 4 turns.

@dcdruck - Thanks, I see what you're saying. They don't ask us for that, and we've covered none of that in class lol, which is why I came here. :D

Here is the assignment straight out of the book.
Starting out with Visual C# 2010 (2nd Edition)
Tony Gaddis ISBN10:0-13-216545-7 Chapter 7, Page #461, Programming Problem #8
"Create an application that simulates a game of toc-tac-toe. [form layout information excluded here]. The application should use a two-dimensional array to simulate the gameboard in memory. When the user clicks the New Game button, the application should step through the array, storing a random number in the range of 0 through 1 in each element. The number 0 represents the letter O, and the number 1 represents the letter X. The form should then be updated to display the game board. The application should display a message indication whether player x won, player Y won, or the game was a tie."

I don't see any way for a tie (although I included the code for the possibility), unless it is the 1-in-a-million that the random numbers happened to get it right.

Ofcourse, someone was slacking writing this one period, because obviously the way they tell you to write the program is flawed, not accounting for turns, simply random number assignment in a nested 2D Array. lol

Yeah it seems like their instructions are flawed. They don't mention anything about taking turns. The instructions should instruct you to alternate between 0 and 1, each time randomly choosing an empty slot from the 2-dimensional array until it is full.

Of course, it could be that the writers aren't concerned with you creating a tic tac toe game that plays by the rules, but are more interested in seeing you randomly fill out a 3x3 grid. Who knows.

The term tie in this context is sort of misleading. I would call it a stalemate (nobody can win).

Exactly. Also, FYI, I noticed this in the omitted form layout description.

"Figure 7-45 Shows an example of the application's form. The form shown in the figure uses eight large Label controls to display the Xs and Os. "

The really funny thing? The picture on the other side (Figure 7-45) clearly shows NINE large Label Controls. XD

Are they outsourcing? Or do they just have a stoner editor?

Sounds to me like you need a new book :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.