# How do A.I. s work generally.....

## Recommended Posts

How do game A.I. s work generally? Not big 3d games or anything but I thought something a little simpler like chess, ckeckers, or as simple as tic tac toe.

I guess I'm asking "How does the program know what move to make next in response to your move?".

I've made a few little scripts here and there that do the most basic things like move the mouse here click the mouse there and other stuff BUT I would like to go beyond that and understand a little more about how things like that work.

Heck, I could write something for playing tic tac toe but right now all I could do is list all the possible moves and then program it accordingly but I thought that there obviously, someone better qualified than me, would be an easier approach to it than that.

Any insight would be appreciated. I have looked at many posts on this forum and a lot of it is a bit overwhelming and haven't found a satisfactory answer so if I missed the post on "A.I. 101" then please direct me to it. lol.

This is my first post here and I hope that this is a worthy question on scripting.

I'm still pretty new to this autoit and I find it to be a great little program.

##### Share on other sites

well tic tac toe would probably be the easiest

and simple ai would be to have it check if the next move would allow u to win wiht your next move. If that is not clear i can explain it aggian. But most ai have an algorithm that anyalizes the possible choices and pick the best.

##### Share on other sites

Algorithm huh. I thougth thats what it might take but my math is a lliiiiitttttlllle rusty when it comes to algorithms. I didn't know if one had to program in all the combinations of moves or that an algorithm would do it. Frankly, I've read about them and I still have a hard time wrapping my brain around them. I could understand a formula eventually. but without knowing exactly what its for and does each step of the way I would be lost.

##### Share on other sites

dont think of algorithms as math but a set of instructions and logic. I would help it seems like a pretty interesting project. To start out i would make a gui and have each input box as a 2d array. you can look at my sudoku solver for examples of algorithms.

##### Share on other sites

thanks I'll take a look at it right now.

##### Share on other sites

like this sounds like a good idea and i would be glad to help.

##### Share on other sites

I don't know anything about sudoku I'm sorry to say. I was never really interested in it.

I took a look at various bits of code and its safe to say know absolutely nothing about arrays. I have looked in the help section and various arrays people have put on the forum but for some reason it eludes me. I just can't seem to understand arrays and how they work especially the way everthing has to be written. As I've said to my friend "It's like people just pull stuff out of the air and it works." I guess I'll have to figure out how those things work somehow. I guess I need to start out with a super simple array heavily documented on each line so I can follow what is going on until I can work my way up to something more complicated and still be able to follow whats going on.

I'm unable to say what I don't understand about arrays because I work kind of like a computer in that if I have never seen it or used it I can't quantify it to understand i guess.

Back to the drawing board as they say.

##### Share on other sites

yeh looking at someone elses code can be very confusing here is the begining for my tic tac toe player.

```;grid[column][row]  1 = X,      0 = O, 2 = empty

#include <GUIConstants.au3>

\$Form1 = GUICreate("Form1", 633, 447, 193, 125)
GUISetFont(20, 400, 0, "MS Sans Serif")
\$button[1][1] = GUICtrlCreateButton("", 128, 56, 83, 73, 0)
\$button[1][2] = GUICtrlCreateButton("", 128, 144, 83, 73, 0)
\$button[1][3] = GUICtrlCreateButton("", 128, 232, 83, 73, 0)
\$button[2][1] = GUICtrlCreateButton("", 232, 56, 83, 73, 0)
\$button[2][2] = GUICtrlCreateButton("", 232, 144, 83, 73, 0)
\$button[2][3] = GUICtrlCreateButton("", 232, 232, 83, 73, 0)
\$button[3][1] = GUICtrlCreateButton("", 336, 56, 83, 73, 0)
\$button[3][2] = GUICtrlCreateButton("", 336, 144, 83, 73, 0)
\$button[3][3] = GUICtrlCreateButton("", 336, 232, 83, 73, 0)
\$Label1 = GUICtrlCreateLabel("Your Turn...", 224, 344, 138, 36)
GUISetState(@SW_SHOW)

\$grid[1][1] = GUICtrlCreateButton("", 128, 56, 83, 73, 0)
\$grid[1][2] = GUICtrlCreateButton("", 128, 144, 83, 73, 0)
\$grid[1][3] = GUICtrlCreateButton("", 128, 232, 83, 73, 0)
\$grid[2][1] = GUICtrlCreateButton("", 232, 56, 83, 73, 0)
\$grid[2][2] = GUICtrlCreateButton("", 232, 144, 83, 73, 0)
\$grid[2][3] = GUICtrlCreateButton("", 232, 232, 83, 73, 0)
\$grid[3][1] = GUICtrlCreateButton("", 336, 56, 83, 73, 0)
\$grid[3][2] = GUICtrlCreateButton("", 336, 144, 83, 73, 0)
\$grid[3][3] = GUICtrlCreateButton("", 336, 232, 83, 73, 0)

For \$i = 1 to 3;column
For \$j = 1 to 3;row
\$grid[\$i][\$j] = 2
Next
Next

While 1
\$nMsg = GUIGetMsg()
Switch \$nMsg
Case \$GUI_EVENT_CLOSE
Exit
Case \$button [1][1]
_win()
_block ()
EndSwitch
WEnd

Func _win ()

EndFunc

Func _block ()

EndFunc```

