jj119120 发表于 2023-3-2 10:19:42

用ChatGpt生成的五子棋游戏代码

本帖最后由 jj119120 于 2023-3-2 15:49 编辑

我的要求是 用autoit3写一个五子棋游戏,要求棋盘15*15,黑白双方轮流下子,先下到五颗同样颜色的子连成一线的获胜

; 五子棋游戏
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
Global $szPlayer = ["黑棋", "白棋"]
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], $g_arChessBoard[$iRow][$iCol])
      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 / $g_nGrid)
    Local $iCol = Int($tPos / $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], $g_arChessBoard[$iRow][$iCol])
      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

结果是各种报错,无法运行,实践证明,也不是特别聪明

holley 发表于 2023-3-3 09:56:46

任何聪明都是有前提的
页: [1]
查看完整版本: 用ChatGpt生成的五子棋游戏代码