The flow of the program was easy enough to figure out - I've done it in the past in other languages for other classes.

- Create a form with 9 Labels on it, a button to generate a new game, and an exit button.
- Create an 2D array to hold either 0s or 1s (0 = Os, 1 = Xs).
- Loop through the array, then
- Generate a 0 or 1.
- Loop through the Label controls.
- Set the Text property of the current label to either "X" or "O", based on the generated number.
- Detect a winner:
- Search through the array for possible victories for Xs and Os:
- Top Row: Array locations [1,1], [1,2], and [1,3] are the same = Win.
- Middle Row: Array locations [2,1], [2,2], and [2,3] are the same = Win.
- Bottom Row: Array locations [3,1], [3,2], and [3,3] are the same = Win.
- Left Col: Array locations [1,1], [2,1], and [3,1] are the same = Win.
- Middle Col: Array locations [1,2], [2,2], and [3,2] are the same = Win.
- Right Col: Array locations [1,3], [2,3], and [3,3] are the same = Win.
- Left-Right Diag: Array locations [1,1], [2,2], and [3,3] are the same = Win.
- Right-Left Diag: Array locations [1,3], [2,2], and [3,1] are the same = Win.
- Let player click "New Game" button to generate the outcome again.

Alas, I ran into a few snags:

- Random numbers weren't generating, regardless of what I did. Rebooting my PC and re-running the program fixed this. Odd...
- In an outcome set of 9 numbers, with 4 zeroes and 5 ones, the labels seemed to all display whatever the first number in the array was.
- Yet, if I displayed each result (referencing the array), it showed the correct values.
- I tested this in a ListBox as well...and sure enough, the random numbers were there.

As an afterthought...I'm almost absolutely certain that this was the problem, as nothing else makes sense.

My problem with the tic tac toe was that the random numbers were generating. So sometimes X would move a disproportional amount of times than O did. The other problem was when they both won, since I set X wins first in the long if/else statement it would show X as the winner in the event that both had won.

ReplyDeleteDid you create two arrays (one for X and one for O), or one? The book only mentioned one, so I wrote a 2D array to hold all of the moves. I just made a stupid mistake...lol.

ReplyDelete