黑白棋python代码
时间: 2023-12-09 20:01:13 浏览: 101
黑白棋,也叫反转棋,是一种非常经典的棋类游戏。下面是一个用Python编写的黑白棋代码:
```python
import copy
# 初始化棋盘
def initialize_board():
board = [[' ' for _ in range(8)] for _ in range(8)]
board[3][3] = 'O'
board[3][4] = 'X'
board[4][3] = 'X'
board[4][4] = 'O'
return board
# 打印棋盘
def print_board(board):
print(' 0 1 2 3 4 5 6 7')
print(' ----------------')
for i in range(8):
print(i, '|', end='')
for j in range(8):
print(board[i][j], end=' ')
print('|')
print(' ----------------')
# 判断位置是否合法
def is_valid_move(board, row, col, color):
if row < 0 or row >= 8 or col < 0 or col >= 8 or board[row][col] != ' ':
return False
other_color = 'O' if color == 'X' else 'X'
directions = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
for dx, dy in directions:
x, y = row + dx, col + dy
if x >= 0 and x < 8 and y >= 0 and y < 8 and board[x][y] == other_color:
x, y = x + dx, y + dy
while x >= 0 and x < 8 and y >= 0 and y < 8 and board[x][y] == other_color:
x, y = x + dx, y + dy
if x >= 0 and x < 8 and y >= 0 and y < 8 and board[x][y] == color:
return True
return False
# 翻转棋子
def flip_pieces(board, row, col, color):
other_color = 'O' if color == 'X' else 'X'
directions = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
for dx, dy in directions:
x, y = row + dx, col + dy
if x >= 0 and x < 8 and y >= 0 and y < 8 and board[x][y] == other_color:
x, y = x + dx, y + dy
while x >= 0 and x < 8 and y >= 0 and y < 8 and board[x][y] == other_color:
x, y = x + dx, y + dy
if x >= 0 and x < 8 and y >= 0 and y < 8 and board[x][y] == color:
while x != row or y != col:
x, y = x - dx, y - dy
board[x][y] = color
# 判断游戏是否结束
def is_game_over(board):
for i in range(8):
for j in range(8):
if board[i][j] == ' ':
return False
return True
# 判断谁是赢家
def get_winner(board):
x_count = 0
o_count = 0
for i in range(8):
for j in range(8):
if board[i][j] == 'X':
x_count += 1
elif board[i][j] == 'O':
o_count += 1
if x_count > o_count:
return 'X'
elif x_count < o_count:
return 'O'
else:
return 'No one'
# 主函数
def main():
board = initialize_board()
print_board(board)
color = 'X'
while not is_game_over(board):
valid_moves = []
for i in range(8):
for j in range(8):
if is_valid_move(board, i, j, color):
valid_moves.append((i, j))
if len(valid_moves) == 0:
print("No valid move for", color)
color = 'O' if color == 'X' else 'X'
continue
print("Valid moves for", color, ": ", valid_moves)
while True:
row = int(input("Enter row: "))
col = int(input("Enter column: "))
if (row, col) in valid_moves:
break
else:
print("Invalid move, please try again.")
board_copy = copy.deepcopy(board)
flip_pieces(board_copy, row, col, color)
board = board_copy
print_board(board)
color = 'O' if color == 'X' else 'X'
winner = get_winner(board)
print("Game over. The winner is", winner)
if __name__ == '__main__':
main()
```
这个代码实现了一个双人黑白棋游戏,玩家可以轮流选择空白位置下棋,并翻转对手棋盘上相邻的棋子。游戏结束后,将根据棋盘上黑白棋子的数量判断胜负。在代码中使用了一个二维列表来表示棋盘,其中空白位置用空格字符表示,黑色棋子用'X'表示,白色棋子用'O'表示。代码中还包含了一些辅助函数,用于判断位置是否合法、翻转棋子、判断游戏是否结束以及判断胜负等。
相关推荐
![](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)
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)