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 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 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 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 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 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)