Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem
Tic-Tac-Toe Problem Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Go Back  Xtreme Visual Basic Talk > > > Tic-Tac-Toe Problem


Reply
 
Thread Tools Display Modes
  #1  
Old 01-21-2006, 07:03 PM
1337php 1337php is offline
Newcomer
 
Join Date: Jan 2006
Posts: 4
Default Tic-Tac-Toe Problem


Hello I am a noob to VB and I am trying to create a Tic-Tac-Toe program I did create it sucessfully but now I am trying to let someone play against the computer rather than someone else. I am just using a randomization function to pick one number from 1 to 9 and I want to place an "O" in that corresponding location. How would I do that? Here's some code to help explain what i'm trying to do.
Code:
Label[Variable to random number here].Text = "O"
Thank you.

Last edited by 1337php; 01-21-2006 at 07:11 PM.
Reply With Quote
  #2  
Old 01-21-2006, 07:18 PM
Timbuk2 Timbuk2 is offline
Regular
 
Join Date: Nov 2005
Location: Ireland
Posts: 81
Default

Welcome to the Forums!!

This would be easy enough to do

First create a control array of labels (It is nearly best to use 9 labels than anything else)

We'll call them "square" in this example

And then when someone clicks on any of the nine squares, the same code will happen for each
Code:
Private Sub square_Click(Index As Integer) Dim randomNumber As Integer, freeSquare As Boolean square(Index).Caption = "X" 'presuming player is X Randomize() randomNumber = Int((Rnd * 9) + 1) 'keep looping until computer finds free square Do While freeSquare = False If square(randomNumber).Caption = "" Then 'Not already used square(randomNumber).Caption = "O" 'presuming computer is O freeSquare = True End If Loop

That should do it
Reply With Quote
  #3  
Old 01-21-2006, 07:49 PM
1337php 1337php is offline
Newcomer
 
Join Date: Jan 2006
Posts: 4
Default

Thanks for your reply!

How would I create the control array for this to work?

Thanks
Reply With Quote
  #4  
Old 01-21-2006, 07:54 PM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

To create a control array in the form designer, copy-past the control. When you do this, it will ask you "... Do you want to create a control array?", say yes. Now you have multiple items with the same name, the only thing that differenciates them from one another is their index number which you can use to access a specific control in the control array.
Reply With Quote
  #5  
Old 01-21-2006, 08:00 PM
1337php 1337php is offline
Newcomer
 
Join Date: Jan 2006
Posts: 4
Default

This doesn't work for me it may be I am running 2005 express. Please explain what I need to do in different words. This is confusing me.

Thank you.
Reply With Quote
  #6  
Old 01-21-2006, 08:02 PM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

Oh, well .Net is a horse of an entirely different color.
[.Net questions better asked in the .Net section]


See here for how to make/use control arrays in .net,
Control Arrays in .NET
Reply With Quote
  #7  
Old 01-21-2006, 08:15 PM
1337php 1337php is offline
Newcomer
 
Join Date: Jan 2006
Posts: 4
Default

this method is confusing to me is there any other way i can do it?

thanks
Reply With Quote
  #8  
Old 01-21-2006, 08:41 PM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

Another way to accomplish your goal of haveing the computer choose a random a Tic-Tac-Toe move? Or another way to do control arrays?

There is another way I can think of to randomly generate a number for the computer to move, but it isnt as pretty as the control array method which makes it much nicer.

you could generate a random number, then check the number is a series of if...then statements and find the corresponding control. For example, generate a number from 1 to 9 and if the number equals 2, then hard code lbl2.Text = "X". Understand what I mean? Basically just use a bunch of if then statements.

But I do encourage you to go back and try to learn how to use control arrays, it is a much better way to do it. I am sure there are lots of examples of how to use them, especially in a tic-tac-toe context, to be found.
Reply With Quote
  #9  
Old 01-22-2006, 05:31 AM
Iceplug's Avatar
IceplugTic-Tac-Toe Problem Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Quote:
Originally Posted by 1337php
this method is confusing to me is there any other way i can do it?

thanks
Other ways would be equally as confusing. The only method that might be easier is to hardcode all of the labels to respond to a certain number:

Code:
Select Case Number
  Case 1
    Label1.Text = "Stuff"
  Case 2
    Label2.Text = "Stuff"
of course, that would be a pain to try to maintain. Control arrays are very easy and if they are confusing you, I'd recommend actually trying them out before knocking them as confusing.
You cannot expect programming to not be confusing.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


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
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
 
Tic-Tac-Toe Problem
Tic-Tac-Toe Problem
 
-->