Professional Documents
Culture Documents
P.1
Introduction
P.2
Assignment 5: Neighbors (A new board game)
● Aim : Practice object-oriented programming!
8 * 8 Board
White piece
○ Diagonally
⚫’s turn
P.4
Rules
● On each turn, the piece can move exactly k squares.
○ k = 3 in this example.
⚫’s turn
P.5
Rules
● Move cases :
○ Case 1: The aim position is an empty square
P.6
Rules
● Goal : Make all his/her pieces adjacent to each other vertically,
horizontally, and diagonally.
Noted:
⚫ wins!
P.7
Rules
● Declarations :
○ A player may also have no possible moves when all of his/her pieces are
isolated with no adjacent neighbors. → pass this turn
○ When both players have no possible moves, the game is also considered
as a draw.
P.8
Class Neighbors
P.9
Tasks:
● Neighbors.cpp
● playgame.cpp
● Reminder : We shall grade your class with our main(), and grade your
main() with our class , so you should not mix the functionalities of the
two files.
P.10
Private Data Members
● char board[8][8];
○ The player in the current move and in the next move respectively
P.12
Public constructor and member functions:
● void printGame() const;
○ Prints the board in the format in the specification.
○ You can refer to the Tutorial 7 to get more details.
● char getCurrentPlayer() const;
○ Returns the current player of the game
● void swapPlayer();
○ Swaps the current and next players in the game
P.13
Public constructor and member functions:
● bool move(string from, string to);
○ From and to are two string whose format is a column letter followed by a
row number.
○ This function will check whether the from and to positions form a valid
move.
■ How to check ? Difficult!
P.14
Public constructor and member functions:
● bool move(string from, string to);
○ Conditions
■ The strings are in the right format: lowercase column letters and the
row number is in the right range.
■ The from position must have the piece of the current player.
■ The to position is either an empty square or an opponent’s piece.
P.15
Public constructor and member functions:
● bool move(string from, string to);
○ Conditions
■ The move length is exactly the number of adjacent neighbors.
■ Hint: you can use two arrays to get the position of adjacent neighbors.
● const int x = {-1, -1, -1, 0, 0, 1, 1, 1}
● const int y = {-1, 0, 1, -1, 1, -1, 0, 1}
● Neighbor_x = from_x + x[i], Neighbor_ x = from_y + y[i]
■ Don’t forget to check the boundary.
P.16
Public constructor and member functions:
● bool move(string from, string to);
○ Conditions
■ The move is in the 8 directions.
■ Hint: check the difference of columns and numbers between from and
to
■ For example,
● If |from_x – to_x | == 0 or |from_y – to_y | == 0 → Horizontal and
vertical. Like a4->b4, a4->a7
● |from_x – to_x | == |from_y – to_y | → diagonal. Like a3 -> c5, |'c' -
'a'| == 2 and |5 - 3| == 2 P.17
Public constructor and member functions:
● bool isIsolated(char p) const;
○ Returns true if player p has all his/her pieces isolated with no adjacent
neighbors in the 8 directions.
○ Helps you to know the play p can make a move or skip his/her turn this
time.
○ How to check?
■ Hint: Find all the pieces of player p and check its neighbors one by
one.
P.18
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Returns true if player p has all his/her pieces connected with each other.
○ How to check ?
○ Hint: (Of course you can solve it by your own solution)
■ Step 1: Array A is all the pieces of player p and Array B is an empty
array. Choose one piece (think?) from Array A and move it into Array
B
■ Step 2: If there exist the piece(s) in array A which is adjacent to any
piece(s) in array B. move it into array B.
■ Repeat Step 2 until it cannot make any movement.
P.19
■
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1:
■ Array A = {a4, b5, c1, c4, d2, d3, d4, e1, e5. e6. f7. g7}
■ Array B = {}
■ Move a4 from Array A to Array B
● A = {b5, c1, c4, d2, d3, d4, e1, e5. e6. f7. g7}
● B = {a4}
⚫ wins!
P.20
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1: First round
■ b5 in array A is the adjacent neighbor of a4 in B
■ Move b5 from array A to array B
● A = {b5, c1, c4, d2, d3, d4, e1, e5. e6. f7. g7}
● B = {a4, b5}
⚫ wins!
P.21
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1: Second round
■ c4 in array A is the adjacent neighbor of b5 in B
■ Move c4 from array A to array B
● A = {c1, d2, d3, d4, e1, e5. e6. f7. g7}
● B = {a4, b5, c4}
⚫ wins!
P.22
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1: Third round
■ d3, d4 in array A are the adjacent neighbor of c4 in B
■ Move d3, d4 from array A to array B
● A = {c1, d2, e1, e5. e6. f7. g7}
● B = {a4, b5, c4, d3, d4}
⚫ wins!
P.23
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1: Fourth round
■ d2 in array A is the adjacent neighbor of d3 in B
■ e5 in array A is the adjacent neighbor of d4 in B
■ Move d2, e5 from array A to array B
● A = {c1, e1, e6, f7, g7}
● B = {a4, b5, c4, d3, d4, d2, e5}
⚫ wins!
P.24
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1: Fifth round
■ c1, e1 in array A is the adjacent neighbor of d2 in B
■ e6 in array A is the adjacent neighbor of e5 in B
■ Move c1, e1, e6 from array A to array B
● A = {f7, g7}
● B = {a4, b5, c4, d3, d4, d2, e5, c1, e1, e6}
⚫ wins!
P.25
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1: Sixth round
■ f7 in array A is the adjacent neighbor of e6 in B
■ Move f7 from array A to array B
● A = {g7}
● B = {a4, b5, c4, d3, d4, d2, e5, c1, e1, e6, f7}
⚫ wins!
P.26
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 1: Seventh round
■ g7 in array A is the adjacent neighbor of f7 in B
■ Move g7 from array A to array B
● A = {}
● B = {a4, b5, c4, d3, d4, d2, e5, c1, e1, e6, f7, g7}
■ Array A is empty now. Returns true!
⚫ wins!
P.27
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 2(take b5 away):
■ Array A = {a4, c1, c4, d2, d3, d4, e1, e5, e6, f7, g7}
■ Array B = {}
■ Move a4 from Array A to Array B
● A = {c1, c4, d2, d3, d4, e1, e5, e6, f7, g7}
● B = {a4}
⚫ wins!
P.28
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Example 2:
■ We can’t find any piece(s) in array A which is the neighbor of a4 in
array B
■ The algorithm ends.
■ Array A is not empty, returns false.
P.29
Public constructor and member functions:
● bool hasConnected(char p) const;
○ Coding hints:
P.30
Public constructor and member functions:
● char gameOver() const;
○ Checks if the game is over.
■ a player has only one piece left and that piece is isolated.
P.31
Client Program (playgame.cpp)
● Performs the flow of the game.
Y
Check if the current player
can make any move
Make the move
N
P.33
Advice
● How to create Multi-File Programs With Visual Studio
○ http://icarus.cs.weber.edu/~dab/cs1410/textbook/5.Structures/
multifile.html
● Useful tools for format checking
…
○ diff command on linux:
https://www.geeksforgeeks.org/diff-command-linux-example
s/
○ Don’t forget to delete all the test code when you submit
your assignment!
P.35
How to test your code
● Example 1:
● Output:
P.36
How to test your code
● Example 2:
P.40
Any Questions?
P.41
Thank you for listening
P.42