象棋游戏代码
时间: 2023-07-03 16:09:04 浏览: 160
cPP-chess.rar_chess_文本代码游戏
以下是一个简单的 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']
]
def print_board(self):
for row in self.board:
print(' '.join(row))
def is_valid_move(self, from_pos, to_pos):
if from_pos == to_pos:
return False
from_col, from_row = from_pos
to_col, to_row = to_pos
piece = self.board[from_row][from_col]
if piece == ' ' or piece == '.':
return False
if piece == 'P': # pawn move
if from_col != to_col: # capture move
if to_row - from_row != 1 or abs(to_col - from_col) != 1:
return False
if self.board[to_row][to_col].isupper(): # can't capture own piece
return False
else: # non-capture move
if to_row - from_row == 1 and self.board[to_row][to_col] == ' ':
return True
if from_row == 1 and to_row == 3 and self.board[2][from_col] == ' ' and self.board[3][from_col] == ' ':
return True
return False
elif piece == 'p': # black pawn move
if from_col != to_col: # capture move
if from_row - to_row != 1 or abs(to_col - from_col) != 1:
return False
if self.board[to_row][to_col].islower(): # can't capture own piece
return False
else: # non-capture move
if from_row - to_row == 1 and self.board[to_row][to_col] == ' ':
return True
if from_row == 6 and to_row == 4 and self.board[5][from_col] == ' ' and self.board[4][from_col] == ' ':
return True
return False
elif piece == 'R': # rook move
if from_row != to_row and from_col != to_col:
return False
if from_row == to_row:
if from_col < to_col:
for col in range(from_col + 1, to_col):
if self.board[from_row][col] != ' ':
return False
else:
for col in range(to_col + 1, from_col):
if self.board[from_row][col] != ' ':
return False
else:
if from_row < to_row:
for row in range(from_row + 1, to_row):
if self.board[row][from_col] != ' ':
return False
else:
for row in range(to_row + 1, from_row):
if self.board[row][from_col] != ' ':
return False
return True
elif piece == 'r': # black rook move
if from_row != to_row and from_col != to_col:
return False
if from_row == to_row:
if from_col < to_col:
for col in range(from_col + 1, to_col):
if self.board[from_row][col] != ' ':
return False
else:
for col in range(to_col + 1, from_col):
if self.board[from_row][col] != ' ':
return False
else:
if from_row < to_row:
for row in range(from_row + 1, to_row):
if self.board[row][from_col] != ' ':
return False
else:
for row in range(to_row + 1, from_row):
if self.board[row][from_col] != ' ':
return False
return True
elif piece == 'N': # knight move
if abs(from_row - to_row) == 2 and abs(from_col - to_col) == 1:
return True
elif abs(from_row - to_row) == 1 and abs(from_col - to_col) == 2:
return True
return False
elif piece == 'n': # black knight move
if abs(from_row - to_row) == 2 and abs(from_col - to_col) == 1:
return True
elif abs(from_row - to_row) == 1 and abs(from_col - to_col) == 2:
return True
return False
elif piece == 'B': # bishop move
if abs(from_row - to_row) != abs(from_col - to_col):
return False
if from_row < to_row and from_col < to_col:
for i in range(1, to_col - from_col):
if self.board[from_row + i][from_col + i] != ' ':
return False
elif from_row < to_row and from_col > to_col:
for i in range(1, from_col - to_col):
if self.board[from_row + i][from_col - i] != ' ':
return False
elif from_row > to_row and from_col < to_col:
for i in range(1, to_col - from_col):
if self.board[from_row - i][from_col + i] != ' ':
return False
else:
for i in range(1, from_col - to_col):
if self.board[from_row - i][from_col - i] != ' ':
return False
return True
elif piece == 'b': # black bishop move
if abs(from_row - to_row) != abs(from_col - to_col):
return False
if from_row < to_row and from_col < to_col:
for i in range(1, to_col - from_col):
if self.board[from_row + i][from_col + i] != ' ':
return False
elif from_row < to_row and from_col > to_col:
for i in range(1, from_col - to_col):
if self.board[from_row + i][from_col - i] != ' ':
return False
elif from_row > to_row and from_col < to_col:
for i in range(1, to_col - from_col):
if self.board[from_row - i][from_col + i] != ' ':
return False
else:
for i in range(1, from_col - to_col):
if self.board[from_row - i][from_col - i] != ' ':
return False
return True
elif piece == 'Q': # queen move
if from_row == to_row or from_col == to_col:
return self.is_valid_move((from_col, from_row), (to_col, to_row))
else:
return self.is_valid_move((from_col, from_row), (to_col, to_row)) and abs(from_row - to_row) == abs(from_col - to_col)
elif piece == 'q': # black queen move
if from_row == to_row or from_col == to_col:
return self.is_valid_move((from_col, from_row), (to_col, to_row))
else:
return self.is_valid_move((from_col, from_row), (to_col, to_row)) and abs(from_row - to_row) == abs(from_col - to_col)
elif piece == 'K': # king move
if abs(from_row - to_row) > 1 or abs(from_col - to_col) > 1:
return False
return True
elif piece == 'k': # black king move
if abs(from_row - to_row) > 1 or abs(from_col - to_col) > 1:
return False
return True
def move_piece(self, from_pos, to_pos):
from_col, from_row = from_pos
to_col, to_row = to_pos
piece = self.board[from_row][from_col]
self.board[from_row][from_col] = ' '
self.board[to_row][to_col] = piece
board = ChessBoard()
board.print_board()
while True:
from_pos = input('Enter the position of the piece you want to move (e.g. "e2"): ')
to_pos = input('Enter the position you want to move the piece to (e.g. "e4"): ')
from_col, from_row = ord(from_pos[0]) - 97, int(from_pos[1]) - 1
to_col, to_row = ord(to_pos[0]) - 97, int(to_pos[1]) - 1
if board.is_valid_move((from_col, from_row), (to_col, to_row)):
board.move_piece((from_col, from_row), (to_col, to_row))
board.print_board()
else:
print('Invalid move, try again.')
```
这个代码实现了一个简单的控制台象棋游戏,玩家可以通过输入起始和目标位置来移动棋子。代码中使用了一些简单的规则来确定哪些移动是合法的,例如:
- 兵(P)只能向前走一格,或者在起始位置时可以向前走两格。
- 车(R)只能沿着横向或纵向移动,并且不能跨越其他棋子。
- 马(N)可以跳过其他棋子,在横向或纵向移动两格,然后向另一个方向移动一格。
- 象(B)只能沿着对角线移动,并且不能跨越其他棋子。
- 后(Q)可以沿着横向、纵向或对角线移动,并且不能跨越其他棋子。
- 王(K)可以沿着横向、纵向或对角线移动一格,并且不能移动到对方可以攻击到的位置。
以上只是一些简单的规则,实际上象棋游戏的规则要更加复杂。如果你想实现一个完整的象棋游戏,需要考虑更多的细节和特殊情况。
阅读全文