You are on page 1of 42

CSCI1120

Introduction to Computing Using C++


Tutorial 9: Assignment 5

P.1
Introduction

P.2
Assignment 5: Neighbors (A new board game)
● Aim : Practice object-oriented programming!

Empty Square Black piece

8 * 8 Board
White piece

Initial game setup


P.3
Rules
● On each turn, the play can move one of their pieces in 8 directions.
○ Horizontally
○ Vertically

○ Diagonally

⚫’s turn
P.4
Rules
● On each turn, the piece can move exactly k squares.

● k is the number of adjacent neighbors.

○ k = 3 in this example.

⚫’s turn

P.5
Rules
● Move cases :
○ Case 1: The aim position is an empty square

○ Case 2: Capture an opponent’s piece

■ opponent’s piece will be removed

■ may not land on a same piece

Noted: A piece may jump over other pieces.


⚫’s turn

P.6
Rules
● Goal : Make all his/her pieces adjacent to each other vertically,
horizontally, and diagonally.

Noted:

○ One move can make your opponent win Or draw

⚫ wins!
P.7
Rules
● Declarations :

○ A player with only one piece left (due to captures) is by definition


connected.

○ 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

○ Implements the functions declared in Neighbors.h

● playgame.cpp

○ Performs the game flow.


Provided Neighbors.h

● 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 game board is represented by a two-dimensional array of char, storing either


‘B’, ‘W’, or ‘.’. board[0][0] board[0][7]

● char currentPlayer, nextPlayer;

○ The player in the current move and in the next move respectively

● int blacks, whites;

○ The total number of black and white pieces


board[7][0]
board[7][7] P.11
Public constructor and member functions:
● Neighbors();
○ Initialize the board → board
○ Initialize the number of pieces → blacks, whites
○ Initialize the play order : black first → currentPlayer,
nextPlayer;

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!

■ All the following conditions need to be met.

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.

○ Pay attention to the special situation:

■ a player has only one piece left and that piece is isolated.
P.31
Client Program (playgame.cpp)
● Performs the flow of the game.

Create a Neighbors object. Get the input

Y
Check if the current player
can make any move
Make the move
N

print some information

Swap the players.

Game over P.32


Sample Run

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/

or Online diffchecker: https://www.diffchecker.com


● Test every function carefully ! P.34
Test your code
● When you finish each function, you need to test it
separately in different cases.
● How to do it?
○ Have a simple main() to create an object and call the member
function by all kind of parameters. …

○ 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:

■ Test move function


P.37
How to test your code
● Example 3:

■ Test isIsolated function P.38


Restrictions
1. You cannot declare any global variables (i.e. variables declared outside any
functions). But global constants or arrays of constants are allowed
2. You can define extra functions in any source files if necessary. However, extra
member functions (instance methods), no matter private or public, are not allowed.
3. Your Neighbors class should not contain any cin statements. All user inputs shall be
done in the client program (playgame.cpp) only.
4. Your Neighbors class should not contain any cout statements except in the
printGame() member function (for printing the game board).
5. When the game is a draw caused by all pieces from both players having no adjacent
neighbors, there is no need to print any pass message (“X has to pass!”). The pass
message is printed only when the current player’s pieces are all isolated but the other
player’s pieces are not. P.39
Reminders
1. Your program source file names should be Neighbors.cpp and playgame.cpp. Submit
the two files in Blackboard (https://blackboard.cuhk.edu.hk/). You do not have to
submit Neighbors.h.
2. Insert your name, student ID, and e-mail as comments at the beginning of both source
files
3. Your program output should be exactly the same as the sample program (same text,
symbols, letter case, spacings, etc.).
4. Your program should be free of compilation errors and warnings
5. Your program should include suitable comments as documentation.
6. Do NOT plagiarize.

P.40
Any Questions?

P.41
Thank you for listening

P.42

You might also like