X=AI
O=USER
USER goes first
this has SOMEwhat of a strategy in it...
EDIT
its more difficult now but still has a few bugs
EDIT
fairly random scenarios based on a valued system.
3|2|3
2|4|2
3|2|3
if there isn't a spot to block\win it goes to find the most valuable, if there are multiple it randomizes it. not exactly predictable. or is it? :S
Opt ('GuiOnEVentMode',1) Dim $GRID[3][3] Dim $row[3], $column[3],$diagonal[2] $nTieCount = 0 $nTurnCount = 0 $nLossCount = 0 $nWinCount = 0 $nAI = -1 $sAI = 'o' $nPLAYER = 1 $sPLAYER = 'x' $turn = $nPLAYER $w_h = 75 $guiw = ($w_h*3)+40 $guih = ($w_h*5) $font_s = ($w_h*2)/4 $window_hwnd = GUICreate ('tic tac toe',$guiw,$guih) GUISetOnEvent (-3,'End') $lWinCount = GUICtrlCreateLabel ('WINS:',10,40+($w_h*3),($guiw-20)/3,$w_h/2,0x0200) $lLossCount = GUICtrlCreateLabel ('LOSSES:',10+($guiw-20)/3,40+($w_h*3),($guiw-20)/3,$w_h/2,0x0200) $lTieCount = GUICtrlCreateLabel ('Ties:',$guiw-($guiw-20)/3,40+($w_h*3),($guiw-20)/3,$w_h/2,0x0200) For $x = 0 To 2 For $y = 0 To 2 $GRID[$x][$y] = GUICtrlCreateButton ('',10+(10*$x)+($x*$w_h),10+(10*$y)+($y*$w_h),$w_h,$w_h) GUICtrlSetOnEvent ($GRID[$x][$y],'Clicked') Next Next Dim $GRIDval2[4] $GRIDval2[0] = $GRID[0][1] $GRIDval2[1] = $GRID[2][0] $GRIDval2[2] = $GRID[2][2] $GRIDval2[3] = $GRID[2][1] Dim $GRIDval3[4] $GRIDval3[0] = $GRID[0][0] $GRIDval3[1] = $GRID[0][2] $GRIDval3[2] = $GRID[2][0] $GRIDval3[3] = $GRID[2][2] $diagonal[0] = $GRID[0][0] & '|' & $GRID[1][1] & '|' & $GRID[2][2] $diagonal[1] = $GRID[2][0] & '|' & $GRID[1][1] & '|' & $GRID[0][2] For $n = 0 To 2 $row[$n] = $GRID[$n][0] & '|' & $GRID[$n][1] & '|' & $GRID[$n][2] $column[$n] = $GRID[0][$n] & '|' & $GRID[1][$n] & '|' & $GRID[2][$n] Next GUICtrlCreateButton ('Reset',10,(3*$w_h)+$w_h+20,$guiw-20,$w_h/2) GUICtrlSetOnEvent (-1,'Start') GUISetState () Start () While 1 CheckStats () CheckWin () Sleep (100) WEnd Func Start () $turn = $nPLAYER $nTurnCount = 0 WinSetState ($window_hwnd,'',@SW_ENABLE) For $n = 0 To 2 For $u = 0 To 2 GUICtrlSetData ($GRID[$n][$u],'') GUICtrlSetState ($GRID[$n][$u],64) Next Next EndFunc Func CheckStats () If GUICtrlRead ($lWinCount) <> 'WINS:'&$nWinCount Then GUICtrlSetData ($lWinCount,'WINS:'&$nWinCount) If GUICtrlRead ($lLossCount) <> 'LOSSES:'&$nLossCount Then GUICtrlSetData ($lLossCount,'LOSSES:'&$nLossCount) If GUICtrlRead ($lTieCount) <> 'TIES:'&$nTieCount Then GUICtrlSetData ($lTieCount,'TIES:'&$nTieCount) EndFunc Func CheckWin () Local $eval, $n $eval = StringEval($diagonal[0]) If $eval = -3 Or $eval = 3 Then Win ($eval) $eval = StringEval($diagonal[1]) If $eval = -3 Or $eval = 3 Then Win ($eval) For $n = 0 To 2 $eval = StringEval($row[$n]) If $eval = -3 Or $eval = 3 Then Win ($eval) $eval = StringEval($column[$n]) If $eval = -3 Or $eval = 3 Then Win ($eval) Next EndFunc Func Win ($value) If $value/$nPLAYER = 3 Then MsgBox (0,'WIN','Player Has Won!',25,$window_hwnd) $nWinCount += 1 EndIf If $value/$nAI = 3 Then MsgBox (0,'WIN','Computer Has Won!',25,$window_hwnd) $nLossCount += 1 EndIf Start () EndFunc Func Check () Local $n If $nTurnCount >= 8 Then MsgBox (0,'TIE','Tie Game',25,$window_hwnd) $nTieCount += 1 Start () Else CheckMove(StringEval($diagonal[0]),$diagonal[0]) CheckMove(StringEval($diagonal[1]),$diagonal[1]) For $n = 0 To 2 CheckMove(StringEval($row[$n]),$row[$n]) CheckMove(StringEval($column[$n]),$column[$n]) Next If $turn = $nAI Then If GUICtrlRead ($GRID[1][1]) == '' Then Move($GRID[1][1]) Else If MiniMaxValuing ($GRIDval3) = -1 Then If MiniMaxValuing ($GRIDval2) = -1 Then Do $r = Random (0,2,1) $c = Random (0,2,1) Until GUICtrlRead ($GRID[$r][$c]) == '' Move ($GRID[$r][$c]) EndIf EndIf EndIf EndIf EndIf EndFunc Func Move ($ID) GUICtrlSetData ($ID,$sAI) GUICtrlSetState ($ID,128) $turn = $nPLAYER $nTurnCount += 1 WinSetState ($window_hwnd,'',@SW_ENABLE) EndFunc Func Clicked () $turn = $nAI $nTurnCount += 1 GUICtrlSetData (@GUI_CtrlId,$sPLAYER) GUICtrlSetState (@GUI_CtrlId,128) WinSetState ($window_hwnd,'',@SW_DISABLE) Check () EndFunc Func CheckMove ($value,$IDarray) If $value = 3 Or $value = -3 Then Win ($value) If $turn = $nAI Then If GUICtrlRead ($GRID[1][1]) == '' Then Move ($GRID[1][1]) Return EndIf If $value = 2 Or $value = -2 Then Local $guiIDarray = StringSplit ($IDarray,'|',2) Local $n = 0 For $n = 0 To UBound ($guiIDarray)-1 $guiIDarray[$n] = Number ($guiIDarray[$n]) If GUICtrlRead ($guiIDarray[$n]) == '' Then Move ($guiIDarray[$n]) Return EndIf Next EndIf Return EndIf EndIf EndFunc Func StringEval ($IDarray) Dim $guiIDarray = StringSplit ($IDarray,'|',2) Dim $valuearray[UBound ($guiIDarray)] Local $value = 0, $n = 0 For $n = 0 To UBound ($guiIDarray)-1 $guiIDarray[$n] = $guiIDarray[$n] If GUICtrlRead ($guiIDarray[$n]) == '' Then $valuearray[$n] = 0 If GUICtrlRead ($guiIDarray[$n]) == $sPLAYER Then $valuearray[$n] = $nPLAYER If GUICtrlRead ($guiIDarray[$n]) == $sAI Then $valuearray[$n] = $nAI $value += $valuearray[$n] Next Return $value EndFunc Func MiniMaxValuing ($array) Local $n, $Count = '' For $n = 0 To UBound ($array,1)-1 If GUICtrlRead ($array[$n]) == '' Then $Count &= $n & '|' Next If $Count == '' Then Return -1 $Count = StringSplit (StringTrimRight ($Count,1),'|',2) Move ($array[$Count[Random (0,UBound ($Count,1)-1,1)]]) Return 1 EndFunc Func End () Exit EndFunc
Edited by CodyBarrett, 27 October 2010 - 02:49 PM.






