 |

06-12-2012, 10:16 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 2
|
|
Tic-Tac-Toe Game
|
Hi, I'm trying to create a Tic-Tac-Toe game as a final project for my grade 11 programming class. I'm very close to being finished but I'm having an issue with the RND function and I'm running out of time.
I have a game board (lines placed in a hash pattern) and the tiles that you would place your letter on (X or O) are labels. I've rigged it so when you click a label, the caption turns into "X", and then the computer takes its turn via this basic code:
Randomize Timer
x = Int(Rnd * 8) + 1
There are 9 labels in an array, lblXO(0 to 8). I have tried many "If"s and "Do While"s but to no avail. I need a way to randomize a DIFFERENT number 9 times. So if the randomizer chose 5, it won't choose 5 again until the board has been reset (the computer has been replacing "O"s with "O"s and on occaision it will replace your "X"s with "O"s, please help me out. I have no idea what to do and I've been stuck at this point for nearlt 4 days now.
|
|

06-12-2012, 11:20 AM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
|
|
The only reliable way to do so is to create a list with all of the items, shuffle it, then deal the items one by one off the top. For example, you'd make the list { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, shuffle it to { 9, 1, 2, 4, 3, 8, 7, 5, 6 }, then iterate over that shuffled list. There's a discussion in this post from a bigger thread and a few others about shuffling cards.
|
|

06-12-2012, 11:28 AM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
|
Search for card sorting or card dealing. {edit: AtmaWeapon slipped in while I was posting}
In your case, rather than shuffle and choose randomly, perhaps a simple array of booleans would suffice.
Initially you have 9 spots available, so you set a variable to the number available and use that in your Rnd selection.
Each time a play is done, you decrement that number by 1.
When the player moves, say they pick label 4, then you would set the boolean in array index 4 to false, saying it is not available and decrement the number available by 1.
When the computer chooses a random value, say 4, then you would start at the beginning of the boolean array, and count the ones that are true (so the 4th one would be skipped since the player has already chosen that one). When you reach the 4th available you would set the boolean array entry false, mark the label entry with the computer's mark (X or 0) and decrement the number available by 1.
That way, as you play the game you pick a smaller and smaller random number depending on the number of available slots, and you just quickly look for the random Nth slot and mark it.
The reason I suggest an array of booleans is, it makes it easier to remove the player's moves from the available list of valid moves if the list is kept sorted and a random item is picked from the sorted list, rather than shuffling and then having to search the list to find the player's move and remove it from the shuffled list.
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

06-12-2012, 05:27 PM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 2
|
|
Quote:
Originally Posted by passel
Search for card sorting or card dealing. {edit: AtmaWeapon slipped in while I was posting}
In your case, rather than shuffle and choose randomly, perhaps a simple array of booleans would suffice.
Initially you have 9 spots available, so you set a variable to the number available and use that in your Rnd selection.
Each time a play is done, you decrement that number by 1.
When the player moves, say they pick label 4, then you would set the boolean in array index 4 to false, saying it is not available and decrement the number available by 1.
When the computer chooses a random value, say 4, then you would start at the beginning of the boolean array, and count the ones that are true (so the 4th one would be skipped since the player has already chosen that one). When you reach the 4th available you would set the boolean array entry false, mark the label entry with the computer's mark (X or 0) and decrement the number available by 1.
That way, as you play the game you pick a smaller and smaller random number depending on the number of available slots, and you just quickly look for the random Nth slot and mark it.
The reason I suggest an array of booleans is, it makes it easier to remove the player's moves from the available list of valid moves if the list is kept sorted and a random item is picked from the sorted list, rather than shuffling and then having to search the list to find the player's move and remove it from the shuffled list.
|
This is a good idea, I forgot booleans existed...
I think I understand what your saying, but for a beginner's sake, could you provide little snippets of code to prove as examples? Don't give me all of the code, I like to learn, not plagiarize. Just kind of elaborate your ideas please
What would the array of booleans look like in code? That part kind of threw me off (again, beginner).
How would I decrement the number by 1? Is the number I'm decrementing a variable or is it the index of the boolean array?
Thanks again.
|
|

06-12-2012, 06:57 PM
|
|
Contributor
|
|
Join Date: Oct 2009
Posts: 719
|
|
TicTacToe coding - another approach..
You can also use arrays to keep track of where
the X's and O's (crosses and naughts) have been placed.
Here is a little mini-tutorial, and the source code is available on this page.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|