|
| 1 | +import Constants |
| 2 | +from Cell import Cell |
| 3 | + |
| 4 | + |
| 5 | +class Board: |
| 6 | +''' |
| 7 | +Class that represents the game board |
| 8 | +''' |
| 9 | + |
| 10 | +def __init__(self, width, height): |
| 11 | +''' |
| 12 | +The board's constructor |
| 13 | +:param width: int > 4 |
| 14 | +:param height: int > 4 |
| 15 | +''' |
| 16 | +self.__height = height |
| 17 | +self.__width = width |
| 18 | +self.board = self.createBoard() |
| 19 | + |
| 20 | +def createBoard(self): |
| 21 | +''' |
| 22 | +Function that creates an empty game board |
| 23 | +:return: matrix of cell |
| 24 | +''' |
| 25 | +board = [] |
| 26 | +for x in range(self.__height): |
| 27 | +board.append([]) |
| 28 | +for y in range(self.__width): |
| 29 | +board[x].append(Cell(Constants.EMPTY, x, y)) |
| 30 | +return board |
| 31 | + |
| 32 | +@property |
| 33 | +def height(self): |
| 34 | +return self.__height |
| 35 | + |
| 36 | +@property |
| 37 | +def width(self): |
| 38 | +return self.__width |
| 39 | + |
| 40 | +def isBoardFull(self, board): |
| 41 | +''' |
| 42 | +
|
| 43 | +:param board: matrix of cells |
| 44 | +:return: True if there are no empty spaces anywhere on the board. |
| 45 | +''' |
| 46 | +for x in range(board.height): |
| 47 | +for y in range(board.width): |
| 48 | +if board.board[x][y].entity == Constants.EMPTY: |
| 49 | +return False |
| 50 | +return True |
| 51 | + |
| 52 | +def __str__(self): |
| 53 | +''' |
| 54 | +
|
| 55 | +:return: string thate represents the game board |
| 56 | +''' |
| 57 | +b = "\033[93m " |
| 58 | +for x in range(self.width): |
| 59 | +b += str(x + 1) + " " |
| 60 | +b += "\033[0m\n" |
| 61 | +for row in self.board: |
| 62 | +for y in row: |
| 63 | +b += str(y) + " " |
| 64 | +b += "\n" |
| 65 | +return b |
| 66 | + |
| 67 | +def getLowestPoint(self, column): |
| 68 | +''' |
| 69 | +:param column: int |
| 70 | +:return: the row number of the lowest empty row in the given column. |
| 71 | +''' |
| 72 | +for y in range(self.height - 1, -1, -1): |
| 73 | +if self.board[y][column].entity == Constants.EMPTY: |
| 74 | +return y |
| 75 | +return -1 |
| 76 | + |
| 77 | +def isValidMove(self, column): |
| 78 | +''' |
| 79 | +:param column: int |
| 80 | +:return: True if the column is valid or not |
| 81 | +''' |
| 82 | +if column < 0 or column >= self.width or self.board[0][column].entity != Constants.EMPTY: |
| 83 | +return False |
| 84 | +return True |
| 85 | + |
| 86 | +def move(self, column, entity): |
| 87 | +''' |
| 88 | +:param column: int |
| 89 | +:param entity: oneOf("human", "computer") |
| 90 | +''' |
| 91 | +if self.isValidMove(column): |
| 92 | +self.board[self.getLowestPoint(column)][column].entity = entity |
| 93 | +else: |
| 94 | +raise ValueError("Error: Column is full") |
| 95 | + |
| 96 | +def isWinner(self, entity): |
| 97 | +''' |
| 98 | +Check if the given entity is a winner or not |
| 99 | +:param entity: oneOf("computer", "human") |
| 100 | +:return: True if winner, false otherwise |
| 101 | +''' |
| 102 | +# checks the horizontal possibilities |
| 103 | +for x in range(self.height): |
| 104 | +for y in range(self.width - 3): |
| 105 | +if self.board[x][y].entity == entity and \ |
| 106 | +self.board[x][y + 1].entity == entity and \ |
| 107 | +self.board[x][y + 2].entity == entity and \ |
| 108 | +self.board[x][ |
| 109 | +y + 3].entity == entity: |
| 110 | +return True |
| 111 | +# checks vertical possibilities |
| 112 | +for x in range(self.height - 3): |
| 113 | +for y in range(self.width): |
| 114 | +if self.board[x][y].entity == entity and self.board[x + 1][y].entity == entity and self.board[x + 2][ |
| 115 | +y].entity == entity and \ |
| 116 | +self.board[x + 3][ |
| 117 | +y].entity == entity: |
| 118 | +return True |
| 119 | + |
| 120 | +# checks the / diagonal possibilities |
| 121 | +for x in range(self.height - 3): |
| 122 | +for y in range(self.width - 3): |
| 123 | +if self.board[x][y].entity == entity and self.board[x + 1][y + 1].entity == entity and \ |
| 124 | +self.board[x + 2][ |
| 125 | +y + 2].entity == entity and self.board[x + 3][ |
| 126 | +y + 3].entity == entity: |
| 127 | +return True |
| 128 | +# checks the / diagonal possibilities |
| 129 | +for x in range(self.height - 3): |
| 130 | +for y in range(3, self.width): |
| 131 | +if self.board[x][y].entity == entity and self.board[x + 1][y - 1].entity == entity and \ |
| 132 | +self.board[x + 2][ |
| 133 | +y - 2].entity == entity and self.board[x + 3][ |
| 134 | +y - 3].entity == entity: |
| 135 | +return True |
| 136 | +return False |
0 commit comments