亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

python最簡單的五子棋代碼怎么寫

小億
230
2024-06-04 18:13:27
欄目: 編程語言

下面是一個簡單的五子棋游戲的Python代碼示例:

def print_board(board):
    for row in board:
        print(' '.join(row))

def check_win(board, player):
    for i in range(5):
        for j in range(5):
            if board[i][j] == player:
                # 檢查橫向是否有五子相連
                if j + 4 < 5 and all(board[i][j+k] == player for k in range(5)):
                    return True
                # 檢查縱向是否有五子相連
                if i + 4 < 5 and all(board[i+k][j] == player for k in range(5)):
                    return True
                # 檢查斜向是否有五子相連
                if i + 4 < 5 and j + 4 < 5 and all(board[i+k][j+k] == player for k in range(5)):
                    return True
                # 檢查反斜向是否有五子相連
                if i + 4 < 5 and j - 4 >= 0 and all(board[i+k][j-k] == player for k in range(5)):
                    return True
    return False

def main():
    board = [['.' for _ in range(5)] for _ in range(5)]
    player = 'X'

    while True:
        print_board(board)
        move = input(f"Player {player}, enter your move (row col): ")
        row, col = map(int, move.split())

        if board[row][col] == '.':
            board[row][col] = player
            if check_win(board, player):
                print_board(board)
                print(f"Player {player} wins!")
                break
            player = 'O' if player == 'X' else 'X'
        else:
            print("Invalid move, try again.")

if __name__ == '__main__':
    main()

這個代碼實現了一個簡單的五子棋游戲,玩家交替下棋,當有任意一方連成五子時游戲結束并宣布勝利。玩家通過輸入行列坐標來下棋,游戲會在控制臺輸出當前棋盤狀態。

0
乌鲁木齐市| 延寿县| 上饶县| 茶陵县| 南投县| 洛南县| 巴南区| 大丰市| 永吉县| 甘德县| 大悟县| 阿瓦提县| 北流市| 晋江市| 沙雅县| 湘潭市| 中西区| 巴林右旗| 德阳市| 金塔县| 浑源县| 建瓯市| 新乡市| 沅江市| 罗江县| 怀集县| 仁寿县| 常宁市| 商丘市| 寿宁县| 新巴尔虎右旗| 丹巴县| 通城县| 明光市| 佳木斯市| 七台河市| 泰州市| 华坪县| 江阴市| 尖扎县| 阜宁县|