Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- game = 0
- import random
- import sqlite3
- with sqlite3.connect("login.db") as db:
- cursor = db.cursor()
- cursor.execute('''
- create table if not exists user(
- userID integer primary key,
- username varchar(20) not null,
- firstname varchar(20) not null,
- surname varchar(20) not null,
- password varchar(20) not null);
- ''')
- def login():
- while True:
- username = input("(+) Enter your username:")
- password = input("(+) Enter your password:")
- with sqlite3.connect("login.db") as db:
- cursor = db.cursor()
- find_user = ('SELECT * FROM user WHERE username = ? AND password = ?')
- cursor.execute(find_user, [(username),(password)]) #[] replaces the values of the ?
- results = cursor.fetchall()
- if results:
- for i in results:
- print("Welcome "+i[2])
- a = 1
- return (i[2])
- else:
- print("(-) Username and password not recognised")
- again = input("Do you want to retry? (Y/N)")
- if again.lower() == "n":
- print ("Goodbye")
- exit()
- return ("Exit")
- def newUser():
- print("(+) Add a new user")
- #check username is taken
- found = 0
- while found == 0:
- username = input("(+) Enter a username: ")
- with sqlite3.connect("login.db") as db:
- cursor = db.cursor()
- find_user = ('SELECT * FROM user WHERE username = ?')#?stops SQL injection
- cursor.execute(find_user,[(username)])#[] replaces the values of the ?
- if cursor.fetchall():
- print("(-) Usename taken")
- else:
- found = 1
- firstname = input("(+) Please enter your first name: ")
- surname = input ("(+) Please enter your last name: ")
- password = input ("(+) Please enter a password: ")
- password1 = input ("(+) Please re-enter your password: ")
- while password != password1:
- print("(-) Passwords do not match")
- password = input("(+) Please enter a password: ")
- password1 = input ("(+) Please re-enter a password: ")
- insertData = '''INSERT INTO user(username,firstname,surname,password)
- VALUES(?,?,?,?)'''
- cursor.execute(insertData,[(username),(firstname),(surname),(password)])
- db.commit()#saves the results to the database
- def userMenu(game):
- score = 0
- score2 = 0
- dice1 = random.randint(1,6)
- dice2 = random.randint(1,6)
- dice3 = random.randint(1,6)
- dice4 = random.randint(1,6)
- RolledDice = dice1 + dice2
- RolledDice2 = dice3 + dice4
- print(" ")
- print("(-- Player 1 Press Enter to roll a dice --)")
- input()
- print("Player 1 has rolled a dice and got",dice1, "(press enter to roll your second dice!)")
- input()
- print("Player1 has rolled another dice and got",dice2,"(press enter to see your total score!)")
- input()
- print("Player1's total score is now",RolledDice)
- score = dice1 + dice2
- if RolledDice % 2 == 0: #used from comments of https://stackoverflow.com/questions/21837208/check-if-a-number-is-odd-or-even-in-python
- print(" ")
- print("your rolled dice added to make an even number so 10 points have been added to your score!")
- score = score + 10
- print(" ")
- print("your score is now",score)
- else:
- print(" ")
- print("your rolled dice added to make an odd number so 5 points will be taken from your score!")
- score = score - 5
- print(" ")
- print("Player1 your score is now",score,"(press enter)")
- input()
- print(" ")
- print("(-- Player 2 Press Enter to roll a dice --)")
- print(" ")
- print("Player 2 has rolled a dice and got",dice3, "(press enter to roll your second dice!)")
- input()
- print("Player2 has rolled another dice and got",dice4,"(press enter to see your total score!)")
- input()
- print("Player2's total score is now",RolledDice2)
- score2 = dice3 + dice4
- if RolledDice2 % 2 == 0:
- print(" ")
- print("your rolled dice added to make an even number so 10 points have been added to your score!")
- score2 = score2 + 10
- print(" ")
- print("your score is now",score2)
- else:
- print(" ")
- print("your rolled dice added to make an odd number so 5 points will be taken from your score!")
- score2 = score2 - 5
- print(" ")
- print("Player2 your score is now",score2,"(press enter)")
- input()
- game = game + 1
- return game
- def rules():
- print("------------------------------------")
- print(" Rules of the game")
- print("------------------------------------")
- print('''
- • The points rolled on each player's dice are added to their score.
- • If the total is an even number, an additional 10 points are added to your score.
- • If the total is an odd number, 5 points are subtracted from your score.
- • If you roll a double, you get to roll one extra die and get the number of points rolled added to your score.
- • The score of a player cannot go below 0 at any point.
- • The person with the highest score at the end of 5 rounds wins.
- • 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 ''')
- print("------------------------------------")
- print(" Press Enter to start the game")
- print("------------------------------------")
- while True:
- print("------------------------------------")
- print(" Welcome to the system ")
- print("------------------------------------")
- print(" ")
- print(" ")
- print("------------------------------------")
- print(" User options")
- print("------------------------------------")
- menu =('''
- 1 - Create New User
- 2 - Login
- 3 - Exit \n ''')
- userChoice = input(menu)
- game = 0
- if userChoice == "1":
- newUser()
- elif userChoice == "2":
- enter = login()
- if enter == "exit":
- break
- else:
- print("Game Starting!")
- askrules = input("Do you want the rules of the game? If you do type *yes* otherwise, press enter")
- if askrules == "yes".lower():
- rules()
- while game < 5:
- game = userMenu(game)
- elif userChoice == "3":
- print("Goodbye")
- exit()
- else:
- print("(-) Input not recognised, please try")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement