Skip to content

Commit e7cfaac

Browse files
rps page + jquery
1 parent e215eb0 commit e7cfaac

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

hello_app/rockpaperscissor.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
class Participant:
2+
def __init__(self, name):
3+
self.name = name
4+
self.points = 0
5+
self.choice = ""
6+
def choose(self,choice):
7+
self.choice = choice
8+
return "{name} selects {choice}".format(name=self.name, choice=self.choice)
9+
10+
def toNumericalChoice(self):
11+
switcher = {
12+
"rock": 0,
13+
"paper": 1,
14+
"scissor": 2,
15+
"lizard": 3,
16+
"spock": 4
17+
}
18+
return switcher[self.choice]
19+
def incrementPoint(self):
20+
self.points += 1
21+
22+
class GameRound:
23+
def __init__(self):
24+
self.rules = [
25+
[0, -1, 1, 1, -1],
26+
[1, 0, -1, -1, 1],
27+
[-1, 1, 0, 1, -1],
28+
[-1, 1, -1, 0, 1],
29+
[1, -1, 1, -1, 0]
30+
]
31+
def compareChoices(self, p1, p2):
32+
result = self.rules[p1.toNumericalChoice()][p2.toNumericalChoice()]
33+
34+
if result > 0:
35+
p1.incrementPoint()
36+
elif result < 0:
37+
p2.incrementPoint()
38+
39+
return "Round resulted in a {result}".format(result=self.getResultAsString(result))
40+
def getResultAsString(self, result):
41+
res = {
42+
0: "draw",
43+
1: "win",
44+
-1: "loss"
45+
}
46+
return res[result]
47+
48+
class Game:
49+
def __init__(self):
50+
self.endGame = False
51+
self.participant = Participant("Spock")
52+
self.secondParticipant = Participant("Kirk")
53+
def start(self):
54+
while not self.endGame:
55+
GameRound(self.participant, self.secondParticipant)
56+
self.checkEndCondition()
57+
def checkEndCondition(self):
58+
answer = input("Continue game y/n: ")
59+
if answer == 'y':
60+
GameRound(self.participant, self.secondParticipant)
61+
self.checkEndCondition()
62+
else:
63+
print("Game ended, {p1name} has {p1points}, and {p2name} had {p2points}".format(p1name=self.participant.name, p1points=self.participant.points, p2name=self.secondParticipant.name, p2points=self.secondParticipant.points))
64+
self.determineWinner()
65+
self.endGame = True
66+
def determineWinner(self):
67+
resultString = "It's a Draw"
68+
if self.participant.points > self.secondParticipant.points:
69+
resultString = "Winner is {name}".format(name=self.participant.name)
70+
elif self.participant.points < self.secondParticipant.points:
71+
resultString = "Winner is {name}".format(name=self.secondParticipant.name)
72+
return resultString

hello_app/templates/layout.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
<meta charset="utf-8" />
55
<title>{% block title %}{% endblock %}</title>
66
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
7+
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
78
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='site.css') }}" />
9+
<script src="https://code.jquery.com/jquery-3.7.1.slim.min.js" integrity="sha256-kmHvs0B+OpCW5GVHUNjv9rOmY0IvSIRcf7zGUDTDQM8=" crossorigin="anonymous"></script>
10+
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js" integrity="sha256-lSjKY0/srUM9BE3dPm+c4fBo1dky2v27Gdjm2uoZaL0=" crossorigin="anonymous"></script>
811
</head>
912

1013
<body>
1114
<div class="navbar">
1215
<a href="{{ url_for('home') }}" class="navbar-brand">Home</a>
1316
<a href="{{ url_for('translator') }}" class="navbar-item">Translator</a>
17+
<a href="{{ url_for('rockpaperscissor') }}" class="navbar-item">RockPaperScissor</a>
1418
<a href="{{ url_for('about') }}" class="navbar-item">About</a>
1519
<a href="{{ url_for('contact') }}" class="navbar-item">Contact</a>
1620
</div>
@@ -20,7 +24,7 @@
2024
{% endblock %}
2125
<hr/>
2226
<footer>
23-
<p>&copy; 2018</p>
27+
<p>&copy; 2024</p>
2428
</footer>
2529
</div>
2630
</body>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends "layout.html" %}
2+
{% block title %}
3+
Rock Paper Scissor
4+
{% endblock %}
5+
{% block content %}
6+
<div class="container">
7+
rockpaperscissor
8+
</div>
9+
{% endblock %}

hello_app/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from flask import Flask, render_template, request
77
from . import app
88

9+
from rockpaperscissor import *
10+
911
@app.route("/")
1012
def home():
1113
return render_template("home.html")
@@ -59,6 +61,10 @@ def translator_post():
5961
target_language=target_language
6062
)
6163

64+
@app.route("/rockpaperscissor/")
65+
def about():
66+
return render_template("rockpaperscissor.html")
67+
6268
@app.route("/about/")
6369
def about():
6470
return render_template("about.html")

0 commit comments

Comments
 (0)