+ All Categories
Home > Documents > CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put...

CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put...

Date post: 26-Mar-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
8
Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 1 CSP1150/CSP5110 Programming Principles Assignment 1: Individual programming assignment (“Dice Game” program) Assignment Marks: Marked out of 20, worth 20% of unit Due Date: 11 September 2017, 9:00AM Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered in the unit so far, including the usage of variables, input/output, data types, selection, iteration, functions and data structures. Above all else, it tests your ability to design and then implement a solution to a problem using these concepts. Assignment Overview You are required to design and implement a program that allows the user to play a single-player dice game based loosely on the game “Farkle” (although you do not need to be familiar with Farkle). The game is played as follows: The game consists of 3 turns, and the aim of the game is to score as many points as possible Each turn begins with a roll of 6 standard dice Matching dice (2 or more of the same number) can be set aside for points, and the remaining dice can then be re-rolled in an attempt to score more points Points are “amount of dice * number on dice”, e.g. rolling three 5s is worth 15 points The player chooses whether to set matching dice aside and whether to re-roll remaining dice If the player chooses not to re-roll the dice, the points accumulated in the turn are added to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn ends – hence, the player must decide how far to push their luck with additional rolls The game involves quite a lot of luck, but there is also a small amount of strategy in choosing whether to set matching dice aside and whether to re-roll the remaining dice. The entirety of this program can be implemented in under 100 lines of code (although implementing CSP5110 requirements or optional additions may result in a program longer than this) – Ask your tutor for advice if you feel your program is unusually long or inefficient.
Transcript
Page 1: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 1

CSP1150/CSP5110 Programming Principles

Assignment 1: Individual programming assignment (“Dice Game” program)

Assignment Marks: Marked out of 20, worth 20% of unit

Due Date: 11 September 2017, 9:00AM

Background Information

This assignment tests your understanding of and ability to apply the programming concepts we have

covered in the unit so far, including the usage of variables, input/output, data types, selection,

iteration, functions and data structures. Above all else, it tests your ability to design and then

implement a solution to a problem using these concepts.

Assignment Overview You are required to design and implement a program that allows the user to play a single-player dice

game based loosely on the game “Farkle” (although you do not need to be familiar with Farkle).

The game is played as follows:

The game consists of 3 turns, and the aim of the game is to score as many points as possible

Each turn begins with a roll of 6 standard dice

Matching dice (2 or more of the same number) can be set aside for points, and the

remaining dice can then be re-rolled in an attempt to score more points

Points are “amount of dice * number on dice”, e.g. rolling three 5s is worth 15 points

The player chooses whether to set matching dice aside and whether to re-roll remaining dice

If the player chooses not to re-roll the dice, the points accumulated in the turn are added

to their score and the turn ends

If no dice can be put aside after a roll, the player loses the points the potential points and

the turn ends – hence, the player must decide how far to push their luck with additional rolls

The game involves quite a lot of luck, but there is also a small amount of strategy in choosing

whether to set matching dice aside and whether to re-roll the remaining dice.

The entirety of this program can be implemented in under 100 lines of code (although implementing

CSP5110 requirements or optional additions may result in a program longer than this) – Ask your

tutor for advice if you feel your program is unusually long or inefficient.

Page 2: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 2

Program Output Example To help you to visualise the program, here is an example screenshot of the game being played:

The game begins.

At the start of each turn, the program tells the user which turn it is and their current score.

The dice are rolled – 1, 2, 2, 4, 5, 6.

A pair of 2s are the only matching dice, so I set them aside for 4 points.

I want more points so I choose to re-roll the remaining 4 dice.

The next roll contains a pair of 5s – nice! I put them aside for 10 points. With 2 dice remaining, I don’t like my chances so I choose to end the turn and secure the 14 points.

Turn 2 begins. Not a bad roll! I choose not to put aside the pair of 3s – hopefully I can do better by re-rolling them! I’m definitely putting aside the pair of 6s though. I re-roll the remaining 4 dice and…

Darn. No matching dice to set aside. The turn ends and I don’t gain those 12 points.

Turn 3 begins… I’ll take it! I put aside the three 3s for 9 points.

I put aside the two 6s for 12 points.

I’m not silly enough to re-roll a single dice, so I end the turn and secure my 21 points. The game ends with my final score being 35.

Page 3: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 3

Pseudocode As emphasised by the case study of Module 5, it is important to take the time to properly design a

solution before starting to write code. Hence, this assignment requires you to write and submit

pseudocode of your program design as well as the code for the program. Furthermore, while your

tutors are happy to provide help and feedback on your assignment work throughout the semester,

they will expect you to be able to show your pseudocode and explain the design of your code.

You will gain a lot more benefit from pseudocode if you actually attempt it before trying to code

your program – even if you just start with a rough draft to establish the overall program structure,

and then revise and refine it as you work on the code. This back and forth cycle of designing and

coding is completely normal and expected, particularly when you are new to programming. The

requirements detailed on the following pages should give you a good idea of the structure of the

program, allowing you to make a start on designing your solution in pseudocode.

See Reading 3.3 for further information and tips regarding writing good pseudocode.

Since the structure of this program is relatively complex, a broad overview has been provided below:

This is an extremely general overview that only aims to reflect the general structure of the code and the main steps of processing involved. Your pseudocode should be significantly more detailed. As always, contact your tutor if you have questions or do not understand any part of the assignment, or if you would like help or feedback on your work.

For each turn

Reset dice and points

For each roll

Roll remaining dice

Find matching dice and prompt user to set them aside

If no dice set aside

End turn without adding points to score

Else

Ask user whether they want to roll again or end turn

If user ends turn

Add points to score and end turn

Else

Continue to next roll

Show end of game messages

Page 4: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 4

Write a separate section of pseudocode for each function you define in your program so that the

pseudocode for the main part of your program is not cluttered with function definitions. Ensure that

the pseudocode for each of your functions clearly describes the parameters that the function

receives and what the function returns back to the program.

It may help to think of the pseudocode of your program as the content of a book, and the

pseudocode of functions as its appendices: It should be possible to read and understand a book

without necessarily reading the appendices, however they are there for further reference if needed.

Only one function is required in this assignment (detailed later in the assignment brief).

Program Requirements Below are the detailed requirements of the program you must implement. In the following

information, numbered points describe a core requirement of the program, and bullet points (in

italics) are additional details, notes and hints regarding the requirement. Refer to the earlier

information or ask your tutor if you do not understand a requirement or would like further details.

1. The game consists of 3 turns. At the start of each turn, print a message indicating which turn it is

and the user’s current score.

At the start of each turn, set the number of remaining dice to 6, and the points earned this turn to 0.

2. In each turn, there are an unknown number of rolls. For each roll, firstly simulate rolling the

remaining dice by producing a list of random integers between 1 and 6.

The amount of numbers in the list should match the number of remaining dice.

Sort the list so that the rolled numbers are in ascending order using the “.sort()” list method.

Print the sorted list of numbers for the user to see.

3. Check the rolled dice for matching numbers (i.e. 2 or more of the same number). For each set of

matching numbers, print the details of the match and the points it is worth, then use the

“getChoice()” function (detailed below) to prompt the user to decide whether they wish to

set those dice aside by entering “yes” or “no”.

One approach is to loop through the numbers 1 to 6, using the “.count()” list method to

determine how many of each number appears in the list of numbers.

Page 5: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 5

4. If the user chooses to set aside matching dice, subtract the number of dice from their remaining

dice and add the points that the matching dice are worth to their points.

Points are simply “amount of dice * number on dice”, but remember that the points a user earns

during a turn are not added to their score until they choose to end the turn.

If the user chooses not to set the dice aside, no change is made to the number of dice or points.

5. If no sets of matching dice were set aside in this roll, continue to the next turn without adding

the points to their score.

A boolean variable can be used to keep track of whether or not any dice have been set aside.

It is possible (although not very clever) for the user to roll matching dice and choose not to set any of

them aside, causing the turn to end.

6. Otherwise, tell the user how many dice are remaining and their current points, and then use the

“getChoice()” function (detailed below) to prompt the user to decide whether or not to re-

roll their remaining dice or end their turn by entering “roll” or “end”.

It is possible (although again, not very clever) for the user to choose to re-roll when they have fewer

than 2 dice remaining. Accounting for this is a CSP5110 requirement.

7. If the user chooses to end their turn, add the points to their score and continue to the next turn.

If they choose to re-roll the remaining dice, perform another roll (repeat from Requirement 2).

8. At the end of the third turn, print a “game over” message and their final score. Then, print an

additional message if they achieved a score above the following thresholds:

If their score is at least 60, print “Excellent score, well done!”

Otherwise, if their score is at least 50, print “Great score!”

Otherwise, if their score is at least 40, print “Good score!”

Otherwise, do not print anything.

Programming Tip: Do not attempt to implement the entire program at once. Work on one small part (a single requirement or even just part of a requirement) and only continue to the next part

once you have made sure that the previous part is working as intended and that you understand it.

It can also be useful to create separate little programs to work on or test small sections of complex code, allowing you to focus on just that part without the rest of the program getting in the way.

Page 6: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 6

The “getChoice()” function Your program must define a function named “getChoice” that receives two parameters:

1. “prompt” – A string containing the text to be shown to the user when getting their input.

2. “validChoices” – A tuple of strings containing the valid responses to the choice.

Usage example: choice = getChoice('Enter "yes" or "no": ', ('yes', 'no'))

prompt validChoices

The function should use these parameters to repeatedly prompt the user for input until they enter

one of the valid choices. If they enter anything that is not a valid choice, print “Invalid choice.” Once

the user enters a valid choice, it should be returned. Implementing this function will most likely

involve a “while” loop, the “in” comparison operator, and an “if” statement.

Revise Module 4 if you are struggling to define or use functions. Ensure that the function receives

parameters and returns its result as described – following function specifications is important.

