def test_slice_four_2d_array(): gc = GameChecker([1, 2, 3, 4], 2) new_array = [[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]] assert gc.slice_four(new_array) == [[[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]], [[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4]], [[2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5]], [[3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]]]
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
def test_calculate_diagonal_nowin(): gc = GameChecker([1, 2, 3, 4], 2) new_array = [[2, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0]] assert gc.calculate_diagonal(new_array) is False
def test_game_checker_default(): gc = GameChecker([1, 2, 3, 4], 2) assert gc.score == 16
def test_calculate_diagonal_left_win(): gc = GameChecker([1, 2, 3, 4], 2) new_array = [[0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 2, 0], [0, 0, 0, 2, 0, 0], [0, 0, 2, 0, 0, 0]] assert gc.calculate_diagonal(new_array) is True
def test_calculate_horizontal_nowin(): gc = GameChecker([1, 2, 3, 4], 2) new_array = [[0, 0, 0, 2, 2, 2], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] assert gc.calculate_horizontal(new_array) is False
def test_calculate_product(): gc = GameChecker([1, 2, 3, 4], 2) new_array = [1, 2, 3, 4] assert gc.calculate_product(new_array) == 24
def test_slice_four_1d_array(): gc = GameChecker([1, 2, 3, 4], 2) new_array = [1, 2, 3, 4, 5, 6] assert gc.slice_four(new_array) == [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
config[section][key] = value with open(file, 'w') as config_file: config.write(config_file) def update_dialogue(self, msg): self.dialogue.config(state="normal") self.dialogue.delete("0.0", "end") self.dialogue.insert("0.0", msg) self.dialogue.config(state="disabled") def delete_games(self): with open("games.txt", "w") as file: file.write("[GAMES]") def add_gamecard(self, frame, allies, enemies, times): GameCard(frame, self, allies, enemies, times) def mainloop(self): self.master.mainloop() if __name__ == "__main__": app = App() GameChecker(app).start() app.mainloop() globals.app_closed = True