联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-11-21 10:10

Assignment 5: Neighbors

Due: 20:00, Mon 23 Nov 2020 File names: Neighbors.cpp

playgame.cpp

Full marks: 100

Introduction

The objective of this assignment is to practice object-oriented programming. You will implement a

board game called Neighbors, which is played on an 8 × 8 board by two players Black and White.

The initial game setup is shown in Figure 1(a). The symbols ‘B’, ‘W’, and ‘.’ denote black piece, white

piece, and empty square respectively. The rows and columns are numbers 0–7 and lowercase letters

a–h respectively. (Lowercase is used to avoid mixing up with the pieces B/W.)

Two players take turns to move one of their pieces horizontally ?, vertically ?, or diagonally ??.

The piece moves exactly ?? squares, where ?? is the number of adjacent neighbors (eight directions)

of the moving piece. E.g., the B in position d0 in Figure 1(a) may move two squares to b2, d2, or f2,

because it has two neighbors (one B to its left at c0 and one W its right at e0). A piece may jump over

other pieces (Figure 1(b)). A piece may land on and capture an opponent’s piece, which will then be

removed from the board (Figure 1(c)). But a piece may not land on a piece of the same player. The

goal of a player is to make all his/her pieces adjacent to each other vertically, horizontally, and

diagonally (Figure 1(d)).

(a)

a b c d e f g h

0 . B B B W W W .

1 W . . . . . . B

2 W . . . . . . B

3 W . . . . . . B

4 B . . . . . . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

(b)

a b c d e f g h

0 . B . B W W W .

1 W . . . . B . .

2 . . W . B . . B

3 W . . . . . . B

4 B . . . . . . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

(c)

a b c d e f g h

0 . B . B . W W .

1 W . . . . . . B

2 . . B . . . . B

3 W W . . . . . B

4 B . W . . . . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

(d)

a b c d e f g h

0 . . . . . . . .

1 . . . . W . W .

2 . . . . W W . W

3 . . . . . . . .

4 W . W B . W B W

5 . W B . B B B B

6 B B W . . . . .

7 . . . . . . . .

Figure 1: (a) Initial setup, (b) Jump over, (c) Capture, and (d) Black wins

A player’s move may result in the opponent forming all adjacent pieces due to capture. This commits

suicide and the opponent wins. A move may also result in both players forming all adjacent pieces.

Such simultaneous connection is considered as a draw. 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. When both players have no possible moves, the

game is also considered as a draw.

○ ○

W

/

PDFM PDF EDITOR

Program Specification

You have to write your program in two source files Neighbors.cpp and playgame.cpp. The

former is the implementation of the class Neighbors, while the latter is a client program of class

Neighbors which performs the game flow. You are recommended to finish the Neighbors class

first before writing the client program. When you write the Neighbors class, implement the

member functions and test them individually one by one. Your two files will be graded separately, so

you should not mix the functionalities of the two files.

Class Neighbors (Neighbors.cpp)

You are given the interface of the Neighbors class in the header file Neighbors.h. You shall not

modify the contents of this header file. Descriptions of its members are given below.

class Neighbors {

public:

Neighbors();

void printGame() const;

char getCurrentPlayer() const;

void swapPlayer();

bool move(string from, string to);

bool isIsolated(char p) const;

bool hasConnected(char p) const;

char gameOver() const;

private:

char board[8][8];

char currentPlayer, nextPlayer;

int blacks, whites;

};

Private Data Members

char board[8][8];

The game board is represented by a two-dimensional array of char, storing either ‘B’, ‘W’, or ‘.’. The

elements board[0][0], board[0][7], board[7][0], and board[7][7] are the squares a0, h0,

a7, and h7 respectively.

char currentPlayer, nextPlayer;

The player in the current move and in the next move respectively. They should be either ‘B’ or ‘W’.

int blacks, whites;

The total number of black and white pieces on the board respectively.

Public Constructor and Member Functions

Neighbors();

This constructor creates a game object and initialize it to the setup in Figure 1(a). Black starts playing

PDFM PDF EDITOR

first. (So White is the next player.) There are 12 black and white pieces each on the board initially.

void printGame() const;

Prints out the game board in the format in Figure 1.

char getCurrentPlayer() const;

Returns the current player of the game, i.e., the value of the data member currentPlayer.

void swapPlayer();

Swaps the current and next players in the game. This is for changing turns during the game play.

bool move(string from, string to);

Carries out the current player’s move from the source position from to the landing position to. The

parameters from and to are strings whose format is a column letter followed by a row number, e.g.,

“a1”, “c7”, and “d4”. The member function shall check whether the from and to positions form a

valid move. A move is valid if all the following conditions are satisfied:

? The parameters from and to are valid board positions. (Only lowercase letters can be valid.)

? The from position contains a piece of the current player.

? The move is either horizontal, vertical, or diagonal.

? The move is exactly as many squares as there are adjacent neighbors of the moving piece.

? The landing position to is either an empty square or an opponent’s piece.

When the move is valid, the array board shall be updated to reflect the result of the move, and the

data members blacks or whites shall be updated if it is a capture. When the move is invalid, no

members of the object shall be updated. The member function returns true if the move is valid; and

false otherwise.

Warning: this member function is difficult to implement!

bool isIsolated(char p) const;

This member function returns true if player p has all his/her pieces isolated with no adjacent

neighbors in the eight directions; and false otherwise.

bool hasConnected(char p) const;

This member function returns true if player p has all his/her pieces adjacent to each other vertically,

horizontally, and diagonally; and false otherwise.

Warning: this is really difficult to implement!

char gameOver() const;

This member function checks if the game is over. It returns either ‘B’, ‘W’, ‘D’, ‘I’, or ‘-’, to mean the

following:

Return value Meaning

‘B’ Black wins. (Black pieces are all adjacent but white pieces are not.)

‘W’ White wins. (White pieces are all adjacent but black pieces are not.)

‘D’ Draw game caused by black pieces being all adjacent and white pieces also being

all adjacent.

‘I’ Also draw game, but caused by all pieces from both players having no adjacent

neighbors.

PDFM PDF EDITOR ‘-’ None of the above. (Game is not yet over.)

Note that when a player has only one piece left and that piece is isolated, we count this situation as

either ‘B’, ‘W’, or ‘D’ (depending on the situation of the other player). We would not count as ‘I’ even

the other player’s pieces are also isolated. As an extreme example, if the board has only one black

piece and one white piece, and the two pieces are isolated, then this member function should return

‘D’ but not ‘I’.

This member function can be written with the help of calling hasConnected() and

isIsolated().

Client Program (playgame.cpp)

Your main program is a client of the Neighbors class; it performs the flow of the game.

1. Create a Neighbors object.

2. If the player’s pieces are not all isolated with no adjacent neighbors, prompt the player to make

a move. The input consists of the source and landing positions, each of which is a character

followed by an integer. E.g., a1 c3. (Hint: You can use cin >> … >> …; to read in two strings.)

3. Make the player move. When the move is invalid, warn the player and prompt the same player

to enter again until a valid move is entered.

4. If all the player’s pieces have no adjacent neighbors, print the message “X has to pass!” instead,

where X is either B or W.

5. Swap the players.

6. If the game is not over, go back to step 2.

7. When the game is over, print the messages “B wins!”, “W wins!”, or “Draw game!” accordingly.

Some Points to Note

? You cannot declare any global variables in all your source files (except const ones).

? 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.

? Your Neighbors class should not contain any cin statements. All user inputs shall be done in

the client program (playgame.cpp) only.

? Your Neighbors class should not contain any cout statements except in the printGame()

member function (for printing the game board).

? 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.

Sample Run

In the following sample run, the blue text is user input and the other text is the program output. 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 the ‘:’ in the program printout.

PDFM PDF EDITOR

a b c d e f g h

0 . B B B W W W .

1 W . . . . . . B

2 W . . . . . . B

3 W . . . . . . B

4 B . . . . . . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

B's move: d0 f2?

a b c d e f g h

0 . B B . W W W .

1 W . . . . . . B

2 W . . . . B . B

3 W . . . . . . B

4 B . . . . . . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

W's move: E0 f1?

Invalid move. Try again!

W's move: k0 g1?

Invalid move. Try again!

W's move: d0 d8?

Invalid move. Try again!

W's move: f0 e1?

Invalid move. Try again!

W's move: f0 e2?

Invalid move. Try again!

W's move: g0 e0?

Invalid move. Try again!

W's move: e0 f1?

a b c d e f g h

0 . B B . . W W .

1 W . . . . W . B

2 W . . . . B . B

3 W . . . . . . B

4 B . . . . . . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

B's move: a4 a2?

PDFM PDF EDITOR

a b c d e f g h

0 . B B . . W W .

1 W . . . . W . B

2 B . . . . B . B

3 W . . . . . . B

4 . . . . . . . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

W's move: f1 f4?

a b c d e f g h

0 . B B . . W W .

1 W . . . . . . B

2 B . . . . B . B

3 W . . . . . . B

4 . . . . . W . W

5 B . . . . . . W

6 B . . . . . . W

7 . W W W B B B .

? (Many moves skipped. See Blackboard for full version.)

a b c d e f g h

0 . . . B . . W .

1 . . . B . W . .

2 . . . . . B . .

3 . W W . . B B .

4 . . . . . B . W

5 W . . B B . . .

6 . W . . . B . W

7 . . . W W . . .

B's move: d0 e1?

a b c d e f g h

0 . . . . . . W .

1 . . . B B W . .

2 . . . . . B . .

3 . W W . . B B .

4 . . . . . B . W

5 W . . B B . . .

6 . W . . . B . W

7 . . . W W . . .

B wins!

PDFM PDF EDITOR


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp