· 7 years ago · Nov 12, 2018, 02:20 PM
1game = 0
2
3import random
4import sqlite3
5
6with sqlite3.connect("login.db") as db:
7 cursor = db.cursor()
8cursor.execute('''
9create table if not exists user(
10userID integer primary key,
11username varchar(20) not null,
12firstname varchar(20) not null,
13surname varchar(20) not null,
14password varchar(20) not null);
15''')
16
17def login():
18 while True:
19 username = input("(+) Enter your username:")
20 password = input("(+) Enter your password:")
21
22 with sqlite3.connect("login.db") as db:
23 cursor = db.cursor()
24 find_user = ('SELECT * FROM user WHERE username = ? AND password = ?')
25 cursor.execute(find_user, [(username),(password)]) #[] replaces the values of the ?
26 results = cursor.fetchall()
27
28 if results:
29 for i in results:
30 print("Welcome "+i[2])
31 a = 1
32
33 return (i[2])
34 else:
35 print("(-) Username and password not recognised")
36 again = input("Do you want to retry? (Y/N)")
37 if again.lower() == "n":
38 print ("Goodbye")
39 exit()
40 return ("Exit")
41
42def newUser():
43 print("(+) Add a new user")
44 #check username is taken
45 found = 0
46 while found == 0:
47 username = input("(+) Enter a username: ")
48 with sqlite3.connect("login.db") as db:
49 cursor = db.cursor()
50 find_user = ('SELECT * FROM user WHERE username = ?')#?stops SQL injection
51 cursor.execute(find_user,[(username)])#[] replaces the values of the ?
52
53 if cursor.fetchall():
54 print("(-) Usename taken")
55 else:
56 found = 1
57
58 firstname = input("(+) Please enter your first name: ")
59 surname = input ("(+) Please enter your last name: ")
60 password = input ("(+) Please enter a password: ")
61 password1 = input ("(+) Please re-enter your password: ")
62 while password != password1:
63 print("(-) Passwords do not match")
64 password = input("(+) Please enter a password: ")
65 password1 = input ("(+) Please re-enter a password: ")
66
67 insertData = '''INSERT INTO user(username,firstname,surname,password)
68 VALUES(?,?,?,?)'''
69 cursor.execute(insertData,[(username),(firstname),(surname),(password)])
70 db.commit()#saves the results to the database
71
72
73
74
75def userMenu(game):
76 score = 0
77 score2 = 0
78 dice1 = random.randint(1,6)
79 dice2 = random.randint(1,6)
80 dice3 = random.randint(1,6)
81 dice4 = random.randint(1,6)
82 RolledDice = dice1 + dice2
83 RolledDice2 = dice3 + dice4
84 print(" ")
85 print("(-- Player 1 Press Enter to roll a dice --)")
86 input()
87 print("Player 1 has rolled a dice and got",dice1, "(press enter to roll your second dice!)")
88 input()
89 print("Player1 has rolled another dice and got",dice2,"(press enter to see your total score!)")
90 input()
91 print("Player1's total score is now",RolledDice)
92 score = dice1 + dice2
93 if RolledDice % 2 == 0: #used from comments of https://stackoverflow.com/questions/21837208/check-if-a-number-is-odd-or-even-in-python
94 print(" ")
95 print("your rolled dice added to make an even number so 10 points have been added to your score!")
96 score = score + 10
97 print(" ")
98 print("your score is now",score)
99 else:
100 print(" ")
101 print("your rolled dice added to make an odd number so 5 points will be taken from your score!")
102 score = score - 5
103 print(" ")
104 print("Player1 your score is now",score,"(press enter)")
105 input()
106
107 print(" ")
108 print("(-- Player 2 Press Enter to roll a dice --)")
109 print(" ")
110 print("Player 2 has rolled a dice and got",dice3, "(press enter to roll your second dice!)")
111 input()
112 print("Player2 has rolled another dice and got",dice4,"(press enter to see your total score!)")
113 input()
114 print("Player2's total score is now",RolledDice2)
115 score2 = dice3 + dice4
116 if RolledDice2 % 2 == 0:
117 print(" ")
118 print("your rolled dice added to make an even number so 10 points have been added to your score!")
119 score2 = score2 + 10
120 print(" ")
121 print("your score is now",score2)
122 else:
123 print(" ")
124 print("your rolled dice added to make an odd number so 5 points will be taken from your score!")
125 score2 = score2 - 5
126 print(" ")
127 print("Player2 your score is now",score2,"(press enter)")
128 input()
129 game = game + 1
130 return game
131
132def rules():
133 print("------------------------------------")
134 print(" Rules of the game")
135 print("------------------------------------")
136 print('''
137 • The points rolled on each player's dice are added to their score.
138 • If the total is an even number, an additional 10 points are added to your score.
139 • If the total is an odd number, 5 points are subtracted from your score.
140 • If you roll a double, you get to roll one extra die and get the number of points rolled added to your score.
141 • The score of a player cannot go below 0 at any point.
142 • The person with the highest score at the end of 5 rounds wins.
143 • If both players have the same score at the end of the 5 rounds, they each roll 1 die and whoever gets the highest score wins (this repeats until someone wins). \n ''')
144 print("------------------------------------")
145 print(" Press Enter to start the game")
146 print("------------------------------------")
147
148
149while True:
150 print("------------------------------------")
151 print(" Welcome to the system ")
152 print("------------------------------------")
153 print(" ")
154 print(" ")
155 print("------------------------------------")
156 print(" User options")
157 print("------------------------------------")
158 menu =('''
159
160 1 - Create New User
161 2 - Login
162 3 - Exit \n ''')
163
164 userChoice = input(menu)
165 game = 0
166 if userChoice == "1":
167 newUser()
168 elif userChoice == "2":
169 enter = login()
170 if enter == "exit":
171 break
172 else:
173 print("Game Starting!")
174 askrules = input("Do you want the rules of the game? If you do type *yes* otherwise, press enter")
175 if askrules == "yes".lower():
176 rules()
177 while game < 5:
178 game = userMenu(game)
179
180 elif userChoice == "3":
181 print("Goodbye")
182 exit()
183 else:
184 print("(-) Input not recognised, please try")