CS2313 Semester A
Assignment 2: A Rock-Paper-Scissors Game
Copyright By PowCoder代写 加微信 powcoder
Deadline: 23:59pm, December 12, 2022, HKT
In this assignment, you are required to write a simple Rock-Paper-Scissors game in C++.
This involves classes/objects, pointers, strings, functions, array of objects, and flow control.
Your tasks are divided into two parts, and you are required to complete both.
Both parts are available for testing on PASS. Note that only a small number of test cases
are provided for testing. We will use more test cases to mark your solutions. It is also worth
mentioning that in this assignment, all the rules ( , Testing and Submission,
Assessment, Plagiarism) are the same as the assessment 1. In particular, late submission may
also lead to 70% marks (within 24hrs after the deadline) or 0% marks (after 24hrs).
1 Basic Level: Initializing a Random Sequence of Shapes
(50% Marks)
Your first task is to write a program that can initialize a sequence of shapes, which is used for
playing the Rock-Paper-Scissors game, as introduced in the advanced level task. The program
takes two integers from the user, where the first one is the total number of rounds, and the
second one is the seed for random number generation. Finally, it prints the entire sequence of
shapes. We provide a skeleton of code in the file asg2 Q1 2022 skeleton.cpp, and you
need to complete the program.
Now to begin, a shape has two attributes: name, and value. There are 3 shapes, and their
names are: “Rock”, “Paper”, and “Scissors”. The value of a shape simply depends on its name:
a “Rock” has a value of 1, “Paper” 2, and “Scissors” 3. All the 3 shape names are already
defined in the main() as an array of cstrings, i.e., char shapeName[][10].
A class called Shape is provided in the skeleton code, with the above attributes defined as
private members. name is a pointer to char, i.e., char *, or equivalently cstrings. Some
access function prototypes are also defined. There is also a prototype of the default constructor.
You need to implement these access functions and the default constructor in order to complete
the class definition of Shape.
To initialize a random sequence of shapes, you need to implement one function. A sequence
is simply an array of Shape objects as you can see in main(). initSequence(Shape *
shapeSeq, char shapeName[][10], int num) is the function to initialize the
sequence of shapes, where the integer value num indicates the number of shapes in this
sequence. When you create each shape, you should randomly select its value from 1 to 3, and
set its name according to the value as mentioned before. Note that the sequence is passed to
the function as a pointer (Shape *), and you should not change this.
We provide a help function called printSequence which simply takes the sequence
(again as a pointer) and the current number of shapes, and prints each Shape object
sequentially. This is called in main() to check output of your code.
A sample run of the program with 20 as the number of shapes and 6 as the seed is shown
below in Figure 1.
Figure 1: A run of the basic level program with 20 as the num and 6 as the seed.
What you need to submit: Based on asg2_Q1_2022_skeleton.cpp, complete the class
definition of Shape. Do not add/remove attributes or member functions. Finish the
implementation of the default constructor and access functions. Finish the implementation of
the initSequence function. Complete the main() function based on the sample output
shown in Figure 1.
What you need to submit: Submit the completed source file.
2 Advanced Level: Playing the Rock-Paper-Scissors Game
(50% Marks)
Your next task is to extend your program in the basic level, so it can play the Rock-Paper-
Scissors game with you as the only player.
The game is as follows. At first, the program asks the user the seed for random number
generation, which will be used in the generation of system shapes, just like the basic level, for
play. Note that you are not required to input the number of shapes, and the maximum number
of system shapes is fixed at 10 now. Then the game starts. Initially, you have $10 dollars. In
each round, the program asks you to enter an integer that indicate that a shape you will
choose, where 1 stands for “Rock”, 2 for “Paper”, and 3 for “Scissors”. Here we would just
assume that the input integer is 1 or 2 or 3, for simplicity. Then the program will generate a
system shape based on the random number generator initialized by the seed, and shows it to
you. The game continues until one of these conditions are met: (1) Your balance becomes 0.
Then the game ends automatically since you are broke. (2) You choose to leave the game with
a positive balance. You can do this by entering “-1” as your input, and the game will end and
your balance will be shown. Note that you have to play at least three hands before you can
leave the game. (3) The number of system shapes has reached the pre-set maximum value 10.
The game automatically ends and your balance is shown as well.
The rules of comparison between shapes are simple: rock beats scissors, paper beats rock,
and scissors beats paper. If you win, your balance will increase $10 dollars. If you lose, it will
decrease $10 dollars. If the program and you perform the same shape, your balance will not be
Some sample runs of the game are shown below. Note your program output should be
EXACTLY the same as the sample output below, using the same seed, if you are using
VS on Win10. If you’re using Mac OS or Linux, your output may be different and it’s okay
because of compiler difference.
Run 1: Figure 2 shows a run with 2 as the seed. We lose the game after 2 hands. The result
of your game is shown right after a hand is dealt to you.
Figure 2: A run of the advance level Rock-Paper-Scissors program with 2 as the seed.
Run 2: Figure 3 shows a run with 2 as the seed. We wanted to quit at the third hand, but
the system does not allow that. We can quit after playing at least 3 hands.
What you need to do: Based on the first part and asg2_Q2_2022_skeleton.cpp,
complete the Rock-Paper-Scissors game as specified above. Do not remove attributes or
member functions of Shape, or other access functions. You may add new member
functions to Shape and new non-member functions to the program. Again, your output
must be EXACTLY the same as the test cases on PASS. Note that your output may be
different from the sample runs here if you are not using VS on Win10. Apparently different
compilers implement rand() differently. You are encouraged to try different seeds and
play different bets to test your own solution (it’s a game anyway :)).
The detailed explanations of srand() and rand() can be found in:
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/cstdlib/rand/
What you need to submit: Submit the completed source file.
http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/cstdlib/rand/
Figure 3: A run of the advance level Rock-Paper-Scissors program with 2 as the seed.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com