0% found this document useful (0 votes)
14 views4 pages

Assg 4

Uploaded by

michaelng1112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Assg 4

Uploaded by

michaelng1112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSCI1520 Computer Principles and C++ Programming, Spring 2020/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Assignment 4: SOS
Due: 20:00, Wed 24 Mar 2021 File name: sos.cpp Full marks: 100

Introduction
The objective of this assignment is to practice (1) defining functions (being a callee), (2) calling
functions (being a caller), and (3) representing special kind of data. You will implement a two-player
paper-and-pencil game called SOS. Two players take turns to mark the spaces in a 3 × 4 grid with
either symbols O or S. Players can use different symbols in different turns. The player who first
creates the sequence S-O-S horizontally ―, vertically |, or diagonally ⟍⟋ in the grid wins the game.
When the grid is full without any S-O-S sequences, the game is a draw. Error! Reference source not
found. shows an example grid. If an S is put into the empty space in the second row, an S-O-S
sequence will be formed.

S . O O
S O . O
. . . S
Figure 1: An Example SOS Game Configuration

Program Specification
This section describes the representation of a grid in a game, the necessary functions, and the flow
of your program.

Basic Requirements
• You cannot declare any global variables (variables declared outside any functions).
• You cannot use any functions in the <cmath> library.
• You cannot use any arrays or vectors.
• You cannot use the string class.

Grid Representation
There are 12 positions in the 3 × 4 grid. Therefore, we shall use integers 1–12 to denote these
positions, where 1–4 denote the spaces in the top row of a grid left-to-right, 5–8 denote the spaces
in the middle row left-to-right, and 9–12 denote the spaces in the bottom row left-to-right.

To encode the whole grid of a game, we use a 12-digit integer 𝑑1 𝑑2 𝑑3 𝑑4 𝑑5 𝑑6 𝑑7 𝑑8 𝑑9 𝑑10 𝑑11 𝑑12.
Each digit 𝑑𝑖 stores the state of position 𝑖, which has only three possible values: 1, 2, or 3. Value 1
denotes an empty (unfilled) space in the position. Value 2 denotes the space is filled with a symbol O.
Value 3 denotes the space is filled with a symbol S. For example, the grid in Error! Reference source
not found. is encoded by the integer 312232121113. Using this representation, an empty grid (with
no spaces filled) is simply encoded as 111111111111.

The data type int in C++ is typically 32-bit and thus not big enough to store a 12-digit integer. In
order to store a game grid, you have to use a variable of a bigger integer type called long long. In
Visual Studio and Xcode, long long is a 64-bit signed integer type, whose range is
−9,223,372,036,854,775,808 … 9,223,372,036,854,775,807.

Copyright © 2021 CSE, CUHK Page 1 of 4


CSCI1520 Computer Principles and C++ Programming, Spring 2020/21
Department of Computer Science and Engineering, The Chinese University of Hong Kong

Provided and Required Functions


Your program must contain the following functions. Some of them are written for you already
(Provided) and you should not modify their contents. The other will be written by you (Required).
These functions must be called somewhere in your program. You must not modify the prototypes of
all these functions. You can design extra functions if you find necessary.

In the functions below, you can assume that (a) parameter grid is a proper encoding of the grid of
an SOS game (that is, 111111111111 ≤ 𝑔𝑟𝑖𝑑 ≤ 333333333333 containing digts 1/2/3 only); (b)
parameter pos is always 1–12; and (c) parameter sym is either ‘O’ or ‘S’.

(Provided) int gridState(long long grid, int pos)


This function returns the state of position pos of grid, that is, 1 for empty, 2 for symbol O, and 3 for
symbol S. E.g., gridState(312232121113, 5) shall return 3, while gridState(312232121113,
9) shall return 1.

(Provided) void printGrid(long long grid)


Prints grid to the screen using the format in Figure 1.

(Required) bool updateGrid(long long &grid, char sym, int pos)


This function updates the posth digit of the reference parameter grid to 2 if parameter sym is ‘O’,
or to 3 if sym is ‘S’. The underlying meaning of this function is actually to perform the task of a player
putting symbol sym in position pos of grid. The function returns true if any new S-O-S sequence(s)
has/have been formed by this move; and returns false otherwise. The following shows some
sample function calls and the expected results.

grid sym pos Value of grid after calling Return Value


updateGrid(grid, sym, pos)
312232121113 ‘S’ 7 312232321113 true (S-O-S formed)
312232121113 ‘O’ 11 312232121123 false (no S-O-S formed)
111111111111 ‘S’ 3 113111111111 false (no S-O-S formed)
333331333333 ‘O’ 6 333332333333 true (S-O-S formed)
Notes:

• Calling gridState(…) is useful when implementing this function.


• You shall not check in this function whether the player move is valid or not. (You check validity
outside of this function. See steps 3–4 of the next section.)
• Do not print anything in this function.
• We shall grade this function individually. That is, we will replace your main() with our testing
code to call this function for grading. So you should not write any code that is outside of this
function’s descriptions.

Program Flow
The program flow of the game is described as follows. You should call the functions above to aid
your implementation.

1. The program starts the game with an empty grid (111111111111). Players 1 and 2 compete the
game. Player 1 takes the first turn.

Copyright © 2021 CSE, CUHK Page 2 of 4


CSCI1520 Computer Principles and C++ Programming, Spring 2020/21
Department of Computer Science and Engineering, The Chinese University of Hong Kong

2. Then, prompt the current player to enter a position to put a symbol in. The input format is
always a character followed by an integer, denoting the symbol and the position respectively.
(E.g., entering S 5 means putting symbol ‘S’ to position 5.)
3. A user input is invalid if the position is outside the range 1–12, or the position was already filled,
or the symbol is neither ‘O’ nor ‘S’. (Lowercase ‘o’ or ‘s’ are considered invalid.) In case the
player makes an invalid input, display a warning message and go back to step 2.
4. Update the grid by putting the symbol to the corresponding position.
5. Swap player to take the next turn.
6. Repeat steps 2–5 until there is a winner or the grid is full.
7. When a game finishes, display the message “Player 1 wins!”, “Player 2 wins!”, or “Draw game!”
accordingly.

Sample Run
The following shows a sample run. The blue text is user input and the other text is the program
printout. You can try the provided sample program for other input. Your program output should be
exactly the same as the sample program (same text, symbols, letter case, spacings, etc.). Note that
there is a space after ‘:’ in the printout.

. . . .
. . . .
. . . .
Player 1's move: S 3↵
. . S .
. . . .
. . . .
Player 2's move: O 11↵
. . S .
. . . .
. . O .
Player 1's move: A 8↵
Invalid move. Try again!
Player 1's move: s 7↵
Invalid move. Try again!
Player 1's move: O 0↵
Invalid move. Try again!
Player 1's move: S 14↵
Invalid move. Try again!
Player 1's move: S 11↵
Invalid move. Try again!
Player 1's move: S 2↵
. S S .
. . . .
. . O .
Player 2's move: O 5↵
. S S .
O . . .
. . O .
Player 1's move: O 1↵

Copyright © 2021 CSE, CUHK Page 3 of 4


CSCI1520 Computer Principles and C++ Programming, Spring 2020/21
Department of Computer Science and Engineering, The Chinese University of Hong Kong

O S S .
O . . .
. . O .
Player 2's move: S 7↵
O S S .
O . S .
. . O .
Player 1's move: S 9↵
O S S .
O . S .
S . O .
Player 2's move: O 6↵
O S S .
O O S .
S . O .
Player 2 wins!

Submission and Marking


➢ Your program file name should be sos.cpp. Submit the file in Blackboard
(https://blackboard.cuhk.edu.hk/).
➢ Insert your name, student ID, and e-mail as comments at the beginning of your source file.
➢ You can submit your assignment multiple times. Only the latest submission counts.
➢ Your program should be free of compilation errors and warnings.
➢ Your program should include suitable comments as documentation.
➢ Do NOT plagiarize. Sending your work to others is subjected to the same penalty as the copier.

Copyright © 2021 CSE, CUHK Page 4 of 4

You might also like