-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_for_game.py
51 lines (40 loc) · 1.49 KB
/
application_for_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask import Flask, render_template, request, jsonify
from GuessGame import GuessGame
from CurrencyRouletteGame import CurrencyGame
from memoryGame import MemoryGame
from score import add_score
from utility import screen_cleaner
app = Flask(__name__)
@app.route('/')
def index():
return render_template('gamecodehtml.html')
@app.route('/play', methods=['POST'])
def play():
try:
data = request.json
game_type = int(data['game'])
difficulty = int(data['difficulty'])
user_input = data.get('user_input', '')
if user_input == '':
return jsonify({'result': 'Please enter a valid input.'}), 400
# Initialize the appropriate game based on user input
if game_type == 1:
game = MemoryGame(difficulty)
elif game_type == 2:
game = GuessGame(difficulty)
elif game_type == 3:
game = CurrencyGame(difficulty)
else:
return jsonify({'result': 'Invalid game type selected'})
# Play the game
user_won = game.play(user_input)
# Update score and clean screen if the user won
if user_won:
add_score(difficulty)
screen_cleaner()
return jsonify({'result': 'You won!' if user_won else 'You lost!'})
except Exception as e:
print(f"Error: {e}")
return jsonify({'result': 'An error occurred'}), 500
if __name__ == '__main__':
app.run(debug=True)