pls use C++ to answer thank you! pls use C++ to answerthank you! pls use C++ to answer thank you!
Section C (20%) - Long Questions Answer any ONE out of the TWO questions in this section in the answer book provided. Each question carries 20 marks. If you answer more than one question, only the first attempted question will be marked. Indicate in your answer book clearly which question you are attempting. Question Cl The program below simulates a "pair-matching memory game". At first, the 4 x 6 game board is covered with 12 pairs of symbols and each box on the game board is closed ('X'). Each turn, the player opens 2 boxes on the game board (assume user input is valid). If the symbols at these 2 boxes are the same, these boxes will remain open (i.e., showing the symbols). Otherwise, they will be closed (i.e., showing 'X') again. The player needs to use the fewest turns to open all boxes on the game board by finding all symbols in pairs. Symbols that are used in the game are shown in Table 1 below: ASCII Symbol 33 34 35 36 37 38 ! " # $ % #include #include #include using namespace std; const char CLOSE = 'X'; const int ROW = 4; const int COL = 6; class Box ( public: Box () { private: ( Table 1. List of symbols that are used in the game symbol <= 0; opened false; 39 40 void open() { opened = true; } bool isOpen() { return opened; } char symbol; // stores symbol in a box }; void setSymbol (Box g [ROW] [COL]) { 41 42 43 44 + 8 ) bool opened; // stores if a box is opened // Your codes for C1 (a) should be inserted here 1
Question C1 (continued) void print (Box g [ROW] [COL], int r1, int cl, int x2, int c2) { // Your codes for C1 (b) should be inserted here } bool check (Box g [ROW] [COL], int ri, int cl, int r2, int c2) { // Your codes for C1 (c) should be inserted here bool gameEnd (Box g [ROW] [COL]) { } // Your codes for C1 (d) should be inserted here void choose (int n, int &rl, int &cl, int &r2, int &c2) { cout << "Choose position " << n << ":" << endl; } cout << "row col? "; if (n = 1) else cin >> r1 >> cl; cin >> r2 >> c2; cout << endl; int main() { srand(time (0)); int rl, cl, r2, c2; int turns = 0; Box gameBoard [ROW] [COL]; setSymbol (gameBoard); do { rl=cl = r2 c2 = -1; cout << "Turns: " << ++turns << endl; cout << "__ ----" << endl; print (gameBoard, rl, cl, r2, c2); choose (1, rl, cl, r2, c2); print (gameBoard, rl, cl, r2, c2); choose (2, r1, cl, r2, c2); print (gameBoard, rl, cl, r2, c2); if (check (gameBoard, r1, cl, r2, c2)) cout << "A match, Good!" << endl; else cout << "Not a match!" << endl;
Question C1 (continued) } system ("pause"); system("cls"); while (!game End (gameBoard)) ; cout << "Game ends. You used " << turns << " turns. \n"; return 0; The sample output below will be shown when all parts are completed correctly. Approriate use of loop structure and constant variables are expected in your answers. Sample Output: (the vertical display is shown in row format due to space concern) Turns: 1 0 1 2 3 4 5 0 X X X X X X 1 X X X X X X 2 X X X X X X 3 X X X X X X Choose position 1: row col? 0 0 (Screen is refreshed) Turns: 2 0 1 2 3 4 5 0 X X X X X X 1 X X X X X X 2 X X X X X X 3 X X X X X X Choose position 1: row col? 0 1 (Screen is refreshed) Turns: 3 0 1 2 3 4 5 0 X $ X X X X 1 X X $ X X X 2 X X X X X X 3 X X X X X X Choose position 1: row col? 0 5 (Screen is refreshed) 0 1 2 3 4 5 0, X X X X X 1 X X X X X X 2 X X X X X X 3 X X X X X X Choose position 2: row col? 1 2 0 1 2 3 4 5 0X $X X X X 1 X X X X X X 2 X X X X X X 3 X X X X X X Choose position 2: row col? 1 2 0 X 0 1 2 3 4 5 X X X + 1 X X $ X X X 2 X X X X X X 3 X X X X X X Choose position 2: row col? 1 5 0 2 3 4 5 0 X X X X X " 1 X X X X X 2 X X X X X X 3 X X X X X X Not a match! 1 0 1 2 3 4 5 0X SX X X X 1 X X X X X 2 X X X X X X 3 X X X X X X A match, Good! 0 1 2 3 4 5 0X $X X X + 1 X X $ X X ! 2 X X X X X X 3 X X X X X X Not a match!
Question CI (continued) Turns: 4 0 1 2 3 4 5 0 X X X X X 1 XXS XXX 2 X X X X X X 3 X X X X X X Choose position 1: row col? 1 1 (After a number of turns, and more positions are open...) Turns: 27 1 2 3 4 5 " + # X 1 2 &, & * X 3) 8 (( " ). Choose position 1: row col? 1 5 0 0 1 2 3 4 5 X X X X 0 X $ 1 X X X X 2 X X X X X X 3 X X X X X X Choose position 2: row col? 05 0 $* T 1 # + $ 0 1 2 3 4 5 0 $*8" + 1 + $ # ! + 2 &, & X 3) 8 (("). Choose position 2: row col? 25 T (Screen is refreshed) Game ends. You used 27 turns. 0 1 2 3 4 5 X X + 0X SX 1 X + $ X X X 2 X X X X X X 3 X X X X X X match, Good! A 0 1 2 3 4 5 0 $ * " 1 # + $ 2 &, & 3) ( " ) A match, Good! 1 + # ! ! (a) Complete the setSymbol() function that assigns symbol randomly for each box on the game board g. The symbols used are listed in Table 1 as shown on page 14, and each symbol should be used twice in the game board. (6 marks) (b) Complete the print() function that prints the game board g, based on the format of the given sample output above. For the boxes that are already opened, or as specified by (r1, cl) and (r2, c2), the symbol in those boxes should be displayed. Otherwise, "X" should be displayed. (6 marks)
pls use C++ to answer thank you! pls use C++ to answer thank you! pls use C++ to answer thank you!
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am