由于答案长度限制,以下为简化版的飞行棋代码,仅包含基本的游戏逻辑和操作。

import random

# 棋盘大小
SIZE = 52

# 棋盘上每个位置的信息
BOARD = [
    {"type": "start"},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "normal", "skip_turn": True},
    {"type": "normal"},
    {"type": "normal"},
    {"type": "stop"}
]

# 飞机的起点和终点位置
START = [0, 10, 20, 30]
END = [51, 41, 31, 21]

# 每个玩家的飞机
PLANES = {
    1: [{"pos": START[i], "finished": False} for i in range(4)],
    2: [{"pos": START[i], "finished": False} for i in range(4)]
}

# 当前玩家
current_player = 1

# 控制台输出
def print_board():
    print("Current player: Player", current_player)
    for i in range(SIZE):
        if i in START:
            print("S", end=" ")
        elif i in END:
            print("E", end=" ")
        else:
            print("+", end=" ")
        for j in range(4):
            if PLANES[j+1][0]["pos"] == i:
                print(j+1, end=" ")
            elif PLANES[j+1][1]["pos"] == i:
                print(j+1, end=" ")
            elif PLANES[j+1][2]["pos"] == i:
                print(j+1, end=" ")
            elif PLANES[j+1][3]["pos"] == i:
                print(j+1, end=" ")
            else:
                print(".", end=" ")
        print()

# 抛骰子
def roll_dice():
    return random.randint(1, 6)

# 移动飞机
def move_plane(plane, steps):
    pos = plane["pos"]
    if pos in START:
        # 飞机在起点
        if steps == 6:
            plane["pos"] = pos + 1
        else:
            print("You need to roll 6 to take off!")
    elif plane["finished"]:
        print("This plane has already reached the end!")
    else:
        # 飞机在棋盘上
        new_pos = pos + steps
        if new_pos > SIZE - 1:
            # 飞机超过终点
            print("This plane cannot move that far!")
        else:
            # 飞机正常移动
            plane["pos"] = new_pos
            square = BOARD[new_pos]
            if square["type"] == "stop":
                # 飞机停在路障上
                print("This plane stops at a barrier!")
            elif square["skip_turn"]:
                # 飞机停在飞行棋上
                print("This plane stops at a flying chess!")
            else:
                # 飞机正常移动到一个空格上
                print("This plane moves to a normal square!")

# 判断游戏是否结束
def check_game_over():
    for i in range(4):
        if not PLANES[current_player][i]["finished"]:
            return False
    return True

# 主循环
while True:
    print_board()

    # 判断当前玩家是否无法移动
    cannot_move = True
    for i in range(4):
        if not PLANES[current_player][i]["finished"]:
            pos = PLANES[current_player][i]["pos"]
            steps = roll_dice()
            if pos in START and steps == 6:
                # 起飞
                cannot_move = False
                print("You roll 6! Take off!")
                PLANES[current_player][i]["pos"] = pos + 1
            elif pos not in START:
                # 移动
                cannot_move = False
                print("Roll dice:", steps)
                move_plane(PLANES[current_player][i], steps)
    
    if cannot_move:
        print("Player", current_player, "cannot move! Skip turn!")

    # 判断游戏是否结束
    if check_game_over():
        print("Game over! Player", current_player, "wins!")
        break

    # 切换到下一个玩家
    current_player = 3 - current_player  # 1 -> 2, 2 -> 1
用Python写个飞行棋代码

原文地址: https://www.cveoy.top/t/topic/FIo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录