Please answer all the questions correctly in the attachement.
Question 1
Apply C++ language to program a Mastermind game. Some of you may be familiar with the game where the player is to guess the colours of four hidden pegs. Our program will use char (between ‘a’ and ‘h’) to simulate the pegs.
You should use arrays to store both the hidden codes and the player’s guesses. Write individual functions to implement each of the following specification of the game.
(a) You should start the game with a welcome message. It should include your name, PI number and a short introduction. Figure Q1(a) shows a sample welcome message.
Figure Q1(a): Welcome Message
(4 marks)
(b) Initialisation – Your program needs to declare arrays and initialise them with appropriate values for both the hidden codes and the player’s guesses. You need to use a random generator to simulate the hidden codes. (4 marks)
(c) Display – Your program needs to display the player’s guesses and the pins that are awarded. Figure Q1(c)(i) shows the initial display after the welcome message. Figure Q1(c)(ii) shows the result after three guesses.
Figure Q1(c)(i): Initial display
Figure Q1(c)(ii): After Three Guesses
(4 marks)
(d) Awarding Pins – your program should compare the player’s input against the hidden codes. If an input matches a hidden code and is in the correct position, then a black pin is awarded. If an input matches a hidden code but is in a wrong position, then a white pin is awarded.
No pins are awarded if none of the inputs matches any of the hidden codes. If there are duplicate letters in the input, there must be a one-to-one match for a pin to be awarded. (8 marks)
(e) Playing the game – Your program should prompt the player to input the letters. It should validate that the letters are between ’a’ and ‘h’ inclusive. A final message is displayed when the game ends i.e. either the player wins or loses the game.
Figure Q1(e)(i) and Figure Q1e(ii) show the message displayed respectively if the player wins or loses the game.
Figure Q1e(i): Player wins the game
Figure Q1e(ii): Player loses the game
You should also write a main() function to run the program by calling the functions you have implemented above.. (10 marks)
Question 2
A SimpleQuaternion can be represented by four data members namely real, vi, vj and vk. All data members are integers.
Identify suitable coding modules for the SimpleQuaternion class with the following specifications:
(a) Define the four data members: real, vi, vj and vk.
(3 marks)
(b) Implement a parameterised constructor that receives four arguments that represent the values of real, vi, vj and vk respectively. The constructor will initialise the three data members with the arguments with the following rules. (5 marks)
(c) Overload the << operator so that the statements
SimpleQuaternion sq1(2, 3, 4, 5); cout << “sq1 = ” << sq1;
will produce the result
sq1 = (2, 3i, 4j, 5k)
(5 marks)
(d) Overload the + operator so that two instances of SimpleQuaternion can be added using the + operator.
For example sq1 = (5, 2i, 6j, 8k) sq2 = (3, 5i, 7j, 6k) sq3 = sq1 + sq2 = (8, 7i, 13j, 14k)
(5 marks)
(e) Overload the * operator so that two instances of SimpleQuaternion can be multiplied using the * operator.
Given that q1 and q2 are Simple Quaternions. Let q1 = (a1, b1i, c1j, d1k) and q2 = (a2, b2i, c2j, d2k) The product (q1 * q2) is ( a1a2 – b1b2 – c1c2 – d1d2, (a1b2 + b1a2 + c1d2 – d1c2)i, (a1c2 + c1a2 + d1b2 – b1d2)j, (a1d2 + d1a2 + b1c2 – c1b2)k )
For example sq1 = (5, 2i, 6j, 8k) sq2 = (3, 5i, 7j, 6k) sq3 = sq1 * sq2 = (-85, 11i, 81j, 38k)
(8 marks)
(f) Overload the == operator so that we can check whether two instances of SimpleQuaternion are equal. Two instances are equal if each element in one instance is equal to the corresponding element in the other instance. (5 marks)
(g) Write the main function that will demonstrate how all the operators can be used. (4 marks)
Question 3
Implement a C++ program that helps to explain the syntax and use of data structures such as arrays and pointers. These data structures form part of the data members and constructors in a C++ class.
(a) Declare the data members of MonthlySales class as follows:
(i) A string representing the month and year e.g. “January 2019” when the sales are made.
(ii) An integer representing the number of sales invoices in the month.
(iii) Three dynamically locations large enough to store the name of customers, invoice number and the amount invoiced.
The locations are referenced by three pointers string* customer. int* invNo, double* amount. (4 marks)
(b) Implement an application using the C++ language in an object-oriented style. Constructors and destructor are used to initialise and remove objects in an object- oriented manner.
You are asked to write the following constructors to initialise a MonthlySales object and a destructor to remove it from memory.
(i) A parameterised constructor
(5 marks)
(ii) A default constructor (Assuming that there are at least three invoices, you may use any valid default values for the data members) (4 marks)
(iii) A copy constructor
(6 marks)
(iv) A destructor
(2 marks)
(c) Write a main() function to demonstrate how the constructors in part (b) are being used. (4 marks)
Question 4
Appraise the use of sprites and sprite sheet in SDL programming. You are required to write a SDL program that loads a sprite sheet as shown in Figure Q4(a).
Figure Q4(a)
Using the above sprite sheet, the SDL program should produce the following graphical output as shown in Figure Q4(b).
Figure Q4(b): Output of SDL program
Replace the title of the window “John Tan A1234567” with your name and id. The size of the window should be just big enough to show the entire output.
Note that all the circles in the sprite sheet have different colours and therefore the output of your program should show the semi-circles in the corresponding colours. (10 marks)


0 comments