Click here to Skip to main content
15,512,456 members
Please Sign up or sign in to vote.
1.60/5 (5 votes)
See more:
Sometimes when I run the program I get...for example....6 O's and 3 X's. That is not realistic.

What I have tried:

The code produces random numbers, either a 0 or 1. 0 is assigned to "O" and 1 is assigned to "X".
Updated 30-Mar-16 23:12pm
Sergey Alexandrovich Kryukov 30-Mar-16 17:03pm    
The question makes no sense at all. If the number 0 or 1 is random, it's random. From this standpoint, 6 O's is perfectly "realistic". Just design your game properly. Why would you need a sequence of 0.. 1 numbers at all? You need either 1, for one player, or 0, for another one, never a sequence. The random can be a cell played.

It appears that you are trying to generate a set of valid board states.

Don't generate the board states totally at random!

Instead generate them the same way a game is played, alternating X and O.
The choice of positions for the X and O could be chosen at random (avoiding collisions), if that makes sense for your purpose.
Share this answer
Sergey Alexandrovich Kryukov 30-Mar-16 17:15pm    
That's a right idea (a 5), but I'm not sure that the inquirer, so confused with some wrong ideas, can understand it. So I placed a more detailed explanation, please see Solution 2 (still not sure :-).
Member 12426808 30-Mar-16 17:19pm    
This is what the directions are.
The application should use a two-dimensional int array to simulate the game board 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 O, and the number 1 represents X. The form should then be updated to display the game board. The application should display a message indicating whether player X won, player Y won, or the game was a tie.
Matt T Heffron 30-Mar-16 17:52pm    
One problem with always filling all 9 positions of the board (even if you account for 5 Xs and 4 Os (or vice versa)) is that some of these board states will not be definite who won. (That's because the end of game state was not a full board.)
For example:
You can't know who won.
You can't know even if you know who had the first move.

You must determine the win/loss/tie based on the board state after each move!
Please see my comment to the question. This is already a solution, but I'm not sure you can understand it, taking into account your very confused design attempt.

You never need need a sequence of 0.. 1 numbers at all. You need either 1, for one player, or 0, for another one, never a sequence, without any randomization. Who supplies the data for next move, depends on your mode: human against human, machine against machine and human against machine.

What can be random is the coordinate of next move: x: 0.. 2 and y: 0.. 2, for classical 3x3 configuration. You should generate (in certain mode) this number as random, in this range, but then you should checkup if the cell is already busy and try again if it is.

However, all this random play is boring and makes no sense. Your machine player can play purely rationally, using the best algorithm, which is trivial and is always winning for the first-move player. This algorithm is easily figured out by any adult player having brains. You can do it, too.

There is another approach: self-learning algorithm. The machine is player randomly at first, and then, taking into account the level of each failure/success, gives "bad" path low priority, finally deriving perfect algorithm. The solution for such self-learning was described by American popular science writer Martin Gardner, many, many years ago; I learned when I was a school child.

Share this answer
Matt T Heffron 30-Mar-16 17:26pm    
Good points. (+5)
I interpreted the question as if OP was generating 9 random values, each 0 or 1 representing an X or O in a "cell". (Perhaps to generate input for a learning algorithm... albeit, not very useful input!) But the problem was that the relative counts of X and O was not balanced (5 to 4): the only valid states for "played" games.
(This is clearly wrong as it accounts for "full" boards only, and many of these board states would be impossible since the game would have ended before filling the board.)
Sergey Alexandrovich Kryukov 30-Mar-16 21:01pm    
Thank you, Matt.
I think your speculations on the inquirer's thinking were correct, but that thinking was counter-productive from the very beginning, pretty weird for such an obvious problem; algorithmically, for a middle school... It was also weird to expect the random sequence to yield "balanced" output. :-)
The code produces random numbers, either a 0 or 1. 0 is assigned to "O" and 1 is assigned to "X".
Don't choose randomly if a cell is "X" or "O", respect the rules.

The rules are that you place "X" and "O" alternatively on the board until there is a winner or the board is full.
So you choose randomly where you place the next "X" or "O" as long as the position is free. And when you get a winner, you stop filling the board.
This way, you ensure all games are valid.

When you draw a position already occupied, draw again or choose the next free position.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900