Computer Science Dice Game
✅ Paper Type: Free Essay | ✅ Subject: Computer Science |
✅ Wordcount: 6260 words | ✅ Published: 24th Sep 2021 |
Analysis
Try and create 3 or more key success criteria for your program.
Success Criteria:
- Replay Ability (Previous scores, reset game)
- User-friendly (Clear instructions, functional time pauses)
- Authentication (Are they an authorised player?)
- Resolving problems (instances of a draw, similar top scores)
- Visual aid (Showing player name, score, rolls, score changes)
- Limited functions (Only deduct or add points once, don’t let the score go past 0)
- Advanced functions (files, randomisation)
Decomposition
Design
You may like to create a flow chart which will show broadly how your program will work. If so include your flow chart in this section.
You must create pseudocode for a part of your program (minimum of 15 lines)
Pseudocode – Main Body of the game (Without SaveScores function)
START
AuthenList = [testplayer1;password1, testplayer2;password2, testplayer3;password3]
INPUT Player1Username
INPUT Player1Password
IF Player1Username AND Player1Password IN Authenlist:
OUTPUT ‘Authenticated Player’
INPUT Player2Username
INPUT Player2Password
IF Player2Username AND Player1Password IN Authenlist:
IF Player2Username NOT Player1Username:
IF Player2Password NOT Player1Password:
OUTPUT ‘Authenticated Player’
INPUT Commence BY player1
IF Commence = ‘START’:
Player1Score = 0
Player2Score = 0
P1GO()
Roll1 = CHOOSE RANDOM 1-6
Roll2 = CHOOSE RANDOM 1-6
OUTPUT Roll1, Roll2
Score = Roll1 + Roll2
IF Score IS EVEN:
Score = Score + 10
ADD TO Player1Score
IF Score IS ODD:
Score = Score – 5
UNLESS Score = 3
THEN Score = 0
P2GO()
Roll1 = CHOOSE RANDOM 1-6
Roll2 = CHOOSE RANDOM 1-6
OUTPUT Roll1, Roll2
Score = Roll1 + Roll2
IF Score IS EVEN:
Score = Score + 10
IF Score IS ODD:
Score = Score – 5
UNLESS Score = 3
THEN Score = 0
LOOP P1GO AND P2GO (4)
IF Player1Score > Player2Score
OUTPUT Player1 ‘IS THE WINNER
IF Player2Score > Player1Score
OUTPUT Player2 ‘IS THE WINNER’
IF Player1Score = Player2Score:
P1 = CHOOSE RANDOM 1-6
P2 = CHOOSE RANDOM 1-6
IF P1 > P2:
OUTPUT Player1 ‘IS THE WINNER’
IF P2 > P1:
OUTPUT Player2 ‘IS THE WINNER
IF P1 = P2:
RETURN TO Line{IF Player1Score = Player2Score:}
Flowchart – SaveScores
SaveScores Function
Test design
- Think of tests that you can carry out to see if your system works
- Remember to try and use normal, boundary and erroneous tests.
- If you wish to, you may add more tests to the table.
My tests:
Test |
What am I testing? |
What data will I use? |
Normal/Boundary/Erroneous? |
Expected Result |
1 |
Element In a list |
String |
Normal |
Authenticates the player |
2 |
“” |
“” |
Boundary |
Doesn’t allow entry as the same person |
3 |
“” |
Any |
Erroneous |
Not allowed entry since it isn’t defined |
4 |
Given input |
Integer -> String |
Normal |
Performs option |
5 |
“” |
Out of range integer |
Boundary |
Terminate with error message |
6 |
“” |
String |
Erroneous |
Terminate with error message |
My test screenshots:
Development
- Copy and paste your code into this section
- Remember to try and add comments to your code to make it more readable!
My program code:
Function – SaveScores
def SaveScores(playername, playerscore):
with open(‘playerscores.txt’,’r’) as value:
lines = value.readlines()
a1 = (lines[0])
a2 = (lines[1])
b1 = (lines[3])
b2 = (lines[4])
c1 = (lines[6])
c2 = (lines[7])
d1 = (lines[9])
d2 = (lines[10])
e1 = (lines[12])
e2 = (lines[13]) #Obtain values from text file
a1 = int(a1) #for ordering in the set list
b1 = int(b1)
c1 = int(c1)
d1 = int(d1)
e1 = int(e1)
removescore = ”
removename = ”
appendscore = playerscore #values given at top of function
appendname = playername
scorelist = [a1, b1, c1, d1, e1] #correct order
scorelist.append(appendscore) #new value is added
scorelist.sort() #in ascending order
scorelist.reverse() #so it is from high -> low
scorelist.pop() #takes off smallest value
isScore = scorelist.index(appendscore) #where is it
if isScore == 0: #first in list
removescore = e1
removename = e2 #replaces each value with previous
e1 = d1
e2 = d2
d1 = c1
d2 = c2
c1 = b1
c2 = b2
b1 = a1
b2 = a2
a1 = appendscore
a2 = appendname
print(a1)
print(a2)
print(b1)
print(b2)
print(c1)
print(c2)
print(d1)
print(d2)
print(e1)
print(e2)
f = open(‘playerscores.txt’, ‘w+’)
f.truncate(0) #clears list
f.write(str(a1) + ‘ ’ + a2 + ‘ ’)
f.write(str(b1) + ‘ ’ + b2 + ‘ ’)
f.write(str(c1) + ‘ ’ + c2 + ‘ ’)
f.write(str(d1) + ‘ ’ + d2 + ‘ ’)
f.write(str(e1) + ‘ ’ + e2)
if isScore == 1: #2nd in list
removescore = e1
removename = e2
e1 = d1
e2 = d2
d1 = c1
d2 = c2
c1 = b1
c2 = b2
b1 = appendscore
b2 = appendname
print(a1)
print(a2)
print(b1)
print(b2)
print(c1)
print(c2)
print(d1)
print(d2)
print(e1)
print(e2)
f = open(‘playerscores.txt’, ‘w+’)
f.truncate(0)
f.write(str(a1) + ‘ ’ + a2 + ‘ ’)
f.write(str(b1) + ‘ ’ + b2 + ‘ ’)
f.write(str(c1) + ‘ ’ + c2 + ‘ ’)
f.write(str(d1) + ‘ ’ + d2 + ‘ ’)
f.write(str(e1) + ‘ ’ + e2 + ‘ ’)
if isScore == 2: #3rd in list
removescore = e1
removename = e2
e1 = d1
e2 = d2
d1 = c1
d2 = c2
c1 = appendscore
c2 = appendname
print(a1)
print(a2)
print(b1)
print(b2)
print(c1)
print(c2)
print(d1)
print(d2)
print(e1)
print(e2)
f = open(‘playerscores.txt’, ‘w+’)
f.truncate(0)
f.write(str(a1) + ‘ ’ + a2 + ‘ ’)
f.write(str(b1) + ‘ ’ + b2 + ‘ ’)
f.write(str(c1) + ‘ ’ + c2 + ‘ ’)
f.write(str(d1) + ‘ ’ + d2 + ‘ ’)
f.write(str(e1) + ‘ ’ + e2 + ‘ ’)
if isScore == 3: #4th in list
removescore = e1
removename = e2
e1 = d1
e2 = d2
d1 = appendscore
d2 = appendname
print(a1)
print(a2)
print(b1)
print(b2)
print(c1)
print(c2)
print(d1)
print(d2)
print(e1)
print(e2)
f = open(‘playerscores.txt’, ‘w+’)
f.truncate(0)
f.write(str(a1) + ‘ ’ + a2 + ‘ ’)
f.write(str(b1) + ‘ ’ + b2 + ‘ ’)
f.write(str(c1) + ‘ ’ + c2 + ‘ ’)
f.write(str(d1) + ‘ ’ + d2 + ‘ ’)
f.write(str(e1) + ‘ ’ + e2 + ‘ ’)
if isScore == 4: #last value to be written in list
removescore = e1
removename = e2
e1 = appendscore
e2 = appendname
print(a1)
print(a2)
print(b1)
print(b2)
print(c1)
print(c2)
print(d1)
print(d2)
print(e1)
print(e2)
f = open(‘playerscores.txt’, ‘w+’)
f.truncate(0)
f.write(str(a1) + ‘ ’ + a2 + ‘ ’)
f.write(str(b1) + ‘ ’ + b2 + ‘ ’)
f.write(str(c1) + ‘ ’ + c2 + ‘ ’)
f.write(str(d1) + ‘ ’ + d2 + ‘ ’)
f.write(str(e1) + ‘ ’ + e2 + ‘ ’)
if isScore == 5: #not in top 5
removescore = appendscore
removename = appendscore
print(a1)
print(a2)
print(b1)
print(b2)
print(c1)
print(c2)
print(d1)
print(d2)
print(e1)
print(e2)
f = open(‘playerscores.txt’, ‘w+’)
f.truncate(0)
f.write(str(a1) + ‘ ’ + a2 + ‘ ’)
f.write(str(b1) + ‘ ’ + b2 + ‘ ’)
f.write(str(c1) + ‘ ’ + c2 + ‘ ’)
f.write(str(d1) + ‘ ’ + d2 + ‘ ’)
f.write(str(e1) + ‘ ’ + e2 + ‘ ’)
Actual Coding (without the function, but referred to)
import os
import random
import time
import sys #all the imported functions needed to make it user-friendly, or function properly
players = 0 #for while loop
Player1 = (”)
Player2 = (”)
os.system(‘clear’) #removes clutter
process = input(‘What do you want to do? 1} Play the dice game or 2} View the scores of a certain player (Type 1 or 2) ’) #different functionalities
if process == ‘1’:
while True: #ability to replay it at the end
os.system(‘clear’) #clear any clutter (used quite often)
while players == 0: #only occurs when no players are present
AuthenPlayers = [‘testplayer1’, ‘testplayer2’, ‘testplayer3’, ‘testplayer4’] #placeholders used in place of actual in testing phase
AuthenPassword = [‘password1’, ‘password2’, ‘password3’, ‘password4’]
print(‘33[1;31;40m’) #RED TEXT
username1 = input(‘Username : ‘) #visual prompting
password1 = input(‘Password : ‘)
if username1 in AuthenPlayers:
if password1 in AuthenPassword:
userid1 = AuthenPlayers.index(username1)
passid1 = AuthenPassword.index(password1)
if userid1 == passid1:
print (‘Authenticated Player’)
Player1 = username1
players = 1
time.sleep(3)
os.system(‘clear’)
break
while players == 1: #only occurs when one player is present
AuthenPlayers = [‘testplayer1’, ‘testplayer2’, ‘testplayer3’, ‘testplayer4’]
AuthenPassword = [‘password1’, ‘password2’, ‘password3’, ‘password4’]
print(‘33[1;34;40m’) #BLUE TEXT
username2 = input(‘Username : ‘)
password2 = input(‘Password : ‘)
if username2 in AuthenPlayers:
if password2 in AuthenPassword:
userid2 = AuthenPlayers.index(username2)
passid2 = AuthenPassword.index(password2)
if userid2 == passid2:
if userid1 != userid2: #not the same as the original
if passid1 != passid2:
print (‘Authenticated Player’)
Player2 = username2
players = 2
time.sleep(3)
os.system(‘clear’)
break
if players == 2: #only occurs when two players are present
print (‘Here are the rules of the game’)
time.sleep(3)
os.system(‘clear’)
print (‘1) There will be 5 rounds 2) You will roll 2 dice 3) If the accumulative score is even, gain 10 points and the amount on the dice 4) If the accumulative score is odd, gain the total on the dice, minus 5 Highest score wins’) #so the player understands
time.sleep(3)
commence = input(‘When you’re ready, type START ’)
if commence == ‘START’: #to start the game
os.system(‘clear’)
diceval = [1, 2, 3, 4, 5, 6] #give viable values
Round = 1
player1score = 0
player2score = 0
while Round <= 5:
print(‘33[1;37;40m’)
print(‘Round ‘ + str(Round)) #useful information for the players
print(‘33[1;31;40m’)
print(username1 + ‘ : ‘ + str(player1score))
print(‘33[1;34;40m’)
print(username2 + ‘ : ‘ + str(player2score))
print(‘33[1;31;40m’)
p1go = input(‘ Player 1: Type ROLL to roll ’) #how they roll the dice
if p1go == ‘ROLL’: #separate p1 and p2
p1roll1 = random.choice(diceval)
p1roll2 = random.choice(diceval) #simulates the diceroll
p1score = p1roll1 + p1roll2 #accumulative score
print(str(p1roll1) + ‘, ‘ + str(p1roll2))
time.sleep(3)
print(p1score)
if p1score == 2 or p1score == 4 or p1score == 6 or p1score == 8 or p1score == 10 or p1score == 12: #Even Values
p1add = p1score
p1add = p1add + 10 #Rewarded for even
player1score = player1score + p1add
print(‘Since your number was even, you gain an extra 10 points. Your new score is: ‘ + str(player1score))
time.sleep(3)
if p1score == 3 or p1score == 5 or p1score == 7 or p1score == 9 or p1score == 11: #Odd Values
if p1score == 3:
p1sub = p1score
p1sub = 0
player1score = player1score + p1sub #To prevent a negative value
print(‘Since your number was odd, you lose 5 points. But since you got 3, your new score is: ‘ + str(player1score))
time.sleep(3)
if p1score == 5 or p1score == 7 or p1score == 9 or p1score == 11:
p1sub = p1score
p1sub = p1sub – 5 #Loss for an odd number
player1score = player1score + p1sub
print(‘Since your number was odd, you lose 5 points. Your new score is: ‘ + str(player1score))
time.sleep(3)
print(‘33[1;34;40m’)
p2go = input(‘ Player 2: Type ROLL to roll ’)
if p2go == ‘ROLL’:
p2roll1 = random.choice(diceval)
p2roll2 = random.choice(diceval)
p2score = p2roll1 + p2roll2
print(str(p2roll1) + ‘, ‘ + str(p2roll2))
time.sleep(3)
print(p2score)
if p2score == 2 or p2score == 4 or p2score == 6 or p2score == 8 or p2score == 10 or p2score == 12:
p2add = p2score
p2add = p2add + 10
player2score = player2score + p2add
print(‘Since your number was even, you gain an extra 10 points. Your new score is: ‘ + str(player2score))
time.sleep(3)
Round = Round + 1
os.system(‘clear’)
if p2score == 3 or p2score == 5 or p2score == 7 or p2score == 9 or p2score == 11:
if p2score == 3:
p2sub = p2score
p2sub = 0
player2score = player2score + p2sub
print(‘Since your number was odd, you lose 5 points. But since you got 3, your new score is: ‘ + str(player2score))
time.sleep(3)
Round = Round + 1
os.system(‘clear’)
if p2score == 5 or p2score == 7 or p2score == 9 or p2score == 11:
p2sub = p2score
p2sub = p2sub – 5
player2score = player2score + p2sub
print(‘Since your number was odd, you lose 5 points. Your new score is: ‘ + str(player2score))
time.sleep(3)
Round = Round + 1 #to increase the round counter to initiate a later function (5 round maximum)
os.system(‘clear’)
if Round > 5: #To terminate the game after 5 rounds
if player1score > player2score: #The first player’s score is greater than the second
SaveScores(username1, player1score) #the function I created
time.sleep(3)
os.system(‘clear’)
print(‘33[1;31;40m’)
print (username1 + ‘ IS THE WINNER WITH ‘ + str(player1score) + ‘ POINTS ‘) #visual congratulations
time.sleep(2)
replay = input(‘ What do you want to do? 1] Replay game 2] Exit (Type either 1, 2)’) #Replayability
if replay == ‘1’: #defined as values
break
if replay == ‘2’:
sys.exit(‘Thank You For Playing’)
if player1score < player2score: #The second player’s score is greater than the first
SaveScores(username2, player2score)
time.sleep(3)
os.system(‘clear’)
print(‘33[1;34;40m’)
print (username2 + ‘IS THE WINNER WITH’ + str(player2score) + ‘POINTS’)
time.sleep(2)
replay = input(‘ What do you want to do? 1] Replay game 2] Exit (Type either 1, 2)’)
if replay == ‘1’:
break
if replay == ‘2’:
sys.exit(‘Thank You For Playing’)
if player1score == player2score: #since the game cannot end in a tie
time.sleep(3)
os.system(‘clear’)
print (‘There is a tie We shall roll 1 more die and whoever gets higher wins’) #tiebreak game
tiebreaker = input(‘Type ROLL to roll both Player 1’s and Player 2’s dice’)
while tiebreaker == ‘ROLL’:
p1tieroll = random.choice(diceval)
p2tieroll = random.choice(diceval)
time.sleep(3)
os.system(‘clear’)
print(‘33[1;31;40m’)
print (‘Player 1 has rolled:…’)
time.sleep(2)
print (str(p1tieroll))
time.sleep(2)
print(‘33[1;34;40m’)
print(‘And Player 2 has rolled:…’)
time.sleep(2)
print (str(p2tieroll))
time.sleep(2)
if p1tieroll > p2tieroll:
time.sleep(1)
print(‘33[1;31;40m’)
print (username1.upper() + ‘IS THE WINNER WITH ON THE TIEBREAKER’)
time.sleep(3)
os.system(‘clear’)
replay = input(‘ What do you want to do? 1] Replay game 2] Exit (Type either 1, 2)’) #replayability
if replay == ‘1’:
break
if replay == ‘2’:
sys.exit(‘Thank You For Playing’)
if p1tieroll < p2tieroll:
time.sleep(1)
print(‘33[1;34;40m’)
print (username2.upper() + ‘IS THE WINNER WITH ON THE TIEBREAKER’)
time.sleep(3)
os.system(‘clear’)
replay = input(‘ What do you want to do? 1] Replay game 2] Exit (Type either 1, 2)’)
if replay == ‘1’:
break
if replay == ‘2’:
sys.exit(‘Thank You For Playing’)
if p1tieroll == p2tieroll:
time.sleep(1)
print(‘33[1;37;40m’)
print (‘You have rolled the same. You’ll have to roll again’)
continue
if process == ‘2’: #show scores
f = open(‘playerscores.txt’,’r+’)
g = f.read()
print(g) #prints whole text file
Testing
- Show you have completed the tests you thought of
- Identify if you needed to make changes to your program
- Include the screenshots of the tests
My tests:
Test |
What am I testing? |
Expected result |
Pass/Fail |
Do I need to change my program? |
1 |
Whether or not the SaveScores Function truly write each value properly |
Leaves a singular line after each one, minus the last one. |
Only just a fail, it just adds an extra unneeded empty line at the bottom of code. Also, position 4 didn’t seem to work |
There isn’t really anything I can do, since there is no function the does the opposite to (moving a line down) |
2 |
The occurrence of a tie |
Go to tiebreaker, roll 2 separate numbers, then announce winner or reset the program. |
Pass, but the player and ‘IS’ is joined together, but this is an easy fix |
I need to recheck my string concatenation and make sure there are gaps when needed |
3 |
If my program prints the top 5 |
Prints the top 5, then returns to selection |
Partial fail, it does print out the code |
I could use a function, a while or for loop, or, though documented as risky or prone to breaking, execfile() |
4 |
If they give a value option of something that isn’t 1-2 |
Prints an error message to tell the user to stay within parameters |
Pass, and it is useful here since it is only at the start of the coding. |
I don’t really need to change this since this is the main starting point, and it functions in a way that works, but may not be the most efficient. |
5 |
If all the colour shortcuts work |
Changing the colour of the text |
Generally a pass, but the background plays up on the first one |
I don’t need to change anything, but I may debug it a little to try to get the background to work. |
My test screenshots:
Row 5
Row 1
Row 3
Row 2
Row 4
Evaluation
- Evaluate how successful your program was. You may find it useful to refer back to your tests
- You should reflect on any new skills you have developed
How successful was my program?
My program was relatively successful, however it did have a few flaws, some I could fix, some that I couldn’t work out how to fix. The gameplay is quite user-friendly with visual aids and simplistic controls, and resolves any ties or mistakes, either in a good or a bad way, unfortunately. I can admit it could be more robust and compact within my coding, considering the fact that my defined function ‘SaveScores()’ is around 188 lines long, due to the fact that the pre-defined functions didn’t function how I wanted them to. The main requisites for the game (5 rounds, saved scores, tie-breaker, etc.) have all been met to the best of my current standards, with some alternatives used. All in all, I think my work has the functionality and the basic look of the expected game, but in hindsight could be more compact, especially if I had some better knowledge of more advanced functions (e.g. arrays, 3D lists, importable functions, etc.). Another flaw with my coding is the viewing of the top 5 scores, since the program terminates after showing the scores, and I would put it into the while loop, but that would mean I would have to move all 405 lines over. In this case, I have instead put a warning on the choice, saying that the program will terminate if option 2 is selected as the pathway.
What new skills have I developed?
Apart from these difficulties in how my code needs to function, I have learnt how to manipulate lists using append, sort, reverse, pop, index, and truncate, all within something else I developed an understanding for, that being defining a function. The skills within this that I have made better are random.choice() and arithmetical operations such as <, > and ==. I have also found out how to use colour within my coding to make it look better, and a form of ASCII text
References within My Code
- #M, E. and Hall, A. (2018). Finding the index of an item given a list containing it in Python. [online] Stack Overflow. Available at: https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python [Accessed 28 Sep. 2018].
- #Coleman, J. (2018). Codio – Teach Code. With confidence.. [online] Codio. Available at: https://codio.com/jcoleman1/adventure-game:59afc31bcdcde722208fd273:nToaQAeAGGuj/tree/adventuregame.py [Accessed 28 Sep. 2018]. {previous Controlled Assessment}
- #Guru99 (2018). Python File Handling: Create, Open, Append, Read, Write. [online] Guru99.com. Available at: https://www.guru99.com/reading-and-writing-files-in-python.html [Accessed 2 Oct. 2018].
- #jozz3 and Gribouillis (2018). Sorting and alligning contents of a text file. [online] DaniWeb. Available at: https://www.daniweb.com/programming/software-development/threads/271141/sorting-and-alligning-contents-of-a-text-file [Accessed 2 Oct. 2018].
- #Pocketwarloc, zondo and Seekheart (2018). How do I print certain lines from a text file every time in python. [online] Stack Overflow. Available at: https://stackoverflow.com/questions/35541684/how-do-i-print-certain-lines-from-a-text-file-every-time-in-python [Accessed 3 Oct. 2018].
- #Bocui, Salvador Dali, Mezgrman and Avinash Raj (2018). Sorting a text file alphabetically (Python). [online] Stack Overflow. Available at: https://stackoverflow.com/questions/27123125/sorting-a-text-file-alphabetically-python [Accessed 5 Oct. 2018].
- #Ctrl S, ondra, Markus and Leeder, D. (2018). How to erase the file contents of text file in Python?. [online] Stack Overflow. Available at: https://stackoverflow.com/questions/2769061/how-to-erase-the-file-contents-of-text-file-in-python [Accessed 19 Oct. 2018]. (Ctrl S et al., 2018) [see attemptatotherroute.py]
- #NPE and Community♦ (2018). Fastest way to check if a value exist in a list. [online] Stack Overflow. Available at: https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exist-in-a-list [Accessed 19 Oct. 2018]. (NPE and Community♦, 2018) [see attemptatotherroute.py]
- #Williams, M. (2015). Add Colour to Text in Python. [online] ozzmaker.com. Available at: http://ozzmaker.com/add-colour-to-text-in-python/ [Accessed 30 Nov. 2018].
- #Patorjk.com. (2018). Text to ASCII Art Generator (TAAG). [online] Available at: http://patorjk.com/software/taag/ [Accessed 30 Nov. 2018].
Cite This Work
To export a reference to this article please select a referencing stye below:
Related Services
View allDMCA / Removal Request
If you are the original writer of this essay and no longer wish to have your work published on UKEssays.com then please: