In: Computer Science
complete the code to determine the highest score value in python
import random
#variables and constants
MAX_ROLLS = 5
MAX_DICE_VAL = 6
#declare a list of roll types
ROLL_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "4 of a kind" ]
pScore = 0
cScore = 0
num_Score = int( input ("Enter a number of round: ") )
print ("\n")
count = 0
while count < num_Score:
#set this to the value MAX_ROLLS
pdice = [0,0,0,0,0]
cdice = [0,0,0,0,0]
#set this to the value MAX_DICE_VAL
pdice = [0,0,0,0,0,0]
cdice = [0,0,0,0,0,0]
#INPUT - get the dice rolls
i = 0
while i < MAX_ROLLS:
pdice[i] = random.randint(1, MAX_DICE_VAL)
cdice[i] = random.randint(1, MAX_DICE_VAL)
i += 1
#end while
#print the player's and computer dice rolls
i = 0
print ( "Player rolled: ", end=" " )
while i < MAX_ROLLS:
print( pdice[i], end = " " )
i += 1
#end while
print ("\n")
i = 0
print ("Computer rolled: ", end=" " )
while i < MAX_ROLLS:
print( cdice[i], end = " " )
i += 1
#end while
#load the tally list so we can determine pair, 3 of a kind,
etc
i = 0
ptally = [0,0,0,0,0,0]
ctally = [0,0,0,0,0,0]
while i < MAX_ROLLS:
ptally[ pdice[i] - 1 ] += 1
ctally[ cdice[i] - 1 ] += 1
i += 1
#end while
# find out pair, 3 of kind, etc
pmax = ptally[0] # init to first element in tally array
cmax = ctally[0]
i = 1
while i < MAX_DICE_VAL:
if pmax < ptally[i]:
pmax = ptally[i]
#end if
if cmax < ctally[i]:
cmax = ctally[i]
#end if
i += 1
#end while
#output - display what was rolled and who won
print("\n")
print(" player rolled: " + ROLL_TYPES[ pmax - 1], end="\n")
print(" computer rolled: " + ROLL_TYPES[ cmax - 1] )
# determine the winner
if pmax > cmax:
print(" player wins!", end="\n" )
print("\n")
pScore += 1
elif cmax > pmax:
print(" computer wins!", end="\n" )
print("\n")
cScore += 1
else:
print(" Tie!", end="\n")
print("\n")
count += 1
#end while
In a program when player wins the game then player score is incremented by 1, if computer wins the game then computer score is incremented by 1.
So here player score and computer score is used in order to determine the highest score value.
so for example if there are 5 rounds and player wins the game 2 times and computer wins the game 1 time and 2 times tie. so here player has score 2 and computer has score 1.
So player has the highest score than the computer so print highest score value is 2.
The program is given below:
import random
MAX_ROLLS = 5
MAX_DICE_VAL = 6
#declare a list of roll types
ROLL_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "4 of a kind" ]
pScore = 0
cScore = 0
num_Score = int( input ("Enter a number of round: ") )
print ("\n")
count = 0
while(count<num_Score):
#set this to the value MAX_ROLLS
pdice = [0,0,0,0,0]
cdice = [0,0,0,0,0]
#set this to the value MAX_DICE_VAL
pdice = [0,0,0,0,0,0]
cdice = [0,0,0,0,0,0]
#INPUT - get the dice rolls
i = 0
while i < MAX_ROLLS:
pdice[i] = random.randint(1, MAX_DICE_VAL)
cdice[i] = random.randint(1, MAX_DICE_VAL)
i += 1
#end while
#print the player's and computer dice rolls
i = 0
print ( "Player rolled: ", end=" " )
while i < MAX_ROLLS:
print( pdice[i], end = " " )
i += 1
#end while
print ("\n")
i = 0
print ("Computer rolled: ", end=" " )
while i < MAX_ROLLS:
print( cdice[i], end = " " )
i += 1
#end while
#load the tally list so we can determine pair, 3 of a kind,
etc
i = 0
ptally = [0,0,0,0,0,0]
ctally = [0,0,0,0,0,0]
while i < MAX_ROLLS:
ptally[ pdice[i] - 1 ] += 1
ctally[ cdice[i] - 1 ] += 1
i += 1
#end while
# find out pair, 3 of kind, etc
pmax = ptally[0] # init to first element in tally array
cmax = ctally[0]
i = 1
while i < MAX_DICE_VAL:
if pmax < ptally[i]:
pmax = ptally[i]
#end if
if cmax < ctally[i]:
cmax = ctally[i]
#end if
i += 1
#end while
#output - display what was rolled and who won
print("\n")
print(" player rolled: " + ROLL_TYPES[ pmax - 1], end="\n")
print(" computer rolled: " + ROLL_TYPES[ cmax - 1] )
# determine the winner
if pmax > cmax:
print(" player wins!", end="\n" )
print("\n")
#if player wins the gamethen increment pScore by 1
pScore += 1
elif cmax > pmax:
print(" computer wins!", end="\n" )
print("\n")
#if computer wins the game then increment cScore by 1
cScore += 1
else:
print(" Tie!", end="\n")
print("\n")
count += 1
#end while
#print Player score
print("Player Score: ",pScore)
#print Computer score
print("Computer Score: ",cScore)
#if player has higher score than computer
if(pScore>cScore):
#print pScore
print("highest score value: ",pScore)
#if player and computer has same score
elif (pScore==cScore):
#print score either using pScore or cScore
print("highest score value: ",pScore)
#else
else:
#print cScore
print("highest score value: ",cScore)
The screenshot of code is gven below:
Output Example 1:
Output Example 2: