Sunteți pe pagina 1din 3

COMP 1010

Fall Lab 10

MATERIAL COVERED
• Array basics

Notes:
• Each lab is scored out of 2 points. Each completed lab exercise is worth 1 point. You must
complete two to receive full marks on the lab.
• Name your files something similar to LastnameFirstnameLab10Silver.pde.
• Your completed lab must be submitted before the deadline to the “Lab 10” submission folder.
Submit your Processing code (pde file) for each question.
• In this lab, each exercise depends on the previous exercise, so you will need to do them in
order.
• You are encouraged to do as many as you can. It’s excellent practice for assignments, tests,
exams, and future courses.
• The Bronze exercise is very small, and all students are encouraged to complete at least the Silver
exercise.
• Lab 11 will be a continuation of this lab, and will allow you to complete a fully-functional single-
person game.

Overall description

In this lab, you will implement a simplified version of the dice game Yahtzee™. For those familiar
with the game:
• Only the top half of the score sheet will be used in Lab 10.
• It is a single player game only.
• In each exercise, you will write two small functions which will add one more aspect to the
game. All other necessary code will be supplied.
• The Bronze level will only allow 5 dice to be rolled.
• The Silver level will allow re-rolling of some of the dice.
• The Gold level will keep score, and complete a simplified game.
The main functions (setup, draw, etc.) will be provided, including all necessary graphics code. You
need only write the small array-based functions.
Roll em!

The game is played by rolling 5 dice. The dice roll will be stored in an int[] array containing 5 integer
values from 1 to 6. A function void drawDie(int position, int value) will be provided, which
will draw one die in the canvas, in one of NUM_DICE locations. The first parameter is the location, which
must be a value from 0 (the leftmost die) to NUM_DICE-1 (the rightmost die). The second parameter is
the number to be shown on the die (from 1 to 6). Start with the file Lab10BronzeTemplate.pde. Add
one array and two small functions to create a program that will roll and display the dice.
1. Create an int[] global variable and initialize it to an array that will hold the NUM_DICE dice.
2. Write a function void rollDice() which will fill the array with a new dice roll – NUM_DICE
random integers between 1 and NUM_SIDES (these constants are already defined for you).
3. Write a function void showDiceRoll() which will draw the dice roll by calling the provided
drawDie function NUM_DICE times – once for each die.
Once complete, the bronze program should roll the dice every time you click on the canvas.

Re-roll some of the dice


The player is now allowed to make two additional rolls. Each time, some of the dice can be left as they
are, and the rest are re-rolled. The objective is to get as many as possible that are showing the same
number. Once this has been done twice, the player must choose one particular number from 1 to 6. The
score for that roll is the sum of only those dice that are showing that number. For example, if the dice are
showing 5-3-2-3-3 and the player chooses 3, the score will be 9 (add up the 3’s only – the 5 and 2 are
ignored). If the player chooses 2, the score will be 2. If the player chooses 1, the score will be 0.
Start with the file Lab10SilverTemplate.pde. The functions that control the overall operation of the
game, and do all of the graphics, are supplied. Add the following two small functions, and your code
from the Bronze exercise, to create a program that will allow the user to re-roll the dice.
1. A function void reroll(boolean[] diceToRoll) which will accept an array of NUM_DICE
boolean values. Dice for which diceToRoll[i] is true should be re-rolled. All the rest should
be left as they are.
2. A function int scoreAs(int chosenNumber) which will calculate and return the score for
the given roll, if the player chooses the chosenNumber. All dice showing that number are added
up, and all others are ignored.
Once complete, the silver program will begin with a message to click to roll a new set of dice. After
that roll, you will see a message to select the dice to be rerolled. As you select the dice by clicking on
them, they will turn grey. After 3 rolls, the scores will be printed and you can click to roll a new set of
dice.
Play 6 turns, and keep score.
To play the complete (still simplified) game, the player is given 6 turns (where each turn is 3 rolls). When
a player chooses to score a particular number, for example 3’s, the score will be entered beside “3’s” on
the scoresheet. Once a number is chosen, it cannot be chosen again. Each number must be used exactly
once. A total score is calculated. If that total score is 63 or more, a bonus of 35 points is added to the score
(once only). The named constants BONUS_THRESHOLD and BONUS_VALUE are defined for you as 63 and 35,
respectively.
The score sheet will be represented by an int[] array containing 8 values. The first six will be the scores
for 1’s to 6’s. A value of EMPTY (predefined for you as -1) will indicate that the row is empty (has not yet
been chosen). The last two rows will be for the Bonus score and the Total score. The int[] variable
scoreSheet has been defined for you. Don’t change its name, as other parts of the code refer to it.
Start with the file Lab10GoldTemplate.pde. Add the following two small functions, and the code from
the Bronze and Silver exercises, to create a program that will allow the user to play and score a
complete game.
1. Write a function void resetScoreSheet() which will create a new blank score sheet. The
entries for 1’s-6’s should be set to EMPTY, and the entries for the bonus and total should be set
to 0.
2. Write a function boolean enterScore(int row, int score) which will add the score for
another turn to the score sheet. The parameter row is the row chosen by the user, which will be
0 to 5 (for the chosen numbers 1 to 6). The parameter score is the score for the turn, to be
entered in that row. If the user has selected an empty row, then this function should return true,
and update the score sheet properly (including adding the bonus if earned, and updating the
total). If the user selected a previously-used row, then this function should return false and
leave the score sheet unchanged.
Once complete, the gold program will begin with a message to click to roll. For each turn, you have 3
rolls to try and collect as many of one number as possible. As in the silver program, click to select the
dice to reroll. After 3 rolls, you will see a message to click on the score card. Click on the empty box
on the score card for the number you want to score. Repeat for all 6 turns.

S-ar putea să vă placă și