CSCI 1520 Computer Principles and C++ Programming: Jingsong Chen SHB 913
CSCI 1520 Computer Principles and C++ Programming: Jingsong Chen SHB 913
Jingsong Chen
SHB 913
[email protected]
1
Outline
2
Requirement
• Assignment 4: Dots and Triangles
• Deadline: 20:00, Wed 1 Apr 2020
• Requirements:
• Filename: dotstriangles.cpp;
• Insert your name, student ID, and e-mail address as comments at the beginning of
your source file;
• The output must exactly match the sample output;
• Include suitable comments as documentation;
• Free of compilation errors and warnings;
• No global variables (variables declared outside any functions);
• No functions in the <cmath> library;
• No arrays.
3
Game Description
• Start condition:
• An empty triangular grid of dots and two players.
• Game stage:
• Two players take turns adding one line between two un-joined adjacent dots
(Player 1 takes the first turn).
• A player who completes the third side of a triangle owns that triangle, earns
one point, and takes an extra turn. A player will not get a third consecutive turn
even if (s)he makes triangle(s) in the extra turn.
• End condition:
• The grid is full, and the player with more points wins. It is a draw if two players
have the same points.
4
Grid Representation
• There are 12 possible line positions and 6 possible triangles in a
triangular grid. We use a 18-digit integer (do not use array) to encode
all these things.
• Each digit is either 0 or 1, denoting whether the corresponding positions 1 to 12
are empty or filled with a line.
𝑑 1 𝑑 2 𝑑 3 … 𝑑 1 2=110001011101
5
Grid Representation
• There are 12 possible line positions and 6 possible triangles in a
triangular grid. We use a 18-digit integer (do not use array) to encode
all these things.
• Each digit is 0 or 1 or 2, where 0 means the triangle is not completed, and 1 or
2 means a completed triangle with the player who owns it.
• Digits are the upper three triangles left-to-right.
• Digits are the lower three triangles left-to-right.
14
15
13
𝑑 13 𝑑 14 𝑑 15 𝑑 1 6 𝑑 1 7 𝑑 1 8 =2 0 21 11
16
18
17 6
Grid Representation
• There are 12 possible line positions and 6 possible triangles in a
triangular grid. We use a 18-digit integer (do not use array) to encode
all these things.
• In C++, integer constants should NOT contain leading zero(s).
Example: an empty grid is not 000000000000000000, but 0.
• in C++, the data type int is typically 32-bit and thus NOT big enough to store an
18-digit integer. In your program, you have to use a bigger integer type called
long long.
Example: “long long a = 111111111111222111;”
7
Grid Representation
• Examples for grid representation:
000111111101001220
111001011101100220
111111101001220
In C++, integer constants should NOT contain leading Line positions Triangles
zero(s).
8
Program Flow
Start
No
End condition?
Yes
Print result
End 9
Provided Functions
• void printGrid(long long grid)
• Print the grid and the player’s scores to the screen using the format of
specification.
printGrid(100000001101000020);
10
Provided Functions
• bool isFilled(long long grid, int pos)
• Return true if position pos of grid is filled with a line; return false
otherwise.
• This function may be called in many places in the program flow.
If grid = 111001011101100220:
bool isFilled(long long grid, int pos)
{ isFilled(grid, 3) returns true,
grid /= 1000000; isFilled(grid, 9) returns true,
for (int i = 0; i < 12 - pos; i++)
grid /= 10; isFilled(grid, 7) returns false,
return (grid % 10 != 0); isFilled(grid, 11) returns false.
}
11
Required Functions
• void updateGrid(long long &grid, int pos, int p) – Required!
• Update the digit of the reference parameter grid to 1, modeling the
game play of Player p putting a line in position pos in grid.
• If any new triangle(s) are completed, the digit of the corresponding
triangle in grid shall be marked as Player p.
12
Required Functions
• void updateGrid(long long &gird, int pos, int p) – Required!
• Update the digit of the reference parameter grid to 1, modeling the
game play of Player p putting a line in position pos in grid.
• If any new triangle(s) are completed, the digit of the corresponding
triangle in grid shall be marked as Player p. (Use isFilled to check.)
• Note that you do NOT have to check in this function whether the
player move is valid or not. (You check validity elsewhere.)
13
Required Functions
• void updateGrid(long long &gird, int pos, int p) – Required!
1. Set the digit to 1: 2. Check if new triangle(s) completed: 3. Set the digit of the new triangle(s) to p:
long long factor = 1; bool b1 = isFilled(grid, 9); int factor = 1;
for (int i = 0; i < 18 - 10; i++) bool b2 = isFilled(grid, 12); for (int i = 0; i < 6 - 5; i++)
factor *= 10; If (b1 && b2) { factor *= 10;
grid += 1 * factor; triangle completed; grid += 2 * factor;
}
100000001001000000 b1 = isFilled(grid, 7); 100000001101000000
b2 = isFilled(grid, 11);
If (b1 && b2) {
100000001101000000 triangle completed; 100000001101000020
14
}
Required Functions
• void updateGrid(long long &gird, int pos, int p) – Required!
• A special case putting one line completes two triangles: pos = 4
1. Set the digit to 1: 2. Check if new triangle(s) completed: 3. Set the digit of the new triangle(s) to p:
long long factor = 1; bool b1 = isFilled(grid, 1); int factor = 1;
for (int i = 0; i < 18 - 4; i++) bool b2 = isFilled(grid, 3); for (int i = 0; i < 6 - 2; i++)
factor *= 10; If (b1 && b2) { factor *= 10;
grid += 1 * factor; triangle completed; grid += 1 * factor;
Repeat once by 3
}
101010100000000000 b1 = isFilled(grid, 5); 101110100000000000
b2 = isFilled(grid, 7);
If (b1 && b2) {
101110100000000000 triangle completed; 101110100000011000
15
}
Required Functions
• int playerScore(long long grid, int p) – Required!
• Return the score of Player p in grid. This is done by counting how many
triangles Player p owns in grid. Use “/” and “%” to check the last 6 digits
of grid, e.g.:
• grid = 101110100000011000;
• The digit of triangles: (grid / 10000) % 10 == 1.
16
Notes
• Do NOT modify the prototypes (name, input parameters, and return
value) of the provided and required functions.
• Do NOT modify the contents of the provided functions.
• Besides the provided and required functions, You can design extra
functions if you find necessary.
17
Modular Testing
• Instead of directly testing your whole program, please individually test
the correctness of the required functions’ implementations.
• We can use a simple main function to test one required function.
Example 1: Example 2:
… …
int main() { int main() {
long long grid = …; long long grid = …;
int pos = …; int s1 = playerScore(grid, 1);
int p = …; int s2 = playerScore(grid, 2);
updateGrid(grid, pos, cout << “Player 1’s score: ” << s1 <<
p); endl;
printGrid(grid); cout << “Player 2’s score: ” << s2 <<
return 0; endl;
} return 0;
}
18
Modular Testing
• Instead of directly testing your whole program, please individually test
the correctness of the required functions’ implementations.
• We can use a simple main function to test one required function.
• Note that the required functions will be separately graded. Only after
both required functions are well debugged, proceed to write the real
main function for the game flow.
19
Something Else in Program Flow
• Print messages in your program:
a) “Player 1’s turn (1-12): ” or “Player 2’s turn (1-12): ”
b) “Invalid move! Try again. \n”
c) “Player 1 wins!” or
“Player 2 wins!” or
“Draw game!”
a)
b)
c)
20
Something Else in Program Flow
• Check if the user input is valid. A user input is invalid if:
a) Outside the range 1–12 OR
b) The input position was already filled. (Use isFilled to check)
21
Something Else in Program Flow
• Check if the user input is valid. A user input is invalid if:
a) Outside the range 1–12 OR
b) The input position was already filled. (Use isFilled to check)
Example:
22
Something Else in Program Flow
• Check if the user input is valid. A user input is invalid if:
a) Outside the range 1–12 OR
b) The input position was already filled. (Use isFilled to check)
Example:
23
Something Else in Program Flow
• Check if keep the current player. Don’t swap the player if:
a) The current player has completed triangle(s) AND
b) This turn is NOT this player’s second consecutive turn.
Example:
24
Something Else in Program Flow
• Check if we need to end the game. Many different ways, e.g.:
a) Check if “grid / 1000000” is equal to a special number.
25
Summary
Function
Refer to the page 21-25 of the slides printGrid(params)
26
Q&A
27