def start(self): game_over = False while not game_over: self.draw_board() for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.MOUSEBUTTONDOWN: if self._service.get_turn() == 1: try: x = event.pos[0] // 45 y = event.pos[1] // 45 self._service.player_move(x, y) if utils.game_over(self._service.get_board(), 1): game_over = True except Exceptions: continue self.draw_board() if not game_over and self._service.get_turn() == -1: self._service.computer_move() if utils.game_over(self._service.get_board(), -1): game_over = True self.draw_board() font = pygame.font.Font('freesansbold.ttf', 70) if self._service.get_turn() == -1: text = font.render('You Won!', True, (0, 0, 143)) text_rect = text.get_rect() text_rect.center = (45 * self._service.get_length() // 2, 45 * self._service.get_length() // 2) self._screen.blit(text, text_rect) elif self._service.get_turn() == 1: text = font.render('You Lost!', True, (0, 0, 150)) text_rect = text.get_rect() text_rect.center = (45 * self._service.get_length() // 2, 45 * self._service.get_length() // 2) self._screen.blit(text, text_rect) pygame.display.update() r = True while r: for event in pygame.event.get(): if event.type == pygame.QUIT: r = False pygame.quit()
def cities(update: Update, context: CallbackContext) -> None: # TODO мб упростить функцию и разнести на более мелкие if context.args and len(context.args) == 1: city = context.args[0].lower() if context.user_data.get('used_cities'): if city[0] not in CITIES or city not in CITIES[city[0]]: msg = f"Такого города не знаю, введите город на букву {context.user_data['last_letter']}" update.message.reply_text(msg) elif city in context.user_data['used_cities']: update.message.reply_text('Увы такой город уже был') else: context.user_data['used_cities'].add(city) bot_city = next_city(last_letter(city), context.user_data['used_cities']) if not bot_city: game_over(update, context.user_data['used_cities'], last_letter(city)) return context.user_data['used_cities'].add(bot_city) msg_part_1 = f'Мой город: {bot_city.title()}\n' context.user_data['last_letter'] = last_letter( bot_city).upper() msg_part_2 = f'Тебе на букву {context.user_data["last_letter"]}' update.message.reply_text(msg_part_1 + msg_part_2) elif city[0] not in CITIES or city not in CITIES[city[0]]: update.message.reply_text("Такого города не знаю") else: context.user_data['used_cities'] = set() context.user_data['used_cities'].add(city) bot_city = next_city(last_letter(city), context.user_data['used_cities']) context.user_data['used_cities'].add(bot_city) msg_part_1 = f'Мой город: {bot_city.title()}\n' context.user_data['last_letter'] = last_letter(bot_city).upper() msg_part_2 = f'Тебе на букву {context.user_data["last_letter"]}' update.message.reply_text(msg_part_1 + msg_part_2) elif context.args: msg = "Тебе следует вводить название одного города.\nВо вводе только один пробел после /cities" update.message.reply_text(msg) elif context.user_data.get('used_cities'): update.message.reply_text( f"Введи /cities и город на букву {context.user_data['last_letter']}" ) else: update.message.reply_text( f"Для старта игры введи /cities и любой существующий в этом мире город" )
def start(self): game_over = False while not game_over: if self._service.get_turn() == 1: try: square = self.get_player_coord() self._service.player_move(square[0], square[1]) if utils.game_over(self._service.get_board(), 1): game_over = True except (ValueError, IndexError): print("Bad input") except InvalidMove as msg: print(msg) else: self._service.computer_move() if utils.game_over(self._service.get_board(), -1): game_over = True self.print_board() # refresh screen if self._service.get_turn() == -1: print("You Won the game!!!") else: print("You lost the game!!!")
def mini_max(self, board_obj, depth, alpha, beta, maximizing_player): """ function alphabeta(node, depth, α, β, maximizingPlayer) is if depth = 0 or node is a terminal node then return the heuristic value of node if maximizingPlayer then value := −∞ for each child of node do value := max(value, alphabeta(child, depth − 1, α, β, FALSE)) α := max(α, value) if α ≥ β then break (* β cut-off *) return value else value := +∞ for each child of node do value := min(value, alphabeta(child, depth − 1, α, β, TRUE)) β := min(β, value) if α ≥ β then break (* α cut-off *) return value """ board = board_obj.get_board valid_locations = self.get_valid_locations(board) is_terminal = self.is_terminal_node(board_obj) if depth == 0 or is_terminal: if is_terminal: if utils.game_over(board, -1): return None, 100000000 elif utils.game_over(board, 1): return None, -100000000 else: # Depth is zero return None, self.score_board(board, -1) if maximizing_player: value = -math.inf point_good = random.choice(valid_locations) for point in valid_locations: row = point[0] col = point[1] b_copy = deepcopy(board_obj) b_copy.move(row, col) new_score = self.mini_max(b_copy, depth - 1, alpha, beta, False)[1] if new_score > value: value = new_score point_good = point alpha = max(alpha, value) if alpha >= beta: break return point_good, value else: # Minimizing player value = math.inf point_good = random.choice(valid_locations) for point in valid_locations: row = point[0] col = point[1] b_copy = deepcopy(board_obj) b_copy.move(row, col) new_score = self.mini_max(b_copy, depth - 1, alpha, beta, True)[1] if new_score < value: value = new_score point_good = point beta = min(beta, value) if alpha >= beta: break return point_good, value
def is_terminal_node(self, board): return utils.game_over(board.get_board, -1) or utils.game_over( board.get_board, 1) or len( self.get_valid_locations(board.get_board)) == 0
from utils import correct_it, game_over, right, wrong, welcome, get_tip import data points = 0 asks = 0 total_types = len(data.exercises) if __name__ == '__main__': welcome() while True: collection = data.exercises[randrange(total_types)] questions = collection["questions"] type_of_collection = collection["type"] question, answers = questions[randrange(len(questions))] print(question) get_tip(collection["tip"]) your_answer = input() if your_answer == "exit": break asks += 1 if correct_it(type_of_collection, question, your_answer, answers): right() points += 1 else: wrong(type_of_collection, question, answers) game_over(asks, points)
def test_game_over(self): b = Board(15) for i in range(5): b._board[i][i] = 1 self.assertTrue(game_over(b.get_board, 1)) self.assertFalse(game_over(b.get_board, -1))
def test_game_over(self): """Test if font object created""" pg.font.init() font_surface = utils.game_over(True) self.assertIsInstance(font_surface, pg.Surface)