Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Game Programming > Tic-Tac-Toe Game


Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2012, 10:16 AM
Hysteria73 Hysteria73 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 2
Exclamation 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.
Reply With Quote
  #2  
Old 06-12-2012, 11:20 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

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.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 06-12-2012, 11:28 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

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.
Reply With Quote
  #4  
Old 06-12-2012, 05:27 PM
Hysteria73 Hysteria73 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 2
Default

Quote:
Originally Posted by passel View Post
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.
Reply With Quote
  #5  
Old 06-12-2012, 06:57 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default 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.
Reply With Quote
Reply

Tags
game, tic tac toe, tic-tac-toe, x's and o's


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->