If you need me to explain anyhting there just ask. But to check it a can add up the arrays and see if they equal a certain number and if they equal that number then i can have the program do watever it needs to.

Edit i change x to equal 5 so the numbers could not get confused as easily

Edited by sccrstvn93

##### Share on other sites

^that doesn't work

Check out my begining:

```#include <GUIConstants.au3>

GUICreate("Tic Tac Toe", 254, 228)
\$A1 = GUICtrlCreateLabel("", 8, 8, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$A2 = GUICtrlCreateLabel("", 88, 8, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$A3 = GUICtrlCreateLabel("", 168, 8, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$B1 = GUICtrlCreateLabel("", 8, 80, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$B2 = GUICtrlCreateLabel("", 88, 80, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$B3 = GUICtrlCreateLabel("", 168, 80, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$C1 = GUICtrlCreateLabel("", 8, 152, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$C2 = GUICtrlCreateLabel("", 88, 152, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$C3 = GUICtrlCreateLabel("", 168, 152, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
GUISetBkColor(0x000000)
GUISetState(@SW_SHOW)

While 1
\$nMsg = GUIGetMsg()
Switch \$nMsg
Case \$GUI_EVENT_CLOSE
Exit
Case \$A1
GUICtrlSetData(\$A1,"X")
Case \$A2
GUICtrlSetData(\$A2,"X")
Case \$A3
GUICtrlSetData(\$A3,"X")
Case \$B1
GUICtrlSetData(\$B1,"X")
Case \$B2
GUICtrlSetData(\$B2,"X")
Case \$B3
GUICtrlSetData(\$B3,"X")
Case \$C1
GUICtrlSetData(\$C1,"X")
Case \$C2
GUICtrlSetData(\$C2,"X")
Case \$C3
GUICtrlSetData(\$C3,"X")
EndSwitch
WEnd```

Can I help?

Edited by Nahuel

##### Share on other sites

lol yeh its just the beggining i just started writing it. Its just so he can see one way to set it up.

##### Share on other sites

I don't know where to start on the questions. Too many things here to understand. I also forgot to mention I have the same problem understanding functions as well. I think I may have gotten too ambitious trying to understand it. I have much more to learn about algorithms, arrays, functions and about 90% of the other things autoit can do. To me, most of it is like stuff just pulled out of the air.

I'm sorry but I'll have to do more work learning what things do before I can proceed.

As I said I could list all the combinations with some time but that is only way I would know how to do it right now. "If this then do that" kind of stuff.

##### Share on other sites

well that is pretty much wat an algorithm is. The tricky part is turning that into code ideally one that works in a loop so you dont have to type all of the possibilities.

##### Share on other sites

Another way is to assign marks for different situations:

e.g.

for 1 in a row the mark would be 1

.x.

...

...

for 2 in a row the mark would be 2

x.x

...

...

3 in a row is win so it will have the highest mark 3

The script will always try to get the highest mark - will try to put 2 in a row instead of putting 2x 1 in a row

x.x

...

...

is better than

x..

..x

...

Also another thing to be considered is that some situations can be considered "mirrored" - you can include them in the same case:

.x.

..x

...

can be considered the same as

...

x..

.x.

or

...

..x

.x.

or

