def call_game(ia_final_move, squareside, allcase, gamemode, fenetre): tiles = image_slicer.slice("/Users/gkuma/Downloads/photo_" + str(squareside * 100) + "_.jpg", squareside * squareside, save=False) image_slicer.save_tiles(tiles, directory='/Users/gkuma/Downloads/', prefix='') replay = 1 while (replay == 1): blocks = [] blocks = utils.build_board(allcase, squareside, blocks, fenetre) fond = pygame.image.load("/Users/gkuma/Downloads/background.jpg") fenetre.blit(fond, (0, 0)) first_draw(squareside, fenetre, blocks, gamemode) pygame.display.flip() if gamemode == 'ia': main_loop(ia_final_move, squareside, fenetre, blocks, fond) if gamemode == 'solo': main_loop_solo(squareside, fenetre, blocks, fond) pygame.time.wait(3000) menu_items = ('Replay', 'MainMenu', 'Quit') pygame.display.set_caption('Replay') replay_menu = ReplayMenu(fenetre, menu_items) # return the choice enter in the menu replay = replay_menu.run() if replay == 2: replay = 1 return replay
def init_game(self): self.board = [BasicBlock(pos=pos) for pos in build_board((12, 5))] for block in self.board: self.add_widget(block) self.keyboard_manager.register(self.paddle_move, 'left', -10) self.keyboard_manager.register(self.paddle_move, 'right', 10) self.keyboard_manager.register(self.player.serve_ball, 'spacebar', self.ball, speed=7) self.game_runner = Clock.schedule_interval(self.update, 1.0 / 60.0)
def BFS_graph(s_x, s_y, g_x, g_y, board_size=8): ## initial process time, to compute the program time start_time = time.process_time() ## construct the board ## the element would be (prev_x, prev_y, current_length) board = utils.build_board(board_size, (0, 0, 0)) frontier = [(s_x, s_y, 0)] ## frontier list as a queue done = False while len(frontier) > 0 and not done: cur_pos = frontier[0] ## get one node from frontier list frontier.remove( cur_pos) ## pop out the current node from frontier list for move in utils.moves: new_x, new_y = cur_pos[0] + move[0], cur_pos[1] + move[ 1] ## compute new node coordinate ## check whether it is valid if utils.check_valid_pos( new_x, new_y, board_size=board_size) and board[new_x][new_y][2] == 0: board[new_x][new_y] = (cur_pos[0], cur_pos[1], cur_pos[2] + 1 ) ## update the board values frontier.append((new_x, new_y, cur_pos[2] + 1)) ## add new node to frontier ## if achieve goal state, break it if new_x == g_x and new_y == g_y: done = True break ## calculate the process time cost_time = time.process_time() - start_time ## construct the path from initial state to goal state if board[g_x][g_y][ 2] == 0: ## if the goal state hasn't achieve, set sol_pth as None sol_pth = None else: sol_pth = [(g_x, g_y)] ## add nodes to sol_pth until the sol_pth has initial state while sol_pth[-1][0] != s_x or sol_pth[-1][1] != s_y: prev_pos = board[sol_pth[-1][0]][sol_pth[-1] [1]] ## get previous node sol_pth.append((prev_pos[0], prev_pos[1])) sol_pth.reverse() return sol_pth, cost_time
def DFS_graph(s_x, s_y, g_x, g_y, board_size=8): ## initial process time, to compute the program time start_time = time.process_time() cur_pth = [(s_x, s_y)] ## construct board, element is "True" ## to record which state has been reach, True means it hasn't expanded board = utils.build_board(board_size, True) ## mark the initial state as expanded, and do the recursion board[s_x][s_y] = False sol_pth = recursive_dfs(s_x, s_y, g_x, g_y, board, 0, cur_pth, board_size=board_size) ## calculate the process time cost_time = time.process_time() - start_time return sol_pth, cost_time
def dfs(s_x, s_y, g_x, g_y, max_steps, board_size=8): cur_pth = [(s_x, s_y)] ## construct board, element is (hasn't expanded, previous expanded depth) board = utils.build_board(board_size, (True, 0)) ## mark the initial state as expanded, and do the recursion board[s_x][s_y] = (False, -1) sol_pth = recursive_dfs(s_x, s_y, g_x, g_y, board, 0, cur_pth, max_steps, board_size=board_size) return sol_pth
def game(): # read the HTTP parameters if "user" not in request.values or "game" not in request.values: TODO game = int(request.values["game"]) user = request.values["user"] # connect to the database, then read the game state conn = open_db() (players,size,state) = utils.get_game_info(conn, game) (board, nextToPlay,letter) = utils.build_board(conn, game,size) return render_template("game.html", gameID=game, players=players, size=3, nextToPlay=players[nextToPlay], thisPlayer=user, state=state, board=board, canPlay=(nextToPlay == user))
def move(): if "user" not in request.values or "game" not in request.values: TODO if "pos" not in request.values and "resign" not in request.values: TODO # connect to the database conn = open_db() game = int(request.values["game"]) (players,size,state) = utils.get_game_info(conn, game) print("game_info:", (players,size,state)) user = request.values["user"] if user not in players: TODO if "resign" in request.values: resign = True else: resign = False pos = request.values["pos"].split(",") assert len(pos) == 2 x = int(pos[0]) y = int(pos[1]) (board,nextPlayer,letter) = utils.build_board(conn, game,size) if user != players[nextPlayer]: TODO if resign: # this user is choosing to resign. Update the game state to reflect that. other_player_name = players[1-nextPlayer] cursor = conn.cursor() cursor.execute("""UPDATE games SET state=%s WHERE id=%s;""", (other_player_name+":resignation",game)) cursor.close() else: assert x >= 0 and x < size assert y >= 0 and y < size assert board[x][y] == "" board[x][y] = "XO"[nextPlayer] # we've done all of our sanity checks. We now know enough to say that # it's safe to add a new move. cursor = conn.cursor() cursor.execute("""INSERT INTO moves(gameID,x,y,letter,time) VALUES(%s,%s,%s,%s,NOW());""", (game,x,y,letter)) if cursor.rowcount != 1: TODO cursor.close() result = utils.analyze_board(board) if result != "": if result == "win": result = players[nextPlayer]+":win" cursor = conn.cursor() cursor.execute("""UPDATE games SET state=%s WHERE id=%s;""", (result,game)) cursor.close() # we've made changes, make sure to commit them! conn.commit() conn.close() # Redirect to a GET operation. This is the POST/REDIRECT/GET pattern. return redirect("%s?game=%d&user=$s" % (url_for("game"), game, user), code=303)