Skip to main content
Question Protected by Peilonrayz
Tweeted twitter.com/StackCodeReview/status/1050898996586270720
Move intro before the code; improve spelling and grammar
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

2 Player Dice-player dice game

Took me 13 hours to codeThis is a game for two users who roll 2 dice 5 times. If the total of dice is even, im so bad xDthe player gains 10 points; if it is odd, they lose 5.

If there is a tie after the five rounds, then the users have to roll one die to determine the winner.

This code is a game which involves two users who roll 2 sets of dice 5 times. If the dice is even, then they gain 10 points. If it is odd, they lose 5. If there is a tie, then the users have to roll one die to determine the winner. This was for my NEA task in computer science, which I have now finished; if anyone has any suggestions on how iI could have made it better they will be greatly appreciated.So So, feel free toplease suggest how I can improve it and such :D!

2 Player Dice game

Took me 13 hours to code, im so bad xD

This code is a game which involves two users who roll 2 sets of dice 5 times. If the dice is even, then they gain 10 points. If it is odd, they lose 5. If there is a tie, then the users have to roll one die to determine the winner. This was for my NEA task in computer science, which I have now finished; if anyone has any suggestions on how i could have made it better they will be greatly appreciated.So, feel free to suggest how I can improve it and such :D

2-player dice game

This is a game for two users who roll 2 dice 5 times. If the total of dice is even, the player gains 10 points; if it is odd, they lose 5.

If there is a tie after the five rounds, then the users have to roll one die to determine the winner.

This was for my NEA task in computer science, which I have now finished; if anyone has any suggestions on how I could have made it better they will be greatly appreciated. So, please suggest how I can improve it!

edited tags
Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158
Source Link
Panda32
  • 51
  • 1
  • 1
  • 5

2 Player Dice game

Took me 13 hours to code, im so bad xD

###### IMPORTING RANDOM AND TIME & DEFINING VARIABLES ######

import random
import time

i = 0
Player1Points = 0
Player2Points = 0
Player1Tiebreaker = 0
Player2Tiebreaker = 0
Winner_Points = 0

###### LOGIN CODE ######

### This Sets logged in to false, and then makes sure the username and password is correct before allowing them to continue ###


logged_in1 = False
logged_in2 = False
while logged_in1 == False:
    username = input('What is your username? ')
    password = input('What is your password? ')
    if username == 'User1' or username == 'User2' or username == 'User3' or username == 'User4' or username == 'User5':
        if password == 'password':
            print('Welcome, ',username,' you have been successfully logged in.')
            logged_in1 = True
            user1 = username
        else:
            print('Incorrect password, try again')  
    else:
        print('Incorrect username, try again')

while logged_in2 == False:
    username = input('What is your username? ')
    password = input('What is your password? ')
    if username == 'User1' or username == 'User2' or username == 'User3' or username == 'User4' or username == 'User5':
        if password == 'password':
            print('Welcome, ',username,' you have been successfully logged in.')
            logged_in2 = True
            user2 = username
        else:
            print('Incorrect password, try again')  
    else:
        print('Incorrect username, try again')


###### DEFINING ROLL ######

### Makes the dice roll for the player and works out the total for that roll ###

 
def roll():

    points = 0
    
    die1 = random.randint(1,6)

    die2 = random.randint(1,6)
    
    dietotal = die1 + die2

    points = points + dietotal

    if dietotal % 2 == 0:
        points = points + 10
    
    else:
        points = points - 5
    
    if die1 == die2:
        die3 = random.randint(1,6)
        points = points +die3

    return(points)

###### DICE ROLL ######

### This rolls the dice 5 times for the players, and then adds up the total. If the scores are equal, it starts a tie breaker and determines the winner off that ###

for i in range(1,5):
    Player1Points += roll()
    print('After this round ',user1, 'you now have: ',Player1Points,' Points')
    time.sleep(1)
    Player2Points += roll()
    print('After this round ',user2, 'you now have: ',Player2Points,' Points')
    time.sleep(1)

if Player1Points == Player2Points:
    while Player1Tiebreaker == Player2Tiebreaker:
        
        
        Player1Tiebreaker = random.randint(1,6)
        Player2Tiebreaker = random.randint(1,6)

    if Player1Tiebreaker > Player2Tiebreaker:
        Player2Points = 0
    elif Player2Tiebreaker > Player1Tiebreaker:
        Player1Points = 0

###### WORKING OUT THE WINNER ######

### This checks which score is bigger, then creates a tuple for my leaderboard code ( Gotton of stack overflow ) ###

if Player1Points>Player2Points:
    Winner_Points = Player1Points
    winner_User = user1
    winner = (Winner_Points, user1)
elif Player2Points>Player1Points:
    Winner_Points = Player2Points
    winner = (Winner_Points, user2)
    winner_User = user2

print('Well done, ', winner_User,' you won with ',Winner_Points,' Points')

###### CODE TO UPLOAD ALL SCORES TO A FILE ######

### This will store the winners username and score in a text file ###

winner = (Winner_Points,',',winner_User)
f = open('Winner.txt', 'a')
f.write(''.join(winner))
f.write('\n')
f.close()


###### CODE TO LOAD, UPDATE AND SORT LEADERBOARD ######

### This loads the leaderboard into an array, then compares the scores just gotton and replaces it ###

f = open('Leaderboard.txt', 'r')
leaderboard = [line.replace('\n','') for line in f.readlines()]
f.close()


for idx, item in enumerate(leaderboard):
    if item.split(', ')[1] == winner[1] and int(item.split(', ')[0]) < int(winner[0]):
            leaderboard[idx] = '{}, {}'.format(winner[0], winner[1])
    else:
        pass 

### This sorts the leaderboard in reverse, and then rewrites it ###

leaderboard.sort(reverse=True)

with open('Leaderboard.txt', 'w') as f:
    for item in leaderboard:
        f.write("%s\n" % item)

This code is a game which involves two users who roll 2 sets of dice 5 times. If the dice is even, then they gain 10 points. If it is odd, they lose 5. If there is a tie, then the users have to roll one die to determine the winner. This was for my NEA task in computer science, which I have now finished; if anyone has any suggestions on how i could have made it better they will be greatly appreciated.So, feel free to suggest how I can improve it and such :D