以下是一个简单的黑白棋人人对战的Python代码:
```python
import numpy as np
# 初始化棋盘
board = np.zeros((8, 8))
board[3][3] = 1
board[4][4] = 1
board[3][4] = -1
board[4][3] = -1
# 定义落子函数
def place(board, player, row, col):
if board[row][col] != 0:
return False
directions = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, 1], [-1, 1], [1, -1]]
other_player = -player
to_flip = []
for d in directions:
r, c = row, col
r += d[0]
c += d[1]
if r < 0 or r >= 8 or c < 0 or c >= 8:
continue
if board[r][c] == other_player:
r += d[0]
c += d[1]
if r < 0 or r >= 8 or c < 0 or c >= 8:
continue
while board[r][c] == other_player:
r += d[0]
c += d[1]
if r < 0 or r >= 8 or c < 0 or c >= 8:
break
if r < 0 or r >= 8 or c < 0 or c >= 8:
continue
if board[r][c] == player:
while True:
r -= d[0]
c -= d[1]
if r == row and c == col:
break
to_flip.append([r, c])
if len(to_flip) == 0:
return False
board[row][col] = player
for r, c in to_flip:
board[r][c] = player
return True
# 定义判断是否有合法落子点的函数
def has_legal_moves(board, player):
for row in range(8):
for col in range(8):
if board[row][col] == 0:
if place(board, player, row, col):
return True
return False
# 定义判断胜负的函数
def get_winner(board):
black_score = np.sum(board == 1)
white_score = np.sum(board == -1)
if black_score > white_score:
return 1
elif white_score > black_score:
return -1
else:
return 0
# 定义主函数
def main():
player = 1
while has_legal_moves(board, player):
print(board)
print("Player", player)
row = int(input("Enter row: "))
col = int(input("Enter column: "))
if place(board, player, row, col):
player = -player
print(board)
winner = get_winner(board)
if winner == 1:
print("Black wins!")
elif winner == -1:
print("White wins!")
else:
print("Tie!")
if __name__ == '__main__':
main()
```
这个代码实现了一个简单的黑白棋人人对战游戏,玩家可以通过输入行和列来落子,程序会自动判断是否合法,并进行翻转。最后,程序会输出胜利者或者平局。