-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (119 loc) · 4.02 KB
/
main.py
File metadata and controls
132 lines (119 loc) · 4.02 KB
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
import curses
import json
from pick import pick
import mystery_number
import rock_paper_scissors
import tic_tac_toe
import user_manager
import utils
import snake
def choose_player(stdscr):
"""
user can choose with wich profile he wants to play
:return: str
"""
users = user_manager.get_user_files()
user_manager.merge_json_files(users)
with open("users.json") as users:
tab = json.load(users)
if tab:
title = "Which player do you want to play with ? \n"
options = []
for i in range(len(tab)):
options.append(tab[i]["username"])
player, index = pick(options, title, screen=stdscr)
stdscr.addstr(9, 0, f"You play as {player} \n")
return player
else:
print("No user found in list... \nUser invite (default) selected ")
user_manager.new_user("invite", stdscr)
return "invite" # if no user exists a default user is created and selected to play
def games_menu(stdscr):
"""
Display games menu and user choose between games or return to main_menu
:return: int
"""
title = """ Choose your game """
options = [
"Mystery number",
"Rock paper scissors",
"Tic Tac Toe",
"Snake",
"Return to main menu",
]
_, manage_choice = pick(options, title, screen=stdscr)
if manage_choice == 0:
player = choose_player(stdscr)
result = mystery_number.mystery_number(player, stdscr)
check_win(result, stdscr)
if manage_choice == 1:
player = choose_player(stdscr)
result = rock_paper_scissors.rock_paper_scissors(player, stdscr)
check_win(result, stdscr)
if manage_choice == 2:
player = choose_player(stdscr)
result = tic_tac_toe.tic_tac_toe(player, stdscr)
check_win(result, stdscr)
if manage_choice == 3:
player = choose_player(stdscr)
result = snake.snake(player, stdscr)
check_win(result, stdscr)
if manage_choice == 4:
return "main"
return manage_choice
def check_win(game_result: [], stdscr):
"""
Check and return true if user wins
increment nb fail or nbwin user's property if fails or wins
:param: array
:return: boolean
"""
user_manager.add_played_game(game_result[2])
if game_result[0] == 1:
if game_result[3] == "mystery_number":
stdscr.addstr(3,0,f"You won with {game_result[1]} attempts")
elif game_result[3] == "shifoumi":
stdscr.addstr(3,0,f"You won with {game_result[1]} points")
elif game_result[3] == "snake":
stdscr.addstr(3,0,f"You ate {game_result[1]} apples")
user_manager.add_win(game_result[2])
return True
elif game_result[0] == -1:
stdscr.addstr(4, 0, """You loose because you doesn't
find the number before the last attempt""")
stdscr.getch()
user_manager.add_fail(game_result[2])
return False
elif game_result[0] == -2:
stdscr.addstr(4, 0, "You loose because you are a monkey")
stdscr.getch()
user_manager.add_fail(game_result[2])
return False
def main(stdscr):
if not user_manager.find_user("invite"):
utils.init_json_files()
running = True
while running:
title = """
🎰 Python Mini Games Console 🎲
"""
options = ["Start", "Users menu", "Quit"]
_, user_choice = pick(options, title, screen=stdscr)
if user_choice == 0:
result = games_menu(stdscr)
while result != "main":
result = games_menu(stdscr)
elif user_choice == 1:
choice = user_manager.users_menu(stdscr)
while choice != "main":
choice = user_manager.users_menu(stdscr)
else:
running = False
stdscr.addstr("Bye\n")
stdscr.refresh()
return 1
if __name__ == "__main__":
try:
curses.wrapper(main)
except KeyboardInterrupt:
utils.handle_exit()