Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Games #105

Open
lighting9999 opened this issue Sep 14, 2024 · 1 comment
Open

NEW Games #105

lighting9999 opened this issue Sep 14, 2024 · 1 comment

Comments

@lighting9999
Copy link

# 初始化一个空白的棋盘
board = [" " for _ in range(9)]

# 定义一个函数来绘制棋盘
def display_board():
    print(board[0] + " | " + board[1] + " | " + board[2])
    print("---------")
    print(board[3] + " | " + board[4] + " | " + board[5])
    print("---------")
    print(board[6] + " | " + board[7] + " | " + board[8])

# 定义一个函数来检查胜利条件
def check_win(player):
    # 检查所有可能的胜利组合
    win_combinations = [(0, 1, 2), (3, 4, 5), (6, 7, 8),
                        (0, 3, 6), (1, 4, 7), (2, 5, 8),
                        (0, 4, 8), (2, 4, 6)]

    for combo in win_combinations:
        if board[combo[0]] == board[combo[1]] == board[combo[2]] == player:
            return True
    return False

# 定义一个函数来进行游戏
def play_game():
    current_player = "X"
    while True:
        display_board()
        move = input(f"玩家 {current_player},请选择一个位置 (1-9): ")
        if not move.isdigit() or int(move) < 1 or int(move) > 9 or board[int(move) - 1] != " ":
            print("无效的选择,请重新选择。")
            continue
        board[int(move) - 1] = current_player
        if check_win(current_player):
            display_board()
            print(f"玩家 {current_player} 获胜!")
            break
        if " " not in board:
            display_board()
            print("游戏平局。")
            break
        current_player = "X" if current_player == "O" else "O"

# 开始游戏
if __name__ == "__main__":
    play_game()
@lighting9999
Copy link
Author

this is new games tic tac toe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant