Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.61 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import random
  5. import re
  6. import sqlite3
  7.  
  8. while True:
  9.  
  10. class RockPaperScissors:
  11. def __init__(self): #Define __init__ method
  12. print("nnThis is a Rock, Paper, Scissors game where you can play against a computer. nThe one who first gets three wins is the champion.n")
  13.  
  14. def menu(self): #Define menu method which displays the menu of the program
  15. user_input = input("--- MENU --- nLogin & Play (1) nCreate a New User (2) nStatistics (3)nExit game (4)nn")
  16. #The player is given an option to choose from four different options
  17. if user_input == "1":
  18. pass
  19. elif user_input == "2":
  20. self.new_user() #If the answer is '2', go to new_user method
  21. elif user_input == "3":
  22. self.statistics() #If the answer is '3', go to statistics method
  23. elif user_input == "4":
  24. print ("nThanks for playing!")
  25. sys.exit() #If the answer is '4', end the running program
  26. else:
  27. print ("Please give a valid answer (1),(2),(3) or (4).n")
  28. self.menu() #If the answer is invalid, return to menu method
  29.  
  30. def player1_username(self): #Define player1_username method where player input a new username for their user
  31. database_connection = sqlite3.connect('test.db') #Connects to a 'test.db' database
  32. player1_username_input = input("Player 1, please give me your username: nn")
  33. self.player1_username_input = player1_username_input
  34. if not re.match("^[a-zA-Z0-9 _]*$", self.player1_username_input): #Checks that only letters, numbers or spaces are given
  35. print("Please enter a valid username using only letters, numbers and spaces.")
  36. database_connection.close() #Closes the connection to database
  37. self.player1_username() #Goes back to player1_username method if the username is invalid
  38. cursor = database_connection.execute("select exists(SELECT * from USERS where USERNAME = '{0}')".format(player1_username_input))
  39. for username in cursor: #Checks if the username exists in database
  40. pass
  41. if username[0] == 1:
  42. pass
  43. elif username[0] == 0:
  44. print ("nThe username was not found in the database. You must create a user if you haven't got one.n")
  45. database_connection.close() #Closes the connection to database
  46. self.menu() #Returns to the menu if the username was not found in the database
  47. else:
  48. print("Something unexpected happened. Please try again")
  49. sys.exit()
  50.  
  51. def player1_password(self): #Define player1_password method where players input a password for their existing user
  52. database_connection = sqlite3.connect('test.db') #Connects to a 'test.db' database
  53. player1_password_input = input("Please give me your password: nn")
  54. if not re.match("^[a-zA-Z0-9 _]*$", player1_password_input): #Checks that only letters, numbers or spaces are given
  55. print("You can only enter a password with letters, numbers and spaces.")
  56. database_connection.close() #Closes the connection to database
  57. self.player1_password() #Goes back to player1_password method if the password is invalid
  58. cursor = database_connection.execute("select exists(SELECT * from USERS where PASSWORD = '{0}' AND USERNAME = '{1}')".format(player1_password_input,self.player1_username_input))
  59. for password in cursor: #Checks if the password matches to the given username in database
  60. pass
  61. if password[0] == 1:
  62. print ("Login successful ")
  63. database_connection.close() #Closes the connection to database if the login was successfull
  64. elif password[0] == 0:
  65. print ("The password doesn't match with the given username. Please try again.")
  66. self.player1_password() #If the password desn't match, the password is being asked again from the player
  67. else:
  68. print("Something unexpected happened. Please try again")
  69. sys.exit()
  70.  
  71. def new_user(self): #Define new_user method where players can create a new user
  72. database_connection = sqlite3.connect('test.db') #Connects to a 'test.db' database
  73. new_user_username_input = input("Hi, please give me your new username. nn")
  74. if not re.match("^[a-zA-Z0-9 _]*$", new_user_username_input): #Checks that only letters, numbers or spaces are given
  75. print("Please enter a valid username. (Only letters and numbers allowed)")
  76. self.new_user() #Goes back to new_user method if the username is invalid
  77. cursor = database_connection.execute("select exists(SELECT * from USERS where USERNAME = '{0}')".format(new_user_username_input))
  78. for username in cursor:
  79. pass
  80. if username[0] == 1:
  81. print("This username is already taken. Please provide a different one.")
  82. self.new_user() #If the username is already taken, it goes back to new_user method
  83. elif username[0] == 0:
  84. pass
  85. else:
  86. print("Something unexpected happened. Please try again")
  87. sys.exit()
  88. new_user_password_input = input("Please give me your new password. nn")
  89. if not re.match("^[a-zA-Z0-9 _]*$", new_user_password_input): #Checks that only letters, numbers or spaces are given
  90. print("Please enter a valid username and password combination. (Only letters and numbers allowed)")
  91. self.new_user() #Goes back to new_user method if the password is invalid
  92. print ("Creating new user... n")
  93. database_connection.execute("INSERT INTO USERS (USERNAME,PASSWORD,WINS,LOSES,GAMESPLAYED,WINPERCENT)
  94. VALUES ('{0}', '{1}',0,0,0,0)".format(new_user_username_input, new_user_password_input)); #Inserts the new user to the database
  95. database_connection.commit() #Performs a commit to the database
  96. database_connection.close() #Closes the connection to database
  97. print ("New user created successfully!n")
  98. self.menu() #Returns to the menu method
  99.  
  100. def statistics(self): #Define statistics method where statistics about the users are shown
  101. database_connection = sqlite3.connect('test.db') #Connects to a 'test.db' database
  102. print("Here are the top 10 players in the order, who has the highest winning percentagen")
  103. order = database_connection.execute("SELECT USERNAME, WINS, LOSES, GAMESPLAYED, WINPERCENT FROM USERS ORDER BY WINPERCENT DESC LIMIT 10")
  104. #The query selects the top 10 users from database in the order that who has the highest winning percentage
  105. database_connection.commit() #Performs a commit to the database
  106. for row in order:
  107. print ("Username = ", row[0])
  108. print ("Wins = ", int(row[1]))
  109. print ("Loses = ", int(row[2]))
  110. print ("Games played = ", int(row[3]))
  111. print ("Winning percent = ", row[4], "n");
  112. database_connection.close() #Closes the connection to database
  113. self.menu() #Returns to the menu method
  114.  
  115. def win_messages(self): #Define win_messages method where the winning messages are shown after a win from one round of the game
  116. self.player1_win_message = ("Congrats {0}, you won computer in the game!".format(self.player1_username_input))
  117. self.computer_win_message = ("Too bad {0}, computer won you in the game!".format(self.player1_username_input))
  118. self.player1_games_won = 0 #Initializes the player1_games_won variable
  119. self.computer_games_won = 0 #Initializes the computer_games_won variable
  120.  
  121. def player1_congrats(self): #Define player1_congrats method where the winning message for player1 is shown after winning three rounds against computer
  122. database_connection = sqlite3.connect('test.db') #Connects to a 'test.db' database
  123. print ("Gongrats {0}, you are the champion!n".format(self.player1_username_input)) #Displays a winning message for player1
  124. database_connection.execute("UPDATE USERS set WINS = WINS + 1 where USERNAME = '{0}'".format(self.player1_username_input));
  125. database_connection.execute("UPDATE USERS set GAMESPLAYED = WINS + LOSES where USERNAME = '{0}'".format(self.player1_username_input));
  126. database_connection.execute("UPDATE USERS set WINPERCENT = WINS/GAMESPLAYED*100 where USERNAME = '{0}'".format(self.player1_username_input));
  127. #The querys above update the WINS, LOSES, GAMESPLAYED and WINPERCENT tables in the database
  128. database_connection.commit() #Performs a commit to the database
  129. database_connection.close() #Closes the connection to database
  130.  
  131. def computer_congrats(self): #Define computer_congrats method
  132. database_connection = sqlite3.connect('test.db') #Connects to a 'test.db' database
  133. print ("Too bad {0}, computer is the champion!n".format(self.player1_username_input)) #Displays a winning message for computer
  134. database_connection.execute("UPDATE USERS set LOSES = LOSES + 1 where USERNAME = '{0}'".format(self.player1_username_input));
  135. database_connection.execute("UPDATE USERS set GAMESPLAYED = WINS + LOSES where USERNAME = '{0}'".format(self.player1_username_input));
  136. database_connection.execute("UPDATE USERS set WINPERCENT = WINS/GAMESPLAYED*100 where USERNAME = '{0}'".format(self.player1_username_input));
  137. #The querys above update the WINS, LOSES, GAMESPLAYED and WINPERCENT tables in the database
  138. database_connection.commit() #Performs a commit to the database
  139. database_connection.close() #Closes the connection to database
  140.  
  141. def new_game(self): #Define new_game method where player1 is being asked to play another round
  142. self.new_game_choice = str.lower(str.strip(input("Do you want to play another round? (yes/no)nn")))
  143. if self.new_game_choice == "yes":
  144. print("Great!nn") #If the player chooses 'yes', the program is started again
  145. elif self.new_game_choice == "no":
  146. print("Thanks for playing!")
  147. sys.exit() #Exit the running program
  148. else:
  149. print("Please enter a yes or no as an answer")
  150. self.new_game() #Call the new_game method again if the answer is invalid
  151.  
  152. def player1_choice(self): #Define player1_choice method asks the player1's input in the choice between rock, paper and scissors
  153. self.player1_guess = str.lower(str.strip(input("So {0} rock, paper or scissors?nn".format(self.player1_username_input))))
  154. if self.player1_guess == "rock":
  155. self.player1_num = 1
  156. elif self.player1_guess == "paper":
  157. self.player1_num = 2
  158. elif self.player1_guess == "scissors":
  159. self.player1_num = 3
  160. else:
  161. print ("Please only insert rock, paper or scissors as an answer!")
  162. self.player1_choice() #Call the p1_choice method again if the answer is invalid
  163.  
  164. def computer_choice(self): #Define computer_choice method defined what option the computer chooses based on a random number
  165. self.computer_num = random.randrange(1,4)
  166. if self.computer_num == 1:
  167. print("The computer chose rock")
  168. elif self.computer_num == 2:
  169. print("The computer chose paper")
  170. elif self.computer_num == 3:
  171. print("The computer chose scissors")
  172.  
  173. def results(self): #Define results method the displays what the current results are for the game
  174. difference = self.computer_num - self.player1_num
  175. if difference == 0:
  176. print ("It's a tie!")
  177. elif difference % 3 == 1: #If the computer won the round, the difference is 1
  178. self.computer_games_won+=1
  179. print (self.computer_win_message)
  180. elif difference % 3 == 2: #If the user won the round, the difference is 2
  181. self.player1_games_won+=1
  182. print (self.player1_win_message)
  183. 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))
  184.  
  185.  
  186. user = RockPaperScissors()
  187. user.menu()
  188. user.player1_username()
  189. user.player1_password()
  190. user.win_messages()
  191. user.player1_choice()
  192. user.computer_choice()
  193. user.results()
  194. while True:
  195. if user.player1_games_won == 3: #If the player 1 won three rounds against computer, call the player1_congrats method
  196. user.player1_congrats()
  197. user.new_game()
  198. break
  199. elif user.computer_games_won == 3: #If the computer won three rounds against user, call the ccomputer_congrats method
  200. user.computer_congrats()
  201. user.new_game()
  202. break
  203. else: #If either has yet won three rounds go back to player1_choice, computer_choice and results methods
  204. user.player1_choice()
  205. user.computer_choice()
  206. user.results()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement