|
| 1 | +import itertools |
| 2 | +import numpy |
| 3 | + |
| 4 | +X = 1 |
| 5 | +O = 2 |
| 6 | + |
| 7 | + |
| 8 | +class Board: |
| 9 | + def __init__(self, dimensions=(3, 3), x_in_a_row=3): |
| 10 | + self.dimensions = dimensions |
| 11 | + self.x_in_a_row = x_in_a_row |
| 12 | + self.board = self.create_board() |
| 13 | + self._directions = self.find_directions() |
| 14 | + self.move_count = 0 |
| 15 | + self.moves = [] |
| 16 | + self.x = [] |
| 17 | + self.o = [] |
| 18 | + self.turn = X |
| 19 | + |
| 20 | + def create_board(self): |
| 21 | + return numpy.zeros(self.dimensions) |
| 22 | + |
| 23 | + def copy(self): |
| 24 | + board = Board(self.dimensions, self.x_in_a_row) |
| 25 | + board.turn = self.turn |
| 26 | + board.board = self.board.copy() |
| 27 | + return board |
| 28 | + |
| 29 | + def get_mark_at_position(self, position): |
| 30 | + position = tuple(position) |
| 31 | + return self.board[position] |
| 32 | + |
| 33 | + def set_mark(self, coordinates, player): |
| 34 | + self.board[coordinates] = player |
| 35 | + if player == X: |
| 36 | + self.x.append(Move(coordinates)) |
| 37 | + else: |
| 38 | + self.o.append(Move(coordinates)) |
| 39 | + |
| 40 | + def is_empty(self, position): |
| 41 | + return self.get_mark_at_position(position) == 0 |
| 42 | + |
| 43 | + def push(self, coordinates): |
| 44 | + coordinates = tuple(coordinates) |
| 45 | + if not self.is_empty(coordinates): |
| 46 | + raise Exception("Position is not empty.") |
| 47 | + move = Move(coordinates) |
| 48 | + self.set_mark(coordinates, self.turn) |
| 49 | + self.turn = X if self.turn == O else O |
| 50 | + self.moves.append(move) |
| 51 | + self.move_count += 1 |
| 52 | + |
| 53 | + def find_directions(self): |
| 54 | + directions = list(itertools.product([1, 0, -1], repeat=len(self.dimensions))) |
| 55 | + correct_directions = [] |
| 56 | + for direction in directions: |
| 57 | + for item in direction: |
| 58 | + if item > 0: |
| 59 | + correct_directions.append(direction) |
| 60 | + break |
| 61 | + elif item < 0: |
| 62 | + break |
| 63 | + return correct_directions |
| 64 | + |
| 65 | + def possible_moves(self): |
| 66 | + return numpy.argwhere(self.board == 0) |
| 67 | + |
| 68 | + def out_of_bounds(self, pos): |
| 69 | + return (pos < 0).any() or (pos >= self.dimensions).any() |
| 70 | + |
| 71 | + def in_bounds(self, pos): |
| 72 | + return not self.out_of_bounds(pos) |
| 73 | + |
| 74 | + def has_won(self, player): |
| 75 | + positions = numpy.argwhere(self.board == player) |
| 76 | + for position in positions: |
| 77 | + for direction in self._directions: |
| 78 | + for in_a_row in range(1, self.x_in_a_row): |
| 79 | + pos = position + numpy.multiply(direction, in_a_row) |
| 80 | + if self.out_of_bounds(pos) or self.board[tuple(pos)] != player: |
| 81 | + break |
| 82 | + else: |
| 83 | + return True |
| 84 | + return False |
| 85 | + |
| 86 | + def result(self): |
| 87 | + if self.has_won(X): |
| 88 | + return X |
| 89 | + elif self.has_won(O): |
| 90 | + return O |
| 91 | + elif self.board.all(): |
| 92 | + return 0 |
| 93 | + |
| 94 | + |
| 95 | +class Move: |
| 96 | + def __init__(self, coordinate_move=None, str_move=None): |
| 97 | + assert coordinate_move or str_move |
| 98 | + self.coordinate_move = coordinate_move |
| 99 | + self.str_move = str_move |
| 100 | + if self.coordinate_move: |
| 101 | + self.str_move = "-".join(map(str, tuple(self.coordinate_move))) |
| 102 | + else: |
| 103 | + self.coordinate_move = tuple(map(int, self.str_move.split("-"))) |
0 commit comments