用python编写一个斗兽棋的小游戏
时间: 2023-09-07 19:13:42 浏览: 85
好的,我可以为您提供一个简单的斗兽棋游戏的代码示例,您可以在此基础上进行修改和拓展。
首先,我们需要定义一些游戏中用到的常量和数据结构。在这个例子中,我们使用字典来表示棋盘,每个字典条目的键是一个二元组表示棋子在棋盘上的位置,值是该位置上的棋子。
```
# 定义常量
BOARD_SIZE = 7
ANIMALS = {'鼠': 1, '猫': 2, '狼': 3, '狗': 4, '豹': 5, '虎': 6, '狮': 7, '象': 8}
ANIMAL_NAMES = list(ANIMALS.keys())
# 初始化棋盘
board = {(i, j): None for i in range(BOARD_SIZE) for j in range(BOARD_SIZE)}
# 初始化棋子位置
board[(0, 0)] = '象'
board[(0, 6)] = '象'
board[(6, 0)] = '鼠'
board[(6, 6)] = '鼠'
board[(0, 2)] = '狮'
board[(0, 4)] = '虎'
board[(2, 0)] = '狼'
board[(2, 2)] = '狗'
board[(2, 4)] = '猫'
board[(2, 6)] = '豹'
board[(6, 2)] = '猫'
board[(6, 4)] = '狗'
board[(4, 0)] = '豹'
board[(4, 2)] = '虎'
board[(4, 4)] = '狮'
board[(4, 6)] = '狼'
```
接下来,我们需要定义一些函数来实现游戏的逻辑。首先是一个函数用于输出棋盘的状态:
```
def print_board(board):
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[(i, j)] is None:
print(' ', end='')
else:
print(board[(i, j)], end='')
print('|', end='')
print()
print('-' * BOARD_SIZE * 3)
```
然后是一个函数用于判断两个棋子之间的关系:
```
def compare_animals(animal1, animal2):
if animal1 == '鼠' and animal2 == '象':
return -1
elif animal1 == '象' and animal2 == '鼠':
return 1
elif ANIMALS[animal1] < ANIMALS[animal2]:
return -1
elif ANIMALS[animal1] > ANIMALS[animal2]:
return 1
else:
return 0
```
接下来是一个函数用于判断棋子是否可以移动到指定位置:
```
def is_valid_move(board, animal, from_pos, to_pos):
if from_pos == to_pos:
return False
if board[from_pos] != animal:
return False
if board[to_pos] is not None and board[to_pos] != '陷':
return False
if from_pos[0] != to_pos[0] and from_pos[1] != to_pos[1]:
return False
if from_pos[0] == to_pos[0]:
start = min(from_pos[1], to_pos[1])
end = max(from_pos[1], to_pos[1])
for j in range(start + 1, end):
if board[(from_pos[0], j)] is not None:
return False
if board[to_pos] == '陷' and animal != '鼠':
return False
if from_pos[1] == to_pos[1]:
start = min(from_pos[0], to_pos[0])
end = max(from_pos[0], to_pos[0])
for i in range(start + 1, end):
if board[(i, from_pos[1])] is not None:
return False
if board[to_pos] == '陷' and animal != '鼠':
return False
return True
```
最后是一个函数用于移动棋子:
```
def move_animal(board, animal, from_pos, to_pos):
if not is_valid_move(board, animal, from_pos, to_pos):
return False
if board[to_pos] == '陷' and animal == '鼠':
board[from_pos] = None
board[to_pos] = animal
return True
if board[to_pos] is not None:
result = compare_animals(animal, board[to_pos])
if result == 0:
board[from_pos] = None
board[to_pos] = None
elif result < 0:
board[from_pos] = None
board[to_pos] = animal
else:
return False
else:
board[from_pos] = None
board[to_pos] = animal
return True
```
最后,我们可以编写一个简单的游戏循环:
```
print('欢迎来到斗兽棋游戏!')
print_board(board)
turn = 0
while True:
animal = ANIMAL_NAMES[turn % 2]
print('玩家{}的回合'.format(animal))
from_pos = input('请选择要移动的棋子位置(如:0,0):')
to_pos = input('请选择要移动到的位置(如:1,0):')
from_pos = tuple(map(int, from_pos.split(',')))
to_pos = tuple(map(int, to_pos.split(',')))
if not move_animal(board, animal, from_pos, to_pos):
print('无效的移动!')
continue
print_board(board)
if board[(0, 3)] == '鼠':
print('玩家鼠获胜!')
break
if board[(6, 3)] == '象':
print('玩家象获胜!')
break
turn += 1
```
这样,我们就完成了一个简单的斗兽棋游戏。您可以根据需要进行修改和拓展,例如增加更多的棋子类型和规则。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)