Reputation: 1
I am making a two-person dice game where there are five rounds, but I want to keep the scores the same from the previous rounds, how do I do that?
The rules of the game are as follows:
• 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 their score. • If the total is an odd number, 5 points are subtracted from their score. • If they roll a double, they get to roll one extra die and get the number of points rolled added to their score. • The score of a player cannot go below 0 at any point. • The person with the highest score at the end of the 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).
import random
import time
total_player2score = 0
total_player1score = 0
rounds = 0
player_1 = 0
player_2 = 0
while rounds != 5:
total_player2score = total_player2score + player_2
total_player1score = total_player1score + player_1
rounds = rounds + 1
number = random.randint(1, 6)
number2 = random.randint(1, 6)
total_player1score = number + number2
print("*"*50)
print("Round {}!".format(rounds))
print("*"*50)
print("Player 1's turn. Type 'roll' to roll the dice.")
player1_input = input(">>> ")
if player1_input == "roll":
time.sleep(0.5)
print("Player 1's first roll is", number)
print("Player 1's second roll.Type 'roll' to roll the dice")
player1_input = input(">>> ")
if player1_input == "roll":
time.sleep(0.5)
print("player 1's second roll is", number2)
if total_player1score % 2 == 0:
total_player1score = total_player1score + 10
print("Player 1's total is even so 10 points will be added")
print("*"*50)
print("Player 1 has", total_player1score, "points")
else:
total_player1score = total_player1score - 5
total_player1score = max(0, total_player2score)
print("player 1's total is odd so 5 points will be deducted")
print("*"*50)
print("Player 1 has", total_player1score, "points")
number = random.randint(1, 6)
number2 = random.randint(1, 6)
total_player2score = number + number2
print("*"*50)
print("Player 2's turn. Type 'roll' to roll the dice")
player2_input = input(">>> ")
if player2_input == "roll":
time.sleep(0.5)
print("Player 2's first roll is", number)
print("Player 2's second roll.Type 'roll' to roll the dice")
player2_input = input(">>> ")
if player2_input == "roll":
time.sleep(0.5)
print("player 2's second roll is", number2)
if total_player2score % 2 == 0:
total_player2score = total_player2score + 10
print("Player 2's total is even so 10 points will be added")
print("*"*50)
print("Player 2 has", total_player2score, "points")
else:
total_player2score = total_player2score - 5
total_player2score = max(0, total_player2score)
print("player 2's total is odd so 5 points will be deducted")
print("*"*50)
print("Player 2 has", total_player2score, "points")
Upvotes: 0
Views: 72
Reputation: 759
def writeUp(score_1,score_2,nameoffile):
with open(f"{nameoffile}.txt","a") as logz:
print(f"score1 {score_1}\nscore2 {score_2}",file=logz)
def readUp(nameoffile):
with open(f"{nameoffile}.txt","r") as data:
lines = data.readlines()
last_2 = lines[:2]
score_1,score_2 = None,None
for element in last_2:
splitt = element.split(' ')
if element.find('score1') != -1:
score_1 = int(splitt[1])
elif element.find('score2') != -1:
score_2 = int(splitt[1])
return score_1,score_2
sci1,sci2 = 50,50
writeUp(sci1,sci2,'mysaves')
sc1,sc2 = readUp('mysaves')
print(sc1,sc2)
#this should help, the function given creates a txt file and store scores into it
Upvotes: 1