Reputation: 167
This is my code from the beginning to the top, I'd like someone to tell me the next steps I need to take to create a file that can store the highest scores from the game. Basically I need the highest score to be taken and ordered into the leaderboard file, this means I need scores added, removed and sorted into the file. The two files I have can be seen in the image below -
``` #This allows for my program to have pre-defined functions.
import time
import random
i = 0
Player1Points = 0
Player2Points = 0
Player1Tiebreaker = 0
Player2Tiebreaker = 0
WinnersPoints = 0
#Allows the user to enter their profile details.
x = 1
#Creating the loop until the details are entered correctly.
while x == 1:
Username1 = str(input("\nPlease enter your Username for player 1: "))
Password1 = str(input("\nPlease enter your Password for player 1: "))
Username2 = str(input("\nPlease enter your Username for player 2: "))
Password2 = str(input("\nPlease enter your Password for player 2: "))
#Validates the given infomation
if Username1 == ("User1"):
if Password1 == ("Password1"):
print("\nWelcome User1, you are verified and may begin!")
if Username2 == ("User2"):
if Password2 == ("Password2"):
print("\nWelcome User2, you are verified and may begin!")
#Updates the loop to stop, becuase the correct information was entered.
x += 1
#If the details are incorrect, the user must retry.
else:
print(
"\nSorry, but you are not verified to use this program. Please try again!"
)
#The program's loop will continue until details are entered correctly.
x = 1
#Preperation for the dice game.
print("\nThe game shall begin in 3 seconds!")
time.sleep(1)
print("\n3")
time.sleep(1)
print("\n2")
time.sleep(1)
print("\n1")
time.sleep(1)
print("\nThe game has begun, User1 rolls first.")
#Dice rolling definition code.
def roll():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
change = 10 if (die1 + die2) % 2 == 0 else -5
points = die1 + die2 + change
if die1 == die2:
points += random.randint(1, 6)
return points
for i in range(0,5):
Player1Points += roll()
time.sleep(1.5)
print("\nThe total score for ",Username1, "is:",Player1Points," Points")
print("\nRolling...")
Player2Points += roll()
time.sleep(1.5)
print("\nThe total score for ",Username2, "is:",Player2Points," Points")
print("\nRolling...")
#Tiebreaker code is from Stack Overflow from a user named "Amir A. Shabani".
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
#This code declares the winner.
if Player1Points > Player2Points:
WinnersPoints = Player1Points
winner_User = Username1
winner = (WinnersPoints, Username1)
elif Player2Points > Player1Points:
WinnersPoints = Player2Points
winner = (WinnersPoints, Username2)
winner_User = Username2
#This is where the code stops from "Amir A. Shabani".
print('\nCongrats,', winner_User,'you won with',WinnersPoints,'Points')
#Uploads data to a file.
if Player1Points < Player2Points:
with open('scores.txt', 'a') as f:
f.write("Total Score For Player 2 is:" + str(Player2Points)+ ("\n"))
f.close
else: Player1Points > Player2Points
with open('scores.txt', 'a') as f:
f.write("Total Score For Player 1 is:" + str(Player1Points)+ ("\n"))
f.close
#Displays the leaderboard file data.
f = open('leaderboard.txt', 'r')
file_contents = f.read()
print (file_contents)
f.close()```
Upvotes: 2
Views: 425
Reputation: 62
You can maybe save a text file like that:
"leaderboard = [['username1', 1996, 12.8], ['username2', 3245, 13.9], ['username3', 1228, 11.9]]
and use command:
exec(open('path/to/this/file.txt').read()) will make a variable of leaderboard.
to save the changed version of leaderboard, use:
file = open('path/to/your/file.txt', 'w') file.write(str(leaderboard.append([player.name, player.score, player.time]))
That will work.
Upvotes: 1