def move(self, board, value): """ Empty is a list of empty cells. The strategy consists in the fact that the computer isn't placing its move on the first empty cell he finds, but instead, he places his move on the cell that will block the most surrounding cells. We return as usually the cell, its line and column and of list of lists(containing the lines and columns of the surrounding cells) """ empty = board.get_empty_cells() if len(empty) == 0: return None max_surr = 0 index_max = 0 for index in range(len(empty)): l = len(board.check_surrounding(empty[index])) #l gets how many surrounding cells that specific cell has if l != 0: #simple algorithm for computing the max if l > max_surr: index_max = index max_surr = l if max_surr != 0: #we place the computer of the cell with the max l board.set_value(empty[index_max].get_line(), empty[index_max].get_column(), value) cell = Cell(empty[index_max].get_line(), empty[index_max].get_column(), value) lst = board.check_surrounding(cell) return [cell, cell.get_line(), cell.get_column(), lst] else: return None
def move(self, line, column, value): if line == -1 and column == -1: return self.__strategy.move(self.board, value) else: self.board.set_value(line, column, value) cell = Cell(line, column, value) lst = self.board.check_surrounding(cell) return [cell, cell.get_line(), cell.get_column(), lst]
def validate_move(self, l, c, value): if l < 0: raise ValueError("index out of range") if l >= self.board.get_lines(): raise ValueError("index out of range") if c < 0: raise ValueError("index out of range") if c >= self.board.get_columns(): raise ValueError("index out of range") line = int(l) column = int(c) if self.board.check_cell(Cell(line, column, value)) is False: raise ValueError('try again') if self.board.check_surrounding(Cell(line, column, value)) is False: raise ValueError('try again') return line, column
def get_all_cells(self): #Will return a list of cell objects cells = [] for i in range(self.__lines): for j in range(self.__columns): cells.append(Cell(i, j, self.__cells[i][j])) return cells
def move(self, board, value): """ Empty is a list of empty cell and the computer will place its move on the first empty cell we find. We return as usually the cell, its line and column and of list of lists(containing the lines and columns of the surrounding cells) """ empty = board.get_empty_cells() #gets a list of cell objects if len(empty) == 0: return None #if the list is empty, there are no cells left => Game over for cell in empty: if len(board.check_surrounding(cell)) != 0: board.set_value(cell.get_line(), cell.get_column(), value) cell = Cell(cell.get_line(), cell.get_column(), value) lst = board.check_surrounding(cell) return [cell, cell.get_line(), cell.get_column(), lst] return None
def move(self, l, c, value): line, column = self.validate_move(l, c, value) self.board.set_value(line, column, value) cell = Cell(line, column, value) lst = self.board.check_surrounding(cell) return [cell, cell.get_line(), cell.get_column(), lst]
def validate_move(self, line, column, value): if self.board.check_cell(Cell(line, column, value)) is False: raise ValueError('try again') if self.board.check_surrounding(Cell(line, column, value)) is False: raise ValueError('try again')
def get_all_cells(self): cells = [] for i in range(self.__lines): for j in range(self.__columns): cells.append(Cell(i, j, self.__cells[i][j])) return cells
def get_cell(self, line, column): #returns a cell object specified by line and column return Cell(line, column, self.__cells[line][column])