Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.12 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import random
  6. import re
  7. import sqlite3
  8.  
  9. while True:
  10.  
  11. class signIn:
  12. def new_user(self):
  13. new_user_username_input = input("Hi, please give me your new username. nn")
  14. if not re.match("^[a-zA-Z0-9 _]*$", new_user_username_input):
  15. print("Please enter a valid username. (Only letters and numbers allowed)")
  16. self.new_user()
  17. sql = ("select exists(SELECT * from USERS where USERNAME = ?)")
  18. args = (new_user_username_input)
  19. cursor = self.database_connection.execute(sql, (args,))
  20. for username in cursor:
  21. pass
  22. if username[0] == 1:
  23. print("This username is already taken. Please provide a different one.")
  24. self.new_user()
  25. elif username[0] == 0:
  26. pass
  27. else:
  28. print("Something unexpected happened. Please try again")
  29. sys.exit()
  30. new_user_password_input = input("Please give me your new password. nn")
  31. if not re.match("^[a-zA-Z0-9 _]*$", new_user_password_input):
  32. print("Please enter a valid username and password combination. (Only letters and numbers allowed)")
  33. self.new_user()
  34. print ("Creating new user... n")
  35. sql = ("INSERT INTO USERS (USERNAME,PASSWORD,WINS,LOSES,GAMESPLAYED,WINPERCENT) VALUES (?,?,0,0,0,0)")
  36. args = (new_user_username_input,new_user_password_input)
  37. self.database_connection.execute(sql, args)
  38. self.database_connection.commit()
  39. print ("New user created successfully!n")
  40. self.menu()
  41.  
  42. class Stats:
  43. def statistics(self):
  44. print("Here are the top 10 players in the order, who has the highest winning percentagen")
  45. order = self.database_connection.execute("SELECT USERNAME, WINS, LOSES, GAMESPLAYED, WINPERCENT FROM USERS ORDER BY WINPERCENT DESC LIMIT 10")
  46. self.database_connection.commit()
  47. for row in order:
  48. print ("Username = ", row[0])
  49. print ("Wins = ", int(row[1]))
  50. print ("Loses = ", int(row[2]))
  51. print ("Games played = ", int(row[3]))
  52. print ("Winning percent = ", round(row[4], 1), "n");
  53. self.menu()
  54.  
  55.  
  56. class Users:
  57. def allUsers(self):
  58. print("Below are listed all the available users.nYou can create a new user from the MENU.n")
  59. order = self.database_connection.execute("SELECT USERNAME FROM USERS ORDER BY USERNAME ASC")
  60. self.database_connection.commit()
  61. for row in order:
  62. print (row[0]);
  63. print ("n")
  64. self.menu()
  65.  
  66.  
  67. class RockPaperScissors(signIn,Stats,Users):
  68. def __init__(self):
  69. RockPaperScissors.database_connection = sqlite3.connect('test.db')
  70. self.database_connection.execute('''CREATE TABLE if not exists USERS
  71. (ID INTEGER PRIMARY KEY AUTOINCREMENT,
  72. USERNAME TEXT NOT NULL,
  73. PASSWORD TEXT NOT NULL,
  74. WINS FLOAT NOT NULL,
  75. LOSES FLOAT NOT NULL,
  76. GAMESPLAYED FLOAT NOT NULL,
  77. WINPERCENT FLOAT NOT NULL );''')
  78.  
  79. def welcome(self):
  80. print("© 2017 Nico NilssonnnThis is a Rock, Paper, Scissors game where you can play against a computer. nThe one who first gets three wins is the champion.n")
  81.  
  82. def menu(self):
  83. user_input = input("--- MENU --- nPlay (1) nUsers (2) nNew user (3) nDelete User (4) nStatistics (5) nExit game (6)nn")
  84. if user_input == "1":
  85. self.player1_username()
  86. elif user_input == "2":
  87. self.allUsers()
  88. elif user_input == "3":
  89. self.new_user()
  90. elif user_input == "4":
  91. self.deleteUsers()
  92. elif user_input == "5":
  93. self.statistics()
  94. elif user_input == "6":
  95. print ("nThanks for playing!")
  96. sys.exit()
  97. else:
  98. print ("Please give a valid answer (1),(2),(3),(4),(5) or (6).n")
  99. self.menu()
  100.  
  101. def player1_username(self):
  102. player1_username_input = input("Player 1, please give me your username: nn")
  103. RockPaperScissors.player1_username_input = player1_username_input
  104. if not re.match("^[a-zA-Z0-9 -äöåÄÖÅ _]*$", self.player1_username_input):
  105. print("Please enter a valid username using only letters, numbers and spaces.")
  106. self.player1_username()
  107. sql = ("select exists(SELECT * from USERS where USERNAME = ?)")
  108. args = (player1_username_input)
  109. cursor = self.database_connection.execute(sql, (args,))
  110. for username in cursor:
  111. pass
  112. if username[0] == 1:
  113. self.player1_password()
  114. elif username[0] == 0:
  115. print ("nThe username was not found.nYou must create a user in MENU if you haven't got one.n")
  116. self.menu()
  117. else:
  118. print("Something unexpected happened. Please try again")
  119. sys.exit()
  120.  
  121. def player1_password(self):
  122. player1_password_input = input("Please give me your password: nn")
  123. if not re.match("^[a-zA-Z0-9 -äöåÄÖÅ _]*$", player1_password_input):
  124. print("You can only enter a password with letters, numbers and spaces.")
  125. self.player1_password()
  126. sql = ("select exists(SELECT * from USERS where PASSWORD = ? AND USERNAME = ?)")
  127. args = (player1_password_input,self.player1_username_input)
  128. cursor = self.database_connection.execute(sql, args)
  129. for password in cursor:
  130. pass
  131. if password[0] == 1:
  132. print ("Login successful ")
  133. elif password[0] == 0:
  134. print ("The password doesn't match with the given username. Please try again.n")
  135. self.menu()
  136. else:
  137. print("Something unexpected happened. Please try again")
  138. sys.exit()
  139.  
  140. def deleteUsers(self):
  141. delete_username_input = input("Which user you would like to delete?nn")
  142. sql = ("select exists(SELECT * from USERS where USERNAME = ?)")
  143. args = (delete_username_input)
  144. cursor = self.database_connection.execute(sql, (args,))
  145. for username in cursor:
  146. pass
  147. if username[0] == 1:
  148. delete_password_input = input("Please give password of the user. nn")
  149. sql = ("select exists(SELECT * from USERS where PASSWORD = ? AND USERNAME = ?)")
  150. args = (delete_password_input,delete_username_input)
  151. cursor = self.database_connection.execute(sql, args)
  152. for password in cursor:
  153. pass
  154. if password[0] == 1:
  155. sql = ("DELETE from USERS where USERNAME = ?;")
  156. args = (delete_username_input)
  157. self.database_connection.execute(sql, (args,))
  158. self.database_connection.commit()
  159. print ("User {0} successfully deleted!n".format(delete_username_input))
  160. self.menu()
  161. elif password[0] == 0:
  162. print("The given password didn't match the usernamen")
  163. self.menu()
  164. else:
  165. print("Something unexpected happened. Please try again")
  166. sys.exit()
  167. elif username[0] == 0:
  168. print("The given username was not found.n")
  169. self.menu()
  170. else:
  171. print("Something unexpected happened. Please try again")
  172. sys.exit()
  173.  
  174. class playAgain(RockPaperScissors):
  175. def new_game(self):
  176. self.new_game_choice = str.lower(str.strip(input("Do you want to play another round? (yes/no)nn")))
  177. if self.new_game_choice == "yes":
  178. print("Great!nn")
  179. elif self.new_game_choice == "no":
  180. print("Thanks for playing!")
  181. self.database_connection.close()
  182. sys.exit()
  183. else:
  184. print("Please enter a yes or no as an answer")
  185. self.new_game()
  186.  
  187.  
  188. class Messages(RockPaperScissors):
  189. def win_messages(self):
  190. Messages.player1_win_message = ("Congrats {0}, you won computer in the game!".format(self.player1_username_input))
  191. Messages.computer_win_message = ("Too bad {0}, computer won you in the game!".format(self.player1_username_input))
  192. Messages.player1_games_won = 0
  193. Messages.computer_games_won = 0
  194.  
  195. def player1_congrats(self):
  196. print ("Gongrats {0}, you are the champion!n".format(self.player1_username_input))
  197. sql = ("UPDATE USERS set WINS = WINS + 1 where USERNAME = ?")
  198. args = (self.player1_username_input)
  199. self.database_connection.execute(sql, (args,))
  200.  
  201. def computer_congrats(self):
  202. print ("Too bad {0}, computer is the champion!n".format(self.player1_username_input))
  203. sql = ("UPDATE USERS set LOSES = LOSES + 1 where USERNAME = ?")
  204. args = (self.player1_username_input)
  205. self.database_connection.execute(sql, (args,))
  206.  
  207. def congrats(self):
  208. sql = ("UPDATE USERS set GAMESPLAYED = WINS + LOSES where USERNAME = ?")
  209. args = (self.player1_username_input)
  210. self.database_connection.execute(sql, (args,))
  211. sql = ("UPDATE USERS set WINPERCENT = WINS/GAMESPLAYED*100 where USERNAME = ?")
  212. args = (self.player1_username_input)
  213. self.database_connection.execute(sql, (args,))
  214. self.database_connection.commit()
  215.  
  216.  
  217. class Choices(Messages):
  218. def player1_choice(self):
  219. self.player1_guess = str.lower(str.strip(input("So {0} rock(1), paper(2) or scissors(3)?nn".format(self.player1_username_input))))
  220. if self.player1_guess == "1":
  221. self.player1_num = 1
  222. elif self.player1_guess == "2":
  223. self.player1_num = 2
  224. elif self.player1_guess == "3":
  225. self.player1_num = 3
  226. else:
  227. print ("Please only insert number 1,2 or 3 as an answer!")
  228. self.player1_choice()
  229.  
  230. def computer_choice(self):
  231. self.computer_num = random.randrange(1,4)
  232. if self.computer_num == 1:
  233. print("The computer chose rock")
  234. elif self.computer_num == 2:
  235. print("The computer chose paper")
  236. elif self.computer_num == 3:
  237. print("The computer chose scissors")
  238.  
  239. def results(self):
  240. difference = self.computer_num - self.player1_num
  241. if difference == 0:
  242. print ("It's a tie!")
  243. elif difference % 3 == 1:
  244. Choices.computer_games_won+=1
  245. print (self.computer_win_message)
  246. elif difference % 3 == 2:
  247. Choices.player1_games_won+=1
  248. print (self.player1_win_message)
  249. print ("So far {0} has won {1} times, and computer has won {2} times".format(self.player1_username_input,self.player1_games_won,self.computer_games_won))
  250.  
  251.  
  252. eka = RockPaperScissors()
  253. eka.welcome()
  254. eka.menu()
  255. toka = signIn()
  256. kolmas = Stats()
  257. neljas = Messages()
  258. neljas.win_messages()
  259. viides = playAgain()
  260. kuudes = Choices()
  261. seitsemas = Users()
  262. while True:
  263. if kuudes.player1_games_won == 3:
  264. neljas.player1_congrats()
  265. neljas.congrats()
  266. viides.new_game()
  267. break
  268. elif kuudes.computer_games_won == 3:
  269. neljas.computer_congrats()
  270. neljas.congrats()
  271. viides.new_game()
  272. break
  273. else:
  274. kuudes.player1_choice()
  275. kuudes.computer_choice()
  276. kuudes.results()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement