-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_jitsu.py
368 lines (278 loc) · 11.1 KB
/
github_jitsu.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
# coding: utf-8
# # Libraries and Modules
#
# In[13]:
# install the following libraries used using the pip command above in the anaconda environment on the macbook air and macbook pro
import libdw
from libdw import sm
# push to firebase using the python wrapper
import pyrebase
# import the relevant libraries to operate in other ways
import numpy
# import the relevant libraries
import random
# # Firebase authentication and signup phase
# ensure that you have created a firebase project authentication
# firebase authentication and configuration in the following dictionary
config = {"apiKey": "",
"authDomain": "",
"databaseURL": "",
"projectId": "",
"storageBucket": "",
"messagingSenderId": "720730224613",
"appId": "",
"measurementId": ""}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
# allow the user to signup and pass firebase authentication
already_created_gmail_account = input("Have you created a gmail account? please enter yes or no: ")
if already_created_gmail_account == "NO" or already_created_gmail_account == "No" or already_created_gmail_account == "no" or already_created_gmail_account == "nO":
print(
"you might want to register at" + "https://accounts.google.com/signin/v2/identifier?hl=en&passive=true&continue=https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dgmail.com%26oq%3Dgmai%26aqs%3Dchrome.1.69i57j69i59j69i61l2j69i60.5101j0j1%26sourceid%3Dchrome%26ie%3DUTF-8&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
ask_user = input("Have you created a gmail account? please enter yes or no: ")
if ask_user == "yes":
print("Ok please sign in with an email and a password")
email = input("Gmail: ")
print("You have entered an email")
pwd = input("Password: ")
print("you have entered a password")
user = auth.create_user_with_email_and_password(email, pwd)
print("Your user account has been created for jitsu. Now you will sign in")
email = input("Gmail: ")
pwd = input("Password : ")
signin = auth.sign_in_with_email_and_password(email, pwd)
else:
sub_times = input("Have you already created a jitsu account?")
if sub_times == 'yes':
email = input("Gmail: ")
pwd = input("Password : ")
signin = auth.sign_in_with_email_and_password(email, pwd)
print("You have signed in successfully")
# Note: The above print statements are also meant for debugging puposes.
#
# # Elemental Introduction and Game
# define the elemental rules for the user and the following game tutorial for the user
def try_tutorial():
print("You have the following elements available to you!")
el_list = ["fire", 'water', "snow"]
for i in el_list:
user_inp = input("What elements would you like to play: ")
if user_inp == 'fire':
print("Well Done, you have played fire")
if user_inp == 'water':
print("Well done, you have played water")
if user_inp == 'snow':
print('you have unleashed the snow jitsu in you.')
def jitsu_tutorial():
print("First the elements that you can play are only these few:")
el_list = ["fire", "water", "ice"]
for i in el_list:
print(i)
print("You can win by elements: fire beats snow, snow beats water and water beats fire")
print("You get it? Now, you can challenge our very own sensei and herbert the polar bear!")
print("Let's try!")
try_tutorial()
print("Now that you have managed to try and play the elements, let's head back!")
class SM:
def start(self):
self.state = self.start_state # set state as startState
def step(self, input):
# print("Step function called")
(next_state, output) = self.get_next_values(self.state, input)
self.state = next_state # updates the current state to be equal to the next state
return output
def transduce(self, inputs):
self.start()
outputs = []
for input_value in inputs:
output = self.step(input_value)
if (self.done(self.state)):
break
else:
outputs.append(output)
return outputs
def done(self, inp):
pass
class elemental(sm.SM):
def __init__(self):
self.attack = rand.choice(5, 12)
self.defence = 1
self.health = 1
def elemental_attack(self, enemy):
damage = self.attack
enemy.health -= damage // 4
return enemy_health
def get_next_values(self, state, inp): # the input should be between 1 to 5
if state >= 9:
if inp < 5:
next_state = 6
self.attack -= 5
elif state < 9:
if inp <= 5:
self.attack += inp * 1.5
next_state = 8
return self.attack # output
# define the character involve in the game
# states will be passed in form of integer values between 5 and 12
# the idea across is to regenerate the next value of the card elements
# let the card numbers range in the form of
# Every player starts as a ninja character
class Ninja(elemental):
def __init__(self, name, val):
self.name = name
self.health = 7
self.attack = 10
self.start_state = int(input("choose a number you would like to put in "))
def quit_game(self):
self.health = 0
return "You have quit the game and you will not earn any of the values even from the saved game."
def elemental_posession(self, enemy):
self.health -= 1
def elemental_check(self, value):
self.attack = value * 1.5 + 7
return self.attack
def get_next_values(self, state, inp): # the input should be between 1 to 5
if state >= 9:
if inp < 5:
next_state = 6
self.attack -= 5
elif state < 9:
if inp <= 5:
self.attack += inp * 1.5
next_state = 8
return self.attack # output
# define the 2 key players that the user can play with
class enemy:
def __init__(self):
self.health = 7
self.attack = 12
def element_choice(self):
elements = ['fire', 'snow', 'water']
self.element = random.choice(elements)
return self.element
def element_magnitude(self):
self.attack -= random.randrange(1, 5)
return self.attack
# the following enables the function to be reused
def print_win():
print("you win!")
def print_lose():
print("you lose :<")
def print_draw():
print("you draw")
def play_the_game():
user_tut = input("Would you like a jitsu tutorial?")
if user_tut == 'yes':
jitsu_tutorial()
player_in_game()
else:
player_in_game()
def player_in_game():
name = input("what is your name?")
player = Ninja(name, 5)
challenger = enemy()
# this happens for the first time
el = ['fire', 'water', 'snow']
el_dict = {"f": 'fire', 's': 'snow', 'w': 'water'}
# following lists are to compare the elements between the players and attack magnitudes between the players
player_lst = []
enemy_lst = []
num_turns = 7
enemy_num_turns = 7
inp = int(input("choose a random number between 5 to 12 to start with"))
# instantiate what happens next
st = int(inp)
print("you have just started the game")
print("choose from the following elements available to you.")
for i in el:
print(i)
start_el = input("type f for fire, w for water and s for snow ")
player_lst.append(el_dict[start_el])
enemy_lst.append(challenger.element_choice)
next_card_val = player.get_next_values(inp, inp)
print("the next value is {}".format(next_card_val)) # this confirms the next card played
while num_turns > 0 and enemy_num_turns > 0:
next_card_val = player.get_next_values(next_card_val, next_card_val)
player_list = []
enemy_list = []
print("the enemy has attacked, yet again.")
print("Now choose an element")
el = input("type f for fire, w for water and s for snow ")
try:
player_list.append(el_dict[el])
except KeyError:
el = input("please type only f, w or s")
player_list.append(el_dict[el])
enemy_list.append(challenger.element_choice())
if player_list[0] != enemy_list[0]:
if player_list[0] == el_dict['f'] and enemy_list[0] == el_dict['w']:
print_lose()
num_turns -= 1
enemy_num_turns += 1
elif player_list[0] == el_dict['w'] and enemy_list[0] == el_dict['f']:
print_win()
num_turns += 1
enemy_num_turns -= 1
elif player_list[0] == el_dict['w'] and enemy_list[0] == el_dict['s']:
print_lose()
num_turns -= 1
enemy_num_turns += 1
elif player_list[0] == el_dict['s'] and enemy_list[0] == el_dict['w']:
print_win()
num_turns += 1
enemy_num_turns -= 1
elif player_list[0] == el_dict['f'] and enemy_list[0] == el_dict['s']:
print_win()
num_turns += 1
enemy_num_turns -= 1
elif player_list[0] == el_dict['s'] and enemy_list[0] == el_dict['f']:
print_lose()
num_turns -= 1
enemy_num_turns += 1
elif player_list[0] == el_dict['f'] and enemy_list[0] == el_dict['w']:
print_lose()
num_turns -= 1
enemy_num_turns += 1
if player_list[0] == enemy_list[0]:
if next_card_val > challenger.element_magnitude():
print_win()
num_turns += 1
enemy_num_turns -= 1
elif next_card_val < challenger.element_magnitude():
print_lose()
num_turns -= 1
enemy_num_turns += 1
elif next_card_val == challenger.element_magnitude():
print_draw()
num_turns += 0
enemy_num_turns += 0
# compute the number of coins earned based on who lost their life first
if enemy_num_turns == 0 and num_turns > 0:
num_coins = 7
write_to_fb(name, num_coins)
if num_turns == 0 and enemy_num_turns > 0:
num_coins = 3
write_to_fb(name, num_coins)
if num_turns == 0 and enemy_num_turns == 0:
num_coins = 0
print("You have won {} for today! Come back next time!".format(num_coins))
num_try = 1
# end the game from firebase
write = write_from_fb()
print("You have a total of {} today".format(write))
return num_coins
# write to firebase
def write_to_fb(name, l):
db = firebase.database()
user_p = {name: l}
db.child("users").push(user_p)
db.child("users").child("name").set(user_p)
# for subsequent games
db.child("name").child("name").update(user_p)
return user_p
def write_from_fb():
db = firebase.database()
users = db.child("users").get()
return users.val()
play_the_game()