首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
讲解数据库SQL|辅导Python编程|辅导Python编程|解析SPSS
项目预算:
开发周期:
发布时间:
要求地区:
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
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代写math 1151, autumn 2024 w...
2024-11-14
代做comp4336/9336 mobile dat...
2024-11-14
代做eesa01 lab 2: weather an...
2024-11-14
代写comp1521 - 24t3 assignme...
2024-11-14
代写nbs8020 - dissertation s...
2024-11-14
代做fin b377f technical anal...
2024-11-14
代做ceic6714 mini design pro...
2024-11-14
代做introduction to computer...
2024-11-14
代做cs 353, fall 2024 introd...
2024-11-14
代做phy254 problem set #3 fa...
2024-11-14
代写n1569 financial risk man...
2024-11-14
代写csci-ua.0202 lab 3: enco...
2024-11-14
代写econ2226: chinese econom...
2024-11-14
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!