Click to See Complete Forum and Search --> : Newbie - looping AI in TicTacToe
SteveCRM
Feb 9th, 2001, 09:38 PM
Why would you use a loop? You mean like
dim active as boolean
while active =true
'some AI stuff here
loop
something like that?
kedaman
Feb 10th, 2001, 05:51 AM
You could go with a four step AI
Task 1: Check if there are empty squares which causes user to win.
Task 2: Check if there are empty squares which causes comp to win
Task 3: Check if there are empty squares which causes user to win in next round whatever comp does.
Task 4: Check if there are empty squares which causes comp to win in next round whatever user does.
predefine the eight possible win-lines, having an array of 8X3 for each square coordinate winlineX(7,2) winlineY(7,2) which are pointers to the gamefield arena 3X3 Gamefield(2,2). Go systematically trough these eight lines and look for 2 possibilities, sum of (X)'s is 2 and sum of (0) are 0 user threatens comp, sum of (0)'s is 2 and sum of (X) is 0, comp threatens user. Now if youre smart enough you could solve task 3 and 4 with a little help of recursion.
amesjustin
Feb 10th, 2001, 11:25 AM
Wow - that just went right over my head - I am pretty new at this. Could you show me an example that i could deconstruct and figure out?
kedaman
Feb 10th, 2001, 11:42 AM
Are you sure you have to do this to get Jr programmer position? If you want to create a simple game that is, don't even think about artificial intelligence.
amesjustin
Feb 10th, 2001, 11:54 AM
I think he just wants to see what my capabilities are. Even if it does not work very well. He wants to see that I have a very good knowledge on 2 things: If-Then statements and loops. I have already proven the If-Then on a Pay Calculator program I wrote - now he wants a single player TTT game with loops for the AI and I have the job. The 2 books I have don't really explain loops very well and I am having a tough time with them. I know this is sort of fudging it, but I really want to get into programming - and I know if could get my foot in the door I could make up for it after. Any help would be extremely appreciated.
SteveCRM
Feb 10th, 2001, 12:30 PM
you can use a loop to check for a tie...like this:
dim IsBoardFull as boolean 'when true the board is full
IsBoardFull = true
dim i as integer 'used to the loop
for i = 1 to 9 'this will check your array (board)
if board(i) = "" then IsBoardFull = false
next i
if IsBoardFull = true then 'the board is full
P.S. if your boss is trying to see what your capabilities are, I think you should try this on your own.
HarryW
Feb 10th, 2001, 12:45 PM
Okay I'll try to explain what I think Kedaman is suggesting.
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
Now what you do is go through each row (this is a loop) and check each of the cases on Kedaman's list (that could be another loop, or not). So you would pick the best row from this (if a move is an instant win you pick that one, otherwise if there's one to stop your opponent winning pick that, otherwise check cases 3 and 4 as Kedaman said). Based on this you could pick the appropriate row.
You can either keep this separate array up to date with a duplicate of the info in the grid array, by adding a 0 or X to it in the appropriate places every time a move is made, or you can keep the X & Y coordinates of the equivalent square in the grid array, and look up in the grid array based on those coordinates whenever you want to know what's in the lookup array.
I hope that's fairly clear, I can see it might seem confusing from the way i've written it.
Sastraxi
Feb 10th, 2001, 12:49 PM
I was going to say something like that, but HarryW beat me to it. But, he probably explained it better than I would have. I suggest doing a loop where it starts when you take your move. I suggest also to have 2 or 3 difficulty levels (if you want) which would do a better job than the last (easy, medium, difficult, maybe even impossible) (impossible would always make the right move, so the only way to beat him would be to set him up.
amesjustin
Feb 10th, 2001, 12:53 PM
Quite helpfull, guys!
I really appreciate it!
amesjustin
Feb 15th, 2001, 12:05 PM
I am almost done with this - just need to finish the End Game win-check loop. I did end up using 3 difficulty levels:
amesjustin
Feb 16th, 2001, 04:59 PM
Please let me know if you can help me condense some of the massive code via loops...
amesjustin
Feb 19th, 2001, 06:46 PM
...
SteveCRM
Feb 19th, 2001, 08:18 PM
Why so much code? Loops should cut down on that a lot! And I'd use a control array instead of seperate controls.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.