Python国际象棋小游戏代码
时间: 2023-07-10 22:21:35 浏览: 560
这里是一个基于 Python 的简单国际象棋小游戏代码,你可以尝试运行它:
```python
class ChessBoard:
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.white_turn = True
self.game_over = False
def print_board(self):
for i in range(8):
for j in range(8):
print(self.board[i][j], end=' ')
print('\n')
def move_piece(self, from_pos, to_pos):
from_row, from_col = from_pos
to_row, to_col = to_pos
piece = self.board[from_row][from_col]
if piece == ' ' or piece == '.':
return False
if self.white_turn:
if piece.islower():
return False
else:
if piece.isupper():
return False
if from_row == to_row and from_col == to_col:
return False
if piece.lower() == 'p':
if self.white_turn:
if from_col == to_col:
if from_row == 6 and to_row == 4 and self.board[5][from_col] == ' ' and self.board[4][from_col] == ' ':
self.board[from_row][from_col] = ' '
self.board[to_row][to_col] = 'P'
self.white_turn = not self.white_turn
return True
elif from_row - 1 == to_row and self.board[to_row][to_col] == ' ':
self.board[from_row][from_col] = ' '
self.board[to_row][to_col] = 'P'
self.white_turn = not self.white_turn
return True
elif abs(from_col - to_col) == 1 and from_row - 1 == to_row and self.board[to_row][to_col].islower():
self.board[from_row][from_col] = ' '
self.board[to_row][to_col] = 'P'
self.white_turn = not self.white_turn
return True
else:
if from_col == to_col:
if from_row == 1 and to_row == 3 and self.board[2][from_col] == ' ' and self.board[3][from_col] == ' ':
self.board[from_row][from_col] = ' '
self.board[to_row][to_col] = 'p'
self.white_turn = not self.white_turn
return True
elif from_row + 1 == to_row and self.board[to_row][to_col] == ' ':
self.board[from_row][from_col] = ' '
self.board[to_row][to_col] = 'p'
self.white_turn = not self.white_turn
return True
elif abs(from_col - to_col) == 1 and from_row + 1 == to_row and self.board[to_row][to_col].isupper():
self.board[from_row][from_col] = ' '
self.board[to_row][to_col] = 'p'
self.white_turn = not self.white_turn
return True
# TODO: Implement moves for other pieces
return False
def play_game(self):
while not self.game_over:
self.print_board()
if self.white_turn:
print('White turn')
else:
print('Black turn')
from_pos = input('Enter from position (row, col): ')
to_pos = input('Enter to position (row, col): ')
try:
from_row, from_col = map(int, from_pos.split(','))
to_row, to_col = map(int, to_pos.split(','))
success = self.move_piece((from_row, from_col), (to_row, to_col))
if not success:
print('Invalid move, try again')
else:
if self.white_turn:
king_pos = self.find_king('K')
if not king_pos:
self.game_over = True
print('Black wins!')
else:
self.white_turn = not self.white_turn
else:
king_pos = self.find_king('k')
if not king_pos:
self.game_over = True
print('White wins!')
else:
self.white_turn = not self.white_turn
except ValueError:
print('Invalid input, try again')
def find_king(self, king_symbol):
for i in range(8):
for j in range(8):
if self.board[i][j] == king_symbol:
return (i, j)
return None
board = ChessBoard()
board.play_game()
```
在该代码中,`ChessBoard` 类表示棋盘,并实现了一个 `move_piece()` 方法来移动棋子。`play_game()` 方法用于运行游戏,该方法会持续循环直到游戏结束。当游戏结束时,会打印出胜利者。
阅读全文