#include-once #include #include Global $CellW = 35 ;Chiều rộng ô Global $CellH = 35 ;Chiều dài ô Global $Map[24][30] ;Vị trí mìn Global $Stats[24][30] ;Các ô Global $Stats2[24][30] ;Trạng thái các ô Global $MWidth = 9 ;Bản đồ Global $MHeight = 9 ;Bản đồ Global $MMines = 10 ;Số mìn Global $UMines = 10 ;Số mìn còn lại Global $Playing = False ;Trạng thái game Global $First_click = False ;cái này cho bộ đếm thời gian Global $Count_time = 0 ;Biến đếm thời gian Global $DMoveX[9] = [-1,0,1,1,1,0,-1,-1,0] ;các hướng di chuyển Global $DMovey[9] = [-1,-1,-1,0,1,1,1,0,0] ;các hướng di chuyển ;// Tạo GUI $hGUI = GUICreate('AutMinesweeper',335,385) GUISetFont(9,default,default,'Arial',default,100) $FMenu = GUICtrlCreateMenu('Game') $NGame = GUICtrlCreateMenuItem('New Game',$Fmenu) GUICtrlCreateMenuItem('',$Fmenu) $SGame = GUICtrlCreateMenuItem('Statistics',$Fmenu) $OGame = GUICtrlCreateMenuItem('Options',$Fmenu) GUICtrlCreateMenuItem('',$Fmenu) $EGame = GUICtrlCreateMenuItem('Exit',$Fmenu) $HMenu = GUICtrlCreateMenu('Help') $VHelp = GUICtrlCreateMenuItem('View Help F1',$Hmenu) GUICtrlCreateMenuItem('',$Hmenu) $AHelp = GUICtrlCreateMenuItem('About AutMinesweeper',$Hmenu) GUICtrlCreateMenuItem('',$Hmenu) $GHelp = GUICtrlCreateMenuItem('Get more AutGame!',$Hmenu) ;//////////////////////////////////////////////////////////// GUICtrlCreateLabel('Õ',50,325,40,40) GUICtrlSetFont(-1,40,400,Default,'Webdings') GUICtrlCreateLabel('Õ',232.5,325,40,40) GUICtrlSetFont(-1,40,400,Default,'Webdings') GUICtrlCreateLabel('l',75,335,40,40) GUICtrlSetFont(-1,11,500) GUICtrlSetBkColor(-1,-2) GUICtrlCreateLabel('!',252,337,40,40) GUICtrlSetFont(-1,10,500,Default,'Webdings') GUICtrlSetBkColor(-1,-2) GUICtrlCreateLabel('\',76,344,40,40) GUICtrlSetFont(-1,8,400) GUICtrlSetBkColor(-1,-2) GUICtrlCreateButton('',5,5,324,324) GUICtrlSetBkColor(-1,0xd0d0d0) GUICtrlSetState(-1,128) GUICtrlCreateLabel('',95,337.5,50,20,0x100b) GUICtrlCreateLabel('',190,337.5,50,20,0x100b) ;//////////////////////////////////////////////////////////// $Timer_Show = GUICtrlCreateLabel(0,96,338.5,48,18,0x201) $Mine_Show = GUICtrlCreateLabel($UMines,191,338.5,48,18,0x201) For $i = 0 to $MWidth-1 For $u = 0 to $MHeight-1 GUICtrlCreateLabel('',10+$CellW*$i,10+$CellH*$u,$CellW-1,$CellH-1,0x100B) GUICtrlSetState(-1,128) $Stats[$i][$u] = GUICtrlCreateLabel('',12+$CellW*$i,12+$CellH*$u,$CellW-5,$CellH-5,0x0201) GUICtrlSetFont(-1,18,800) GUICtrlSetBkColor(-1,0xa0a0ff) Next Next newgame() GUISetState() While True $msg = GUIGetMsg() Switch $msg case -3 Exit case $EGame Exit case $Ngame newgame() case $SGame MsgBox(4096,'AutMinesweeper','Not available now!',0,$hGUI) case $OGame MsgBox(4096,'AutMinesweeper','Not available now!',0,$hGUI) case $VHelp MsgBox(4096,'AutMinesweeper','Not available now!',0,$hGUI) case $AHelp MsgBox(4096,'AutMinesweeper','Not available now!',0,$hGUI) case $GHelp ShellExecute('http://hocautoit.com/') EndSwitch WEnd Func newgame() AdlibUnRegister('timer') AdlibUnRegister('event') Dim $Map[24][30] Dim $Stats2[24][30] For $i = 0 to $MWidth-1 For $u = 0 to $MHeight-1 GUICtrlSetData($Stats[$i][$u],'') GUICtrlSetFont($Stats[$i][$u],18,800) GUICtrlSetColor($Stats[$i][$u],0x) GUICtrlSetBkColor($Stats[$i][$u],0xa0a0ff) $Stats2[$i][$u] = 1 Next Next Dim $UMines = $MMines Dim $Playing = True Dim $First_click = False addmines() AdlibRegister('event',100) EndFunc Func event() If WinActive($hGUI) == 0 then return False Local $MPo = GUIGetCursorInfo($hGUI) If @error Then Return False For $i = 0 to $MWidth-1 For $u = 0 To $MHeight-1 If $MPo[4] == $Stats[$i][$u] Then If $Playing == True Then If _IsPressed(01) And _IsPressed(02) Then Updown($i,$u,0) While _IsPressed(01) And _IsPressed(02) Sleep(10) WEnd Updown($i,$u,1) QuestionMarks($i,$u) ElseIf _IsPressed(02) Then FlagCell($i,$u) While _IsPressed(02) Sleep(10) WEnd ElseIf _IsPressed(01) Then If $First_click == False Then $First_click = True $Count_time = TimerInit() AdlibRegister('timer',100) EndIf $Res = open($i,$u,1) If $Res == 'Bom' Then Boooom() $Playing = False AdlibUnRegister('timer') MsgBox(4096,'AutMinesweeper','Game over',0,$hGUI) EndIf While _IsPressed(01) Sleep(10) WEnd EndIf EndIf EndIf Next Next If checkwin() == True Then $Playing = False AdlibUnRegister('timer') AdlibUnRegister('event') MsgBox(4096,'AutMinesweeper','Win',0,$hGUI) EndIf EndFunc Func color($snum) Switch $snum case 1 Return 0x0100FE case 2 return 0x017F01 case 3 return 0xFE0000 case 4 Return 0x010080 case 5 return 0x810102 case 6 Return 0x008081 case 7 return 0x000000 case 8 return 0x808080 EndSwitch return 0xabcdef EndFunc Func timer() Local $GetTime = Floor(TimerDiff($Count_time)/1000) If GUICtrlRead($timer_show) <> $GetTime then GUICtrlSetData($timer_show,$Gettime) EndFunc Func Updown($r,$c,$type) For $n = 0 to 8 If maps($r+$DMoveX[$n],$c+$DMovey[$n]) == True Then If $stats2[$r+$DMoveX[$n]][$c+$DMovey[$n]] == 1 Then If $type = 1 Then GUICtrlSetBkColor($stats[$r+$DMoveX[$n]][$c+$DMovey[$n]],0xa0a0ff) Else GUICtrlSetBkColor($stats[$r+$DMoveX[$n]][$c+$DMovey[$n]],0xd0d0d0) EndIf EndIf EndIf Next EndFunc Func QuestionMarks($r,$c) If $Stats2[$r][$c] == 1 or $Stats2[$r][$c] == 3 Then Return False If Maps($r,$c) == False Then Return False Local $Num = 0 For $n = 0 to 7 If maps($r+$DMoveX[$n],$c+$DMovey[$n]) == True Then If $map[$r+$DMoveX[$n]][$c+$DMovey[$n]] == 1 And $Stats2 [$r+$DMoveX[$n]][$c+$DMovey[$n]] == 1 Then $Num += 1 EndIf EndIf Next If $Num == 0 Then For $n = 0 to 7 If maps($r+$DMoveX[$n],$c+$DMovey[$n]) == True Then If $Stats2[$r+$DMoveX[$n]][$c+$DMovey[$n]] == 1 Then Open($r+$DMoveX[$n],$c+$DMovey[$n],1) EndIf EndIf Next EndIf EndFunc Func countMines($r,$c) Local $Num = 0 For $n = 0 to 7 If maps($r+$DMoveX[$n],$c+$DMovey[$n]) == True Then If $map[$r+$DMoveX[$n]][$c+$DMovey[$n]] == 1 Then $Num += 1 EndIf EndIf Next Return $Num EndFunc Func FlagCell($r,$c) If $Stats2[$r][$c] == 2 Then Return False If $Stats2[$r][$c] == 1 Then $Stats2[$r][$c] = 3 $UMines -= 1 GUICtrlSetData($Stats[$r][$c],'w') GUICtrlSetColor($Stats[$r][$c],0xff0000) GUICtrlSetFont($Stats[$r][$c],25,800,Default,'Webdings') ElseIf $Stats2[$r][$c] == 3 Then $Stats2[$r][$c] = 1 $UMines += 1 GUICtrlSetData($Stats[$r][$c],'') GUICtrlSetColor($Stats[$r][$c],0x) GUICtrlSetFont($Stats[$r][$c],18,800,Default,'Arial') EndIf GUICtrlSetData($Mine_show,$UMines) Return True EndFunc Func open($r,$c,$type) If $Stats2[$r][$c] == 2 or $Stats2[$r][$c] == 3 Then Return False If Maps($r,$c) == False Then Return False If $map[$r][$c] == 1 Then If $type == 1 Then Return 'Bom' Return False EndIf Local $Cnt = countMines($r,$c) $Stats2[$r][$c] = 2 GUICtrlSetBkColor($Stats[$r][$c],0xd0d0d0) If $Cnt > 0 Then GUICtrlSetData($Stats[$r][$c],$Cnt) GUICtrlSetColor($Stats[$r][$c],color($Cnt)) Else For $n = 0 to 7 $rX = $r+$DMoveX[$n] $cY = $c+$DMoveY[$n] If Maps($rX,$cY) == True Then open($rX,$cY,0) EndIf Next EndIf Return False EndFunc Func Maps($r,$c) If $r > -1 and $r < $MWidth and $c > -1 and $c < $MHeight Then Return True Return False EndFunc Func Boooom() for $i = 0 to $MWidth-1 for $u = 0 to $MHeight-1 If $Map[$i][$u] == 1 Then GUICtrlSetData($Stats[$i][$u],'!') GUICtrlSetFont($Stats[$i][$u],20,600,Default,'Webdings') GUICtrlSetBkColor($Stats[$i][$u],0xd0d0d0) EndIf Next Next EndFunc Func checkwin() Local $MaxCellAvai = $MWidth*$MHeight-$MMines Local $Opened = 0 For $i = 0 to $MWidth-1 For $u = 0 to $MHeight-1 If $Stats2[$i][$u] == 2 then $Opened += 1 Next Next If $Opened == $MaxCellAvai then Return True Return False EndFunc Func addmines() For $i = 1 to $MMines Do Local $mmr_ = Random(0,$MWidth-1,1) Local $mmr__ = Random(0,$MHeight-1,1) If $Map[$mmr_][$mmr__] == '' Then $Map[$mmr_][$mmr__] = 1 ExitLoop EndIf Until True == False Next EndFunc