首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
代做C/C++编程|代做Web开发|代做Processing|代做 Statistics统计、回归、
项目预算:
开发周期:
发布时间:
要求地区:
Due: 23:59, Sat 9 Dec 2023 File names: RushHour.cpp Full marks: 100 playgame.cpp
Introduction
The objective of this assignment is to practice object-oriented programming. You will write a class and a client program to play a sliding block puzzle game called Rush Houra.
The game is played on a grid of size 8 × 8 with at most 10 cars on it. The cars are aligned either vertically or horizontally in the grid and occupy two or three tiles. There is an exit hole on the right side of the grid. The goal of the puzzle is to move the cars forward or backward (but not change direction) so that a designated car (called Car 0) moves to the exit hole. Figure 1(a) shows an example puzzle configuration, in which Cars 0, 1, 6, and 7 are horizontal and can move left or right, and Cars 2, 3, 4, and 5 are vertical and can move up or down. Moving Car 1 to the right by one tile yields the state in Figure 1(b). Continuing to move the cars carefully, you can yield the solved state as in Figure 1(c) where Car 0 reaches the exit hole on the right.
########
#11...2#
#3..4.2#
#3004.2.
#3..4..#
#5...66#
#5.777.#
########
(a) Initial
(b) Move Car 1
(c) Solved
→
→...→
########
#.11..2#
#3..4.2#
#3004.2.
#3..4..#
#5...66#
#5.777.#
########
Figure 1: An Example Rush Hour Configuration and its Solved State
Program Specification
You shall write your program in two source files RushHour.cpp and playgame.cpp. The former is the implementation of the class RushHour, while the latter is a client program of class RushHour which performs the program flow. You are recommended to finish the RushHour class first before writing the client program. When you write the RushHour 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 RushHour (RushHour.cpp)
You are given the interface of the RushHour class in the header file RushHour.h. You shall not modify the contents of this header file. Descriptions of the class are given below.
########
#311...#
#3.....#
#3....00
#5..4.2#
#5664.2#
#7774.2#
########
a You may play Rush Hour online at: https://www.mahjongfun.com/rush-hour/
Page 1 of 7
class RushHour {
public:
RushHour(const string g[]);
bool locateCar(int car, int &row, int &col);
int moveCar(int car, int step);
bool isSolved();
int getTotalSteps();
void print();
private:
string grid[8];
int totalSteps;
};
Private Data Members
string grid[8];
The Rush Hour puzzle is represented by an array of string. Each element grid[𝑖] stores the contents in row 𝑖 of the grid. E.g., for the configuration in Figure 1(a), grid[0] is “########”, grid[1] is “#11...2#”, grid[2] is “#3..4.2#”, etc. All characters in the strings are either ‘#’ (border), ‘.’ (empty), or digits ‘0’–‘9’ (cars). All the strings are of length 8.
int totalSteps;
The total number of steps that a player has moved during puzzle play. Note that moving a car by, say, two tiles, is counted as two steps.
Public Constructor and Member Functions
RushHour(const string g[]);
This constructor initializes the Rush Hour puzzle using the array parameter g, which contains contents for row 1 to row 6 of the grid. (That is, g[0] is used to initialize grid[1]; g[1] is used to initialize grid[2]; ...; g[5] is used to initialize grid[6].) Note that row 0 and row 7 are not needed in the parameter because the two rows always contain only the ‘#’ symbol. You just have to initialize grid[0] and grid[7] as “########”. E.g., suppose g is {"#11...2#", "#3..4.2#", "#3004.2.", "#3..4..#", "#5...66#", "#5.777.#"}. Then grid shall be initialized such that grid[0]...grid[7] become “########”, “#11...2#”, “#3..4.2#”, “#3004.2.”, “#3..4..#”, “#5...66#”, “#5.777.#”, and “########” respectively (that is, the configuration in Figure 1(a)).
You can assume that:
➢ Array parameter g is always of size 6;
➢ Each string g[0], ..., g[5] is always of length 8, and always contains eight symbols of either ‘#’
(border), ‘.’ (empty), or digits ‘0’–‘9’ (cars);
➢ The strings g[0], ..., g[5] always start and end with the border ‘#’. An exception is on g[2] (for
grid[3]), where its last character is always the exit hole (‘.’).
➢ The cars always occupy either two or three tiles, and are always properly aligned vertically or
horizontally.
Page 2 of 7
You do not have to handle situations where the parameter g does not conform to these assumptions. Remember to also initialize the data member totalSteps as 0.
bool locateCar(int car, int &row, int &col);
Finds the position of the parameter car in the puzzle. The position of a vertical car is its topmost tile. The position of a horizontal car is its leftmost tile. The row and column indices of the found position are assigned to the reference parameters row and col respectively, and the member function shall return true. E.g., locating Car 2 in Figure 1(a) shall write 1 to row and 6 to col and return true, because Car 2 appears at row 1, column 6. In case car cannot be located in the grid (e.g., there is no Car 8 in Figure 1(a)), the member function shall not update row and col and return false.
int moveCar(int car, int step);
Performs the action of moving the parameter car by step tiles. A positive value for step means moving down (for vertical car) or right (for horizontal car); a negative value means moving up (for vertical car) or left (for horizontal car). E.g., in Figure 1(a), moving Car 6 left by three tiles has a step of -3. A move is valid only if all the followings are satisfied:
➢ The parameter car exists in the grid.
➢ The parameter step is non-zero. (Moving a car by 0 tile is meaningless.)
➢ There is enough space to allow the car to move by step tiles, without hitting other cars or the
border, or going beyond the exit hole. (E.g., in Figure 1(a), Car 6 cannot move left by four tiles (-4 steps), as it would hit Car 5.)
If the move is valid, then data members grid and totalSteps shall be updated accordingly. Otherwise, no updates to grid and totalSteps are needed. This member function shall return either 0, 1, or 2, according to the following conditions:
Condition
Move is valid
car does not exist in the grid or step is zero
Hit other cars or border or go beyond the exit hole
Return value
0 1 2
Implementation hint: First, find the position of car (with the help of locateCar()). Then determine which one of the four cases the prospective move is: move left/right/up/down. Implement the four cases one by one. For each case, check whether the move is valid or not. If valid, update the car to the new position (by updating the grid contents).
bool isSolved();
Returns true if Car 0 touches the exit hole; returns false otherwise.
int getTotalSteps();
Returns the total number of steps that a player has moved the cars. That is, the value of the data member totalSteps.
void print();
Prints out the Rush Hour puzzle and the number of steps that the player has used. The following is an example output of print().
Page 3 of 7
########
#311..2#
#3....2#
#300..2.
#5..4..#
#5664..#
#7774..#
########
Steps: 10
Client Program (playgame.cpp)
Your main program is a client of the RushHour class. You create a RushHour object here and call its member functions to implement the following program flow.
1. The program starts with prompting the user to enter six strings (separated by spaces), which are the initial grid contents for row 1 to row 6 of the puzzle. Then create a RushHour object using the user input. You can assume that the six strings follow the same assumptions stated in the constructor above).
2. Prompt the user to move a car. You can assume that the user always enters two integers, denoting the car and the steps of the prospective move respectively. (Positive step means moving down/right; negative step means moving up/left.)
3. In case the input is not a valid move (see definition in the moveCar() member function above), the program prints a warning message and goes back to Step 2. Otherwise, move the car accordingly.
Note: there are two kinds of warning messages. When the car does not exist or the step is zero, the message “Invalid car or step! Try again.” shall be printed. When the move would hit other cars or border or go beyond the exit hole, the message “Hit! Try again.” shall be printed.
4. If the puzzle is not yet solved after the move and the total number of steps is smaller than 100, go back to Step 2.
5. Finally, print the puzzle and either the winning message “Congrats! You finished in 𝑋 steps.” (where 𝑋 is the number of steps used) or the losing message “Oops! You could not solve in 100 steps.”
Some Points to Note
➢ You cannot declare any global variables in all your source files (except const ones).
➢ Remember to #include "RushHour.h" in both RushHour.cpp and playgame.cpp.
➢ You can write extra functions in any source files if necessary. However, extra member functions
(instance methods), no matter private or public, are not allowed.
➢ Your RushHour class shall not contain any cin statements. All user inputs shall be done in the
client program (playgame.cpp) only.
➢ The RushHour class shall not contain any cout statements except in the print() member
function for printing the puzzle.
Page 4 of 7
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. More sample runs and initial grids are available in Blackboard.
Enter initial grid: #11...2# #3..4.2# #3004.2. #3..4..# #5...66# #5.777.# ########
#11...2#
#3..4.2#
#3004.2. #3..4..# #5...66# #5.777.# ########
Steps: 0
Move a car: 1 4 Hit! Try again. Move a car: 1 1 ######## #.11..2# #3..4.2# #3004.2. #3..4..# #5...66# #5.777.# ########
Steps: 1
Move a car: 3 0
Invalid car or step! Try again. Move a car: 3 -2
Hit! Try again.
Move a car: 8 1
Invalid car or step! Try again. Move a car: 3 -1
########
#311..2#
#3..4.2#
#3004.2.
#...4..#
#5...66#
#5.777.#
########
Steps: 2
Move a car: 5 -1
Page 5 of 7
########
#311..2#
#3..4.2#
#3004.2.
#5..4..#
#5...66#
#..777.#
########
Steps: 3
Move a car: 7 -2 ########
#311..2#
#3..4.2#
#3004.2.
#5..4..#
#5...66#
#777...#
########
Steps: 5
Move a car: 6 -3 ########
#311..2# #3..4.2# #3004.2. #5..4..# #566...# #777...# ########
Steps: 8
Move a car: 4 2 ######## #311..2# #3....2# #300..2. #5..4..# #5664..# #7774..# ########
Steps: 10
Move a car: 2 3 ######## #311...# #3.....# #300.... #5..4.2# #5664.2# #7774.2# ########
Steps: 13
Move a car: 0 5 Hit! Try again. Move a car: 0 4
Page 6 of 7
########
#311...#
#3.....#
#3....00
#5..4.2#
#5664.2#
#7774.2#
########
Steps: 17
Congrats! You finished in 17 steps.
Submission and Marking
➢ Your program file names should be RushHour.cpp and playgame.cpp. Submit the two files in Blackboard (https://blackboard.cuhk.edu.hk/). You do not have to submit RushHour.h.
➢ Insert your name, student ID, and e-mail as comments at the beginning of all your files. ➢Besides the above information, your program should include suitable comments as
documentation in all your files.
➢ You can submit your assignment multiple times. Only the latest submission counts.
➢ Your program should be free of compilation errors and warnings.
➢ Do NOT share your work to others and do NOT plagiarize. Those who shared their work and/or
plagiarized others work shall be penalized.
Page 7 of 7
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代做ceng0013 design of a pro...
2024-11-13
代做mech4880 refrigeration a...
2024-11-13
代做mcd1350: media studies a...
2024-11-13
代写fint b338f (autumn 2024)...
2024-11-13
代做engd3000 design of tunab...
2024-11-13
代做n1611 financial economet...
2024-11-13
代做econ 2331: economic and ...
2024-11-13
代做cs770/870 assignment 8代...
2024-11-13
代写amath 481/581 autumn qua...
2024-11-13
代做ccc8013 the process of s...
2024-11-13
代写csit040 – modern comput...
2024-11-13
代写econ 2070: introduc2on t...
2024-11-13
代写cct260, project 2 person...
2024-11-13
热点标签
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
软件定制开发网!