Skip to content

Instantly share code, notes, and snippets.

@cashlo
Created June 3, 2020 16:45
Show Gist options
  • Save cashlo/b0aafb1bd0d4b4fee06c4228a48116e1 to your computer and use it in GitHub Desktop.
Save cashlo/b0aafb1bd0d4b4fee06c4228a48116e1 to your computer and use it in GitHub Desktop.
Gomoku Board
class GomokuBoard:
def __init__(self, size=9):
self.size = size
self.board = [0]*(size*size)
self.last_move = None
self.total_moves = 0
def clone_board(self):
new_board = GomokuBoard(self.size)
new_board.board = self.board.copy()
new_board.total_moves = self.total_moves
new_board.last_move = self.last_move
return new_board
def check_board(self, test=False):
if self.last_move is None:
return Gomoku.IN_PROGRESS
x = self.last_move%self.size
y = self.last_move//self.size
cell = self.board[self.last_move]
min_x = max(0, x-(Gomoku.LINE_LENGTH-1))
min_y = max(0, y-(Gomoku.LINE_LENGTH-1))
max_x = min(self.size-1, x+(Gomoku.LINE_LENGTH-1))
max_y = min(self.size-1, y+(Gomoku.LINE_LENGTH-1))
main_diagonal_start = self.last_move - (self.size+1)*min(y-min_y, x-min_x)
main_diagonal_end = self.last_move + (self.size+1)*min(max_y-y, max_x-x)
anti_diagonal_start = self.last_move - (self.size-1)*min(y-min_y, max_x-x)
anti_diagonal_end = self.last_move + (self.size-1)*min(max_y-y, x-min_x)
move_line_list = [
(self.size*y+min_x, self.size*y+max_x+1, 1),
(self.size*min_y+x, self.size*max_y+x+1, self.size),
(main_diagonal_start, main_diagonal_end+1, self.size+1),
(anti_diagonal_start, anti_diagonal_end+1, self.size-1),
]
if test:
for line in move_line_list:
for m in range(line[0],line[1],line[2]):
self.board[m] = Gomoku.WHITE
return 0
for line in move_line_list:
count = 0
for p in self.board[line[0]:line[1]:line[2]]:
if p == cell:
count += 1
else:
count = 0
if count == Gomoku.LINE_LENGTH:
return cell
if self.total_moves == self.size*self.size:
return Gomoku.DRAW
return Gomoku.IN_PROGRESS
def place_move(self, move, player):
if self.board[move] != Gomoku.EMPTY:
raise ValueError("Bad Position")
self.board[move] = player
self.last_move = move
self.total_moves += 1
def rollback_move(self, move, last_move):
self.board[move] = Gomoku.EMPTY
self.last_move = last_move
self.total_moves -= 1
def random_move(self, player):
empty_position_list = [i for i, p in enumerate(self.board) if p == Gomoku.EMPTY]
return random.choice(empty_position_list)
def print(self):
header = ' '
for x in range(self.size):
if self.last_move is not None and x == self.last_move%self.size:
header += '▼ '
else:
header += f'{x+1} '
print(header)
for y in range(self.size):
row_name = y+1 if self.last_move is None or self.last_move//self.size != y else ' ►'
row = f'{row_name:2} '
for x in range(self.size):
cell = self.board[self.size*y+x]
if cell == Gomoku.BLACK:
row += '○'
elif cell == Gomoku.WHITE:
row += '●'
elif x == 0 and y == 0:
row += '┌ '
elif x == 0 and y == self.size-1:
row += '└ '
elif x == self.size-1 and y == 0:
row += '┐'
elif x == self.size-1 and y == self.size-1:
row += '┘'
elif x == 0:
row += '├ '
elif y == 0:
row += '┬ '
elif x == self.size-1:
row += '┤'
elif y == self.size-1:
row += '┴ '
else:
row += '┼ '
print(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment