Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. import sqlite3
  2. import time
  3. import random
  4.  
  5. with sqlite3.connect("dice.db") as db:
  6. cursor = db.cursor()
  7.  
  8. cursor.execute('''
  9. CREATE TABLE IF NOT EXISTS user(
  10. userID INTEGER PRIMARY KEY,
  11. username VARCHAR(20) NOT NULL,
  12. firstname VARCHAR(20) NOT NULL,
  13. surname VARCHAR(20) NOT NULL,
  14. password VARCHAR(20) NOT NULL);
  15. ''')
  16.  
  17. cursor.execute("""
  18. INSERT INTO user(username,firstname,surname,password)
  19. VALUES("test","test","big","test")
  20. """)
  21. db.commit()
  22.  
  23. import random
  24.  
  25. def gameactual():
  26. game1 = 0
  27. finalscoreone = 0
  28. print("###RULES### \nIf the total score adds up to an even number, an additional 10 points are added to your score \nIf the total score adds up to an odd number, 5 points is subtracted form your score \nIf you roll a double, you get to roll one extra dice and get the number of points rolled added to your score\n")
  29. while game1 < 5:
  30. ask = input("Would you like to roll the dice player 1 (y) ")
  31. if ask == "y":
  32. dice1 = random.randint(1,6)
  33. dice11 = random.randint(1,6)
  34. score1 = dice1 + dice11
  35. if score1 % 2 == 0:
  36. finalscoreone = finalscoreone + 10
  37. else:
  38. finalscoreone = finalscoreone - 5
  39. if finalscoreone < 0:
  40. finalscoreone = 0
  41. finalscoreone = finalscoreone + score1
  42. print("You rolled",dice1,dice11,"// PLAYER 1 SCORE:", finalscoreone)
  43. if dice1 == dice11:
  44. dicedoubleplayer11 = random.randint(1,6)
  45. dicedoubleplayer1 = random.randint(1,6)
  46. askdouble = input(" ### You rolled a double would you like to roll again? ### (roll) ")
  47. if askdouble == "roll":
  48. score1 = score1 + dicedoubleplayer1 + dicedoubleplayer11
  49. finalscoreone = finalscoreone + score1
  50. print("You rolled ",dicedoubleplayer1,dicedoubleplayer11,"// PLAYER 1 SCORE:", finalscoreone)
  51. game1 = game1 + 1
  52.  
  53. game2 = 0
  54. finalscoretwo = 0
  55. while game2 < 5:
  56. ask2 = input("Would you like to roll the dice player 2 (y) ")
  57. if ask2 == "y":
  58. dice2 = random.randint(1,6)
  59. dice22 = random.randint(1,6)
  60. score2 = dice2 + dice22
  61. if score2 % 2 == 0:
  62. finalscoretwo = finalscoretwo + 10
  63. else:
  64. finalscoretwo = finalscoretwo - 5
  65. finalscoretwo = finalscoretwo + score2
  66. if finalscoretwo < 0:
  67. finalscoretwo = 0
  68. print("You rolled ",dice1,dice22,"// PLAYER 2 SCORE:", finalscoretwo)
  69. if dice2 == dice22:
  70. dicedouble = random.randint(1,6)
  71. dicedouble2 = random.randint(1,6)
  72. askdouble = input("### You rolled a double would you like to roll again? ### (roll) ")
  73. if askdouble == "roll":
  74. score2 = score2 + dicedouble + dicedouble2
  75. finalscoretwo = finalscoretwo + score2
  76. print("You rolled ",dicedouble,dicedouble2,"// PLAYER 2 SCORE:", finalscoretwo)
  77. game2 = game2 + 1
  78.  
  79. if finalscoretwo == finalscoreone:
  80. asksd = input("Both players have the same score. The game will now go into sudden death, player 1 would you like to roll? (y)")
  81. if asksd == "y":
  82. dice2 = random.randint(1,6)
  83. dice22 = random.randint(1,6)
  84. score2 = dice2 + dice22
  85. if score2 % 2 == 0:
  86. finalscoretwo = finalscoretwo + 8
  87. else:
  88. finalscoretwo = finalscoretwo - 8
  89. finalscoretwo = finalscoretwo + score2
  90. if finalscoretwo < 0:
  91. finalscoretwo = 0
  92. print("You rolled ",dice1,dice22,"// PLAYER 2 SCORE:", finalscoretwo)
  93. if dice2 == dice22:
  94. dicedouble = random.randint(1,6)
  95. dicedouble2 = random.randint(1,6)
  96. askdouble = input("### You rolled a double would you like to roll again? ### (roll) ")
  97. if askdouble == "roll":
  98. score2 = score2 + dicedouble + dicedouble2
  99. finalscoretwo = finalscoretwo + score2
  100. print("You rolled ",dicedouble,dicedouble2,"// PLAYER 2 SCORE:", finalscoretwo)
  101.  
  102.  
  103. if finalscoretwo > finalscoreone:
  104. print("player 2 wins with a score of ", finalscoretwo)
  105.  
  106. if finalscoreone > finalscoretwo:
  107. print("player 1 wins with a score of ", finalscoreone)
  108.  
  109.  
  110.  
  111. def login():
  112. while True:
  113. username = input("Please enter your username player 1: ")
  114. password = input("Please enter your password player 1: ")
  115. with sqlite3.connect("dice.db") as db:
  116. cursor = db.cursor()
  117. find_user = ("SELECT * FROM user WHERE username = ? AND password = ?")
  118. cursor.execute(find_user,[(username),(password)])
  119. results = cursor.fetchall()
  120.  
  121. if results:
  122. for i in results:
  123. print("Welcome "+i[2])
  124. player2()
  125. #return("exit")
  126. break
  127.  
  128. else:
  129. print("Username and password not recognised")
  130. again = input("Would you like to try again?(y/n): ")
  131. if again.lower == "n":
  132. print("Goodbye")
  133. time.sleep(1)
  134. #return("exit")
  135. break
  136.  
  137.  
  138.  
  139. def login2():
  140. while True:
  141. username = input("Please enter your username player 2: ")
  142. password = input("Please enter your password player 2: ")
  143. with sqlite3.connect("dice.db") as db:
  144. cursor = db.cursor()
  145. find_user = ("SELECT * FROM user WHERE username = ? AND password = ?")
  146. cursor.execute(find_user,[(username),(password)])
  147. results = cursor.fetchall()
  148.  
  149. if results:
  150. for i in results:
  151. print("Welcome "+i[2])
  152. gameactual()
  153. #return("exit")
  154. break
  155.  
  156. else:
  157. print("Username and password not recognised")
  158. again = input("Would you like to try again?(y/n): ")
  159. if again.lower == "n":
  160. print("Goodbye")
  161. time.sleep(1)
  162. #return("exit")
  163. break
  164. def player2():
  165. player2login = input("Would you like to create an account player 2 (y/n) ")
  166. if player2login == "y":
  167. newUser2()
  168. else:
  169. player2login = input("Would you like to create an account player 2 (y/n) ")
  170.  
  171.  
  172. if player2login == "n":
  173. login2()
  174. else:
  175. player2login = input("Would you like to create an account player 2 (y/n) ")
  176. def newUser2():
  177. found = 0
  178. while found ==0:
  179. username = input("Please enter a username: ")
  180. with sqlite3.connect("dice.db") as db:
  181. cursor = db.cursor()
  182. findUser = ("SELECT * FROM user WHERE username = ?")
  183. cursor.execute(findUser,[(username)])
  184.  
  185. if cursor.fetchall():
  186. print("Username taken please try again")
  187. else:
  188. found = 1
  189.  
  190. firstName = input("Enter your desired first name: ")
  191. surname = input("Enter your desired surname: ")
  192. password = input("Please enter your password: ")
  193. password1 = input("please enter your password again: ")
  194. while password !=password1:
  195. print("Your passwords did not match")
  196. password = input("Please enter your password: ")
  197. password1 = input("pleae enter your password again: ")
  198. insertData = '''INSERT INTO user(username,firstname,surname,password)
  199. VALUES(?,?,?,?)'''
  200. cursor.execute(insertData,[(username),(firstName),(surname),(password)])
  201. db.commit()
  202. gameactual()
  203.  
  204. def newUser():
  205. found = 0
  206. while found ==0:
  207. username = input("Please enter a username: ")
  208. with sqlite3.connect("dice.db") as db:
  209. cursor = db.cursor()
  210. findUser = ("SELECT * FROM user WHERE username = ?")
  211. cursor.execute(findUser,[(username)])
  212.  
  213. if cursor.fetchall():
  214. print("Username taken please try again")
  215. else:
  216. found = 1
  217.  
  218. firstName = input("Enter your desired first name: ")
  219. surname = input("Enter your desired surname: ")
  220. password = input("Please enter your password: ")
  221. password1 = input("please enter your password again: ")
  222. while password !=password1:
  223. print("Your passwords did not match")
  224. password = input("Please enter your password: ")
  225. password1 = input("pleae enter your password again: ")
  226. insertData = '''INSERT INTO user(username,firstname,surname,password)
  227. VALUES(?,?,?,?)'''
  228. cursor.execute(insertData,[(username),(firstName),(surname),(password)])
  229. db.commit()
  230. player2()
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. def testlog():
  240. test1 = input("Would you like to create an account player 1? (y/n) ")
  241. if test1 == "y":
  242. newUser()
  243. else:
  244. test1 = input("Would you like to create an account player 1? (y/n) ")
  245.  
  246. if test1 == "n":
  247. login()
  248. else:
  249. test1 = input("Would you like to create an account player 1? (y/n) ")
  250. testlog()
  251.  
  252.  
  253.  
  254.  
  255. testlog()
  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.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289. #8=============================================================D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement