CS 1037辅导、讲解C/C++编程、辅导OWL留学生、讲解C/C++设计

- 首页 >> C/C++编程

CS 1037 A (2018)

Assignment 2

Due: October 30, 11:55pm

Weight: 10% of final grade

Overview

This assignment is designed to test your ability to use classes (inheritance and overloading

operators).

You may freely use code from my lecture slides and from the code folder in OWL.

Your program will let you play Rock-Paper-Scissors against the computer.

The rules of the game are as follows:

1. Both players (computer and human) choose either rock, paper, or scissors at the same time.

2. If both players chose the same thing, the round is a tie and no points are awarded.

3. If it’s not a tie, then Rock beats Scissors, Scissors beat Paper, and Paper beats Rock.

4. Each player starts the game with an initial score. Every time a player wins a round, their score is

reduced by one. The first player to reach 0 score wins.

5. We are biased towards humans, so the human in our game starts with 3 points, and the computer

starts with 5 points.

Submission Requirements

* Submission MUST be done through OWL.

* See the syllabus for late penalties.

* You will be submitting the following 9 files, named exactly:

“assn2.cpp”

“Guess.cpp”

“Guess.h”

“Player.cpp”

“Player.h”

“Human.cpp”

“Human.h”

“Computer.cpp”

“Computer.h”

* This may include any non-container standard c++ header files (ie. cstdlib).

* Submission files will NEVER be zipped.

* If your code does not run as-is in Microsft Visual Studio 2017, you will lose the majority of

your marks.

* You will not receive any marks for following these instructions, but marks will be deducted

for not following them.

Project Setup

I recommend creating a “Empty Project” as you did in lab 2, and create your assn2.cpp file as

you did in the same lab. Unlike assignment 1, you MUST require the user to input a character to keep

the window open after the game is completed. The assn2.cpp file will contain your main function.

Main program flow (Understand/code the classes before trying to write main!)

The main function will prompt (cin) the user for a human name. It will then create a Computer

and a Human player. It will continue prompting the user for a human guess and getting the computer to

generate a random guess through the Human/Computer class objects, and updating scores until one

player has reached 0.

Each loop iteration will print both human and computer guesses, who won the round, and the

updated scores for the human and computer. After a player has reached 0, the program will print out

who won either by displaying the human name or “Computer”.

Guess class

You will create a Guess class, which will have 1 private char member named guess. The valid

values for guess are ‘R’,’P’, or ‘S’ meaning Rock,Paper, or scissors respectively. Guess will have an

inline getter function for the guess value. It will have 3 different constructors. The first is the default

contructor, which will randomly assign a guess value. The second constructor will take a char as a

parameter, if the parameter is upper or lower case R, it will set the guess to R, if it is upper or lower

case P, it will set the guess to P, and all other values will result in the guess being ‘S’. The third

constructor will take an integer, if the value is 0, guess is ‘R’, if it is 1, guess is ‘P’, else guess is ‘S’.

The only other method will have a prototype as follows:

int compare(const Guess &other) const;

This function will return 0 if (this) guess is the same as other.guess. It will return -1 if (this) wins, or

return +1 if other wins.

Player class

The Player class will serve as the base class for both the Human and Computer. It will have a

private int member named score. It will have a single constructor which takes an inital score and sets

the score accordingly. It will have an inline const getter method for score. You will overload the –

(prefix decrement operator) to decrease the score by 1. This is the prototype for the prefix decrement

operator: Player& operator--()

Human class

The Human class will be a subclass of the Player. It will have a string name member (include

<string>). It will have a single constructor which takes an inital name and sets the name accordingly,

and at the same time initialize the base class score to 3 (See initialization lists in the lecture slides). It

will have an inline const getter method for name. It will have the following method to prompt the user

for a guess:

Guess getGuess() const;

The user should enter R,P, or S, and you should call the appropriate Guess constructor and return it

from this method.

Computer class

The Computer class will be a subclass of the Player. It will have a default constructor which

initializes the base class score to 5 (See initialization lists in the lecture slides). It will have the

following method to create a random guess:

Guess getGuess() const;

Sample game output

Player name:Jay

Enter (R,P,S):P

Human:P Computer:R

Human:2 Computer:5

Enter (R,P,S):R

Human:R Computer:P

Human:2 Computer:4

Enter (R,P,S):S

Human:S Computer:S

Human:2 Computer:4

Enter (R,P,S):R

Human:R Computer:P

Human:2 Computer:3

Enter (R,P,S):R

Human:R Computer:P

Human:2 Computer:2

Enter (R,P,S):R

Human:R Computer:R

Human:2 Computer:2

Enter (R,P,S):R

Human:R Computer:R

Human:2 Computer:2

Enter (R,P,S):R

Human:R Computer:P

Human:2 Computer:1

Enter (R,P,S):R

Human:R Computer:S

Human:1 Computer:1

Enter (R,P,S):R

Human:R Computer:P

Human:1 Computer:0

The computer WON!!!

Questions

This is a short assignment, but it will require you to understand the lecture/slide material.

Before asking questions, I expect you to have looked up the relevant slide material and phrase your

questions in relationship to a specific slide.

Marking

* If your code does not compile and execute, you will receive 0 marks for functional test cases.

* Marks will be deducted for not following instructions. (Both formatting and functional)

* Marks will be deducted for “messy” code – ie. Not properly indented.

* Use of meaningful variable names where appropriate and comments for complex code is required.

* I do not expect the code to be optimized, however, needless inefficiencies may be deducted.

Code similarities are both violations of Academic Dishonesty for those sharing

their code as for those using other's code. If you wish to help another

student, explain concepts in words, draw diagrams, or refer the student to specific

reference material. You are not helping by providing the code!

Similarity detection software will be used on all assignments. Any students who

achieve a high score for improbable code similarities will receive a mark of 0. In

addition, further Academic Dishonesty reporting may be performed, and

additional penalties applied.


站长地图