Additional Requirements for CSP5110 Students If you are in CSP5110, the following additional requirements apply. If you are not in CSP5110, you

do not need to implement these requirements (but you are encouraged to do so if you want). Ask

your tutor if you do not understand any of the requirements or would like further information.

1. Before the first turn of the game, use the “getChoice()” function (detailed above) to prompt

the user to select a difficulty level by entering “normal” or “easy”. If they choose “easy”, the

game should consist of 4 turns rather than 3.

Make sure that the message telling the user which turn it is, e.g. “Turn 1 of 3” is always correct,

regardless of whether the user chose normal or easy mode.

2. Before asking the user whether they would like to re-roll the remaining dice (i.e. between

Requirements 5 and 6), check if there are fewer than 2 dice remaining. If so, print a message

saying that there are too few dice to keep rolling, add their points to the score, and end the turn.

If there are 2 or more dice remaining, continue with Requirement 6 as normal.

Page 7: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 7

Optional Additions and Enhancements Below are some suggestions for minor additions and enhancements that you can make to the

program to further test and demonstrate your programming ability. They are not required and you

can earn full marks in the assignment without implementing them.

1. If a turn ends due to not setting any dice aside, pause the program until the user presses Enter

between telling them of their misfortune and beginning the next turn. This makes the program

more user-friendly, giving the user a moment to acknowledge the end of a turn before being

faced with the result of the next turn’s roll.

This can be achieved simply using the “input()” function – no need to even store what they enter.

If you’ve implemented CSP5110 Requirement 2, also pause the program if a turn ends due to having

insufficient dice remaining.

2. Incorporate special point values for certain combinations of dice, as follows:

Rolling 4 or more 1s is known as an “all for one”, worth 20 points (instead of 4-6 points). The user still

chooses whether or not to set the dice aside.

Rolling 1, 2, 3, 4, 5, 6 is known as a “straight”, worth 50 points (instead of 0 points). Since the

alternative is ending the turn with 0 points, this should be set aside without asking the user to choose.

Do not rely on random chance to test that this functionality has been implemented correctly!

Temporarily modify your code to manipulate the rolled dice or create a separate testing program.

3. Use the Unicode characters for dice values instead of the numbers 1 to 6 whenever you print

dice values in the program. The characters are ⚀, ⚁, ⚂, ⚃, ⚄ and ⚅ (or find them here).

Implementing this efficiently is a challenge, but can be done quite elegantly. I suggest making a list

of those 6 characters and using “random.choice()” to randomly select from the list when rolling

the dice instead of using “random.randint()”.

Using the “enumerate()” function (see Lecture 3, Slide 38) when checking for sets of matching

dice can ensure that you have access to an integer variable representing the value of the dice.

You are welcome to come up with and implement additional extra features in your program, but be

sure to check with your tutor first if they alter or impact the core requirements of the assignment!

Page 8: CSP1150/CSP5110 Programming Principles · to their score and the turn ends If no dice can be put aside after a roll, the player loses the points the potential points and the turn

Semester 2, 2017 CSP1150/CSP5110 Assignment 1 Page 8

Submission of Deliverables Once your assignment is complete, submit both your pseudocode (PDF format) and source code

(“.py” file) to the appropriate location in the Assessments area of Blackboard. An assignment cover

sheet is not required, but be sure to include your name and student number at the top of both files

(not just in the filenames).

Referencing, Plagiarism and Collusion The entirety of your assignment must be your own work (unless otherwise referenced) and

produced for the current instance of the unit. Any use of unreferenced content you did not create

constitutes plagiarism, and is deemed an act of academic misconduct. All assignments will be

submitted to plagiarism checking software which includes previous copies of the assignment, and

the work submitted by all other students in the unit.

Remember that this is an individual assignment. Never give anyone any part of your assignment –

even after the due date or after results have been released. Do not work together with other

students on individual assignments – helping someone by explaining errors in their code/logic or

directing them to the relevant resources is appropriate, but doing it for or alongside them, or

showing them how you did it is not. An unacceptable level of cooperation between students on an

assignment is collusion, and is deemed an act of academic misconduct. If you are uncertain about

plagiarism, collusion or referencing, simply contact your tutor, lecturer or unit coordinator and ask.

You may be asked to explain and demonstrate your understanding of the work you have submitted.

Marking Key Marks are allocated as follows for this assignment.

Criteria Marks

Pseudocode

These marks are awarded for submitting pseudocode which suitably represents the design of your source code. Pseudocode will be assessed on the basis of whether it clearly describes the steps of the program in English, and whether the program is well structured.

5

Functionality

These marks are awarded for submitting source code that implements the requirements specified in this brief, in Python 3. Code which is not functional or contains syntax errors will lose marks, as will failing to implement requirements as specified.

10

Code Quality

These marks are awarded for submitting well-written source code that is efficient, well-formatted and demonstrates a solid understanding of the concepts involved. This includes appropriate use of commenting and adhering to best practise.

5

Total: 20


Recommended