.x.

x..

...

You have to look for patterns and to act accordingly.

It is a nice project and it will need alot of work (too bad I don't have that spare time).

Good luck,

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

##### Share on other sites

A lot of work I could do but understanding is quite another. LOL

I think I'll go back to the simpler things and work from there. I leaped before I looked on this one I believe.

I can say one thing. I do wish the help section in autoit was a little easier to understand for those of us with learning difficulties. Some examples are a little to complicated. I wish the developers could start with like

example #1 = an idiot could understand

part of example #1 but more complicated on to example #2 = a slight idiot could understand

part of example #2 but more complicated on to example #3 = average intelligence can understand

and so on ...............................and so on ...............................and so on ...............................

Some things like arrays and functions I think I fall into the "Example#000 = total moron" category sometimes

##### Share on other sites

The examples that AutoIt comes with are really helpful and very basic. I learned a lot from them.

If there's anything you don't understand, just ask! And play with it, you learn a lot from the mistakes you make.

##### Share on other sites

i just saw your edit here is wat i ahve so far.

```#include <GUIConstants.au3>

Global \$turn = True;true means player false means ai
Global \$grid[4][4]
Global \$button[4][4]

\$Form1 = GUICreate("Form1", 633, 447, 193, 125)
GUISetFont(20, 400, 0, "MS Sans Serif")
\$button[1][1] = GUICtrlCreateButton("", 128, 56, 83, 73, 0)
\$button[2][1] = GUICtrlCreateButton("", 128, 144, 83, 73, 0)
\$button[3][1] = GUICtrlCreateButton("", 128, 232, 83, 73, 0)
\$button[1][2] = GUICtrlCreateButton("", 232, 56, 83, 73, 0)
\$button[2][2] = GUICtrlCreateButton("", 232, 144, 83, 73, 0)
\$button[3][2] = GUICtrlCreateButton("", 232, 232, 83, 73, 0)
\$button[1][3] = GUICtrlCreateButton("", 336, 56, 83, 73, 0)
\$button[2][3] = GUICtrlCreateButton("", 336, 144, 83, 73, 0)
\$button[3][3] = GUICtrlCreateButton("", 336, 232, 83, 73, 0)
\$Label1 = GUICtrlCreateLabel("Your Turn...", 224, 344, 138, 36)
GUISetState(@SW_SHOW)

For \$i = 1 to 3;column
For \$j = 1 to 3;row
\$grid[\$i][\$j] = 2
Next
Next

While 1
\$nMsg = GUIGetMsg()
Switch \$nMsg
Case \$GUI_EVENT_CLOSE
Exit
Case \$button [1][1]
_clicked (1,1)
If _win () = 1 then _randompick ()
Case \$button[1][2]
_clicked (1,2)
If _win () = 1 then _randompick ()
Case \$button [1][3]
_clicked (1,3)
If _win () = 1 then _randompick ()
Case \$button [2][1]
_clicked (2,1)
If _win () = 1 then _randompick ()
Case \$button[2][2]
_clicked (2,2)
If _win () = 1 then _randompick ()
Case \$button [2][3]
_clicked (2, 3)
If _win () = 1 then _randompick ()
Case \$button [3][1]
_clicked (3,1)
If _win () = 1 then _randompick ()
Case \$button[3][2]
_clicked (3,2)
If _win () = 1 then _randompick ()
Case \$button [3][3]
_clicked (3,3)
If _win () = 1 then _randompick ()
EndSwitch
WEnd

Func _win ()
local \$total
For \$r = 1 to 3
For \$c = 1 to 3
\$total = \$grid[1][\$c] + \$grid[2][\$c] + \$grid[3][\$c];see total down
If \$total = 2 Then;only one empty other are O
For \$i = 1 to 3;find empty
If \$grid[\$i][\$c] = 2 Then
_clicked (\$i, \$c)
MsgBox (0, "game over", "")
Return 2
ExitLoop
EndIf
Next
EndIf

\$total = \$grid[\$r][1] + \$grid[\$r][2] + \$grid[\$r][3];see total down
If \$total = 2 Then;only one empty other are O
For \$i = 1 to 3;find empty
If \$grid[\$r][\$i] = 2 Then
_clicked (\$r, \$i)
MsgBox (0, "game over", "")
Return 2
ExitLoop
EndIf
Next
EndIf

Next
Next
Return 1
EndFunc

Func _block ()

EndFunc

Func _randompick ()
While 1
\$row = Random (1, 3)
\$column = Random (1, 3)
If \$grid[\$row][\$column] = 2 Then
_clicked (\$row, \$column)
ExitLoop
EndIf
WEnd
EndFunc

Func _clicked (\$row, \$column)
If \$turn = true Then
GUICtrlSetData (\$button[\$row][\$column], "X")
GUICtrlSetState (\$button[\$row][\$column], \$gui_disable)
GUICtrlSetData (\$Label1, "Thinking...")
\$grid[\$row][\$column] = 5
\$turn = False
Return 1
EndIf

If \$turn = False Then
GUICtrlSetData (\$button[\$row][\$column], "O")
GUICtrlSetState (\$button[\$row][\$column], \$gui_disable)
\$grid[\$row][\$column] = 0
\$turn = True
EndIf
EndFunc```

There is an error somewhere with clicked but i cant find it cause i dont have time. feel free to do watever.

Edit: i found th error and fixed it. It can win if u let it, but only across and down.

Edited by sccrstvn93

##### Share on other sites

The Error is This Line:

38: _clicked (1,1)

ERROR: C:\Documents and Settings\Jar\Desktop\test.au3(126,30) : ERROR: _clicked() called by a previous line with 0 arg(s). Min = 2. First previous line calling this Func is 38.

Func _clicked (\$row, \$column)

##### Share on other sites

yeh that was the error i was talking about but i jsut fixed it.

##### Share on other sites

COOL! Thats a really great, usefull start!

##### Share on other sites

^Awesome!

I was playing and making it choose random squares, but this doesn't work. The problem seems to be with GuiCtrlGetState()

```#include <GUIConstants.au3>
#include <Constants.au3>

GUICreate("Tic Tac Toe", 254, 228)
\$A1 = GUICtrlCreateLabel("", 8, 8, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$A2 = GUICtrlCreateLabel("", 88, 8, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$A3 = GUICtrlCreateLabel("", 168, 8, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$B1 = GUICtrlCreateLabel("", 8, 80, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$B2 = GUICtrlCreateLabel("", 88, 80, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$B3 = GUICtrlCreateLabel("", 168, 80, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$C1 = GUICtrlCreateLabel("", 8, 152, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$C2 = GUICtrlCreateLabel("", 88, 152, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
\$C3 = GUICtrlCreateLabel("", 168, 152, 76, 65, BitOR(\$SS_CENTER,\$SS_SUNKEN))
GUIctrlsetBkColor(-1,0xFFFFFF)
GUICtrlSetFont(-1,40)
GUISetBkColor(0x000000)
GUISetState(@SW_SHOW)

While 1
\$nMsg = GUIGetMsg()
Switch \$nMsg
Case \$GUI_EVENT_CLOSE
Exit
Case \$A1
jugar(\$A1)
Case \$A2
jugar(\$A2)
Case \$A3
jugar(\$A3)
Case \$B1
jugar(\$B1)
Case \$B2
jugar(\$B2)
Case \$B3
jugar(\$B3)
Case \$C1
jugar(\$C1)
Case \$C2
jugar(\$C2)
Case \$C3
jugar(\$C3)
EndSwitch
WEnd

Func jugar(\$oCasi)
GUICtrlSetData(\$oCasi,"X")
GUICtrlSetState(\$oCasi,\$GUI_DISABLE)
while 1
\$oRan=Random(1,9,1)

If \$oRan=1 Then \$iCas=\$A1
If \$oRan=2 Then \$iCas=\$A2
If \$oRan=3 Then \$iCas=\$A3
If \$oRan=4 Then \$iCas=\$B1
If \$oRan=5 Then \$iCas=\$B2
If \$oRan=6 Then \$iCas=\$B3
If \$oRan=7 Then \$iCas=\$C1
If \$oRan=8 Then \$iCas=\$C2
If \$oRan=9 Then \$iCas=\$C3

If GUICtrlGetState(\$iCas) = \$GUI_ENABLE Then
ExitLoop
EndIf
WEnd

GUICtrlSetData(\$iCas,"O")

EndFunc```

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...