ตัวโค้ดเดิมเป็น AI (Artifical Intelligence)ที่คนเขียน(ฝรั่ง)เขาคิดขึ้นมาเอง แล้วผมนำมาดัดแปลง การจะทำแบบนี้ได้ต้องเข้าใจคำสั่ง AutoIt และหลักตรรกะเปรียบเทียบ ซึ่งคุณยังเพิ่งศึกษา AutoIt การเอาโค้ดไปดูอาจจะไม่เข้าใจก็ได้ แต่ก็ลองดูก็แล้วกันครับ โค้ดต้นฉบับอยู่ด้านล่าง
++++++++++++++++++++++++
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
#include <Misc.au3>
$dll = DllOpen("user32.dll")
Global $window = GUICreate("Jewel Thief", 400, 300, 100, 100)
Global $graphic = GUICtrlCreateGraphic(20, 20, 20 * 8 + 2 + 9, 20 * 8 + 2 + 9)
Global Const $COLORS[7] = ["Red", "Orange", "Yellow", "Green", "Blue", "Purple", "White"]
Global $sol[10][10]
;GUICtrlSetColor($graphic, 0)
GUISetState(@SW_SHOW)
Global $tick = -1
Global $afx = 0, $afy = 0
Global $ox = -1
Global $oy = -1
Global $oox = -1
Global $ooy = -1
Global $status = GUICtrlCreateLabel("", 10, 270, 380, 20)
Global $mt[10][10] ; master table
Global $tt[10][10] ; temp table
GUICtrlSetData($status, "Running")
While 1
DoEvents()
; Search
GUICtrlSetData($status, "Searching for window")
$flag = False
While Not $flag
DoEvents()
;Sleep(100)
FindTable()
WEnd
GUICtrlSetData($status, "Found blitz window!")
;mousemove(GetBlitzX(1),GetBlitzY(1))
; GAME LOGIC START
$time = True
While $time
$tick = $tick + 1
;While _IsPressed("A0", $dll)
; GUICtrlSetData($status, "SHIFT HOLD!")
; Sleep(200)
;WEnd
;GUICtrlSetData($status, "Playing...")
;$c = PixelChecksum($ox, $oy, $ox + 280, $oy + 280, 40)
;Sleep(100)
;While $c <> PixelChecksum($ox, $oy, $ox + 280, $oy + 280, 40)
; GUICtrlSetData($status, "Waiting for window to hold still...")
; Sleep(100)
; $c = PixelChecksum($ox, $oy, $ox + 280, $oy + 280, 40)
;WEnd
;GUICtrlSetData($status, "Updating graphic...")
UpdateColors()
;GUICtrlSetData($status, "Playing...")
;Sleep(100)
DoEvents()
If $tick > 999 Then
;DidTableMove()
$tick = 0
EndIf
$time = GetIsTimeLeft()
WEnd
GUICtrlSetData($status, "Out of time! Waiting for Play Again button")
While PixelGetColor($ox - 50, $oy + 280) <> 14772916
DoEvents()
ClipPut(PixelGetColor($ox - 50, $oy + 280))
;Sleep(250)
WEnd
GUICtrlSetData($status, "Waiting for new game to start.")
While GetIsInGame() = False
Sleep(50)
WEnd
; GAME LOGIC END
WEnd
DllClose($dll)
Exit
Func GetBlitzX($x)
Return $ox + ($x - 1) * 40
EndFunc ;==>GetBlitzX
Func GetBlitzY($y)
Return $oy + ($y - 1) * 40 + 1
EndFunc ;==>GetBlitzY
Func GetIsInGame()
If PixelGetColor($ox - 100, $oy + 150) <> 4337712 Then Return False
Return True
EndFunc ;==>GetIsInGame
Func GetIsTimeLeft()
If PixelGetColor($ox - 14, $oy + 310) = 4397348 Then
Return False
EndIf
Return True
EndFunc ;==>GetIsTimeLeft
Func DoEvents()
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then
GUIDelete()
Exit
EndIf
EndFunc ;==>DoEvents
Func UpdateMasterTable()
$flag = False
For $y = 1 To 8
For $x = 1 To 8
$nc = PixelGetColor(GetBlitzX($x), GetBlitzY($y))
If $mt[$x][$y] <> $nc Then
$flag = True
$mt[$x][$y] = $nc
EndIf
Next
Next
Return $flag
EndFunc ;==>UpdateMasterTable
Func UpdateColors()
If UpdateMasterTable() = True Then
SearchSolutions()
For $y = 1 To 8
For $x = 1 To 8
$clr = GetColorFromName(GetColorName($mt[$x][$y]))
If $sol[$x][$y] > 0 Then
GUICtrlSetGraphic($graphic, $GUI_GR_COLOR, $clr, $clr)
Else
GUICtrlSetGraphic($graphic, $GUI_GR_COLOR, $clr, 0)
EndIf
$tx = 21 * ($x - 1) + 2
$ty = 21 * ($y - 1) + 2
GUICtrlSetGraphic($graphic, $GUI_GR_RECT, $tx, $ty, 20, 20)
Next
Next
GUICtrlSetGraphic($graphic, $GUI_GR_REFRESH)
UpdateClippy()
EndIf
EndFunc ;==>UpdateColors
Func FindTable()
$co = "="&chr(13)&chr(10)
For $v = 350 To 733
For $u = 280 To 788
$flag = False
$fcolor = PixelGetColor($u, $v)
$fcount = 0
If Not $fcolor = 0 Then
For $y = 350 To 733
For $x = 280 To 788
If PixelGetColor($x, $y) = $fcolor Then
$fcount = $fcount + 1
If $fcount > 1 Then $flag = True
EndIf
If $flag = True Then ExitLoop
Next
GUICtrlSetData($status, "Finding: (" & $u & "," & $v & "," & $x & "," & $y & ") " & $fcount & " " & $fcolor)
If $flag = True Then ExitLoop
Next
If $fcount = 1 Then
Beep(1000, 1)
mousemove($u,$v)
$co =$co+ $u & ", " & $v & ", " & $fcolor & Chr(13) & Chr(10)
ClipPut($co)
EndIf
EndIf
Next
Next
Exit
$sc = PixelSearch(0, 0, @DesktopWidth, @DesktopHeight, 1249842)
If @error Then
GUICtrlSetData($status, "Still searching for blitz window...")
DoEvents()
Else
$oox = $sc[0]
$ooy = $sc[1]
$ox = $oox + 11
$oy = $ooy + 51
;~ mousemove($oox,$ooy,10)
;~ sleep(100)
;~ mousemove($ox,$oy,10)
;~ Exit
$flag = True
EndIf
EndFunc ;==>FindTable
Func DidTableMove()
If PixelGetColor($oox, $ooy) <> 1249842 Then
Sleep(100)
FindTable()
EndIf
EndFunc ;==>DidTableMove
Func UpdateClippy()
Return
$o = ""
For $y = 1 To 8
$o = $o & GetColorName($mt[1][$y])
For $x = 2 To 8
$o = $o & "," & GetColorName($mt[$x][$y])
Next
$o = $o & Chr(13) & Chr(10)
Next
ClipPut($o)
EndFunc ;==>UpdateClippy
Func GetColorName($c)
Select
Case $c = 1091362
Return "Green"
Case $c = 15198183
Return "White"
Case $c = 15362338
Return "Orange"
Case $c = 16709156
Return "Yellow"
Case $c = 16456503
Return "Red"
Case $c = 15798257
Return "Purple"
Case $c = 1150206
Return "Blue"
Case $c = 0
Return "Black"
Case Else
Return "Unknown"
EndSelect
EndFunc ;==>GetColorName
Func GetColorFromName($c)
Select
Case $c = "Green"
Return 065280
Case $c = "White"
Return 016777215
Case $c = "Orange"
Return 016744192
Case $c = "Yellow"
Return 016776960
Case $c = "Red"
Return 016711680
Case $c = "Purple"
Return 016711935
Case $c = "Blue"
Return 0255
Case $c = "Black"
Return 0
Case Else
Return 0
EndSelect
EndFunc ;==>GetColorFromName
Func SearchSolutions()
SearchValidMoves()
If Not SearchFiveInARow() Then
If Not SearchFiveLShape() Then
If Not SearchFutureFiveInARow() Then
If Not SearchFutureFiveLShape() Then
If Not SearchFutureFourInARow() Then
If Not SearchFourInARow() Then
SearchForAThree()
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
EndFunc ;==>SearchSolutions
Func SearchValidMoves()
;Clear Solution Table
EndFunc ;==>SearchValidMoves
Func SearchFiveInARow()
Return False
EndFunc ;==>SearchFiveInARow
Func SearchFiveLShape()
Return False
EndFunc ;==>SearchFiveLShape
Func SearchFourInARow()
Return False
EndFunc ;==>SearchFourInARow
Func SearchFutureFiveInARow()
Return False
EndFunc ;==>SearchFutureFiveInARow
Func SearchFutureFiveLShape()
Return False
EndFunc ;==>SearchFutureFiveLShape
Func SearchFutureFourInARow()
Return False
EndFunc ;==>SearchFutureFourInARow
Func SearchForAThree()
LoadTT()
$flag = False
$rr = 6
If $tick > 0 Then $rr = 1
For $y = $rr To 8
For $x = 1 To 8
$sol[$x][$y] = 0
If GetColorName($tt[$x][$y]) <> "Black" And GetColorName($tt[$x][$y]) <> "Unknown" Then
If $x < 8 And $y > 1 And $y < 8 Then
If $tt[$x][$y] = $tt[$x + 1][$y + 1] And $tt[$x][$y] = $tt[$x + 1][$y - 1] Then $sol[$x][$y] = 7002 ; G
EndIf
If $x > 1 And $y > 1 And $y < 8 Then
If $tt[$x][$y] = $tt[$x - 1][$y + 1] And $tt[$x][$y] = $tt[$x - 1][$y - 1] Then $sol[$x][$y] = 8004 ; H
EndIf
If $y > 2 Then
If $x < 8 Then
If $tt[$x][$y] = $tt[$x + 1][$y - 1] And $tt[$x][$y] = $tt[$x + 1][$y - 2] Then $sol[$x][$y] = 1002 ; A
EndIf
If $x > 1 Then
If $tt[$x][$y] = $tt[$x - 1][$y - 1] And $tt[$x][$y] = $tt[$x - 1][$y - 2] Then $sol[$x][$y] = 2004 ; B
EndIf
EndIf
If $y < 7 Then
If $x < 8 Then
If $tt[$x][$y] = $tt[$x + 1][$y + 1] And $tt[$x][$y] = $tt[$x + 1][$y + 2] Then $sol[$x][$y] = 5002 ; E
EndIf
If $x > 1 Then
If $tt[$x][$y] = $tt[$x - 1][$y + 1] And $tt[$x][$y] = $tt[$x - 1][$y + 2] Then $sol[$x][$y] = 6004 ; F
EndIf
EndIf
If $x > 3 Then
If $tt[$x][$y] = $tt[$x - 2][$y] And $tt[$x][$y] = $tt[$x - 3][$y] Then $sol[$x][$y] = 15004 ; O
EndIf
If $x < 6 Then
If $tt[$x][$y] = $tt[$x + 2][$y] And $tt[$x][$y] = $tt[$x + 3][$y] Then $sol[$x][$y] = 17002 ; P
EndIf
If $y > 1 And $x > 1 And $x < 8 Then
If $tt[$x][$y] = $tt[$x - 1][$y - 1] And $tt[$x][$y] = $tt[$x + 1][$y - 1] Then $sol[$x][$y] = 11001 ; K
EndIf
If $y < 8 And $x > 1 And $x < 8 Then
If $tt[$x][$y] = $tt[$x - 1][$y + 1] And $tt[$x][$y] = $tt[$x + 1][$y + 1] Then $sol[$x][$y] = 12003 ; L
EndIf
;If Mod($tick, 2) = 1 Then
If $x < 7 Then
If $y > 1 Then
If $tt[$x][$y] = $tt[$x + 1][$y - 1] And $tt[$x][$y] = $tt[$x + 2][$y - 1] Then $sol[$x][$y] = 3001 ; C
EndIf
If $y < 8 Then
If $tt[$x][$y] = $tt[$x + 1][$y + 1] And $tt[$x][$y] = $tt[$x + 2][$y + 1] Then $sol[$x][$y] = 10003 ; J
EndIf
EndIf
If $x > 2 Then
If $y > 1 Then
If $tt[$x][$y] = $tt[$x - 1][$y - 1] And $tt[$x][$y] = $tt[$x - 2][$y - 1] Then $sol[$x][$y] = 4001 ; D
EndIf
If $y < 8 Then
If $tt[$x][$y] = $tt[$x - 1][$y + 1] And $tt[$x][$y] = $tt[$x - 2][$y + 1] Then $sol[$x][$y] = 9003 ; I
EndIf
EndIf
If $y < 6 Then
If $tt[$x][$y] = $tt[$x][$y + 2] And $tt[$x][$y] = $tt[$x][$y + 3] Then $sol[$x][$y] = 13003 ; M
EndIf
If $y > 3 Then
If $tt[$x][$y] = $tt[$x][$y - 2] And $tt[$x][$y] = $tt[$x][$y - 3] Then $sol[$x][$y] = 14001 ; N
EndIf
EndIf
if $sol[$x][$y]<>0 then ExitLoop
Next
if $sol[$x][$y]<>0 then ExitLoop
Next
$flag = False
For $y = 8 to $rr step -1
For $x = 1 To 8
If $sol[$x][$y] <> 0 Then
$flag = True
ExitLoop
EndIf
Next
If $flag = True Then ExitLoop
Next
MakeAMove($x, $y, $sol[$x][$y])
Return True
EndFunc ;==>SearchForAThree
Func LoadTT()
For $x = 0 To 9
For $y = 0 To 9
$tt[$x][$y] = 0
Next
Next
For $x = 1 To 8
For $y = 1 To 8
$tt[$x][$y] = $mt[$x][$y]
$sol[$x][$y] = False
Next
Next
Return True
EndFunc ;==>LoadTT
Func MakeAMove($x, $y, $dd)
If Not _IsPressed("A0", $dll) Then
$xd = 0
$yd = 0
If $x > 0 And $x < 9 And $y > 0 And $y < 9 And ($x + $xd) > 0 And ($x + $xd < 9) And ($y + $yd) > 0 And ($y + $yd) < 9 Then
$d = $dd
While $d >= 1000
$d = $d - 1000
WEnd
If $d > 4 Then $d = 0
Select
Case $d = 1
$yd = -1
Case $d = 2
$xd = 1
Case $d = 3
$yd = +1
Case $d = 4
$xd = -1
EndSelect
GUICtrlSetData($status, "Moving " & $x & "," & $y & " ->" & $x + $xd & "," & $y + $yd & " (" & $dd & ")" & GetColorName($tt[$x][$y]))
;MouseClickDrag("primary", GetBlitzX($x), GetBlitzY($y), GetBlitzX($x + $xd), GetBlitzY($y + $yd),0)
MouseMove(GetBlitzX($x), GetBlitzY($y), 0)
Sleep(50)
MouseClick("primary", GetBlitzX($x), GetBlitzY($y), 1)
MouseMove(GetBlitzX($x + $xd), GetBlitzY($y + $yd), 1)
Sleep(50)
MouseClick("primary", GetBlitzX($x + $xd), GetBlitzY($y + $yd), 1)
EndIf
DoEvents()
EndIf
EndFunc ;==>MakeAMove