qazwsx Posted September 25, 2007 Share Posted September 25, 2007 hmm i dunno, i would check excalty wat guigetstate returns. I find it is easier and quickers to use an array to work with instead of checking the gui. Link to comment Share on other sites More sharing options...
Nahuel Posted September 25, 2007 Share Posted September 25, 2007 ^Mmm... that's true... it should be easier. Anyway, here's the 'randomized' version. It can't solve anything yet, but it's funny. expandcollapse popup#include <GUIConstants.au3> #include <Constants.au3> Global $over=0 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) Do $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 Until win()=1 OR win()=2 OR $over=1 If win()=1 Then MsgBox(0,"","You Win!") EndIf If win()=2 Then MsgBox(0,"","You Lose!") EndIf If $over=1 AND win()=0 Then MsgBox(0,"","It's a tie!") EndIf Func jugar($oCasi) GUICtrlSetData($oCasi,"X") GUICtrlSetState($oCasi,$GUI_DISABLE) $a=0 Do $a+=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 GUICtrlRead($iCas)="" Then ExitLoop Until $a=100 If $a<>100 Then GUICtrlSetData($iCas,"O") GUICtrlSetState($iCas,$GUI_DISABLE) Else $over=1 EndIf EndFunc Func win() ;"X" If GUICtrlRead($A1)="X" and GUICtrlRead($A2)="X" and GUICtrlRead($A3)="X" Then Return 1 EndIf If GUICtrlRead($B1)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($B3)="X" Then Return 1 EndIf If GUICtrlRead($C1)="X" and GUICtrlRead($C2)="X" and GUICtrlRead($C3)="X" Then Return 1 EndIf If GUICtrlRead($A1)="X" and GUICtrlRead($B1)="X" and GUICtrlRead($C1)="X" Then Return 1 EndIf If GUICtrlRead($A2)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($C2)="X" Then Return 1 EndIf If GUICtrlRead($A3)="X" and GUICtrlRead($B3)="X" and GUICtrlRead($C3)="X" Then Return 1 EndIf If GUICtrlRead($A1)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($C3)="X" Then Return 1 EndIf If GUICtrlRead($A3)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($C1)="X" Then Return 1 EndIf ;"O" If GUICtrlRead($A1)="O" and GUICtrlRead($A2)="O" and GUICtrlRead($A3)="O" Then Return 2 EndIf If GUICtrlRead($B1)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($B3)="O" Then Return 2 EndIf If GUICtrlRead($C1)="O" and GUICtrlRead($C2)="O" and GUICtrlRead($C3)="O" Then Return 2 EndIf If GUICtrlRead($A1)="O" and GUICtrlRead($B1)="O" and GUICtrlRead($C1)="O" Then Return 2 EndIf If GUICtrlRead($A2)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($C2)="O" Then Return 2 EndIf If GUICtrlRead($A3)="O" and GUICtrlRead($B3)="O" and GUICtrlRead($C3)="O" Then Return 2 EndIf If GUICtrlRead($A1)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($C3)="O" Then Return 2 EndIf If GUICtrlRead($A3)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($C1)="O" Then Return 2 EndIf EndFunc Link to comment Share on other sites More sharing options...
qazwsx Posted September 25, 2007 Share Posted September 25, 2007 (edited) heres my updated version it can do everyhitn but diagnols ill add those tommorow. CODE#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 If _block() = 1 then _randompick () EndIf Case $button[1][2] _clicked (1,2) If _win () = 1 then If _block() = 1 then _randompick () EndIf Case $button [1][3] _clicked (1,3) If _win () = 1 then If _block() = 1 then _randompick () EndIf Case $button [2][1] _clicked (2,1) If _win () = 1 then If _block () = 1 then _randompick () EndIf Case $button[2][2] _clicked (2,2) If _win () = 1 then If _block() = 1 then _randompick () EndIf Case $button [2][3] _clicked (2, 3) If _win () = 1 then If _block() = 1 then _randompick () EndIf Case $button [3][1] _clicked (3,1) If _win () = 1 then If _block() = 1 then _randompick () EndIf Case $button[3][2] _clicked (3,2) If _win () = 1 then If _block () = 1 then _randompick () EndIf Case $button [3][3] _clicked (3,3) If _win () = 1 then If _block () = 1 then _randompick () EndIf 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", "I win") Exit 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 () 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 = 12 Then ;only one empty other are X For $i = 1 to 3 ;find empty If $grid[$i][$c] = 2 Then _clicked ($i, $c) Return 2 ExitLoop EndIf Next EndIf $total = $grid[$r][1] + $grid[$r][2] + $grid[$r][3] ;see total down If $total = 12 Then ;only one empty other are O For $i = 1 to 3 ;find empty If $grid[$r][$i] = 2 Then _clicked ($r, $i) Return 2 ExitLoop EndIf Next EndIf Next Next Return 1 EndFunc Func _randompick () For $row = 1 to 3 For $column = 1 to 3 If $grid[$row][$column] = 2 Then _clicked ($row, $column) Return 1 EndIf Next Next MsgBox (0, "", "Its a tie") 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) GUICtrlSetData ($Label1, "Your Turn...") $grid[$row][$column] = 0 $turn = True EndIf EndFunc Edited September 25, 2007 by sccrstvn93 Link to comment Share on other sites More sharing options...
Nahuel Posted September 25, 2007 Share Posted September 25, 2007 ==> Unknown function name.: _clicked (1,1)You missed it? Link to comment Share on other sites More sharing options...
qazwsx Posted September 25, 2007 Share Posted September 25, 2007 (edited) ohh oops i only copied par tof my code ill fix it now. Edit: ok it should be fixed now Edited September 25, 2007 by sccrstvn93 Link to comment Share on other sites More sharing options...
Nahuel Posted September 25, 2007 Share Posted September 25, 2007 (edited) Look, I made it! it actually beats me sometimes. expandcollapse popup#include <GUIConstants.au3> #NoTrayIcon Global $over=0 Global $win=0 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) Randomize() While 1 clear() Do $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 win() Until $win=1 OR $win=2 OR $over=1 If $win=1 Then MsgBox(0,"","You won!") EndIf If $win=2 Then MsgBox(0,"","You lost!") EndIf If $win=0 AND $over=1 Then MsgBox(0,"","It's a tie!") EndIf WEnd Func jugar($oCasi) GUICtrlSetData($oCasi,"X") GUICtrlSetState($oCasi,$GUI_DISABLE) If GUICtrlRead($B2)="" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf ;TRATAR DE GANAR ;Horizontal= If GUICtrlRead($A1)="O" AND GUICtrlRead($A2)="O" AND GUICtrlRead($A3)="" Then GUICtrlSetData($A3,"O") GUICtrlSetState($A3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="" AND GUICtrlRead($A2)="O" AND GUICtrlRead($A3)="O" Then GUICtrlSetData($A1,"O") GUICtrlSetState($A1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="O" AND GUICtrlRead($A2)="" AND GUICtrlRead($A3)="O" Then GUICtrlSetData($A2,"O") GUICtrlSetState($A2,$GUI_DISABLE) Return EndIf If GUICtrlRead($B1)="O" AND GUICtrlRead($B2)="O" AND GUICtrlRead($B3)="" Then GUICtrlSetData($B3,"O") GUICtrlSetState($B3,$GUI_DISABLE) Return EndIf If GUICtrlRead($B1)="" AND GUICtrlRead($B2)="O" AND GUICtrlRead($B3)="O" Then GUICtrlSetData($B1,"O") GUICtrlSetState($B1,$GUI_DISABLE) Return EndIf If GUICtrlRead($B1)="O" AND GUICtrlRead($B2)="" AND GUICtrlRead($B3)="O" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($C1)="O" AND GUICtrlRead($C2)="O" AND GUICtrlRead($C3)="" Then GUICtrlSetData($C3,"O") GUICtrlSetState($C3,$GUI_DISABLE) Return EndIf If GUICtrlRead($C1)="" AND GUICtrlRead($C2)="O" AND GUICtrlRead($C3)="O" Then GUICtrlSetData($C1,"O") GUICtrlSetState($C1,$GUI_DISABLE) Return EndIf If GUICtrlRead($C1)="O" AND GUICtrlRead($C2)="" AND GUICtrlRead($C3)="O" Then GUICtrlSetData($C2,"O") GUICtrlSetState($C2,$GUI_DISABLE) Return EndIf ;======== ;Vertical= If GUICtrlRead($A1)="O" AND GUICtrlRead($B1)="" AND GUICtrlRead($C1)="O" Then GUICtrlSetData($B1,"O") GUICtrlSetState($B1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="" AND GUICtrlRead($B1)="O" AND GUICtrlRead($C1)="O" Then GUICtrlSetData($A1,"O") GUICtrlSetState($A1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="O" AND GUICtrlRead($B1)="O" AND GUICtrlRead($C1)="" Then GUICtrlSetData($C1,"O") GUICtrlSetState($C1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A2)="O" AND GUICtrlRead($B2)="" AND GUICtrlRead($C2)="O" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A2)="" AND GUICtrlRead($B2)="O" AND GUICtrlRead($C2)="O" Then GUICtrlSetData($A2,"O") GUICtrlSetState($A2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A2)="O" AND GUICtrlRead($B2)="O" AND GUICtrlRead($C2)="" Then GUICtrlSetData($C2,"O") GUICtrlSetState($C2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="O" AND GUICtrlRead($B3)="" AND GUICtrlRead($C3)="O" Then GUICtrlSetData($B3,"O") GUICtrlSetState($B3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="" AND GUICtrlRead($B3)="O" AND GUICtrlRead($C3)="O" Then GUICtrlSetData($A3,"O") GUICtrlSetState($A3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="O" AND GUICtrlRead($B3)="O" AND GUICtrlRead($C3)="" Then GUICtrlSetData($C3,"O") GUICtrlSetState($C3,$GUI_DISABLE) Return EndIf ;============== ;Diagonal= If GUICtrlRead($A1)="O" AND GUICtrlRead($B2)="O" AND GUICtrlRead($C3)="" Then GUICtrlSetData($C3,"O") GUICtrlSetState($C3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="O" AND GUICtrlRead($B2)="" AND GUICtrlRead($C3)="O" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="" AND GUICtrlRead($B2)="O" AND GUICtrlRead($C3)="O" Then GUICtrlSetData($A1,"O") GUICtrlSetState($A1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="O" AND GUICtrlRead($B2)="O" AND GUICtrlRead($C1)="" Then GUICtrlSetData($C1,"O") GUICtrlSetState($C1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="O" AND GUICtrlRead($B2)="" AND GUICtrlRead($C1)="O" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="" AND GUICtrlRead($B2)="O" AND GUICtrlRead($C1)="O" Then GUICtrlSetData($A3,"O") GUICtrlSetState($A3,$GUI_DISABLE) Return EndIf ;====================== ;TRATAR DE EVITAR QUE GANE= ;Horizontal= If GUICtrlRead($A1)="X" AND GUICtrlRead($A2)="X" AND GUICtrlRead($A3)="" Then GUICtrlSetData($A3,"O") GUICtrlSetState($A3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="" AND GUICtrlRead($A2)="X" AND GUICtrlRead($A3)="X" Then GUICtrlSetData($A1,"O") GUICtrlSetState($A1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="X" AND GUICtrlRead($A2)="" AND GUICtrlRead($A3)="X" Then GUICtrlSetData($A2,"O") GUICtrlSetState($A2,$GUI_DISABLE) Return EndIf If GUICtrlRead($B1)="X" AND GUICtrlRead($B2)="X" AND GUICtrlRead($B3)="" Then GUICtrlSetData($B3,"O") GUICtrlSetState($B3,$GUI_DISABLE) Return EndIf If GUICtrlRead($B1)="" AND GUICtrlRead($B2)="X" AND GUICtrlRead($B3)="X" Then GUICtrlSetData($B1,"O") GUICtrlSetState($B1,$GUI_DISABLE) Return EndIf If GUICtrlRead($B1)="X" AND GUICtrlRead($B2)="" AND GUICtrlRead($B3)="X" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($C1)="X" AND GUICtrlRead($C2)="X" AND GUICtrlRead($C3)="" Then GUICtrlSetData($C3,"O") GUICtrlSetState($C3,$GUI_DISABLE) Return EndIf If GUICtrlRead($C1)="" AND GUICtrlRead($C2)="X" AND GUICtrlRead($C3)="X" Then GUICtrlSetData($C1,"O") GUICtrlSetState($C1,$GUI_DISABLE) Return EndIf If GUICtrlRead($C1)="X" AND GUICtrlRead($C2)="" AND GUICtrlRead($C3)="X" Then GUICtrlSetData($C2,"O") GUICtrlSetState($C2,$GUI_DISABLE) Return EndIf ;======== ;Vertical= If GUICtrlRead($A1)="X" AND GUICtrlRead($B1)="" AND GUICtrlRead($C1)="X" Then GUICtrlSetData($B1,"O") GUICtrlSetState($B1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="" AND GUICtrlRead($B1)="X" AND GUICtrlRead($C1)="X" Then GUICtrlSetData($A1,"O") GUICtrlSetState($A1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="X" AND GUICtrlRead($B1)="X" AND GUICtrlRead($C1)="" Then GUICtrlSetData($C1,"O") GUICtrlSetState($C1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A2)="X" AND GUICtrlRead($B2)="" AND GUICtrlRead($C2)="X" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A2)="" AND GUICtrlRead($B2)="X" AND GUICtrlRead($C2)="X" Then GUICtrlSetData($A2,"O") GUICtrlSetState($A2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A2)="X" AND GUICtrlRead($B2)="X" AND GUICtrlRead($C2)="" Then GUICtrlSetData($C2,"O") GUICtrlSetState($C2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="X" AND GUICtrlRead($B3)="" AND GUICtrlRead($C3)="X" Then GUICtrlSetData($B3,"O") GUICtrlSetState($B3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="" AND GUICtrlRead($B3)="X" AND GUICtrlRead($C3)="X" Then GUICtrlSetData($A3,"O") GUICtrlSetState($A3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="X" AND GUICtrlRead($B3)="X" AND GUICtrlRead($C3)="" Then GUICtrlSetData($C3,"O") GUICtrlSetState($C3,$GUI_DISABLE) Return EndIf ;============== ;Diagonal= If GUICtrlRead($A1)="X" AND GUICtrlRead($B2)="X" AND GUICtrlRead($C3)="" Then GUICtrlSetData($C3,"O") GUICtrlSetState($C3,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="X" AND GUICtrlRead($B2)="" AND GUICtrlRead($C3)="X" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A1)="" AND GUICtrlRead($B2)="X" AND GUICtrlRead($C3)="X" Then GUICtrlSetData($A1,"O") GUICtrlSetState($A1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="X" AND GUICtrlRead($B2)="X" AND GUICtrlRead($C1)="" Then GUICtrlSetData($C1,"O") GUICtrlSetState($C1,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="X" AND GUICtrlRead($B2)="" AND GUICtrlRead($C1)="X" Then GUICtrlSetData($B2,"O") GUICtrlSetState($B2,$GUI_DISABLE) Return EndIf If GUICtrlRead($A3)="" AND GUICtrlRead($B2)="X" AND GUICtrlRead($C1)="X" Then GUICtrlSetData($A3,"O") GUICtrlSetState($A3,$GUI_DISABLE) Return EndIf ;====================== ;Random= $a=0 Do $a+=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 GUICtrlRead($iCas)="" Then ExitLoop Until $a=100 If $a<>100 Then GUICtrlSetData($iCas,"O") GUICtrlSetState($iCas,$GUI_DISABLE) Else $over=1 EndIf EndFunc;==>Jugar() Func win() ;"X" If GUICtrlRead($A1)="X" and GUICtrlRead($A2)="X" and GUICtrlRead($A3)="X" Then $win=1 Return EndIf If GUICtrlRead($B1)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($B3)="X" Then $win=1 Return EndIf If GUICtrlRead($C1)="X" and GUICtrlRead($C2)="X" and GUICtrlRead($C3)="X" Then $win=1 Return EndIf If GUICtrlRead($A1)="X" and GUICtrlRead($B1)="X" and GUICtrlRead($C1)="X" Then $win=1 Return EndIf If GUICtrlRead($A2)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($C2)="X" Then $win=1 Return EndIf If GUICtrlRead($A3)="X" and GUICtrlRead($B3)="X" and GUICtrlRead($C3)="X" Then $win=1 Return EndIf If GUICtrlRead($A1)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($C3)="X" Then $win=1 Return EndIf If GUICtrlRead($A3)="X" and GUICtrlRead($B2)="X" and GUICtrlRead($C1)="X" Then $win=1 Return EndIf ;"O" If GUICtrlRead($A1)="O" and GUICtrlRead($A2)="O" and GUICtrlRead($A3)="O" Then $win=2 Return EndIf If GUICtrlRead($B1)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($B3)="O" Then $win=2 Return EndIf If GUICtrlRead($C1)="O" and GUICtrlRead($C2)="O" and GUICtrlRead($C3)="O" Then $win=2 Return EndIf If GUICtrlRead($A1)="O" and GUICtrlRead($B1)="O" and GUICtrlRead($C1)="O" Then $win=2 Return EndIf If GUICtrlRead($A2)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($C2)="O" Then $win=2 Return EndIf If GUICtrlRead($A3)="O" and GUICtrlRead($B3)="O" and GUICtrlRead($C3)="O" Then $win=2 Return EndIf If GUICtrlRead($A1)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($C3)="O" Then $win=2 Return EndIf If GUICtrlRead($A3)="O" and GUICtrlRead($B2)="O" and GUICtrlRead($C1)="O" Then $win=2 Return EndIf EndFunc;===>Win() Func clear() GUICtrlSetData($A1,"") GUICtrlSetState($A1,$GUI_ENABLE) GUICtrlSetData($A2,"") GUICtrlSetState($A2,$GUI_ENABLE) GUICtrlSetData($A3,"") GUICtrlSetState($A3,$GUI_ENABLE) GUICtrlSetData($B1,"") GUICtrlSetState($B1,$GUI_ENABLE) GUICtrlSetData($B2,"") GUICtrlSetState($B2,$GUI_ENABLE) GUICtrlSetData($B3,"") GUICtrlSetState($B3,$GUI_ENABLE) GUICtrlSetData($C1,"") GUICtrlSetState($C1,$GUI_ENABLE) GUICtrlSetData($C2,"") GUICtrlSetState($C2,$GUI_ENABLE) GUICtrlSetData($C3,"") GUICtrlSetState($C3,$GUI_ENABLE) $win=0 $over=0 EndFunc Func Randomize() Sleep(200) GUICtrlSetData($A1,"TA") Sleep(200) GUICtrlSetData($B2,"TE") Sleep(200) GUICtrlSetData($C3,"TI") Sleep(200) GUICtrlSetData($A1,"") Sleep(200) GUICtrlSetData($B2,"") Sleep(200) GUICtrlSetData($C3,"") Sleep(200) EndFunc Edited September 25, 2007 by Nahuel Link to comment Share on other sites More sharing options...
JustinReno Posted September 25, 2007 Share Posted September 25, 2007 I like yours Nahuel! I can't even beat it!! WOW! Good Job! Link to comment Share on other sites More sharing options...
Nahuel Posted September 25, 2007 Share Posted September 25, 2007 I like yours Nahuel! I can't even beat it!! WOW! Good Job! Thanks! I think I'll add it to 'my scripts' It actually works. Link to comment Share on other sites More sharing options...
enaiman Posted September 25, 2007 Share Posted September 25, 2007 Nahuel - you did a veeery nice job And very clean One point though ... it can be beaten. I'm 99% done on my script and I'm pretty sure it will be unbeatable It will be finished by tomorrow. Anyway a big "thumbs-up" for your awesome work 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 :) Link to comment Share on other sites More sharing options...
Nahuel Posted September 25, 2007 Share Posted September 25, 2007 Nahuel - you did a veeery nice job And very clean One point though ... it can be beaten.I'm 99% done on my script and I'm pretty sure it will be unbeatable It will be finished by tomorrow.Anyway a big "thumbs-up" for your awesome work haha, yeah it still needs working. Can't wait to see yours Link to comment Share on other sites More sharing options...
BrettF Posted September 25, 2007 Share Posted September 25, 2007 Im as smart as it! all i can do is tie Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Nahuel Posted September 25, 2007 Share Posted September 25, 2007 Im as smart as it! all i can do is tie haha, I'm a sucker for tic tac toe and it beats me.. so.. Link to comment Share on other sites More sharing options...
BrettF Posted September 25, 2007 Share Posted September 25, 2007 (edited) haha, I'm a sucker for tic tac toe and it beats me.. so.. lolEDIT: woops... triple posted? Wow... new low... My Bad! Edited September 25, 2007 by Bert Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
James Posted September 25, 2007 Share Posted September 25, 2007 Out of 90 games it has beaten me once I pressed the wrong place (I was very distracted at the time)!! Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
enaiman Posted September 26, 2007 Share Posted September 26, 2007 (edited) Finally here it is I'm 99% sure that it can't be beaten - if you beat it, please send me the winning sequence - thanks I'm sure I could do more work on it but unfortunately I don't have the necessary time right now - I'll try to "refine" it on weekend Enjoy the fun ... and the challenge I have to give credit to sccrstvn93 for the starting interface and "grid" idea - THANKS sccrstvn93 expandcollapse popup#include <GUIConstants.au3> #include <ARRAY.AU3> Global $turn = True;true means player false means ai Global $grid[4][4] Global $button[4][4] Global $string_eval[9] Global $content[9] Global $moves = 0 Global $computer_win = 0 Global $total = 0 Global $playerwin = 0 $content[1] = "31,32,33" $content[2] = "21,22,23" $content[3] = "11,12,13" $content[4] = "11,22,33" $content[5] = "11,21,31" $content[6] = "12,22,32" $content[7] = "13,23,33" $content[8] = "13,22,31" $Form1 = GUICreate("Beat-me Tic-Tac-Toe", 326, 347, 193, 125) GUICtrlCreateLabel("Computer Win", 10, 10, 71, 17) $cwin = GUICtrlCreateLabel("00", 90, 10, 25, 17) GUICtrlCreateLabel("Player Win", 140, 10, 55, 17) $pwin = GUICtrlCreateLabel("00", 205, 10, 25, 17) GUICtrlCreateLabel("Games", 250, 10, 37, 17) $games = GUICtrlCreateLabel("00", 295, 10, 16, 17) $Button[1][1] = GUICtrlCreateButton("", 44, 72, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[1][2] = GUICtrlCreateButton("", 134, 72, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[1][3] = GUICtrlCreateButton("", 224, 72, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[2][3] = GUICtrlCreateButton("", 224, 152, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[2][1] = GUICtrlCreateButton("", 44, 152, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[2][2] = GUICtrlCreateButton("", 134, 152, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[3][3] = GUICtrlCreateButton("", 224, 232, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[3][2] = GUICtrlCreateButton("", 134, 232, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[3][1] = GUICtrlCreateButton("", 44, 232, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Label1 = GUICtrlCreateLabel("Press any Button", 100, 40, 200, 17) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") $reset = GUICtrlCreateButton("Reset", 112, 312, 97, 25, 0) GUISetState(@SW_SHOW) For $i = 1 to 3;column For $j = 1 to 3;row $grid[$i][$j] = 0 Next Next While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $button [1][1] If $grid[1][1] = 0 And BitAND(GUICtrlGetState($button [1][1]),$GUI_ENABLE) Then _clicked (1,1) EndIf Case $button [1][2] If $grid[1][2] = 0 And BitAND(GUICtrlGetState($button [1][2]),$GUI_ENABLE) Then _clicked (1,2) EndIf Case $button [1][3] If $grid[1][3] = 0 And BitAND(GUICtrlGetState($button [1][3]),$GUI_ENABLE) Then _clicked (1,3) EndIf Case $button [2][1] If $grid[2][1] = 0 And BitAND(GUICtrlGetState($button [2][1]),$GUI_ENABLE) Then _clicked (2,1) EndIf Case $button [2][2] If $grid[2][2] = 0 And BitAND(GUICtrlGetState($button [2][2]),$GUI_ENABLE) Then _clicked (2,2) EndIf Case $button [2][3] If $grid[2][3] = 0 And BitAND(GUICtrlGetState($button [2][3]),$GUI_ENABLE) Then _clicked (2,3) EndIf Case $button [3][1] If $grid[3][1] = 0 And BitAND(GUICtrlGetState($button [3][1]),$GUI_ENABLE) Then _clicked (3,1) EndIf Case $button[3][2] If $grid[3][2] = 0 And BitAND(GUICtrlGetState($button [3][2]),$GUI_ENABLE) Then _clicked (3,2) EndIf Case $button [3][3] If $grid[3][3] = 0 And BitAND(GUICtrlGetState($button [3][3]),$GUI_ENABLE) Then _clicked (3,3) EndIf Case $reset If $computer_win = 1 Then GUICtrlSetData($cwin, GUICtrlRead($cwin)+1) If $playerwin = 1 Then GUICtrlSetData($pwin, GUICtrlRead($pwin)+1) GUICtrlSetData ($Label1, "Your Turn") For $i = 1 to 3;column For $j = 1 to 3;row $grid[$i][$j] = 0 GUICtrlSetState($button[$i][$j], $GUI_ENABLE) GUICtrlSetData($button[$i][$j], "") Next Next $moves = 0 $turn = True $total +=1 $computer_win = 0 $playerwin = 0 GUICtrlSetData($games, $total) EndSwitch If $moves = 9 And $computer_win = 0 Then GUICtrlSetData ($Label1, "Nobody won") $moves = 0 EndIf WEnd Func string_eval() For $j=1 To 8 $string_eval[$j] = 0 Next For $j=1 To 3 $string_eval[1] += $grid[3][$j] $string_eval[2] += $grid[2][$j] $string_eval[3] += $grid[1][$j] $string_eval[5] += $grid[$j][1] $string_eval[6] += $grid[$j][2] $string_eval[7] += $grid[$j][3] Next $string_eval[4] = $grid[1][1]+$grid[2][2]+$grid[3][3] $string_eval[8] = $grid[1][3]+$grid[2][2]+$grid[3][1] EndFunc Func _clicked ($row, $column) If $moves = 9 Then Return Local $found_win = 0 If $turn = true Then If BitAND(GUICtrlGetState($button [$row][$column]),$GUI_ENABLE) Then GUICtrlSetData ($button[$row][$column], "X") GUICtrlSetState ($button[$row][$column], $gui_disable) GUICtrlSetData ($Label1, "Thinking...") $grid[$row][$column] = 5 $turn = False $moves +=1 string_eval() For $i=1 To 8 If $string_eval[$i] = 15 Then ;player won $playerwin = 1 GUICtrlSetData ($Label1, "You Won") Return EndIf Next If $moves = 9 Then Return Else Return EndIf EndIf If $turn = False Then Select Case $moves = 1 If $grid[2][2] = 5 Then GUICtrlSetData ($button[1][1], "O") GUICtrlSetState ($button[1][1], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][1] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf If $grid[2][2] = 0 Then GUICtrlSetData ($button[2][2], "O") GUICtrlSetState ($button[2][2], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[2][2] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Case $moves >2 string_eval() For $i=1 To 8 If $string_eval[$i] = 2 Then ;go for win $found_win = 1 $row_played = StringSplit($content[$i], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "I Won") $computer_win=1 $grid[$ids[1]][$ids[2]] = 1 Return EndIf Next EndIf Next For $i=1 To 8 If $string_eval[$i] = 10 Then ;2 in a row from player $row_played = StringSplit($content[$i], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[$ids[1]][$ids[2]] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Next EndIf Next If $string_eval[4] = 11 Then If $grid [1][1] = 5 And $grid [3][3] = 5 And $moves < 4 Then GUICtrlSetData ($button[1][2], "O") GUICtrlSetState ($button[1][2], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][2] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf If $grid [2][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then GUICtrlSetData ($button[1][3], "O") GUICtrlSetState ($button[1][3], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][3] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf EndIf If $grid [1][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then GUICtrlSetData ($button[1][3], "O") GUICtrlSetState ($button[1][3], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][3] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf If $grid [2][3] = 5 And $grid [3][2] = 5 And $moves < 4 Then GUICtrlSetData ($button[3][3], "O") GUICtrlSetState ($button[3][3], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[3][3] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf If $grid [1][2] = 5 And $grid [3][1] = 5 And $moves < 4 Then GUICtrlSetData ($button[1][1], "O") GUICtrlSetState ($button[1][1], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][1] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf For $x=1 To 8 If $string_eval[$x] <> 10 And $string_eval[$x] <> 2 Then ;2 in a row from player If $string_eval[$x] = 1 Then $row_played = StringSplit($content[$x], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[$ids[1]][$ids[2]] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Next ElseIf $string_eval[$x] = 5 Then $row_played = StringSplit($content[$x], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[$ids[1]][$ids[2]] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Next EndIf EndIf Next EndSelect EndIf EndFunc EDIT: fixed the winning pattern found by Nahuel - Thanks EDIT: fixed the pattern discovered by Sardith - Thanks I guess I fixed all "corner" schemes. - Code updated. Edited September 26, 2007 by enaiman 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 :) Link to comment Share on other sites More sharing options...
Nahuel Posted September 26, 2007 Share Posted September 26, 2007 (edited) Tricky! I beat it twice but when I click the winning button it appears an "O" instead of "X".. that's cheating, you know? I couldn't recreate it though...Anyways, here's the way of winning:I have to admit you did an amazing job. It is VERY hard to beat. The only thing that I kinda don't like is that it always follows the same sequence according to what you press. So you can always win if you do the same moves, but that's what makes it hard to beat I suppose.-edit-Also, the user always starts.. give us some advantage Edited September 26, 2007 by Nahuel Link to comment Share on other sites More sharing options...
enaiman Posted September 26, 2007 Share Posted September 26, 2007 (edited) Congrats Nahuel on beating it and for sharing the solution - I'll fix that one today so you'll have a new challenge And - of course the user starts first always Tricky! I beat it twice but when I click the winning button it appears an "O" instead of "X".. that's cheating, you know?It's not my intention to cheat - that could happen, I can't test all the positions ... that's why the feedback is very important because it allows me to improve it.Thanks for your feedback The winning pattern is fixed now and the code updated - the challenge is back Thanks again Nahuel Edited September 26, 2007 by enaiman 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 :) Link to comment Share on other sites More sharing options...
Nahuel Posted September 26, 2007 Share Posted September 26, 2007 (edited) I know it's not your intention to cheat, it's just a bug or I'm blind... Your script works awesomely! It's really hard to beat Edited September 26, 2007 by Nahuel Link to comment Share on other sites More sharing options...
enaiman Posted September 26, 2007 Share Posted September 26, 2007 Thanks Nahuel However I like your script more - it looks very nice and it behaves very "realistic". The begining of my script (first 2 moves) are quite "automated" I made the script to look first for the central position (the most important) - if it is empty to take it and if it is taken then to take the first corner. After that all is about avoiding certain winning posibilities (3 situations so far), looking for a win situation, and trying to get 2 in a row when safe. Indeed the script is not behaving so randomly - the user will be the one which will "dictate" the script's behaviour. Basically the script is most of the time "defending" itself but if a win situation is born from its defensive work then it will go for it I'm sure the script has some redundant code but I'll "refine" it sometime in the future. Thanks to everyone trying it. 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 :) Link to comment Share on other sites More sharing options...
Sardith Posted September 26, 2007 Share Posted September 26, 2007 Took 8 games for me to beat it. Hmm.. expandcollapse popup#include <GUIConstants.au3> #include <ARRAY.AU3> Global $turn = True;true means player false means ai Global $grid[4][4] Global $button[4][4] Global $string_eval[9] Global $content[9] Global $moves = 0 Global $computer_win = 0 Global $total = 0 Global $playerwin = 0 $content[1] = "31,32,33" $content[2] = "21,22,23" $content[3] = "11,12,13" $content[4] = "11,22,33" $content[5] = "11,21,31" $content[6] = "12,22,32" $content[7] = "13,23,33" $content[8] = "13,22,31" $Form1 = GUICreate("Beat-me Tic-Tac-Toe", 326, 347, 193, 125) GUICtrlCreateLabel("Computer Win", 10, 10, 71, 17) $cwin = GUICtrlCreateLabel("00", 90, 10, 25, 17) GUICtrlCreateLabel("Player Win", 140, 10, 55, 17) $pwin = GUICtrlCreateLabel("00", 205, 10, 25, 17) GUICtrlCreateLabel("Games", 250, 10, 37, 17) $games = GUICtrlCreateLabel("00", 295, 10, 16, 17) $Button[1][1] = GUICtrlCreateButton("", 44, 72, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[1][2] = GUICtrlCreateButton("", 134, 72, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[1][3] = GUICtrlCreateButton("", 224, 72, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[2][3] = GUICtrlCreateButton("", 224, 152, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[2][1] = GUICtrlCreateButton("", 44, 152, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[2][2] = GUICtrlCreateButton("", 134, 152, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[3][3] = GUICtrlCreateButton("", 224, 232, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[3][2] = GUICtrlCreateButton("", 134, 232, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Button[3][1] = GUICtrlCreateButton("", 44, 232, 57, 57, 0) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") $Label1 = GUICtrlCreateLabel("Press any Button", 100, 40, 200, 17) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") $reset = GUICtrlCreateButton("Reset", 112, 312, 97, 25, 0) GUISetState(@SW_SHOW) For $i = 1 to 3;column For $j = 1 to 3;row $grid[$i][$j] = 0 Next Next While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $button [1][1] If $grid[1][1] = 0 And BitAND(GUICtrlGetState($button [1][1]),$GUI_ENABLE) Then _clicked (1,1) EndIf Case $button [1][2] If $grid[1][2] = 0 And BitAND(GUICtrlGetState($button [1][2]),$GUI_ENABLE) Then _clicked (1,2) EndIf Case $button [1][3] If $grid[1][3] = 0 And BitAND(GUICtrlGetState($button [1][3]),$GUI_ENABLE) Then _clicked (1,3) EndIf Case $button [2][1] If $grid[2][1] = 0 And BitAND(GUICtrlGetState($button [2][1]),$GUI_ENABLE) Then _clicked (2,1) EndIf Case $button [2][2] If $grid[2][2] = 0 And BitAND(GUICtrlGetState($button [2][2]),$GUI_ENABLE) Then _clicked (2,2) EndIf Case $button [2][3] If $grid[2][3] = 0 And BitAND(GUICtrlGetState($button [2][3]),$GUI_ENABLE) Then _clicked (2,3) EndIf Case $button [3][1] If $grid[3][1] = 0 And BitAND(GUICtrlGetState($button [3][1]),$GUI_ENABLE) Then _clicked (3,1) EndIf Case $button[3][2] If $grid[3][2] = 0 And BitAND(GUICtrlGetState($button [3][2]),$GUI_ENABLE) Then _clicked (3,2) EndIf Case $button [3][3] If $grid[3][3] = 0 And BitAND(GUICtrlGetState($button [3][3]),$GUI_ENABLE) Then _clicked (3,3) EndIf Case $reset If $computer_win = 1 Then GUICtrlSetData($cwin, GUICtrlRead($cwin)+1) If $playerwin = 1 Then GUICtrlSetData($pwin, GUICtrlRead($pwin)+1) GUICtrlSetData ($Label1, "Your Turn") For $i = 1 to 3;column For $j = 1 to 3;row $grid[$i][$j] = 0 GUICtrlSetState($button[$i][$j], $GUI_ENABLE) GUICtrlSetData($button[$i][$j], "") Next Next $moves = 0 $turn = True $total +=1 $computer_win = 0 $playerwin = 0 GUICtrlSetData($games, $total) EndSwitch If $moves = 9 And $computer_win = 0 Then GUICtrlSetData ($Label1, "Nobody won") $moves = 0 EndIf WEnd Func string_eval() For $j=1 To 8 $string_eval[$j] = 0 Next For $j=1 To 3 $string_eval[1] += $grid[3][$j] $string_eval[2] += $grid[2][$j] $string_eval[3] += $grid[1][$j] $string_eval[5] += $grid[$j][1] $string_eval[6] += $grid[$j][2] $string_eval[7] += $grid[$j][3] Next $string_eval[4] = $grid[1][1]+$grid[2][2]+$grid[3][3] $string_eval[8] = $grid[1][3]+$grid[2][2]+$grid[3][1] EndFunc Func _clicked ($row, $column) If $moves = 9 Then Return Local $found_win = 0 If $turn = true Then If BitAND(GUICtrlGetState($button [$row][$column]),$GUI_ENABLE) Then GUICtrlSetData ($button[$row][$column], "X") GUICtrlSetState ($button[$row][$column], $gui_disable) GUICtrlSetData ($Label1, "Thinking...") $grid[$row][$column] = 5 $turn = False $moves +=1 string_eval() For $i=1 To 8 If $string_eval[$i] = 15 Then ;player won $playerwin = 1 GUICtrlSetData ($Label1, "You Won") Return EndIf Next If $moves = 9 Then Return Else Return EndIf EndIf If $turn = False Then Select Case $moves = 1 If $grid[2][2] = 5 Then GUICtrlSetData ($button[1][1], "O") GUICtrlSetState ($button[1][1], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][1] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf If $grid[2][2] = 0 Then GUICtrlSetData ($button[2][2], "O") GUICtrlSetState ($button[2][2], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[2][2] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Case $moves >2 string_eval() For $i=1 To 8 If $string_eval[$i] = 2 Then ;go for win $found_win = 1 $row_played = StringSplit($content[$i], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "I Won") $computer_win=1 $grid[$ids[1]][$ids[2]] = 1 Return EndIf Next EndIf Next For $i=1 To 8 If $string_eval[$i] = 10 Then ;2 in a row from player $row_played = StringSplit($content[$i], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[$ids[1]][$ids[2]] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Next EndIf Next If $string_eval[4] = 11 Then If $grid [1][1] = 5 And $grid [3][3] = 5 And $moves < 4 Then GUICtrlSetData ($button[1][2], "O") GUICtrlSetState ($button[1][2], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][2] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf If $grid [2][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then GUICtrlSetData ($button[1][3], "O") GUICtrlSetState ($button[1][3], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][3] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf EndIf If $grid [1][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then GUICtrlSetData ($button[1][3], "O") GUICtrlSetState ($button[1][3], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[1][3] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf If $grid [2][3] = 5 And $grid [3][2] = 5 And $moves < 4 Then GUICtrlSetData ($button[3][3], "O") GUICtrlSetState ($button[3][3], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[3][3] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf For $x=1 To 8 If $string_eval[$x] <> 10 And $string_eval[$x] <> 2 Then ;2 in a row from player If $string_eval[$x] = 1 Then $row_played = StringSplit($content[$x], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[$ids[1]][$ids[2]] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Next ElseIf $string_eval[$x] = 5 Then $row_played = StringSplit($content[$x], ",") For $k=1 To 3 $ids = StringSplit($row_played[$k], "") If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there GUICtrlSetData ($button[$ids[1]][$ids[2]], "O") GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable) GUICtrlSetData ($Label1, "Your Turn...") $grid[$ids[1]][$ids[2]] = 1 $moves +=1 If $moves = 9 Then Return $turn = True Return EndIf Next EndIf EndIf Next EndSelect EndIf EndFunc [font="Verdana"]Valik:Get it straight - I'm not here to say please, I'm here to help - if my help's not appreciated then lotsa luck, gentlemen.[/font] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now