使用python写一个可以运行的斗兽棋代码
时间: 2024-02-04 15:02:14 浏览: 42
好的,以下是一个简单的斗兽棋代码示例:
```python
import random
# 初始化棋盘
board = [
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
]
# 定义棋子
pieces = {
'R1': '🦁', 'R2': '🐯', 'R3': '🐻', 'R4': '🐮', 'R5': '🐺',
'C1': '🐔', 'C2': '🐔', 'E1': '🐘', 'E2': '🐘', 'M': '🐵',
'D': '🐉', 'P': '🐲', 'S': '🦑', 'X': '🦀'
}
# 定义动物属性
animals = {
'R': {'name': 'Lion', 'power': 7, 'isRat': False},
'C': {'name': 'Chicken', 'power': 2, 'isRat': False},
'E': {'name': 'Elephant', 'power': 8, 'isRat': False},
'M': {'name': 'Monkey', 'power': 4, 'isRat': False},
'D': {'name': 'Dragon', 'power': 10, 'isRat': False},
'P': {'name': 'Panda', 'power': 6, 'isRat': False},
'S': {'name': 'Squid', 'power': 3, 'isRat': True},
'X': {'name': 'Crab', 'power': 2, 'isRat': True},
' ': {'name': 'River', 'power': 0, 'isRat': False}
}
# 定义玩家
players = {'R': 'Red', 'B': 'Black'}
# 选择先手
current_player = random.choice(['R', 'B'])
# 定义游戏结束函数
def game_over(winner):
print(f'{players[winner]} wins!')
exit()
# 定义移动动物函数
def move_animal(src_x, src_y, dst_x, dst_y):
# 获取源和目标位置的动物
src_animal = board[src_x][src_y][0]
dst_animal = board[dst_x][dst_y][0]
# 判断源位置是否有动物
if src_animal == ' ':
print('There is no animal at the source position!')
return False
# 判断动物是否可以移动到目标位置
if not is_valid_move(src_x, src_y, dst_x, dst_y):
print('Invalid move!')
return False
# 判断是否将老鼠吃掉
if dst_animal == 'S' and src_animal != 'X':
print('The rat can only be eaten by the crab!')
return False
# 判断是否进入水中
if board[dst_x][dst_y] == ' ' and animals[src_animal]['isRat']:
print('Rats cannot enter the water!')
return False
# 判断是否成功进入对方老巢
if dst_x == 0 or dst_x == 7:
if dst_y == 3:
if src_animal == 'R':
game_over('R')
else:
print('Only the lion can enter the enemy\'s den!')
return False
else:
print('Animals cannot enter the palace from this direction!')
return False
# 判断是否与自己的动物重叠
if dst_animal[0] == src_animal[0]:
print('Animals cannot overlap!')
return False
# 移动动物
board[dst_x][dst_y] = board[src_x][src_y]
board[src_x][src_y] = ' '
# 输出移动信息
print(f'{animals[src_animal]["name"]} from ({src_x}, {src_y}) moved to ({dst_x}, {dst_y})')
return True
# 定义判断移动是否合法的函数
def is_valid_move(src_x, src_y, dst_x, dst_y):
# 获取源和目标位置的动物
src_animal = board[src_x][src_y][0]
dst_animal = board[dst_x][dst_y][0]
# 判断目标位置是否在棋盘内
if dst_x < 0 or dst_x > 7 or dst_y < 0 or dst_y > 7:
return False
# 判断是否越过河流
if (src_x < 5 and dst_x >= 5) or (src_x >= 5 and dst_x < 5):
if dst_animal != ' ' and dst_animal != 'S':
return False
# 判断是否在同一行或同一列
if src_x != dst_x and src_y != dst_y:
return False
# 判断是否移动了动物
if src_x == dst_x and src_y == dst_y:
return False
# 判断是否跳过河流
if src_x == dst_x:
for i in range(min(src_y, dst_y) + 1, max(src_y, dst_y)):
if board[src_x][i] != ' ':
return False
else:
for i in range(min(src_x, dst_x) + 1, max(src_x, dst_x)):
if board[i][src_y] != ' ':
return False
# 判断是否成功进入对方老巢
if dst_x == 0 or dst_x == 7:
if dst_y == 3:
if src_animal != 'R':
return False
else:
return False
return True
# 输出棋盘
def print_board():
print(' 0 1 2 3 4 5 6 7')
print(' +---+---+---+---+---+---+---+---+')
for i in range(8):
row = f'{i}|'
for j in range(8):
row += f' {board[i][j]} |'
print(row)
print(' +---+---+---+---+---+---+---+---+')
# 游戏主循环
while True:
# 输出当前玩家
print(f'{players[current_player]}\'s turn:')
# 获取动物位置
src_x, src_y, dst_x, dst_y = input('Enter the source and destination positions (e.g. 2,3,3,3): ').split(',')
src_x, src_y, dst_x, dst_y = int(src_x), int(src_y), int(dst_x), int(dst_y)
# 移动动物
if move_animal(src_x, src_y, dst_x, dst_y):
# 切换玩家
current_player = 'B' if current_player == 'R' else 'R'
# 输出棋盘
print_board()
```
注意:此代码只是一个简单的示例,还有很多改进的空间,比如添加更多的错误检查和游戏规则,增加 AI 对手等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)