五子棋游戏

# 定义棋盘大小
size = 15
# 定义棋盘
chessboard = [['+' for _ in range(size)] for _ in range(size)]
# 定义当前玩家,初始为黑棋
current_player = 'black'

# 打印棋盘
def print_board(chessboard):
    for row in chessboard:
        print(' '.join(row))

# 检查是否五子连珠
def check_win(chessboard, row, col):
    directions = [(1, 0), (0, 1), (1, 1), (-1, 1)] # 横向、纵向、斜向检查
    for d in directions:
        count = 1
        for i in range(1, 5):
            new_row = row + i * d[0]
            new_col = col + i * d[1]
            if 0 <= new_row < size and 0 <= new_col < size and chessboard[new_row][new_col] == chessboard[row][col]:
                count += 1
            else:
                break
        for i in range(1, 5):
            new_row = row - i * d[0]
            new_col = col - i * d[1]
            if 0 <= new_row < size and 0 <= new_col < size and chessboard[new_row][new_col] == chessboard[row][col]:
                count += 1
            else:
                break
        if count >= 5:
            return True
    return False

# 开始游戏
def play():
    while True:
        print_board(chessboard)
        print(current_player + " player's turn.")
        move = input("Please enter your move (row, col): ")
        try:
            row, col = map(int, move.split(','))
            if 0 <= row < size and 0 <= col < size and chessboard[row][col] == '+':
                chessboard[row][col] = 'O' if current_player == 'black' else 'X'
                if check_win(chessboard, row, col):
                    print_board(chessboard)
                    print(current_player + " player wins!")
                    break
                current_player = 'white' if current_player == 'black' else 'black'
            else:
                print("Invalid move. Please try again.")
        except ValueError:
            print("Invalid input. Please enter row and column separated by comma.")

# 开始游戏
play()

蜘蛛纸牌

import random

# 定义纸牌花色和面值
suits = ['♠', '♥', '♦', '♣']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

# 创建一副标准的纸牌
deck = [rank + suit for suit in suits for rank in ranks]

# 混洗纸牌
random.shuffle(deck)

# 定义牌堆
tableaus = [[] for _ in range(10)]  # 10个牌堆
foundations = [[] for _ in range(8)]  # 8个基础牌堆

# 发牌
for i in range(4):
    for j in range(5):
        tableaus[j].append(deck.pop())

# 显示牌局
def display_game():
    print("Tableaus:")
    for i, tableau in enumerate(tableaus):
        print(f"{i+1}: {' '.join(tableau)}")
    print("\nFoundations:")
    for i, foundation in enumerate(foundations):
        print(f"{chr(65+i)}: {' '.join(foundation)}")

# 移动牌
def move_card(source, target):
    card = tableaus[source].pop()
    tableaus[target].append(card)

# 判断移动是否合法
def is_valid_move(source, target):
    if source == target:
        return False
    if len(tableaus[source]) == 0:
        return False
    if len(tableaus[target]) == 0:
        return True
    source_card = tableaus[source][-1]
    target_card = tableaus[target][-1]
    source_rank, source_suit = source_card[:-1], source_card[-1]
    target_rank, target_suit = target_card[:-1], target_card[-1]
    if suits.index(source_suit) % 2 != suits.index(target_suit) % 2 and ranks.index(source_rank) + 1 == ranks.index(target_rank):
        return True
    return False

# 对输入进行解析
def parse_input(input_str):
    source_str, target_str = input_str.split()
    source = int(source_str) - 1
    target = ord(target_str.upper()) - 65
    return source, target

# 游戏主循环
def play():
    while True:
        display_game()
        move = input("Please enter your move (source target): ")
        try:
            source, target = parse_input(move)
            if is_valid_move(source, target):
                move_card(source, target)
            else:
                print("Invalid move. Please try again.")
            if len(deck) == 0 and all(len(tableau) == 0 for tableau in tableaus):
                print("Congratulations! You win!")
                break
        except ValueError:
            print("Invalid input. Please enter source and target separated by a space.")

# 开始游戏
play()