Added astroid game
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// 1. This file should contain backend API documentation
|
||||
// TODO: Import Flask and PyMongo (line 1-2)
|
||||
from flask import Flask, session, redirect, url_for, request, jsonify
|
||||
from flask_pymongo import PyMongo
|
||||
|
||||
// TODO: Connect to MongoDB database (line 4-6)
|
||||
app = Flask(__name__)
|
||||
app.config['MONGO_URI'] = 'mongodb://localhost:27017/student_db'
|
||||
mongo = PyMongo(app)
|
||||
|
||||
// TODO: Define API endpoint for getting questions (line 8-10)
|
||||
@app.route('/get_question')
|
||||
def get_question():
|
||||
question_num = session.get('current_question', 0)
|
||||
question = mongo.db.questions.find_one({"question_number": question_num})
|
||||
|
||||
if question:
|
||||
return jsonify({
|
||||
"question": question['question'],
|
||||
"options": question['options'],
|
||||
"question_number": question_num,
|
||||
"current_prize": calculate_prize(question_num)
|
||||
})
|
||||
else:
|
||||
return jsonify({"game_over": True, "final_score": calculate_final_score()})
|
||||
|
||||
// TODO: Define API endpoint for submitting answers (line 12-14)
|
||||
@app.route('/answer', methods=['POST'])
|
||||
def answer_question():
|
||||
user_answer = request.json.get('answer')
|
||||
question_num = session.get('current_question', 0)
|
||||
current_question = mongo.db.questions.find_one({"question_number": question_num})
|
||||
|
||||
is_correct = current_question['options'][current_question['correct_answer']] == user_answer
|
||||
|
||||
if is_correct:
|
||||
session['current_question'] = question_num + 1
|
||||
return jsonify({
|
||||
"correct": True,
|
||||
"correct_answer": current_question['correct_answer'],
|
||||
"game_over": question_num + 1 >= TOTAL_QUESTIONS
|
||||
})
|
||||
else:
|
||||
return jsonify({
|
||||
"correct": False,
|
||||
"correct_answer": current_question['correct_answer'],
|
||||
"game_over": True
|
||||
})
|
||||
|
||||
// TODO: Add error handling for database connection (line 16-18)
|
||||
try:
|
||||
mongo.db.command("ping")
|
||||
print("Successfully connected to MongoDB!")
|
||||
except Exception as e:
|
||||
print(f"MongoDB connection error: {e}")
|
||||
@@ -0,0 +1,45 @@
|
||||
// 1. This file should contain database connection information
|
||||
// TODO: Import MongoDB driver (line 1)
|
||||
import pymongo
|
||||
|
||||
// TODO: Connect to MongoDB using the connection URI (line 3-5)
|
||||
client = pymongo.MongoClient("mongodb://localhost:27017/")
|
||||
|
||||
// TODO: Select the appropriate database (line 7)
|
||||
db = client["student_db"]
|
||||
|
||||
// TODO: Select the questions collection (line 9)
|
||||
questions_collection = db["questions"]
|
||||
|
||||
// TODO: Add error handling for database connection (line 11)
|
||||
try:
|
||||
client.admin.command('ping')
|
||||
print("Successfully connected to MongoDB!")
|
||||
except Exception as e:
|
||||
print(f"MongoDB connection error: {e}")
|
||||
|
||||
// 2. Database population instructions (new section)
|
||||
// TODO: Create function to insert questions into database (line 13-25)
|
||||
def insert_questions():
|
||||
questions = [
|
||||
{
|
||||
"question_number": 0,
|
||||
"question": "What is the capital of Russia?",
|
||||
"options": ["Moscow", "Saint Petersburg", "Novosibirsk", "Yekaterinburg"],
|
||||
"correct_answer": 0
|
||||
},
|
||||
{
|
||||
"question_number": 1,
|
||||
"question": "Which river is the longest in Russia?",
|
||||
"options": ["Volga", "Yenisey", "Ob", "Amur"],
|
||||
"correct_answer": 1
|
||||
}
|
||||
]
|
||||
|
||||
# Insert questions into collection
|
||||
db.questions.insert_many(questions)
|
||||
print("Questions inserted successfully!")
|
||||
|
||||
// TODO: Call insert_questions function (line 27)
|
||||
if __name__ == "__main__"::
|
||||
insert_questions()
|
||||
@@ -0,0 +1,46 @@
|
||||
// 1. This file should contain frontend implementation instructions
|
||||
|
||||
// TODO: Create HTML structure with game container (line 1-5)
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Russian Quiz</title>
|
||||
</head>
|
||||
|
||||
// TODO: Add question display elements (line 7-10)
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Russian Quiz</h1>
|
||||
<div class="score">Prize: <span id="prize">0</span> ₽</div>
|
||||
|
||||
<div class="question-box">
|
||||
<div class="question-number">Question <span id="q-number">1</span>/5</div>
|
||||
<div class="question" id="question-text">Loading...</div>
|
||||
|
||||
<div class="options" id="options">
|
||||
<!-- Options go here -->
|
||||
</div>
|
||||
|
||||
<div class="result" id="result"></div>
|
||||
</div>
|
||||
|
||||
// TODO: Create answer option buttons (line 12-15)
|
||||
<div class="options" id="options">
|
||||
<!-- Options will be dynamically inserted here -->
|
||||
</div>
|
||||
|
||||
<button class="lifeline" onclick="useFiftyFifty()">50:50 Lifeline</button>
|
||||
|
||||
// TODO: Implement game controls and lifelines (line 17-20)
|
||||
<div class="game-over" id="game-over" style="display: none;">
|
||||
<h2>Game Over!</h2>
|
||||
<p>You won: <span id="final-prize">0</span> ₽</p>
|
||||
<button onclick="restartGame()">Play Again</button>
|
||||
</div>
|
||||
|
||||
<script src="{{ url_for('static', filename='script.js') }}"></script>
|
||||
</body>
|
||||
|
||||
// TODO: Add game over screen UI (line 22-25)
|
||||
// Game over screen is already included above as part of the main structure
|
||||
Reference in New Issue
Block a user