-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
280 lines (244 loc) · 8.75 KB
/
main.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
#!/usr/bin/python3
# Created by Jordan Leich on 6/6/2020
# Edited by Adam Smith
import sys
import webbrowser
from blackjack import *
from other.colors import print_green, print_red, print_yellow, print_blue
def load_or_save_game(save_game=None, stats=False):
# save_game = class instance (i.e. save_game=self)
"""
loads the game only when Load Game feature used
"""
user_score, user_balance, dealer_balance, bot_number = 0, 1000, 5000, 0
bot_balances = []
if not save_game and path.exists("data.json"):
with open('data.json', 'r') as user_data_file:
user_data = json.load(user_data_file)
user_score = user_data['score']
user_balance = user_data['balance']
dealer_balance = user_data['deal_balance']
bot_balances = user_data['bot_balances']
if not stats:
print_green('Save file loaded and saved successfully!\n')
elif not save_game:
if not stats:
print_yellow('Save file not found, A new save file will be created shortly!\n')
sleep(1)
default_stats = {'balance': user_balance,
'score': user_score,
'deal_balance': dealer_balance,
'bot_balances': bot_balances,
}
with open('data.json', 'w') as user_data_file:
json.dump(default_stats, user_data_file)
else:
user_score, user_balance, dealer_balance, bot_balances = save_game.get_data()
print_yellow('Saving game... Please do not exit the program!\n')
default_stats = {'balance': user_balance,
'score': user_score,
'deal_balance': dealer_balance,
'bot_balances': bot_balances,
}
with open('data.json', 'w') as user_data_file:
json.dump(default_stats, user_data_file)
return user_balance, user_score, dealer_balance, bot_number, bot_balances
def open_github(print_text, website_append=''):
"""
Open github in the users default web browser
"""
print_green(print_text)
webbrowser.open_new(f'https://github.com/JordanLeich/Blackjack-21{website_append}')
sleep(1)
def donation_opener(website):
"""
Open a donation page in the users default web browser
"""
print_green("Opening PayPal Donation page...\n")
webbrowser.open_new(website)
sleep(2)
def audio_options(audio_option):
choice = str(input('Would you like to have audio sound effects (On / Off): '))
print()
sleep(1)
if choice.lower() in ['y', 'yes', 'on', 'sure']:
audio_option = True
print_green('Audio sounds effects have been turned on!\n')
elif choice.lower() in ['n', 'no', 'nope', 'off']:
audio_option = False
print_green('Audio sounds effects have been turned off!\n')
else:
print_red('Audio input option error found!\n')
sleep(1)
audio_options(audio_option)
sleep(1)
return audio_option
def view_leaderboards():
print_green('---User hands won leaderboards---\n')
sleep(.5)
with open('leaderboards.txt', 'r') as f:
leaderboard = [line.replace('\n', '') for line in f.readlines()]
for i in leaderboard:
print(i)
print()
sleep(2)
def extra():
"""
Main hub UI for the user to view additional information or extra parts of this project such as donations and releases
"""
while True:
print("(1) View Stats")
print("(2) View Leaderboards")
print("(3) Audio")
print("(4) Project Releases")
print("(5) Credits")
print("(6) Donate")
print("(7) Main Menu")
print("(8) Exit\n")
choice = input("Which choice would you like to pick: ")
print()
sleep(.5)
if choice == '1':
view_stats()
elif choice == '2':
view_leaderboards()
elif choice == '3':
audio_options(True)
elif choice == '4':
open_github("Opening the latest stable release...\n", "/releases")
elif choice == '5':
open_github("Opening all contributors of this project...\n", "/graphs/contributors")
elif choice == '6':
donation_opener("https://www.paypal.com/donate/?business=8FGHU8Z4EJPME&no_recurring=0¤cy_code=USD")
elif choice == '7':
return
elif choice == '8':
load_or_save_game()
sleep(1)
sys.exit()
else:
print_red("Please select a number from 1 - 8.\n")
def view_stats():
"""
Prints the users current in game stats based upon a load file
"""
user_balance, user_score, dealer_balance, _, bot_balances = load_or_save_game(stats=True)
print_green('Your current in game stats will now be displayed below!\n')
sleep(1)
print(f'Your balance is ${user_balance}')
print(f'The dealers balance is ${dealer_balance}')
print(f'Total hands won is {user_score}')
for c, v in enumerate(bot_balances):
print(f"Player{c}'s balance is ${v}")
print()
sleep(3)
def game(user_balance=1000, user_score=None, dealer_balance=5000, players=0, bot_balances=None, name=''):
"""
Main code used for the game entirely
"""
if user_score is None:
user_score = [0, 0]
if bot_balances is None:
bot_balances = []
if name == '':
name = str(input('Enter your name: '))
print()
sleep(1)
single_player = Blackjack(user_balance, user_score, dealer_balance, name)
if bot_balances:
single_player.add_bot_balances(bot_balances)
for _ in range(players):
single_player.add_bots()
single_player.play_game()
def bot_player_choice():
"""
User chooses how many bots to play with
"""
while True:
player_num = int(input('How many bots would you like to play with: '))
print()
sleep(.5)
get_plural = 'bot' if player_num == 1 else 'bots'
print_green(f'{player_num} {get_plural} will be added into the game!\n')
sleep(1)
return player_num
def custom_user_input(user_message, result=0):
"""
helper function for custom_game_setup() - validates that user input is a number greater than 0
"""
while result <= 0:
try:
result = int(input(user_message))
except ValueError:
print_red('Please enter a number.')
continue
print()
sleep(.5)
if result <= 0:
print('Please choose a number greater than 0.')
return result
def custom_game_setup():
"""
Set up a custom game
"""
user_balance = custom_user_input('How much money would you like to start with: ')
dealer_balance = custom_user_input('How much money would you like the dealer to start with: ')
user_score = custom_user_input('How much would you like to set your scoring count to: ')
return user_balance, user_score, dealer_balance
def game_options():
"""
Allows the end-user to be able to play the game with custom money, win counts, and more
"""
while True:
print("(1) Play alone")
print("(2) Play with bots")
print("(3) Play with custom settings")
print("(4) Tutorial")
print("(5) Main menu")
print("(6) Exit\n")
game_choice = input("Which choice would you like to pick: ")
print()
sleep(.5)
if game_choice == '1':
game()
elif game_choice == '2':
game(players=bot_player_choice())
elif game_choice == '3':
game()
elif game_choice == '4':
print_green('A youtube video should now be playing... ')
webbrowser.open("https://www.youtube.com/watch?v=eyoh-Ku9TCI", new=1)
elif game_choice == '5':
return
elif game_choice == '6':
load_or_save_game()
sleep(1)
sys.exit()
else:
print_red("Please select a number from 1 - 6.\n")
def main():
while True:
print_green("Welcome to BlackJack 21\n")
print("(1) New Game") # tutorial
print("(2) Load Existing Game")
print("(3) Options") # view stats, extras, releases, donate
print("(4) Quit\n")
choice = input('Which choice would you like to pick: ')
print()
sleep(.5)
if choice == '1':
print_blue("Welcome Player!\n")
game_options()
elif choice == '2':
game()
elif choice == '3':
extra()
elif choice == '4':
load_or_save_game()
sleep(1)
print_green("Exiting the Game...\n")
sys.exit()
else:
print_red("Please select a number from 1 - 4.\n")
if __name__ == '__main__':
main()