; 五子棋游戏
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
Global $szPlayer[2] = ["黑棋", "白棋"]
Global $g_bGameOver = False
Global $g_nLevel = 15 ; 棋盘大小
Global $g_nGrid = 30 ; 宽高
Global $g_nChessman = -1
Global $g_arChessBoard
Global Const $IDC_BTN_EXIT = 1000
Global Const $IDC_BTN_RESTART = 1001
Func _Main()
Local $hGUI = GUICreate("五子棋游戏", $g_nLevel * $g_nGrid, $g_nLevel * $g_nGrid)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_PAINT, "WM_PAINT")
GUIRegisterMsg($WM_LBUTTONDOWN, "WM_LBUTTONDOWN")
_InitChessBoard() ; 初始化棋盘
Local $hBtnRestart = GUICtrlCreateButton("重新开始", 8 * $g_nGrid, ($g_nLevel + 1) * $g_nGrid, 4 * $g_nGrid, 2 * $g_nGrid, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW, $hBtnRestart)
GUICtrlSetOnEvent(-1, "BTN_RESTART")
Local $hBtnExit = GUICtrlCreateButton("退出游戏", 12 * $g_nGrid, ($g_nLevel + 1) * $g_nGrid, 4 * $g_nGrid, 2 * $g_nGrid, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW, $hBtnExit)
GUICtrlSetOnEvent(-1, "BTN_EXIT")
GUISetState(@SW_SHOW)
While (1)
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
GUIDelete()
EndFunc
Func WM_PAINT()
Local $hWnd = WinGetHandle("五子棋游戏")
Local $hDC = _WinAPI_GetDC($hWnd)
_DrawChessBoard($hDC) ; 画棋盘
Local $tPos
For $iRow = 0 To $g_nLevel - 1
For $iCol = 0 To $g_nLevel - 1
If Not UBound($g_arChessBoard[$iRow][$iCol]) Then
ContinueLoop
EndIf
$tPos = WinAPI_GetPos($iRow, $iCol)
_DrawChessman($hDC, $tPos, 1, $g_arChessBoard[$iRow][$iCol][0], $g_arChessBoard[$iRow][$iCol][1])
Next
Next
If $g_bGameOver Then
Local $hWnd = WinGetHandle("五子棋游戏")
Local $szText = StringFormat("游戏结束,%s赢了", $szPlayer[$g_nChessman - 1])
WinAPI_MessageBox($hWnd, $szText, "五子棋", BitOR($MB_ICONASTERISK, $MB_OK))
EndIf
WinAPI_ReleaseDC($hWnd, $hDC)
EndFunc
Func WM_LBUTTONDOWN()
If $g_bGameOver Then
Return 0
EndIf
Local $tPos = WinAPI_GetPos(GUICtrlRead($hGUI, $IDC_GUICONTROL))
Local $iRow = Int($tPos[0] / $g_nGrid)
Local $iCol = Int($tPos[1] / $g_nGrid)
If UBound($g_arChessBoard[$iRow][$iCol]) Then
Return 0 ; 有棋子,不做处理
EndIf
$g_arChessBoard[$iRow][$iCol] = [++$g_nChessman, 0]
Local $bWin = _IsWin($iRow, $iCol, $g_nChessman)
If $bWin Then
$g_bGameOver = True
Else
$g_nChessman = BitXOR($g_nChessman, 3)
EndIf
Local $hWnd = WinGetHandle("五子棋游戏")
Local $hDC = _WinAPI_GetDC($hWnd)
_DrawChessBoard($hDC) ; 画棋盘
For $iRow = 0 To $g_nLevel - 1
For $iCol = 0 To $g_nLevel - 1
If Not UBound($g_arChessBoard[$iRow][$iCol]) Then
ContinueLoop
EndIf
$tPos = WinAPI_GetPos($iRow, $iCol)
_DrawChessman($hDC, $tPos, 1, $g_arChessBoard[$iRow][$iCol][0], $g_arChessBoard[$iRow][$iCol][1])
Next
Next
If $g_bGameOver Then
Local $szText = StringFormat("游戏结束,%s赢了", $szPlayer[$g_nChessman - 1])
WinAPI_MessageBox($hWnd, $szText, "五子棋", BitOR($MB_ICONASTERISK, $MB_OK))
EndIf
WinAPI_ReleaseDC($hWnd, $hDC)
EndFunc
Func BTN_EXIT()
Exit
EndFunc
Func BTN_RESTART()
_InitChessBoard() ; 初始化棋盘
Local $hWnd = WinGetHandle("五子棋游戏")
Local $hDC = _WinAPI_GetDC($hwnd)
_DrawChessBoard($hDC) ; 画棋盘
WinAPI_ReleaseDC($hWnd, $hDC)
EndFunc