” CSCI1120课程 写作、C++编程实验调试CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 1 of 12Project: ChessDue: 23:59, Sun 13 Dec 2020 Full marks: 100IntroductionThe objective of this project is to let you apply the OOP concepts learned, including inheritance andpolymorphism, to programming through developing a game application Chess in C++.Chess is a two-player strategy board Game played on a checkered board with 64 cells arranged in an88 grid. The initial board configuration is shown in Figure 1. In a text console environment, we usethe letters listed in Table 1 to denote the six types of chess pieces on the board. To denote a positionon the board, we use a Coordinate system similar to a spreadsheet program, e.g. A2 refers to thecell in the first column and The second row. Suppose that a C++ array board is used to represent thecheckerboard, cell address A2 will be mapped to board[1][0]. The original chess game has manycomplicated rules. We will simplify the game by relaxing some rules in this project. In our modifiedversion of chess, Each piece is assigned a specific score as shown in Table 1. The scores will be usefulfor determining the player who wins the game in Some cases.Figure 1: Initial configuration of the game boardTable 1: Chess piecesPiece Black Symbol Black Label White Symbol White Label Count ScoreKing K k 1 1000Queen Q q 1 50Bishop B b 2 20Knight N n 2 10Rook R r 2 5Pawn P p 8 1CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 2 of 12The two players, namely Black and White make moves in turns. In convention, White plays thefirst move. Each type of piece has its own way of moving. In Figure 2, the white dots mark the cellsto which the piece can Move into if there are no intervening piece(s) of either color (except theknight, which leaps over any intervening pieces).Figure 2: Valid positions for the moves of each kind of game pieceSummary of the rules of moving each piece: The king moves one cell in any of the eight directions as long as no piece of its own color occupiesthe target cell. If the target cell is occupied by an opponent piece, the king can capture it. A rook can move any number of cells along a row (rank) or column (file) but cannot leap overother pieces. A bishop can move any number of cells diagonally but cannot leap over other pieces. The queen combines the power of a rook and bishop and can move any number of squares alonga row, column or diagonal, but cannot leap over other pieces. A knight moves to any of the closest cells that are not on the same row, column or diagonal. Thus,the move forms an L-shape: two cells vertically and one cell horizontally, or two cells horizontallyand one cell vertically. The knight is the only piece that can leap over other pieces. A pawn can move forward to the unoccupied cell immediately in front of it on the same column,or on its first move it can advance two cells along the same column, provided both cells areunoccupied (white dots in the diagram); or the pawn can capture an opponents piece on celldiagonally in front of it on an adjacent column, by moving to that cell (black xs).CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 3 of 12For simplicity, special moves known as (1) castling, (2) en passant, and (3) promotion of a pawn(when it advances to the eighth rank) will also be skipped in this project. Please make sure not toimplement these to let Our grading be consistent.The objective of the game is to checkmate the opponents king by placing it under an inescapablethreat of capture. To achieve that, a players pieces are used to attack and capture the opponentspieces, while supporting or protecting one another.Game-over conditions:The game is over when either of the kings is captured by an opponents piece. Lets say Blacks kingis captured successfully by a white piece, then the White player wins. It is possible for the game todraw. In the original chess design, there are complicated rules about how to arrive at a draw gameincluding stalemate, threefold repetition rule, fifty-move rule, mutual agreement and impossibility ofcheckmate. Again, we will skip them all and use a custom way to define a draw game as follows.We extend the original chess game with two features: per-piece score and an ad hoc way to stop thegame. Each piece is assigned a specific score as shown in Table 1. For example, the queen is muchmore worthy than other pieces but incomparable with the king losing the king (worth 1000 points)will surely lead to losing the game. During a game run, either player can enter a special imaginarymove, namely Z0 Z0 (from cell Z0 to cell Z0 which are both invalid positions). The program willdetect this input to stop the game. While both kings are not yet captured, the player with a highertotal score of his/her pieces that are Still alive (not yet captured) will win the game. If both playershave the same score, the game will draw. This ad hoc halt mimics the draw by agreement (when thegame becomes too long and boring) although our version of handling makes poor sense in that thestop is triggered by a single Player alone rather than having mutual agreement.Program SpecificationYou have to write the program composed of a total of 25 files (C++ header and source files) listed inthe following tables. We have provided a zip file of starter source code to assist your design of thissizeable program. Basically, you need not design but implement the game.Header files for class definitions:File Description Remarks1 game.h The Game class interface Provided2 board.h The Board class interface3 piece.h The Piece class interface (an abstract base class)4 player.h The Player class interface (an abstract base class)5 human.h The Human class interface (inheriting the Player class)6 machine.h The Machine class interface (inheriting the Player class)7 king.h The King class interface (inheriting the Piece class)8 queen.h The Queen class interface (inheriting the Piece class) To be created9 bishop.h The Bishop class interface (inheriting the Piece class)10 knight.h The Knight class interface (inheriting the Piece class)11 rook.h The Rook class interface (inheriting the Piece class)12 pawn.h The Pawn Class interface (inheriting the Piece class) ProvidedCSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 4 of 12Source files for client code and class implementations:File Description Remarks13 main.cpp The client program To fill in TODO parts14 game.cpp The Game class implementation15 board.cpp The Board class implementation16 piece.cpp The Piece class implementation17 player.cpp The Player class implementation18 human.cpp The Human class implementation19 machine.cpp The Machine class implementation20 king.cpp The King class implementation Provided21 queen.cpp The Queen class implementation To be created22 bishop.cpp The Bishop class implementation23 knight.cpp The Knight class implementation24 rook.cpp The Rook class implementation25 pawn.cpp The Pawn class implementation ProvidedNote the Remarks column of the above tables:(1) Provided: all these source files need no changes and should be kept unmodified.(2) To fill in TODO parts: in these files, some code segments are missing. Please look for all TODOcomments in them for instructions about what to do and fill in the missing code to finish theimplementation of each class.(3) To be created: such files are not provided and are to be created by you.Game FlowThe main() function in main.cpp creates a Game object and calls its run() method to start a chessgame. The Game() constructor accepts a game mode parameter which can be one of the following: Value 1 means Human vs. Human, i.e. two human players enter moves via console; Value 2 means Human vs. Machine, i.e. one human player enters moves via console and theopponent player is the computer (which makes random moves on the game board); Value 3 means Machine vs. Machine, two computer players make random moves in turnswithout human attention.(The Mode enum in game.h has defined these three constant values.)When the Game object is being created, the constructor will create two Player objects of type(Human / Machine) according to the above mode setting, set game state as RUNNING (0) and createthe Board object. The Board constructor will call Board::configure() method to set up the initialgame board by filling the cells array With pointers to King, Queen, Bishop, objects that are beingcreated in the method. The created pieces will be added to each players list of pieces as well.The Game objects run() is the main loop of the program, which repeats calling the current playersmakeMove(board) function and printing the board. The current player is flipped in each iteration.For a Human player, its makeMove() will prompt the user for two cell addresses, e.g. A7 B8, with theformer denoting the source cell and the latter denoting the destination cell on the board. These textbasedcell addresses will be translated to array indexes to access the Board objects cells array.Reminder: As mentioned earlier, if a human player enters Z0 Z0 as the input for his/her turn, thissignals that the Player Wants to stop the game and print the final message about who wins.CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 5 of 12For a Machine player, its makeMove() will make a random but valid move. To do so, first, pick a piecerandomly from the players pieces vector and get its position (y1, x1). (This part has been done foryou in the provided Source machine.cpp). Then, generate a random target position (y2, x2) and trymoving the piece from (y1, x1) to (y2, x2). If the move is invalid, repeat these steps until a valid moveis resulted.Class HierarchyThe basic structure of this complicated program can be broken down as follows.Abstract base classesThere are two abstract base classes in this program:Class Piece (piece.h and piece.cpp) It serves as the base or parent class of all game pieces like King, Queen, etc. Each Piece object has a label (K for King, Q for Queen, etc.), color (BLACK or WHITE), score, andposition (y, x) on the board, and a back link (a pointer) to the Board object. (Through that backlink, the Piece object can reach and call methods of Board and Game when necessary.) It has a move(int y, int x) function for making a move. (We have provided the basic actionsneeded by a move, e.g. capturing the opponent piece if the target position has been occupied byan opponent piece.) It has a pure virtual function of signature isMoveValid(int y, int x).Class Player (player.h and player.cpp) It serves as the base or parent class of Human and Machine classes. Each Player object Has a name (in string, Black or White) and a color (in Color enum, BLACK orWHITE). (note: Color enum is defined in piece.h.) It has a pure virtual function of signature makeMove(Board* board). This method must beimplemented by the concrete classes Human and Machine. It keeps a list of pieces that are still alive and on the game board. (The list is implemented using avector storing Pointers to the Piece objects.) It provides methods to retrieve the count of pieces in the list, get a piece from the list, add apiece to or delete a piece from the list.Concrete classesSubclasses of PieceWe have provided the following two classes that inherit from Piece as examples of how to completethe implementation of a working game piece: Class King (king.h and king.cpp) Class Pawn (pawn.h and pawn.cpp)They have implemented their own isMoveValid(int y, int x). In particular, the Pawn class has aspecial need to override the move(int y, int x) function to reduce the steps it can move from 2to 1 after making the first move (this is a chess game rule).CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 6 of 12One of your main tasks is to add the following subclasses that inherit from Piece, each of which mustimplement the Pure virtual function isMoveValid(int y, int x) according to the rules governingthe moves of each piece type. Follow our provided examples (King and Pawn classes) and rememberto add include guard compiler directives in all header files. Class Queen (queen.h and queen.cpp) Class Bishop (bishop.h and bishop.cpp) Class Knight (knight.h and knight.cpp) Class Rook (rook.h and rook.cpp)Subclasses of Player Class Human (human.h and machine.cpp)o It implements makeMove(Board* board) to repeatedly prompt the current human player toenter two cell addresses of a move until a valid move is obtained. The ad hoc move Z0 Z0 isdetected here and if received, the game state is set to STOPPED and the method returns atonce. In the loop body, it calls the move() method of the board object, which will validatethe move and update the board if the move is confirmed valid. Class Machine (machine.h and machine.cpp)o It implements makeMove(Board* board) to make a random valid move on the board by anested loop. First, pick a piece randomly from the players pieces vector and get its position(y1, x1). Then generate a random position (y2, x2) and try moving the piece from (y1, x1) to(y2, x2). If the trial move is found invalid, repeat the second step until reaching a valid moveor a maximum bound of trials (preset as 4096). If it happens that a valid move cannot befound within the Maximum trials bound, pick a piece from the players pieces list randomlyagain to redo the random search process. The maximum times of the repeated piece pickingis 10 x piece count in the vector. In case that the program cannot find any valid move withinthe trials bound for all piece pickings, the move is regarded forfeited the turn finishes withno actual move (no update of the board), and the turn is passed to the opponent player.o Note that ad hoc move Z0 Z0 is NOT generated by a machine player. There is no prematuretermination of the game and no draw game for the machine vs. machine mode.Note that each move will go through a three-level call hierarchy:player-makeMove(board); // player can be a Human or Machine objectboard-move(y1, x1, y2, x2);piece-move(y2, x2); // piece is an object of any Piece subclassesThe Board-level move() includes validation against illegal cases like accessing out-of-bound positionsor moving a piece that belongs to the opponent. Read the TODO comments in board.cpp for moredetails.The Piece-level move() includes capturing the opponent piece and is called only if calling piece-isMoveValid(y2, x2) returns true, i.e. passed validation against the moving rules of the specificpiece type, e.g. Bishop can move only diagonally.CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 7 of 12Assumptions The board size is Always 8×8. Still, we use two global constants H and W to represent the boardsheight (Number of rows) and width (number of columns). They are both set to 8. We assume that cell address inputs always follow the format of one letter (A-Z or a-z) plus oneinteger (1-26). Lowercase inputs like a1, h10, etc. will be accepted as normal. Except for Z0(case-sensitive) which is treated as a sentinel for game stop, you need NOT handle weird inputslike AA1, A01, abc, A3.14, #a2, for cell addresses and inputs like -1, 6.28,#$@, for the number of pits. The behavior upon receiving these is unspecified, probablyrunning into forever loop or program crash.Restrictions You cannot declare any global variables (i.e. variables declared outside any functions). But globalconstants or arrays of constants are allowed. Your final program should contain a total of 25 files (the C++ header and source files listedabove). Note your spelling – do not modify any file names.o For class names, use camel case (e.g. Knight); for file names, use lower case for all letters(e.g. knight.h, knight.cpp). You should not modify the provided code (unless specified otherwise).Sample RunsIn the following sample runs, the blue text is user input and the other text is the program printout.You can try the provided sample program for other input. Your program output should be exactly thesame as the sample program (same text, symbols, letter case, spacings, etc.). Note that there is aspace after the : in the program printout.Sample run in Human vs. Human mode:Whites turn should be moving a white piece(now located in rows 7 and 8).This is a valid move. Cell address inputs are case-insensitive.CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering,CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering,Now the white king is in check (being attacked by the black bishop).Whites turn should make a move that saves the king from the capture.But White made a foolish move here that leads to losing the game.CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering,By entering Z0 Z0 (case-sensitive), White wants to stop the game.As no piece is captured, both players have the same total score.The game draws.CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering,Whites turn: F4 E3White wins! There are too many Combinations of possible inputs. Please check your program correctnessagainst the results produced by our sample program executable posted on Blackboard.Submission and Marking Submit a zip of your source files to Blackboard ( httpss://blackboard.cuhk.edu.hk/). Insert your name, Student ID, and e-mail as comments at the beginning of your main.cpp file. You can submit your assignment multiple times. Only the latest submission counts. Your program Should be free of compilation errors and warnings. Your program should include suitable comments as documentation. Do NOT plagiarize. Sending your work to others is subject to the same penalty for copying work.
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。