def make_move(self): """ Function to make calculated move :return: list, coordinate of move """ for s in self.available_space: temp = copy.deepcopy(self.input_matrix) temp[s[0]][s[1]] = self.computer_score checker = GameChecker(temp, self.computer_score) # if computer can win next move go for it if checker.check_matrix(): return s else: # if human win next move block it temp[s[0]][s[1]] = self.enemy_score checker = GameChecker(temp, self.enemy_score) if checker.check_matrix(): return s # else choose one of the most dense to infiltrate return random.choice(self.best_choices)
def update(self): """ Function to render the main game """ if self.current_disk is not None: self.current_disk.display() if self.falling and self.current_disk.y < self.stop_point_y: self.current_disk.y += SPEED self.playable = False # prevent mind changing mid-fall self.board.display() # When the disk touch the lowest point possible # switch turn and reset state while recording # the score and count down to game over if self.current_disk.y >= self.stop_point_y: self.current_disk.y = self.stop_point_y self.disks.append(self.current_disk) self.scored[self.final_row][self.final_col] = \ self.current_score if len(self.disks) > 6: # only start checking after 6 disks to save memory score_to_check = int(self.current_score) checker = GameChecker(self.scored, score_to_check) if checker.check_matrix(): fill(0) textSize(TEXT_SIZE) self.win = PLAYERS[int(self.turn + 0.5)]['color'].upper() text(self.win + " WINS!", self.space['x'] / TEXT_COORDINATE_X, self.offset / TEXT_COORDIATE_Y) self.playable = False self.total = -1 if self.turn == 0: self.enter_name() self.turn = -1 else: self.turn = 1 - (int(self.turn + 0.5)) self.reset_all() self.total -= 1 # display existing disks for disk in self.disks: disk.display() self.board.display() self.board.display() # AI Component ###################### if self.turn != 0: self.timer -= 1 self.playable = False # check if game is over by seeing if board is filled if self.turn == 1 and self.timer <= 0: computer_disk = self.computer_play() to_drop = self.top_spaces[computer_disk[1]] self.create_new_disk(to_drop, computer_disk[0], computer_disk[1]) self.falling = True self.turn = 0.5 # trick to make sure the game render ####################### if self.total == 0: fill(0) textSize(TEXT_SIZE) text("GAME OVER", self.space['x'] / TEXT_COORDINATE_X, self.offset / TEXT_COORDIATE_Y) self.playable = False self.turn = -1
def test_check_matrix_win(): new_array = [[2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0], [2, 0, 2, 0, 0, 0], [2, 0, 0, 2, 0, 0]] gc = GameChecker(new_array, 2) assert gc.check_matrix() is True