def main(): n_ids = 3242 ids_to_score_path = '../static/numpy/ids_to_score.npy' board = generate_board(n_ids) invalid_guesses = set([picture['pic_id'] for picture in board]) predictor = Predictor(board, ids_to_score_path, invalid_guesses) best_guess, best_scores = predictor.get_best_guess_and_scores() round_scores = [round(s) for s in best_scores] board_types = [b['type'] for b in board] for t, s in zip(board_types, round_scores): print(f'Type:{t} || Score:{s}') predictor.display_board(best_guess, round_scores, shape=(5, 5))
def index(): """ Homepage for the website. Create a random board. """ n_ids = 3242 board = generate_board(n_ids) invalid_guesses = [picture['pic_id'] for picture in board] board.insert( 0, { "difficulty": "easy", "invalid_guesses": invalid_guesses, 'blue': 1.0, 'red': 1.0, 'neutral': 1.0 }) return render_template('html/page.html', board=board)
def main(): parser = argparse.ArgumentParser() parser.add_argument("--text", action="store_true", default=False, help="Run in text mode") args = parser.parse_args() board = utils.generate_board() if args.text: solver = Solver(board) solver.print_board() print('*' * 60, "SOLUTION", '*' * 60) solver.solve() solver.print_board() return GraphicalGame(board).game_loop()
# draws the flooded cells def draw_flooded(self, cell): cell.color = self.current_color cell.flooded = True cell.draw() # GAME LOOP def game_loop(self, done): while not self.done: for event in pygame.event.get(): # if you quit if event.type == pygame.QUIT: self.done = True # if you click if event.type == pygame.MOUSEBUTTONUP: if len(self.to_flood) == 0: pos = pygame.mouse.get_pos() self.current_color = self.find_color(pos) self.to_flood = [self.board[0]] self.update_flooded() pygame.display.update() self.clock.tick(10) board = utils.generate_board() game1 = Game(board) game1.game_loop(False) pygame.quit()
def fight(player_board): PLAYER_BOARD = copy.deepcopy(player_board) COMPUTER_BOARD = make_computer_board() COMPUTER_BOARD_STATIC = copy.deepcopy(COMPUTER_BOARD) COMP = generate_board() Fight = True FOUND_SHIP = None turn = False count = 0 START_NEW_GAME_POS = (SCREEN_LENGTH / 2 - 100, BANNER_y + 300) myfont = pygame.font.SysFont('Comic Sans MS', 30) counter_title = myfont.render('Step counts', True, WHITE) while Fight: screen.blit(picture, (0, 0)) draw_board(PLAYER_BOARD) draw_board(COMPUTER_BOARD) counter = myfont.render('{0}'.format(count), True, WHITE) screen.blit(new_game, START_NEW_GAME_POS) screen.blit(counter_title, (SCREEN_LENGTH / 2 - 50, BANNER_y + 100)) screen.blit(counter, (SCREEN_LENGTH / 2, BANNER_y + 150)) pygame.display.flip() for event in pygame.event.get(): if event.type is QUIT: pygame.quit() elif event.type is MOUSEBUTTONDOWN: if button_selector(event.pos, [START_NEW_GAME_POS], [new_game.get_rect()[2:]]) is not None: start_new_game() elif check_on_board( event.pos, COMPUTER_BOARD, PARTICLE_HEIGHT, PARTICLE_WIDTH ) is not None and event.button == 1 and turn is False: brd = check_on_board(event.pos, COMPUTER_BOARD, PARTICLE_HEIGHT, PARTICLE_WIDTH) if turn is False: if COMP[brd[0]][brd[1]] == 1: friend = False show_blowup_animation( COMPUTER_BOARD[brd[0]][brd[1]][1:]) COMPUTER_BOARD[brd[0]][brd[1]][0] = 3 COMP[brd[0]][brd[1]] = 0 positions, diagonals = find_neighboor_computer( COMPUTER_BOARD, brd) for p in positions: if COMP[p[0]][p[1]] == 1: friend = True draw_invalid_places( COMPUTER_BOARD, diagonals) break if friend is False: draw_invalid_places(COMPUTER_BOARD, diagonals + positions) if check_finish_computer(COMP): finish(player=False) count += 1 COMPUTER_BOARD_STATIC = copy.deepcopy( COMPUTER_BOARD) elif COMPUTER_BOARD_STATIC[brd[0]][brd[1]][0] is 0: show_fail_animation( COMPUTER_BOARD[brd[0]][brd[1]][1:]) turn = True pygame.display.flip() COMPUTER_BOARD[brd[0]][brd[1]][0] = 1 COMPUTER_BOARD_STATIC = copy.deepcopy( COMPUTER_BOARD) step = ai_turn(PLAYER_BOARD) while step is not None: draw_board(PLAYER_BOARD) pygame.display.flip() FPSCLOCK.tick(60) if check_finish_player(PLAYER_BOARD): finish(player=True) step = ai_turn(PLAYER_BOARD) if check_finish_player(PLAYER_BOARD): finish(player=True) draw_board(PLAYER_BOARD) pygame.display.flip() turn = False elif event.type == MOUSEMOTION: brd = check_on_board(event.pos, COMPUTER_BOARD, PARTICLE_HEIGHT, PARTICLE_WIDTH) if brd is not None and COMPUTER_BOARD[brd[0]][ brd[1]][0] is not 3: COMPUTER_BOARD = copy.deepcopy(COMPUTER_BOARD_STATIC) COMPUTER_BOARD[brd[0]][brd[1]][0] = 1 else: COMPUTER_BOARD = copy.deepcopy(COMPUTER_BOARD_STATIC)
def test_generate_board(self): self.assertEqual(len(utils.generate_board()), consts.BOARD_SIZE**2)
def test_fix_links(self): board = utils.generate_board() self.assertEqual(board[0].right, board[1]) self.assertEqual(board[1].left, board[0]) self.assertEqual(board[9].right, None) self.assertEqual(board[99].right, None)
def post_game(): """ Create a new game --- tags: - game definitions: - schema: id: Game properties: id: type: string description: Unique identifier of the game status: type: string description: Any of playing, win or lost. board: type: array items: type: object properties: has_mine: type: boolean description: Indicates if there is a mine in this cell exploded: type: boolean description: Indicates if there is this cell has exploded. revealed: type: boolean description: Indicates if there is this cell has revealed. flagged: type: boolean description: Indicates if there is this cell has been flagged. value: type: integer description: After its revealed, it indicates the number of adjacent mines. description: 2D array of the game cells parameters: - in: body name: body schema: id: GameRequest properties: rows: type: integer description: Number of rows cols: type: integer description: Number of cols mines: type: integer description: Number of mines (not to exceed 50% ratio) responses: 200: description: Game created schema: $ref: '#/definitions/Game' """ params = request.json check_create_params(params) new_game = { 'rows': params.get('rows', DEFAULT_ROWS) if params else DEFAULT_ROWS, 'cols': params.get('cols', DEFAULT_COLS) if params else DEFAULT_COLS, 'mines': params.get('mines', DEFAULT_MINES) if params else DEFAULT_MINES, 'status': 'playing' } validate_game(new_game) generate_board(new_game) add_mines(new_game) calculate_value(new_game) games_col.insert_one(new_game) return dumps(create_view(new_game))