In: Computer Science
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions)
1) all the players has to play until he/she reaches to at least 500 points in first round.
When user reach 500 points for the first time, user may choose to immediately end his turn to prevent losing the points.
If you user did choose to save his turn and if he get farkle the score will be 0. (This is just for the first round.)
2) fix my scoring according to point table.
point table for game:
5 = 50 points
1 = 100 points
Triplet of 1 = 300 points "Example(1,1,1)"
Triplet of 2 = 200 points
Triplet of 3 = 300 points
Triplet of 4 = 400 points
Triplet of 5 = 500 points
Triplet of 6 = 600 points
Four of a Kind [Example(4,4,4,4)] = 1,000 points
Five of a Kind[Example(4,4,4,4,4)] = 2,000 points
Six of a Kind [Example(4,4,4,4,4,4)] = 3,000 points
(1,2,3,4,5,6 ) = 1,500 points
Three Pairs [Example(4,4,2,2,1,1)]= 1,500 points
Four of a Kind + a Pair [Example(4,4,4,4,1,1)]= 1,500 points
Two sets of Three of a Kind [Example(4,4,4,6,6,6)] = 2,500 points
NOTE(single 2,3,4,6 does not have any point.)
3) there are six die in game and I have already implement it but it not working as rules.
computer have to Take out any dice worth points after each roll.
user has to Roll the remaining dice,
removing any dice worth points and adding them to your running total.
If user are ever able to set aside all six dice, user can roll again all of his dice and keep building his running total.
If ever user are unable to set aside any dice ( dice contain 0 points), user have Farkled. user lose his running point total and his turn is over.
4) Winning (I have also did little bit but i want to make it simple so can you see it and fix)
If any player score 10,000 points in game
The end game sequence will start
Each other player has one turn to try to beat score of player who scored 10,000 points.
After all remaining players have had their turn, the player with the highest score wins.
you should enter the number of players as a parameter ( 2 to 6 players) .
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import random
import math
class Dice:
def __init__(self, sides=6):
self.sides = sides
def roll(self, n=1):
return [int(math.floor(random.random() * 6) + 1) for _ in range(n)]
class Farkle:
def __init__(self):
print ("Lets play Farkle")
self.gamers = [] #INIT
self.num_gamers = int(input("No. of gamers? ")) #INIT
if(self.num_gamers < 2 or self.num_gamers > 6):
print("No of gamers need to be more than 2 and less than 6")
else:
for i in range(self.num_gamers):
#get name of gamer
gamer = input("Name of gamer {0}: ".format(i+1))
self.gamers.append(gamer) #list of gamers
#INIT
self.scores = {'_highest':[0, '']}
for gamer in self.gamers:
self.scores[gamer] = 0
self.Counter_round = 0 #INIT
self.dice = Dice()
def Begin(self):
if(self.num_gamers >= 2 and self.num_gamers <= 6):
print ("The game has %s gamers. Their names are:" % self.num_gamers)
self.gamer_scores()
input("Enter if you are ready to begin!")
while self.scores['_highest'][0] < 10000:
self.Round()
self.End()
def End(self):
if(self.num_gamers >= 2 and self.num_gamers <= 6):
print ("Hey Congratulations, {0}! You are the winner with a score of {1}.".format(self.scores['_highest'][1], self.scores['_highest'][0]))
print ("The total scores were:")
self.gamer_scores()
def Round(self):
self.Counter_round += 1
print ("******************")
print (" Round {0}".format(self.Counter_round))
print ("******************")
if self.Counter_round > 1:
print ("{0} is currently is leading with {1} points.".format(self.scores['_highest'][1], self.scores['_highest'][0]));
print ("The total scores are:")
self.gamer_scores();
for gamer in self.gamers:
self.AnotherRound(gamer)
print(input("ENTER when you are ready to proceed to the next round"))
return
def AnotherRound(self, gamer):
input("{0}, it's your AnotherRound. Enter when you are ready to roll.".format(gamer))
#create empty dictionery for count every number
s_num = {}
roll = self.dice.roll(6)
#single 5's has 50 points
z_s = 0
#count any kind of number frequency and stored in dictionary
for i in roll:
s_num[i] = roll.count(i)
#condition for three pairs or 1 pair and 4 any kind of
flag_pairs = True
l = 0
k = 0
m = 0
for key,value in s_num.items():
if(key in [1,2,3,4,5] and value == 2):
l+=1
#4 any kind of
elif(key in [1,2,3,4,5] and value == 4):
k +=1
elif(key in [1,2,3,4,5] and value == 3):
m +=1
#for three pairs
if(l==3):
flag_pairs = False
z_s += 1500
#for 1 pair and 4 any kind of
elif(l==1 and k==1):
flag_pairs = False
z_s += 1500
#two sets any three kind of
elif(m==2):
flag_pairs = False
z_s += 2500
#simple for [1,2,3,4,5,6]
elif(roll == [1,2,3,4,5,6]):
flag_pairs = False
z_s += 1500
if(flag_pairs):
for key,value in s_num.items():
#5's
if(key == 5 and value in [1,2]):
z_s += 50*value
#1's
elif(key == 1 and value in [1,2]):
z_s += 300*value
#three 2's
elif(key == 2 and value == 3):
z_s += 200
#three 3's
elif(key == 3 and value == 3):
z_s += 300
#three 4's
elif(key == 4 and value == 3):
z_s += 400
#three 5's
elif(key == 5 and value == 3):
z_s += 500
#three 6's
elif(key == 6 and value == 3):
z_s += 600
# 4 any kind of
elif(key in [1,2,3,4,5,6] and value == 4):
z_s += 1000
# 5 any kind of
elif(key in [1,2,3,4,5,6] and value == 5):
z_s += 1500
# 6 any kind of
elif(key in [1,2,3,4,5,6] and value == 6):
z_s += 3000
else:
z_s += key*value
score = z_s
print (" You scored {0}".format(roll))
self.scores[gamer] += score
print (" Your score is now: {0}".format(self.scores[gamer]));
if self.scores[gamer] > self.scores['_highest'][0]:
self.scores['_highest'][0] = self.scores[gamer]
self.scores['_highest'][1] = gamer
return
def gamer_scores(self):
for gamer in self.gamers:
print (gamer + "; Score: " + str(self.scores[gamer]))
if __name__ == '__main__':
play = True
while play:
print(
'''Farkle is a dice game that is multiplayer (2 to 6 gamers)!
Whoever reaches 10000 points first wins
Rules:
5s : 50 points
1s : 100 points
2,2,2 : 200 points
3,3,3 : 300 points
4,4,4 : 400 points
5,5,5 : 500 points
6,6,6 : 600 points
Four of a kind : 1000 points
Five of a kind : 2000 points
Six of a kind : 3000 points
A straight 1 to 6 : 1500 points
Four of a kind + Pair: 1500 points
Two sets of Three of a Kind = 2500''')
f = Farkle()
f.Begin()
replay = input("Would you like to play again? ")
while True:
if replay.upper() in ['Y', 'YE', 'YES']:
break
elif replay.upper() in ['N', 'NO']:
play = False
break
else:
print("Please enter yes or no")
break
Question: To correct the given python code of game Farkle
Solution:
Code Images:
Output
Python Code:
# Importing Lib
import random
import math
# Declaring class Dice
class Dice:
# constructor for class Dice
def __init__(self, sides=6):
self.sides = sides
# Roll function
def roll(self, n=1):
return [int(math.floor(random.random() * 6) + 1) for _ in
range(n)]
# Declaring class Frakle
class Farkle:
# Constructor For Frakle
def __init__(self):
print ("Lets play Farkle")
self.gamers = [] #INIT
self.num_gamers = int(input("No. of gamers? ")) #INIT
if(self.num_gamers < 2 or self.num_gamers > 6):
print("No of gamers need to be more than 2 and less than 6")
else:
for i in range(self.num_gamers):
#get name of gamer
gamer = input("Name of gamer {0}: ".format(i+1))
self.gamers.append(gamer) #list of gamers
#INIT
self.scores = {'_highest':[0, '']}
for gamer in self.gamers:
self.scores[gamer] = 0
self.Counter_round = 0 #INIT
self.dice = Dice()
# Begin Function to start game
def Begin(self):
if(self.num_gamers >= 2 and self.num_gamers <= 6):
print ("The game has %s gamers. Their names are:" %
self.num_gamers)
self.gamer_scores()
# Input anyting to start game
input("Enter if you are ready to begin!")
while self.scores['_highest'][0] < 10000:
self.Round()
## else end the game
self.End()
# End function to end the game
def End(self):
if(self.num_gamers >= 2 and self.num_gamers <= 6):
hig_gamer=self.scores['_highest'][1]
hig_score=self.scores['_highest'][0]
print ("Hey Congratulations, {0}! You reached the highest limit
with a score of {1}.".format(hig_gamer,
hig_score))
# lets give other players to reach the goal
print("lets give other players one chance to reach the goal")
for gamer in self.gamers:
if gamer!=hig_gamer:
self.AnotherRound(gamer)
# congratulating the player with high score
print ("Hey Congratulations, {0}! You are the winner with a score
of {1}.".format(self.scores['_highest'][1],
self.scores['_highest'][0]))
print ("The total scores were:")
self.gamer_scores()
# Round function to start a round
def Round(self):
# Declaring Counter to count # of rounds
self.Counter_round += 1
# Starting the round
print ("******************")
print (" Round {0}".format(self.Counter_round))
print ("******************")
if self.Counter_round ==1: # if counter ==1
for gamer in self.gamers:
self.Round_1(gamer)
if self.Counter_round > 1:
print ("{0} is currently is leading with {1}
points.".format(self.scores['_highest'][1],
self.scores['_highest'][0]))
##### Here You have typed a semicolon after end of python
statement
print ("The total scores are:")
self.gamer_scores()
##### Here You have typed a semicolon after end of python
statement
for gamer in self.gamers:
self.AnotherRound(gamer)
print(input("ENTER when you are ready to proceed to the next
round"))
return
# Function to evaluate score for every roll
def evaluate_score(self,roll):
#create empty dictionery for count every number
s_num = {}
#single 5's has 50 points
z_s = 0
#count any kind of number frequency and stored in dictionary
for i in roll:
s_num[i] = roll.count(i)
#condition for three pairs or 1 pair and 4 any kind of
flag_pairs = True
l = 0
k = 0
m = 0
for key,value in s_num.items():
if(key in [1,2,3,4,5] and value == 2):
l+=1
#4 any kind of
elif(key in [1,2,3,4,5] and value == 4):
k +=1
elif(key in [1,2,3,4,5] and value == 3):
m +=1
#for three pairs
if(l==3):
flag_pairs = False
z_s += 1500
#for 1 pair and 4 any kind of
elif(l==1 and k==1):
flag_pairs = False
z_s += 1500
#two sets any three kind of
elif(m==2):
flag_pairs = False
z_s += 2500
#simple for [1,2,3,4,5,6]
elif(roll == [1,2,3,4,5,6]):
flag_pairs = False
z_s += 1500
if(flag_pairs):
for key,value in s_num.items():
#5's
if(key == 5 and value in [1,2]):
z_s += 50*value
#1's
elif(key == 1 and value in [1,2]):
z_s += 100*value
##### The value should be multiplied with 100
#three 2's
elif(key == 2 and value == 3):
z_s += 200
#three 3's
elif(key == 3 and value == 3):
z_s += 300
#three 4's
elif(key == 4 and value == 3):
z_s += 400
#three 5's
elif(key == 5 and value == 3):
z_s += 500
#three 6's
elif(key == 6 and value == 3):
z_s += 600
# 4 any kind of
elif(key in [1,2,3,4,5,6] and value == 4):
z_s += 1000
# 5 any kind of
elif(key in [1,2,3,4,5,6] and value == 5):
z_s += 1500
# 6 any kind of
elif(key in [1,2,3,4,5,6] and value == 6):
z_s += 3000
else:
z_s += key*value
return z_s
# Function Round_1 for round 1
def Round_1(self,gamer):
print("{0},Its your round 1".format(gamer))
score=0
# roll till you get score above 5
while score<500:
input("{0}, your score is below 500. Enter when you are ready to
roll.".format(gamer))
roll = self.dice.roll(6)
print (" You scored {0}".format(roll))
score=self.evaluate_score(roll)
print (" Your score is {0}".format(score))
self.scores[gamer] += score
# After getting score > 500 save chance or not
while True:
replay = input("{0}, your score is above 500. Would you like to
save chance?".format(gamer))
if replay.upper() in ['Y', 'YE', 'YES']: # if typed yes
input("{0}, it's your AnotherRound. Enter when you are ready to
roll.".format(gamer))
roll = self.dice.roll(6)
score=self.evaluate_score(roll)
print (" Your score is {0}".format(score))
if score>500: # if again got score above 500
self.scores[gamer] += score
else: # else points reset to 0 and chance gone
print("{0}, you get farkle your score reset to
0".format(gamer))
self.scores[gamer]=0
break
elif replay.upper() in ['N', 'NO']:
break
else: # if input is wrong
print("Please enter yes or no")
# printing final score after Round 1
print (" Your score is now:
{0}\n\n".format(self.scores[gamer]))
if self.scores[gamer] > self.scores['_highest'][0]:
self.scores['_highest'][0] = self.scores[gamer]
self.scores['_highest'][1] = gamer
# Function AnotherRound
def AnotherRound(self, gamer):
input("{0}, it's your AnotherRound. Enter when you are ready to
roll.".format(gamer))
roll = self.dice.roll(6)
score=self.evaluate_score(roll)
##### Intendation of below print statement is wrong
print (" You scored {0}".format(roll))
self.scores[gamer] += score
print (" Your score is now: {0}".format(self.scores[gamer]))
##### Here You have typed a semicolon after end of python
statement
if self.scores[gamer] > self.scores['_highest'][0]:
self.scores['_highest'][0] = self.scores[gamer]
self.scores['_highest'][1] = gamer
return
### Function to print game score
def gamer_scores(self):
for gamer in self.gamers:
print (gamer + "; Score: " + str(self.scores[gamer]))
if __name__ == '__main__':
play = True
while play:
# Printing instructions of Frakle game
print('''Farkle is a dice game that is multiplayer (2 to 6
gamers)!
Whoever reaches 10000 points first wins
Rules:
5s : 50 points
1s : 100 points
2,2,2 : 200 points
3,3,3 : 300 points
4,4,4 : 400 points
5,5,5 : 500 points
6,6,6 : 600 points
Four of a kind : 1000 points
Five of a kind : 2000 points
Six of a kind : 3000 points
A straight 1 to 6 : 1500 points
Four of a kind + Pair: 1500 points
Two sets of Three of a Kind = 2500''')
# Creating Frakle game object and calling Begin() function to start
game
f = Farkle()
f.Begin()
# After end of game asking user to play again or not
replay = input("Would you like to play again? ")
while True:
if replay.upper() in ['Y', 'YE', 'YES']:
break
elif replay.upper() in ['N', 'NO']:
play = False
break
else:
print("Please enter yes or no")
replay = input("Would you like to play again? ")
##### Here break statement will not come as it will break loop even
when user doesn't responded correctly
##### Rather ask them to input again by using input command
##### Added a exit statement
print("had fun with you. Visit again!!")
# Importing Lib import random import math # Declaring class Dice class Dice: # constructor for class Dice def init (self, sides-6): self.sides sides # Roll function def roll(self, n-1) return [int (math.floor (random.random( 6) 1) for in range(n)]
#Declaring class Frakle class Farkle: # Constructor For Frak le def init_(self): print ("Lets play Farkle") self.gamers self.num gamers #INIT int (input ("No. of gamers? ")) # INIT if(self.num_gamers < 2 or self.num gamers 6) print ("No of gamers need to be more than 2 and less than 6") else: for i in range (self.num_gamers): #get name of gamer input ("Name of gamer te: ". format (i+1)) gamer self.gamers.append (gamer) #List of gamers #INIT highest[e, 1 self.scores for gamer in self.gamers: self.scores[gamer] self.Counter_round = 0 #INIT self.dice Dice # Begin Function to start game def Begin (self) if (self.num gamers 2 and self.num gamers <= 6)
# Begin Function to start game def Begin(self) if (self.num_gamers > 2 and self.num_gamers <6) print ("The game has %s gamers. Their names are:" % self.num gamers) self.gamer_scores () # Input anyting to start game input("Enter if you are ready to begin!") while self.scores['_highest' ][e] self. Round () ## else end the game self. End () 10000: # End function to end the game def End(self) if (self.num_gamers > 2 and self.num_gamers = 6) #Congratulating players print ("Hey Congratulations, fe! You are the winner with a score of (1}.".format (self.scores[ highest'][1], self.scores[' highest '] [0] )) print ("The total scores were:") self.gamer_scores () # Round function to start a round def Round (self): # Declaring Counter to count of rounds self.Counter_round +1 Activate Wind Go to Settings to a
# Round function to start a round def Round(self): # Declaring Counter to count # of rounds self.Counter_round +1 # Starting the round print ("** print (" Round e".format(self.Counter_round)) print ("* ****) ") if self.Counter_round> 1: print ("fe is currently is leading with (1 points.".format (self . scores [' _highest'][1], self.scores [' _highest'][e] )) ####Here You have typed a semicolon after end of python statement print ("The total scores are:") self.gamer_scores () ##### Here You have typed a semicolon after end of python statement for gamer in self. gamers: self.AnotherRound (gamer) print (input"ENTER when you are ready to proceed to the next round")) return # Function AnotherRound def AnotherRound ( self, gamer): input("(e, it's your AnotherRound. Enter when you are ready to roll.".format(gamer)) Activate Window Go to Settings to activa
# Function AnotherRound def AnotherRound (self, gamer): input ("(0, it's your AnotherRound. Enter when you are ready to rol1.".format (gamer)) #create empty dictionery for count every number s_num self.dice.roll(6) roll #single 5's has 50 points 2 s e #count any kind of number frequency and stored in dictionary for i in roll s_num[i] roll.count (i) #condition for three pairs or 1 pair and 4 any kind of flag_pairs 1= e True k m e for key, value in s_num.items if (key in [1,2,3,4,5] and value 2): 1+-1 # 4 any kind of elif (key in [1,2,3,4,5] and value 4) == k 1 elif (key in [1,2,3,4,5] and value 3): == m 1
#for three pairs if (1-3) flag_pairs False Z_S + 1500 #for 1 pair and 4 any kind of elif (1-1 and k--1) flag_pairs False = Z S +15e0 #two sets any three kind of elif (m-2) flag_pairs False = Z S +2500 #simple for [1,2,3,4,5,6] elif (roll flag_pairs [1,2,3,4,5,6]): False == = Z S 1500 if (flag_pairs): for key, value in s_num.items () #5 's =5 and value in [1,21 ): if (key Z_s +50*value # 1 's elif (key z_s 100*value #####The value should be multiplied with 100 =1 and value in [1,21): #three 2's
Z S 1500 #6 any kind of elif (key in [1,2,3,4,5,6] and value 6): Z S +3000 else: z s key*value Score Z S #### Intendation of below print statement is wrong print (" You scored e".format (roll)) self.scores [gamer] score print (" Your score is now: 0".format(self.scores [gamer])) ###Here You have typed a semicolon after end of python statement if self.scores[gamer] self.scores[_highest' ] [0]: self.scores[' highest ' ] [0] self.scores['_highest' ] [1] gamer self.scores[gamer] return ###Function to print game score def gamer_scores (self): for gamer in self.gamers: print (gamer + "; Score: str(self.scores [gamer]))
main if name play while play: Printing instructions of Frak Le game print(Farkle is a dice game that is multiplayer (2 to 6 gamers)! Whoever reaches 10000 points first wins Rules: True = 5s 50 points 1s 100 points 2,2,2 200 points 3,3,3 300 points 4,4,4 400 points 5,5,5 500 points 6,6,6 600 points Four of a kind 1000 points Five of a kind 2000 points Six of a kind 3000 points A straight 1 to 6 1500 points Four of a kindPair: 150e points Two sets of Three of a Kind 2500 ) # Creating Frakle game object and calling Begin() function to start game Farkle) f.Begin) # After end of game asking user to play again or not replay while True: input("Would you like to play again? ") if replay.upper in ['Y, YE, 'YES 1 break
# Creating Frakle game object and calling Begin () function to start game Farkle) f.Begin) f # After end of game asking user to play again or not replay input ("Would you like to play again? ") while True: if replay.upper in 'Y', YE, 'YES ] break elif replay.upper in ['N', NO'] play False break else: print ("Please enter yes or no") replay input ("Would you like to play again? ") # Here break statement will not come as it will break Loop even when user doesn't responded correctly #####Rather ask them to input again by using input command Activate Go to Setti ##### Added a exit statement print ("had fun with you. Visit again!!")
Farkle is a dice game that is multiplayer (2 to 6 gamers)! Whoever reaches 1000e points first wins Rules: 5s 50 points 1s 100 points 2,2,2 200 points 3,3,3 300 points 4,4,4 400 points 5,5,5 500 points 6,6,6 600 points Four of a kind 1000 points Five of a kind 2000 points Six of a kind 3000 points A straight 1 to 6 150e points Four of a kind Pair 1500 points Two sets of Three of a Kind = 2500 Lets play Farkle No. of gamers? 2 Name of gamer 1: 1 Name of gamer 2: m The game has 2 gamers. Their names are: 1; Score: e m; Score: 0 Enter if you are ready to begin!
m; Score: Enter if you are ready to begin! Round 1 1, it's your AnotherRound. Enter when you are ready to roll. You scored [2, 6, 1, 2, 5, 61 Your score is now: 166 m, it's your AnotherRound. Enter when you are ready to roll. You scored [1, 6, 1, 5, 5, 2] Your score is now: 308 ENTER when you are ready to proceed to the next round Round 2 m is currently is leading with 308 points The total scores are: 1; Score: 166 m; Score: 308 1, it's your AnotherRound. Enter when you are ready to roll. You scored [4, 6, 4, 5, 5, 5 Your score is now: 680 m, it's your AnotherRound. Enter when you are ready to roll. You scored [1, 5, 2, 4, 6, 2] Your score is now: 472 ENTER when you are ready to proceed to the next round Round 3
Round 37 1 is currently is leading with 9730 points The total scores are 1; Score 9730 m; Score: 9477 1, it's your AnotherRound. Enter when you are ready to roll. You scored [4, 2, 3, 5, 3, 3] Your score is now: 10086 m, it's your AnotherRound. Enter when you are ready to roll You scored [1, 2, 6, 5, 1, 1] Your score is now: 9538 ENTER when you are ready to proceed to the next round Hey Congratulations, 1! You are the winner with a score of 10086 The total scores were: 1; Score: 10086 m; Score: 9538 Would you like to play again? Please enter yes or no Would you like to play again? Please enter yes or no Would you like to play again? no had fun with you. Visit again!!
Farkle is a dice game that is multiplayer (2 to 6 gamers)! Whoever reaches 10e00 points first wins Rules: 5s 50 points 1s 100 points 2,2,2 200 points 3,3,3 300 points 4,4,4 400 points 5,5,5 500 points 6,6,6 600 points Four of a kind 1000 points Five of a kind 2000 points Six of a kind 3000 points A straight 1 to 6 150e points Four of a kind Pair: 1500 points Two sets of Three of a Kind= 2500 Lets play Farkle No. of gamers? 1 No of gamers need to be more than 2 and less than 6 Would you like to play again? y Farkle is a dice game that is multiplayer (2 to 6 gamers)! Whoever reaches 1000e points first wins Rules 5s 50 points 1s 100 points