帮我写一个用python实现的五子棋代码
时间: 2023-03-26 07:00:49 浏览: 92
好的,我可以帮您写一个用 Python 实现的五子棋代码。以下是代码:
```
import numpy as np
# 定义棋盘大小
BOARD_SIZE = 15
# 定义棋子类型
EMPTY =
BLACK = 1
WHITE = 2
class Board:
def __init__(self):
self.board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=int)
self.current_player = BLACK
def get_current_player(self):
return self.current_player
def get_board(self):
return self.board
def place(self, x, y):
if self.board[x][y] != EMPTY:
return False
self.board[x][y] = self.current_player
self.current_player = WHITE if self.current_player == BLACK else BLACK
return True
def is_win(self, player):
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if self.board[i][j] == player:
if self.check_horizontal(i, j, player):
return True
if self.check_vertical(i, j, player):
return True
if self.check_diagonal(i, j, player):
return True
return False
def check_horizontal(self, x, y, player):
count =
for i in range(y, BOARD_SIZE):
if self.board[x][i] == player:
count += 1
else:
break
if count >= 5:
return True
return False
def check_vertical(self, x, y, player):
count =
for i in range(x, BOARD_SIZE):
if self.board[i][y] == player:
count += 1
else:
break
if count >= 5:
return True
return False
def check_diagonal(self, x, y, player):
count =
for i in range(BOARD_SIZE):
if x + i >= BOARD_SIZE or y + i >= BOARD_SIZE:
break
if self.board[x+i][y+i] == player:
count += 1
else:
break
if count >= 5:
return True
count =
for i in range(BOARD_SIZE):
if x + i >= BOARD_SIZE or y - i < :
break
if self.board[x+i][y-i] == player:
count += 1
else:
break
if count >= 5:
return True
return False
```
这是一个简单的五子棋代码,您可以在此基础上进行修改和完善。
阅读全文