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 Game #2345

Open
lighting9999 opened this issue Sep 14, 2024 · 2 comments
Open

NEW Game #2345

lighting9999 opened this issue Sep 14, 2024 · 2 comments

Comments

@lighting9999
Copy link

lighting9999 commented Sep 14, 2024

# 初始化一个空白的棋盘
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()

这是游戏代码,This is tic tac toe.

@NitkarshChourasia
Copy link
Contributor

English would be much better for this repository and it's standard, thank you.

@lighting9999
Copy link
Author

lighting9999 commented Nov 16, 2024

Game Update:

import os
import pygame
# Initialize a blank chessboard.
board = [" " for _ in range(9)]

# Define a function to draw a chessboard
  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])

# Define a function to check the victory condition
def check_win(player):
    # Check all possible winning combinations
    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 

# Define a function to play a game
def play_game():
    current_player = "X"
    while True:
        display_board()
        move = input(f"game player {current_player},Please choose a location (1-9): ")
        if not move.isdigit() or int(move) < 1 or int(move) > 9 or board[int(move) - 1] != " ":
            print("Invalid selection, please reselect.")
            continue
        board[int(move) - 1] = current_player
        if check_win(current_player):
            display_board()
            print(f"Game player {current_player} WIN !")
            break
        if " " not in board:
            display_board()
            print("The game is tied.")
            break
        current_player = "X" if current_player == "O" else "O"

# Start the game
if __name__ == "__main__":
    play_game()

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

2 participants