Skip to content

Commit 554b624

Browse files
committed
classes code
1 parent a92cfb5 commit 554b624

File tree

2 files changed

+333
-2
lines changed

2 files changed

+333
-2
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import random"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 2,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"class Roll:\n",
19+
"\n",
20+
" def __init__(self, name):\n",
21+
" self.name = name\n",
22+
" self.beats = None\n",
23+
" self.loses = None\n",
24+
" \n",
25+
" def can_defeat(self, beats):\n",
26+
" if self.beats == beats:\n",
27+
" return True\n",
28+
" else:\n",
29+
" return False\n",
30+
" \n",
31+
" def loses_to(self, loses):\n",
32+
" if self.loses == loses:\n",
33+
" return True\n",
34+
" else:\n",
35+
" return False"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"def create_rolls():\n",
45+
" paper = Roll('Paper')\n",
46+
" rock = Roll('Rock')\n",
47+
" scissors = Roll('Scissors')\n",
48+
" \n",
49+
" paper.beats = rock\n",
50+
" paper.loses = scissors\n",
51+
" \n",
52+
" rock.beats = scissors\n",
53+
" rock.loses = paper\n",
54+
" \n",
55+
" scissors.beats = paper\n",
56+
" scissors.loses = rock \n",
57+
" \n",
58+
" return paper, rock, scissors"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 4,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"def game_action(player,computer):\n",
68+
" print(f'You chose {player.name}, computer chose {computer.name}')\n",
69+
" if player.can_defeat(computer):\n",
70+
" print('You won!')\n",
71+
" elif player.loses_to(computer):\n",
72+
" print('You lost!')\n",
73+
" else:\n",
74+
" print('Tie!')"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 33,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"def player_input(rolls):\n",
84+
" choice = input(\"[R]ock, [P]aper, [S]cissors: \")\n",
85+
" if choice.upper() == 'R':\n",
86+
" return rolls[1]\n",
87+
" elif choice.upper() == 'P':\n",
88+
" return rolls[0]\n",
89+
" elif choice.upper() == 'S':\n",
90+
" return rolls[2]\n",
91+
" else:\n",
92+
" print(\"Invalid input\")"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 34,
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"rolls = create_rolls()"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 35,
107+
"metadata": {},
108+
"outputs": [
109+
{
110+
"name": "stdin",
111+
"output_type": "stream",
112+
"text": [
113+
"[R]ock, [P]aper, [S]cissors: r\n"
114+
]
115+
}
116+
],
117+
"source": [
118+
"player_roll = player_input(rolls)"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 30,
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"data": {
128+
"text/plain": [
129+
"'Scissors'"
130+
]
131+
},
132+
"execution_count": 30,
133+
"metadata": {},
134+
"output_type": "execute_result"
135+
}
136+
],
137+
"source": [
138+
"player_roll.name"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": 6,
144+
"metadata": {},
145+
"outputs": [
146+
{
147+
"data": {
148+
"text/plain": [
149+
"'Rock'"
150+
]
151+
},
152+
"execution_count": 6,
153+
"metadata": {},
154+
"output_type": "execute_result"
155+
}
156+
],
157+
"source": [
158+
"computer_choice = random.choice(rolls)\n",
159+
"computer_choice.name"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 11,
165+
"metadata": {},
166+
"outputs": [
167+
{
168+
"data": {
169+
"text/plain": [
170+
"'Rock'"
171+
]
172+
},
173+
"execution_count": 11,
174+
"metadata": {},
175+
"output_type": "execute_result"
176+
}
177+
],
178+
"source": [
179+
"my_choice = rolls[1]\n",
180+
"my_choice.name"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 12,
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"name": "stdout",
190+
"output_type": "stream",
191+
"text": [
192+
"You chose Rock, computer chose Rock\n",
193+
"Tie!\n"
194+
]
195+
}
196+
],
197+
"source": [
198+
"game_action(my_choice,computer_choice)"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": null,
204+
"metadata": {},
205+
"outputs": [],
206+
"source": [
207+
"def game_loop:\n",
208+
" "
209+
]
210+
}
211+
],
212+
"metadata": {
213+
"kernelspec": {
214+
"display_name": "Python 3",
215+
"language": "python",
216+
"name": "python3"
217+
},
218+
"language_info": {
219+
"codemirror_mode": {
220+
"name": "ipython",
221+
"version": 3
222+
},
223+
"file_extension": ".py",
224+
"mimetype": "text/x-python",
225+
"name": "python",
226+
"nbconvert_exporter": "python",
227+
"pygments_lexer": "ipython3",
228+
"version": "3.6.6"
229+
}
230+
},
231+
"nbformat": 4,
232+
"nbformat_minor": 2
233+
}

