def online_save_game(): if "active_online_id" in session: game = OnlineGame.query.filter_by(id=session["active_online_id"]).first() assert game, "Unable to get game #{}".format(session["active_online_id"]) moves = BigBoard.from_json(game.board).get_move_history() if moves: export = { "ai": session.get("ai", False), "ai_mode": session.get("ai_mode", None), "start": moves[0][0], "moves": [ (b_r, b_c, s_r, s_c) for (unused_turn, b_r, b_c, s_r, s_c, unused_choice) in moves ], } response = make_response(dumps(export)) response.headers.set("Content-Type", "text/json") response.headers.set( "Content-Disposition", "attachment", filename="TicTacCeption-game.json" ) return response return redirect(url_for("online_game"))
def online_play(board_row, board_col, row, col): game = OnlineGame.query.filter_by(id=session["active_online_id"]).first() assert game, "Unable to get game #{}".format(session["active_online_id"]) assert session["online_pass"] == ( game.xPASS if session["online_player"] == "X" else game.oPASS ), "Wrong password for player {} in game #{}!".format( session["online_player"], game.id ) board = BigBoard.from_json(game.board) assert session["online_player"] == board.get_turn(), "Not your turn to move..." small = str(3 * board_row + board_col) valid_moves = board.get_valid_moves() if board.is_choosing(): row, col = valid_moves[small][0] if (small in valid_moves) and ((row, col) in valid_moves[small]): board.make_move(board_row, board_col, row, col) game.board = board.to_json() db.session.commit() return redirect(url_for("online_game"))
def online_move_history(): moves = [] if "active_online_id" in session: game = OnlineGame.query.filter_by(id=session["active_online_id"]).first() assert game, "Unable to get game #{}".format(session["active_online_id"]) moves = BigBoard.from_json(game.board).get_move_history() return render_template("online-play-by-play.html", moves=moves)
def online_create(): if str(request.referrer).replace(request.host_url, "").startswith("online/home"): xPASS, oPASS = request.form.get("xPASS"), request.form.get("oPASS") assert len(xPASS) in range( 4, 25 ), "The password for player X should be between 4 and 24 characters long." assert len(oPASS) in range( 4, 25 ), "The password for player O should be between 4 and 24 characters long." assert xPASS != oPASS, "The passwords for the players cannot be the same!" # need to decode because postgres encodes it again when inserting xPASS = bcrypt.generate_password_hash(xPASS).decode("utf8") oPASS = bcrypt.generate_password_hash(oPASS).decode("utf8") game_board = BigBoard().to_json() new_game = OnlineGame( timestamp=datetime.utcnow().isoformat(), xPASS=xPASS, oPASS=oPASS, board=game_board, ) db.session.add(new_game) db.session.commit() session["newly_created_id"] = new_game.id return redirect(url_for("online_home")) abort(401)
def make_ai_move(): if "board" in session: board = BigBoard.from_json(session["board"]) b_row, b_col, s_row, s_col = choose_move(board, session.get("ai_mode", None)) return play(b_row, b_col, s_row, s_col) return redirect(url_for("game"))
def load_game(): if request.method == "GET": return render_template("load-game.html") elif request.method == "POST": try: if ( str(request.referrer) .replace(request.host_url, "") .startswith("load-game") ): data = load(request.files["game_json"]) session["ai"] = data.get("ai", False) session["ai_mode"] = data.get("ai_mode", None) if session["ai"] and session["ai_mode"] is None: session["ai_mode"] = "random" new_game = BigBoard() new_game._turn = data["start"] for move in data["moves"]: big_row, big_col, sm_row, sm_col = move new_game.make_move(big_row, big_col, sm_row, sm_col) session["board"] = new_game.to_json() except Exception: message = "Could not load the game." flash(message, "danger") return redirect(url_for("game"))
def game(): if "board" not in session: session["board"] = BigBoard().to_json() if "ai" not in session: session["ai"] = False board = BigBoard.from_json(session["board"]) if board.is_over(): return render_template( "game-over.html", board=board.get_board(), winner=board.check_winner() ) elif session["ai"] and board.get_turn() == "O": return make_ai_move() else: return render_template( "game.html", board=board.get_board(), turn=board.get_turn(), valid=board.get_valid_moves(), choice=board.is_choosing(), )
def play(board_row, board_col, row, col): if "board" in session: board = BigBoard.from_json(session["board"]) small = str(3 * board_row + board_col) valid_moves = board.get_valid_moves() if board.is_choosing(): row, col = valid_moves[small][0] if (small in valid_moves) and ((row, col) in valid_moves[small]): board.make_move(board_row, board_col, row, col) session["board"] = board.to_json() return redirect(url_for("game"))
def online_game(): if "active_online_id" not in session: return redirect(url_for("online_home")) game = OnlineGame.query.filter_by(id=session["active_online_id"]).first() assert game, "Unable to get game #{}".format(session["active_online_id"]) assert session["online_pass"] == ( game.xPASS if session["online_player"] == "X" else game.oPASS ), "Wrong password for player {} in game #{}!".format( session["online_player"], game.id ) board = BigBoard.from_json(game.board) if board.is_over(): return render_template( "game-over.html", board=board.get_board(), winner=board.check_winner() ) elif board.get_turn() != session["online_player"]: return render_template( "online-game.html", board=board.get_board(), turn=board.get_turn(), valid=[], choice=board.is_choosing(), wait=True, game_id=game.id, ) else: return render_template( "online-game.html", board=board.get_board(), turn=board.get_turn(), valid=board.get_valid_moves(), choice=board.is_choosing(), wait=False, game_id=game.id, )
def split_game(moves): new_game = BigBoard() player_0 = [] player_1 = [] new_game.turn = p0 = moves[0][0] for i, m in enumerate(moves): all_cells = new_game.get_board() small_wins = [ all_cells[i][j].check_winner() for j in range(3) for i in range(3) ] small_wins = [ 2 if small_wins[3 * i + j] == new_game.get_turn() else (1 if small_wins[3 * i + j] is None else 0) for j in range(3) for i in range(3) ] small_wins = convert_board_to_int(small_wins) small_boards = [] for i in range(3): for j in range(3): cur_board = all_cells[i][j].get_board() cur_board = [ 2 if cur_board[i][j] == new_game.get_turn() else (1 if cur_board[i][j] is None else 0) for j in range(3) for i in range(3) ] cur_board = convert_board_to_int(cur_board) small_boards.append(cur_board) move_data = [small_wins, *small_boards, *m[1:]] if m[0] == p0: player_0.append(move_data) else: player_1.append(move_data) new_game.make_move(*m[1:-1]) return player_0, player_1
def generate_n_games(n, verbose, pct): games = {} parts = ceil(n * 0.01 * pct) for i in range(n): if verbose and i % parts == 0: print(f"Generating game #{i} ({((i+1)/n)*100:.2f}%)") new_game = BigBoard() while not new_game.is_over(): row, col, x, y = Random.choose_best_move(new_game) new_game.make_move(row, col, x, y) games[f"game#{i}"] = { "moves": new_game.get_move_history(), "winner": new_game.check_winner(), } if verbose: print("Done") return games
def save_game(): if "board" in session: moves = BigBoard.from_json(session["board"]).get_move_history() if moves: export = { "ai": session.get("ai", False), "ai_mode": session.get("ai_mode", None), "start": moves[0][0], "moves": [ (b_r, b_c, s_r, s_c) for (unused_turn, b_r, b_c, s_r, s_c, unused_choice) in moves ], } response = make_response(dumps(export)) response.headers.set("Content-Type", "text/json") response.headers.set( "Content-Disposition", "attachment", filename="TicTacCeption-game.json" ) return response return redirect(url_for("game"))
def move_history(): if "board" in session: moves = BigBoard.from_json(session["board"]).get_move_history() else: moves = [] return render_template("play-by-play.html", moves=moves)
def clear_board(): session["board"] = BigBoard().to_json() return redirect(url_for("game"))