python国际象棋小游戏代码
时间: 2024-05-20 09:09:28 浏览: 82
Python国际象棋小游戏代码可以通过各种方式实现。这里提供一种简单的实现方式:
```
class ChessGame:
def __init__(self):
self.board = [
["R", "N", "B", "Q", "K", "B", "N", "R"],
["P", "P", "P", "P", "P", "P", "P", "P"],
[" ", ".", " ", ".", " ", ".", " ", "."],
[".", " ", ".", " ", ".", " ", ".", " "],
[" ", ".", " ", ".", " ", ".", " ", "."],
[".", " ", ".", " ", ".", " ", ".", " "],
["p", "p", "p", "p", "p", "p", "p", "p"],
["r", "n", "b", "q", "k", "b", "n", "r"]
]
self.current_player = 0
self.game_over = False
def play(self, start, end):
x1, y1 = start
x2, y2 = end
piece = self.board[x1][y1]
if piece == ' ' or piece == '.':
return False
if (piece.islower() and self.current_player == 0) or (piece.isupper() and self.current_player == 1):
return False
if piece.lower() == 'p' and x2 == x1 + 2 and y1 == y2:
if self.board[x1 + 1][y1] != ' ':
return False
if piece.lower() == 'p' and x2 == x1 + 1 and abs(y2 - y1) == 1:
if self.board[x2][y2] == ' ':
return False
if piece.lower() == 'p' and x2 == x1 + 1 and y2 == y1:
if self.board[x2][y2] != ' ':
return False
if piece.lower() == 'r' and x1 != x2 and y1 != y2:
return False
if piece.lower() == 'n' and not ((abs(x2 - x1) == 2 and abs(y2 - y1) == 1) or (abs(x2 - x1) == 1 and abs(y2 - y1) == 2)):
return False
if piece.lower() == 'b' and abs(x2 - x1) != abs(y2 - y1):
return False
if piece.lower() == 'q' and not ((x1 == x2 or y1 == y2) or (abs(x2 - x1) == abs(y2 - y1))):
return False
if piece.lower() == 'k' and not ((abs(x2 - x1) <= 1) and (abs(y2 - y1) <= 1)):
return False
self.board[x2][y2] = self.board[x1][y1]
self.board[x1][y1] = ' '
self.current_player = 1 - self.current_player
return True
def print_board(self):
print(" A B C D E F G H ")
print(" +--+")
for i in range(8):
print(str(8-i) + " | ", end="")
for j in range(8):
print(self.board[i][j] + " ", end="")
print("| " + str(8-i))
print(" +---------------+")
print(" A B C D E F G H ")
def check_game_over(self):
kings = []
for i in range(8):
for j in range(8):
if self.board[i][j].lower() == 'k':
kings.append((i, j))
for king in kings:
x, y = king
for i in range(max(0, x-1), min(8, x+2)):
for j in range(max(0, y-1), min(8, y+2)):
if i != x or j != y:
if self.board[i][j] == ' ' or self.board[i][j].isupper() == self.board[x][y].isupper():
continue
if self.check_move_valid((x, y), (i, j)):
return False
self.game_over = True
return True
def check_move_valid(self, start, end):
copy_board = [row[:] for row in self.board]
copy_game = ChessGame()
copy_game.board = copy_board
copy_game.current_player = self.current_player
return copy_game.play(start, end)
game = ChessGame()
while not game.game_over:
game.print_board()
player = game.current_player + 1
start = input("Player {} move: ".format(player))
end = input("to: ")
x1, y1 = ord(start) - ord('A'), 8 - int(start[1])
x2, y2 = ord(end) - ord('A'), 8 - int(end)
valid_move = game.check_move_valid((x1, y1), (x2, y2))
while not valid_move:
print("Invalid move!")
start = input("Player {} move: ".format(player))
end = input("to: ")
x1, y1 = ord(start) - ord('A'), 8 - int(start)
x2, y2 = ord(end) - ord('A'), 8 - int(end)
valid_move = game.check_move_valid((x1, y1), (x2, y2))
game.play((x1, y1), (x2, y2))
game.check_game_over()
print("Game over!")
```
阅读全文