Click to See Complete Forum and Search --> : Looping through a control array - DEAR GOD HELP ME!!!!
amesjustin
Feb 28th, 2001, 11:37 PM
Hey guys,
Yes this is related to my single player TTT game that still tortures me. OK - I have 9 labels in a control array like this:
0 1 2
3 4 5
6 7 8
I already have the code in place so that whenever you click on a label, it checks to see if it is available or not. What I need now is a loop or 3 to determine what move the computer is going to make next.
I already have the basics planned out - there are 3 difficulty levels:
Easy -
1. AI trys for the center square first - lblMove(4) becuase it is in 4 of the 8 possible Win lines (012, 345, 678, 036, 147, 258, 048, & 246).
2. Then it randomly tries for on of the 4 corners (0, 2, 6, 8) - they have 3 possible win lines.
3. Finally it randomly chooses one of the four remaing squares (1, 3, 5, 7).
Medium -
AI looks for one of the 24 possible eminent human player wins along the 8 win lines (12x, 69x, 57x, etc) and attempts to block it. If there are no eminent human wins, the AI moves on to the Easy level moves.
Hard -
AI looks for one of the 24 possible eminent Computer player wins along the 8 win lines and attempts to win. If there are no eminent computer wins, the AI moves on to the Medium level moves.
I have attached my current form. The game is basically complete except for the AI moves and the wincheck loop. I already know how to approach that, I just need to get this other part in first.
please please PLEASE help me if you think you can!
Thank you!!!
-Justin
HarryW
Mar 1st, 2001, 12:24 AM
Did you decide against the approach suggested before?
amesjustin
Mar 1st, 2001, 12:26 AM
Not yours - I have some questions about it though. Check the other thread. Thanx!
-Justin
HarryW
Mar 1st, 2001, 12:48 AM
Yes, I just found it (oops) :)
HarryW
Mar 1st, 2001, 12:49 AM
I hardly ever check the general VB forum any more by the way (only when I'm really bored), but I check games & graphics a lot, so it might be better if we continue it here.
plenderj
Mar 1st, 2001, 06:17 AM
WeHaveAWinnder() Function.
Note: Set the caption of the boxes to "", not " ".
Private Function WeHaveAWinner() As Boolean
Dim i As Long
For i = 0 To 3
Select Case i
Case 0:
If ((lblMove(0).Caption <> "") And (lblMove(1).Caption <> "") And (lblMove(2).Caption <> "")) Then If (lblMove(0).Caption = lblMove(1).Caption) And (lblMove(1).Caption = lblMove(2).Caption) Then WeHaveAWinner = True
If ((lblMove(0).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(8).Caption <> "")) Then If (lblMove(0).Caption = lblMove(4).Caption) And (lblMove(4).Caption = lblMove(8).Caption) Then WeHaveAWinner = True
If ((lblMove(0).Caption <> "") And (lblMove(3).Caption <> "") And (lblMove(6).Caption <> "")) Then If (lblMove(0).Caption = lblMove(3).Caption) And (lblMove(3).Caption = lblMove(6).Caption) Then WeHaveAWinner = True
Case 1:
If ((lblMove(1).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(7).Caption <> "")) Then If (lblMove(1).Caption = lblMove(4).Caption) And (lblMove(4).Caption = lblMove(7).Caption) Then WeHaveAWinner = True
Case 2:
If ((lblMove(2).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(6).Caption <> "")) Then If (lblMove(2).Caption = lblMove(4).Caption) And (lblMove(6).Caption = lblMove(2).Caption) Then WeHaveAWinner = True
Case 3:
If ((lblMove(3).Caption <> "") And (lblMove(4).Caption <> "") And (lblMove(5).Caption <> "")) Then If (lblMove(3).Caption = lblMove(4).Caption) And (lblMove(4).Caption = lblMove(5).Caption) Then WeHaveAWinner = True
End Select
Next i
End Function
- jamie
amesjustin
Mar 1st, 2001, 12:52 PM
Thanks for the reply, jamie, but that piece of code I already have written (Similar to yours). :)
What I was looking for was a loop that would determine what moves the computer player would make in response to the Human players moves.
Thanks,
-Justin
amesjustin
Mar 1st, 2001, 06:06 PM
Harry,
I cannot get these 2 peices of code to work - I get an expected end of statement error:
code:--------------------------------------------------------------------------------Dim LookupArray(1 To 3)(1 To 8) As Coord2D--------------------------------------------------------------------------------
code:--------------------------------------------------------------------------------Dim GridArray(1 To 3)(1 To 3) As String*1--------------------------------------------------------------------------------
I also am having a hard time following the logic structure of this code:
code:--------------------------------------------------------------------------------LookupArray(1, 1).X = 1
LookupArray(1, 1).Y = 1
LookupArray(2, 1).X = 1
LookupArray(2, 1).Y = 2
LookupArray(3, 1).X = 1
LookupArray(3, 1).Y = 3
--------------------------------------------------------------------------------
If you or anyone else can shed a little light on this, I would be VERY appreciative!!!
-Justin
HarryW
Mar 1st, 2001, 07:54 PM
Okay well if you remember this is what I said:
You have your board, a 3x3 array like this
_ _ _
|_|_|_|
|_|_|_|
|_|_|_|
If you number them 1 to 9 like this:
123
456
789
then you have the 8 possible winning lines of 3 here in this list:
123,
456,
789,
147,
258,
369,
159,
357.
So what you do is you create a second array to represent the list you have, so that each row in the array represents a winning line. This array will be 8 rows by 3 columns. Call this the lookup array.
When you add a 0 or X to the 3x3 grid array, you would add that same 0 or X to each of the entries In the lookup array. So, say you had this grid:
0|_|_
0|X|_
X| |0
Your lookup array would be like this:
0 _ _
0 X _
X _ 0
0 0 X
_ X _
_ _ 0
0 X 0
_ X X
You have to fill in your lookup array with the coordinates of the grid squares that are indicated by the numbers on this grid:
123
456
789
147
258
369
159
357
where your grid array looks like this:
123
456
789
The code I gave you was just setting up the coordinates in the lookup array so that they point to the right square. I'm not sure I can explain it any better than that. It's a fairly simple idea, perhaps it's difficult to get it across in text. Remember, this code is executed one time only, at startup. It's not working out the next move, it's creating a resource for you to use in your main game code, to make life easier.
I don't know why those array declarations aren't working :( Maybe it's because of where you're putting them. Maybe try ReDim instead of Dim?
Sastraxi
Mar 1st, 2001, 09:25 PM
Dim A(1 to 8,1 to 3) as variant
THAT is the format, not
Dim A(1 to 8)(1 to 3) as variant
HarryW
Mar 1st, 2001, 09:28 PM
Ohhh yeah, sorry. I'm used to C++ syntax, I got confused :rolleyes:
Sastraxi
Mar 1st, 2001, 09:29 PM
Sub FillLookUpArray()
... 'code here, such as
LUA(1,1)=GridArray(1,1)
LUA(2,1)=GridArray(2,1)
LUA(3,1)=GridArray(3,1)
LUA(1,2)=GridArray(1,2)
LUA(2,2)=GridArray(2,2)
LUA(3,2)=GridArray(3,2)
... etc, following HarryW's guidelines.
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.