python代码实现一个斗兽棋
时间: 2023-07-25 22:17:06 浏览: 71
好的,以下是一个简单的 Python 斗兽棋游戏的实现。
```python
# coding: utf-8
class Animal:
def __init__(self, name, rank, is_red):
self.name = name
self.rank = rank
self.is_red = is_red
self.is_alive = True
def __str__(self):
return self.name
def __repr__(self):
return self.name
class Game:
def __init__(self):
# 棋盘,0 表示没有棋子,1 表示红方,2 表示蓝方
self.board = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
]
# 棋子列表
self.animals = [
Animal('象', 8, True),
Animal('狮', 7, True),
Animal('虎', 6, True),
Animal('豹', 5, True),
Animal('狼', 4, True),
Animal('狗', 3, True),
Animal('猫', 2, True),
Animal('鼠', 1, True),
Animal('象', 8, False),
Animal('狮', 7, False),
Animal('虎', 6, False),
Animal('豹', 5, False),
Animal('狼', 4, False),
Animal('狗', 3, False),
Animal('猫', 2, False),
Animal('鼠', 1, False),
]
# 红方先行
self.turn_is_red = True
def print_board(self):
for i in range(7):
for j in range(7):
if self.board[i][j] == 0:
print(' ', end='')
elif self.board[i][j] == 1:
print('R' + str(self.get_animal(i, j).rank), end='')
elif self.board[i][j] == 2:
print('B' + str(self.get_animal(i, j).rank), end='')
print('\t', end='')
print()
def get_animal(self, x, y):
for animal in self.animals:
if animal.is_alive and animal.is_red == (self.board[x][y] == 1):
if animal.name == '鼠' or animal.name == '猫':
if abs(x - animal_x) + abs(y - animal_y) == 1:
return animal
else:
if abs(x - animal_x) == 1 and abs(y - animal_y) == 0 or \
abs(x - animal_x) == 0 and abs(y - animal_y) == 1:
return animal
def move(self, animal_x, animal_y, x, y):
animal = self.get_animal(animal_x, animal_y)
if animal is None or not self.is_valid_move(animal_x, animal_y, x, y):
return False
self.board[animal_x][animal_y] = 0
if self.board[x][y] != 0:
self.get_animal(x, y).is_alive = False
self.board[x][y] = 1 if animal.is_red else 2
self.turn_is_red = not self.turn_is_red
return True
def is_valid_move(self, animal_x, animal_y, x, y):
if not 0 <= x <= 6 or not 0 <= y <= 6:
return False
if (animal_x == x and animal_y == y) or self.board[x][y] != 0:
return False
animal = self.get_animal(animal_x, animal_y)
if animal is None or animal.is_red != self.turn_is_red:
return False
if animal.name == '鼠' and x > animal_x or animal.name == '猫' and y != animal_y:
return False
if animal.name == '象' and self.board[(animal_x + x) // 2][(animal_y + y) // 2] != 0:
return False
if animal.rank < self.get_animal(x, y).rank:
return False
return True
def is_over(self):
red_alive = False
blue_alive = False
for animal in self.animals:
if animal.is_alive:
if animal.is_red:
red_alive = True
else:
blue_alive = True
return not (red_alive and blue_alive)
def play(self):
while True:
self.print_board()
if self.is_over():
break
animal_x, animal_y, x, y = map(int, input().split())
if self.move(animal_x, animal_y, x, y):
print('Valid move!')
else:
print('Invalid move!')
print('Game over!')
if __name__ == '__main__':
game = Game()
game.play()
```
这是一个简单的斗兽棋游戏,实现了基本的移动和胜利判断。但是由于没有 AI,所以需要两个人手动控制棋子,进行游戏。