You are on page 1of 7

Project

(A)

import random
players = []
plcount = 0
gcount = 1
ru=0
def addPlayer():
temp=input("Enter player's name and <Enter> to stop: ")
if(len(temp)==0):
return 0
for i in range(0,len(players)):
if(players[i][0]==temp):
print(temp+" is already recorded. Please enter a different name.")
return 2

players.append([temp])
return 1
def run(p):

if(ru==0):
players[p].append([random.randrange(1, 7)])
players[p].append([random.randrange(1, 7)])
for i in range(0,3):
players[p][1].append(random.randrange(1, 7))
players[p][2].append(random.randrange(1,7))
else:
if (len(players[p][1]) >= 5):
players[p][1].pop()
players[p][2].pop()
for i in range (0,4):
players[p][1][i]=(random.randrange(1, 7))
players[p][2][i]=(random.randrange(1, 7))
if(players[p][1][3]==players[p][2][3]):
players[p][1].append(random.randrange(1, 7))
players[p][2].append(random.randrange(1, 7))
print("Round R1 R2 R3 R4 R5")
print("Throw 1 ", end=' ')
for i in range(0, 5):
print(str(players[p][1][i])+" ", end=' ')
print("\nThrow 2 ", end=' ')
for i in range(0, 5):
print(str(players[p][2][i]) + " ", end=' ')
else:
print("Round R1 R2 R3 R4")
print("Throw 1 ", end=' ')
for i in range(0, 4):
print(str(players[p][1][i]) + " ", end=' ')
print("\nThrow 2 ", end=' ')
for i in range(0, 4):
print(str(players[p][2][i]) + " ", end=' ')

loop=1
temp=0
ctotal = []
ptotal=[]
while(1):
while(loop==1):
temp=addPlayer()
if(temp==0):
if(plcount<2):
print("There must be at least two players ")
else:
loop=0
elif(temp!=2):
plcount=plcount+1
print("Game summary for Game "+ str(gcount))

for i in range(0,plcount):
ctotal.append(0)
print("\nPlayer "+ players[i][0])
run(i)

print("\nScore ",end=' ')


for j in range(0,4):
if(players[i][1][j]==players[i][2][j]):
print(str(players[i][1][j] + players[i][2][j]+players[i][1][j+1] + players[i][2][j+1]) + " ", end=' ')
else:
print(str(players[i][1][j]+players[i][2][j]) + " ", end=' ')
print("\nAcc. Sc ",end=' ')
for j in range(0,4):
if (players[i][1][j] == players[i][2][j]):
ctotal[i] = ctotal[i] + players[i][1][j] + players[i][2][j]+players[i][1][j+1] + players[i][2][j+1]
else:
ctotal[i] = ctotal[i] + players[i][1][j] + players[i][2][j]
print(str(ctotal[i]) + " ", end=' ')
if(len(ptotal)==0):
print("\nPrevious total: 0 Current Total: "+str(ctotal[i]))
else:
ctotal[i]=ctotal[i]+ptotal[i]
print("\nprevious total: "+ str(ptotal[i])+ ' Current total: '+str(ctotal[i]))

ru = 1
print("\nOverall winner: "+players[ctotal.index(max(ctotal))][0] +" with total score "+str(max(ctotal)))
if(len(input("Do you want to play again? <ENTER> for no: "))==0):
print("\nOverall winner: " + players[ctotal.index(max(ctotal))][0] + " with total score " +
str(max(ctotal)))
break
ptotal=ctotal.copy()
ctotal.clear()
Output:
(B)

I have used List for the single collection for the following reasons:
1. List is dynamic so I can append as many players to the game I do not have to worry about the
maximum number of players.
2. List is easier to iterate than other data structures such as linked list which I cannot access
through index

(C)

The following reasons enhance the maintainability of my program

1. I have used 3-D list which makes it easier to maintain the data as it is placed in one list and can
be easily changed
2. The list helps provide dynamic size, so it becomes very easy to add features like increasing
number of dice, turns etc.

(D)

The code is for copying current total score to the previous total score in this ctotal is list for current total
score and ptotal is for previous total score. The code with list comprehension is concise and is easy to
maintain as it takes less space

Without list comprehension:

for i in range(0,len(ctotal)):
ptotal.append(ctotal[i])

With list comprehension:

ptotal=[i for i in ctotal]

You might also like