跳棋小游戏Python
时间: 2024-01-07 08:22:18 浏览: 159
以下是一个简单的跳棋小游戏的Python代码示例:
```python
# 定义棋盘
board = [
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]
]
# 打印棋盘
def print_board(board):
for row in board:
print(row)
# 检查是否合法移动
def is_valid_move(board, start_row, start_col, end_row, end_col):
if board[start_row][start_col] == 1 and board[end_row][end_col] == 0:
if abs(start_row - end_row) == 2 and start_col == end_col:
return True
elif abs(start_col - end_col) == 2 and start_row == end_row:
return True
return False
# 执行移动
def make_move(board, start_row, start_col, end_row, end_col):
board[start_row][start_col] = 0
board[(start_row + end_row) // 2][(start_col + end_col) // 2] = 0
board[end_row][end_col] = 1
# 游戏主循环
def game_loop():
while True:
print_board(board)
start_row = int(input("请输入要移动的棋子的行号:"))
start_col = int(input("请输入要移动的棋子的列号:"))
end_row = int(input("请输入目标位置的行号:"))
end_col = int(input("请输入目标位置的列号:"))
if is_valid_move(board, start_row, start_col, end_row, end_col):
make_move(board, start_row, start_col, end_row, end_col)
else:
print("无效的移动,请重新输入。")
# 运行游戏
game_loop()
```
阅读全文