You are on page 1of 9

Concordia University

Comp 218 – Fall 2018


Fundamentals of Object Oriented Programming
Assignment 4 - Due by 11:59pm Thursday November 29/2018
_____________________________________________________________________________
Purpose: The purpose of this assignment is to practice classes and arrays.

General Guidelines When Writing Programs:


Refer to handouts for assignments 1 and 2 for details.

Question #1 – Classes (13 pts)


You will write and use two classes to program the dice game “Round the Clock”.
The object of the game is to be the 1st player to throw all the numbers from 1 to 12 in order.

a) (2 pts) A PairOfDice object represent a pair of dice. Write a declaration for the class
PairOfDice including its data members and function members based on the following
UML and save it in a file called PairOfDice.h.

PairOfDice
- die1: integer
- die2: integer
+ PairOfDice()
+ rollDice(): void
+ rolledRequestedValue(integer): boolean
+ totalRoll(): integer
+ showRoll(): void

Note: die1 and die2 store the result of rolling the dice.

b) (2 pts) A Player object represents a player of the game. Write a declaration for the
class Player including its data members and function members based on the following
UML and save it in a file called Player.h.

Player
- name: string
- nextNum: integer
+ Player()
+ Player(string)
+ getName(): string
+ getNextNum(): integer
+ setName(string): void
+ increaseNextNum(): void
+ nextMove(): void

COMP218/Fall 2018- Assignment 4 Page 1 of 9


Note: name stores the name of the player; nextNum stores the number the player is
looking for on his/her next turn.
c) (3 pts) Implement the member functions for the class PairOfDice based on the following
descriptions, and save them in a file called PairOfDice.cpp.
PairOfDice(): default constructor which sets the value of the two dice to zero.
rollDice(): simulates the rolling of the dice using a random number generator. To simulate
the rolling of a die, you generate a random number from 1 to 6. You are required to generate 2
numbers to simulate the rolling of 2 dice and not one number from 1 to 12 as the odds are not
the same.
Please refer to the code random.cpp which you downloaded at the same time as this handout
to see how to use the random number generator in C++.

rolledRequestedValue(): returns true if either of the dice equals the passed value or the
sum of the two dice equals the passed value and false otherwise. For example, a 3 and 6 can be
counted for a 3, 6 or 9.
totalRoll(): returns the sum of the two dice.
showRoll(): displays on the console the value of each dice with a descriptive message.

d) (3 pts) Implement the member functions for the class Player based on the following
descriptions, and save them in a file called Player.cpp.

Player(): default constructor which sets the name attribute to "" and the nextNum
attribute to 1.
Player(): a second constructor which sets the name to the passed name and the nextNum
attribute to 1.
getName(): returns the name stored in the name attribute .

getNextNum(): returns the value stored in the attribute nextNum.


setName(): assigns to the name attribute the passed value.
increaseNextNum(): increments by 1 the attribute nextNum.

nextMove(): displays on the console the value that the player needs to role in a descriptive
message including his/her name.

e) (5 pts) You will use the classes PairOfDice and Player to simulate a human playing
against the computer the dice game “Round The Clock”.

Directions for play: (edited from https://icebreakerideas.com/dice-


games/#Round_the_Clock_Dice_Game)

1. The first player throws both dice, hoping to throw a 1. Players


then take turns (in a clockwise direction) trying to throw a 1.

COMP218/Fall 2018- Assignment 4 Page 2 of 9


2. On the next round, those players who threw a 1 will try to throw a 2. Any player who did
not throw a 1 in the first round will try again this round. Players can do so by throwing a
2 or two 1s.
3. Play continues round-by-round with players trying to throw all the numbers from 1 to 12
in sequence. Players may count the spots on just one die or on both dice added
together. As mentioned earlier, throwing a 3 and a 6 could be counted as 3, 6, or 9.
4. The first player to go “Round the Clock” – throw all the numbers from 1 to 12 in order –
wins the game.

Write a C++ program which simulates the game as described above using the methods of the class
PairOfDice and Player which you developed in parts a) to d) of this question. Here are the main
steps:
1. Display a welcome header/message.
2. Ask the human player for his/her name.
3. Ask the human player if they want to keep the computer’s name as Computer or change it. If
they wish to change the name, ask them for the name.
4. Now that you have the names of the players you can create 2 Player objects with the
appropriate names assigned.
5. Create a PairOfDice object .
6. Play as many rounds as necessary until there is a winner. Keep track of the number of rounds it
took for there to be a winner. As soon as there is a winner the game is over.
7. Display the winner of the game along with the number of rounds it took for the game to end.

Following are some screen shots of a game to illustrate the expected behavior of your program. Note
this is a sample, meaning you can change the messages and the format but not the content. As this
game took 91 rounds, intermediate results have been omitted.

COMP218/Fall 2018- Assignment 4 Page 3 of 9


COMP218/Fall 2018- Assignment 4 Page 4 of 9
COMP218/Fall 2018- Assignment 4 Page 5 of 9
Rounds 15 to 86 omitted for brevety.

COMP218/Fall 2018- Assignment 4 Page 6 of 9


Question #2 – Arrays (7 pts)
Your friend has asked you to write a program that deletes repeated characters from an array. For
example, if an array contains the letters <a a a b a> the resulting array should be <a b>. When a
letter is deleted, the remaining letters are moved forward to fill in the gap. This will create empty
positions at the end of the array so that less of the array is used.
You can assume that the user enters only lower case letters.

COMP218/Fall 2018- Assignment 4 Page 7 of 9


Your task is to write a C++ program which
1. Display a welcome header/message.
2. Ask the user for the number of characters they wish to store in the array. Make sure input is
greater than or equal to 3 and less than or equal to 20.
3. Read and store the requested number of characters into a character array.
4. Remove all repeated characters.
5. Display the new array as well as how much of the array is used after the repeated characters are
removed.

Following are some sample runs to illustrate the expected behavior of your program. Note this is a
sample, meaning you can change the messages and the format but not the content.

COMP218/Fall 2018- Assignment 4 Page 8 of 9


Submitting Assignment 4
- Be sure to test your programs in Visual Studio.
- Zip the 5 files for question 1 and the file for question 2 together.
- Naming convention for zip file: Create one zip file, containing all files for your assignment using
the following naming convention:
The zip file should be called a#_studentID, where # is the number of the assignment
studentID is your student ID number. So for the fourth assignment, student 123456
would submit a zip file named a4_123456.zip

Evaluation Criteria for Assignment 4

Question 2 (13 pts)


Class Dice declaration (.h file) 1pt
Class Dice implementation (.cpp file) 2pts
Class Player declaration (h. file) 1.5pts
Class Player implementation (.cpp file) 2.5pts
Driver program 6pts
Question 2 (7pts)
Prompt user for number of characters 1pt
Set up array of requested size 1pt
Read and store numbers in array 1pt
Remove repeated letters 3pts
Display content of array and number of characters 1pt
Total 20 pts

COMP218/Fall 2018- Assignment 4 Page 9 of 9

You might also like