days/13-15-text-games/code/game.py

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1+
import random
2+
3+
14
def main():
25
print_header()
3-
player = ask_player_name()
6+
ask_player_name()
7+
game_loop(create_rolls())
8+
9+
10+
class Roll:
11+
12+
def __init__(self, name):
13+
self.name = name
14+
self.beats = None
15+
self.loses = None
16+
17+
def can_defeat(self, beats):
18+
if self.beats == beats:
19+
return True
20+
else:
21+
return False
22+
23+
def loses_to(self, loses):
24+
if self.loses == loses:
25+
return True
26+
else:
27+
return False
428

529

630
def print_header():
@@ -10,12 +34,86 @@ def print_header():
1034
def ask_player_name():
1135
name = input("What is your name: ")
1236
continue_game = input(f'are you ready to rock {name}? Type YES or NO: ')
13-
if continue_game == 'YES':
37+
if continue_game.upper() == 'YES':
1438
print('Heck YEA!')
1539
else:
1640
print(f'Bye {name}...')
1741
exit()
1842

1943

44+
def create_rolls():
45+
paper = Roll('Paper')
46+
rock = Roll('Rock')
47+
scissors = Roll('Scissors')
48+
49+
paper.beats = rock
50+
paper.loses = scissors
51+
52+
rock.beats = scissors
53+
rock.loses = paper
54+
55+
scissors.beats = paper
56+
scissors.loses = rock
57+
58+
return paper, rock, scissors
59+
60+
61+
def player_input(rolls):
62+
choice = input("[R]ock, [P]aper, [S]cissors: ")
63+
if choice.upper() == 'R':
64+
return rolls[1]
65+
elif choice.upper() == 'P':
66+
return rolls[0]
67+
elif choice.upper() == 'S':
68+
return rolls[2]
69+
else:
70+
print("Invalid input")
71+
72+
73+
def computer_input(rolls):
74+
computer = random.choice(rolls)
75+
return computer
76+
77+
78+
def game_action(player, computer):
79+
print(f'You chose {player.name}, computer chose {computer.name}')
80+
if player.can_defeat(computer):
81+
print('You won!')
82+
return 1
83+
elif player.loses_to(computer):
84+
print('You lost!')
85+
return 0
86+
else:
87+
print('Tie!')
88+
return -1
89+
90+
91+
def game_loop(rolls):
92+
tracker = {'player': 0, 'computer': 0, 'tie': 0}
93+
count = 0
94+
while count < 3:
95+
computer_roll = computer_input(rolls)
96+
player_roll = player_input(rolls)
97+
98+
outcome = game_action(player_roll, computer_roll)
99+
100+
if outcome == 1:
101+
tracker["player"] += 1
102+
elif outcome == 0:
103+
tracker["computer"] += 1
104+
else:
105+
tracker["tie"] += 1
106+
107+
count += 1
108+
# print(tracker)
109+
110+
if tracker.get("player") > tracker.get("computer"):
111+
print("YOU WON THE WHOLE THING!")
112+
elif tracker.get("player") < tracker.get("computer"):
113+
print("YOU LOST TO A COMPUTER OMG")
114+
else:
115+
print("HOW DID YOU TIE THREE TIMES IN A ROW??!?")
116+
117+
20118
if __name__ == '__main__':
21119
main()

0 commit comments

Comments
 (0)