CSCI1120程序 写作、 辅导C++编程设计

” 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 8Assignment 4: OthelloDue: 20:00, Sat 7 Nov 2020 File name: othello.cpp Full marks: 100IntroductionThe objective of this assignment is to let you practice the use of 2-D arrays with loops in C++. You areto write a program to simulate a classic board game known as Othello (or Reversi).Othello is a two-player game which is played on a square uncheckered board with some game pieces.A piece is a disc With two sides in black and white colors respectively. At the beginning of a game,there are four pieces placed on the board as shown in Figure 1. We use a dot . to denote an emptycell in the board, the character X to denote a black piece, and the character O to denote a whitepiece. To denote a Position in a game board, we use a coordinate system similar to a spreadsheetprogram, e.g. A2 refers to the cell in the first column and the second row. Suppose that a C++ arrayarr is used to represent the game board, cell address A2 will be mapped to arr[1][0].Figure 1: Initial configuration of the game boardThe two players, namely Black and White make moves in turns by putting a disc of their color onan empty cell of the board. Black plays the first move. A move cannot be made arbitrarily to any ofthe empty cells. As a valid move, the newly placed disc must be in a position such that it forms atleast one straight (horizontal, vertical, or diagonal) occupied line between the new piece and anotherpiece of the Same color, with one or more contiguous pieces of the opposite color between them.After the move, That contiguous pieces will all be flipped to become the color of the new piece.Figure 2 shows an example move made by White. Note that a move can simultaneously form morethan one straight line. In such a case, the sandwiched pieces on all the lines formed will be flipped.Figure 3 shows such an example.CSCI1120 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 8Figure 2: An example move by White (O) at position D7, forming a vertical occupied line from D7 toD2. Four black (X) pieces are clipped by two white pieces at both ends and are flipped.Figure 3: An example move by Black at F5, forming three occupied lines (i) F5 to F3, (ii) F5 to D5, and(iii) F5 to D3. All sandwiched pieces of white are flipped to black.A game is over when either the board is full or both players have no valid moves. The player havingmore pieces of his/her color than the opponent wins the game. If both players have the samenumber of pieces, the game draws.To add more fun and challenges to the game, this assignment will extend the original Othello gamewith an additional feature: some pits can be added to the game board, preoccupying some cells sothat both players cannot place a disc onto such positions. Pits are added before the game begins. Forexample, Figure 4 shows a board with cells A1, A2, B1, B2, G5, G6, G7 and F7 initialized as pits. Letscall them pit cells. Then players cannot make a move to such positions. Apart from fewer choicesfor making a move and greater likelihood that the players run out of valid moves before the boardbecomes full, the overall rules of the game stay the same as in the case without pit cells.Figure 4: Initial configuration of the game board with pits (grey areas) addedCSCI1120 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 8Program SpecificationGame boardYou should use a two-dimensional array of char type to represent the game board. For example,you may define the following in your main() function:char board[N][N];Note that defining the board array as a global variable is highly discouraged. You should also avoiddeclaring it in a Hard coded manner like char board[8][8];Make sure your code can support array sizes other than 8. We demand that the array size should bedefined as a global Constant near the top of your code to make it easily scalable.const int N = 8; // board sizeWhen grading, we may change N to a different value for more thorough testing of your code. Pleasefollow the above constant definition exactly, i.e. declaring a constant of int type and naming it witha single capital letter N to facilitate our marking process.Game Flow The program needs to set up the game board first. Before a game begins, the program willprompt the user to enter the number of pit cells to add to the board. It can be zero or up to halfof the boards area at most. For example, if N (board size) = 8, the maximum number of pit cellsallowed would be (8 * 8) / 2 = 32. This ensures that the board still has enough empty cells formaking moves. It is illegal to set any of the initial four pieces central in the board as a pit.Entering a cell address that refers to a pit already defined or a cell out of the boards size range isillegal too. If the user enters an exceedingly value for N or any illegal cell addresses, the programrepeats prompting until valid inputs are received. Then the game starts with round 1 on the board initialized with the first four discs as shown inFigure 1 and optionally with pit cells. Black moves first. Black and White make moves alternately. For each move, the program should prompt the playerto enter the cell address of the intended move, e.g. D3, and it will convert the characters intotwo integer values (x, y) that represent the column and row indexes, respectively, to access thecorresponding element of the array board[y][x]. You should check whether a players input refers to a valid move. If the input position is not valid(either because the input coordinate is out of bounds of the board or because the input positioncannot flip any opponents pieces), the player should be prompted again for a valid move.o In fact, it can happen that the board with empty cells has really run out of positions for aplayer to make a valid move. Thus, your program needs to check whether there still existsat least one empty cell that allows the current player to make a valid move beforeprompting him/her for entering a cell address. This involves scanning the whole gameboard for any cells for a valid move per round before prompting.o In case of no possible valid moves, the players round will be passed to the opponent. If a valid move is made, All relevant pieces should be flipped to the opposite color.CSCI1120 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 8 The game should end when either (1) the board is full, or (2) two consecutive passes are made(i.e. Black passes to White but White immediately passes back to Black since both of them haveno valid moves to make). If Black wins, the message Player X wins! should be printed; if Whitewins, the message Player O wins! gets printed; and if the game is a draw, Draw game! shouldbe printed. Your program should be reasonably decomposed into at least four functions (including main) forvarious tasks such as printing the game board, initializing the board, adding pits, checking if amove is valid, and updating the board, etc. The game board array and other data like the inputcoordinate values can be passed between the functions.Assumptions The board is always a square. So only a single global constant N is needed to define its size. The minimum and maximum board sizes are 4 and 26 respectively. So, the largest column is upto Z only. And you need not spend too much effort on testing a big game board. N is always even, so we wont have 5×5, 7×7, 9×9, boards but 4×4, 6×6, 8×8, boards only. The row index markers on the left of the printed board are always right adjusted at a fixed widthof 2 characters, i.e. for single-digit row indexes, there is a single space ahead of them. Forexample, Figure 5 shows a board with N = 10. Cell addresses being entered will still be like A1,B8, C10, etc. No zero padding is used, so the expected inputs are not like A01, B08, etc. 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. You need NOThandle weird inputs like 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,probably running into forever loop or program crash.A B C D E F G H I J1 . . . . . . . . . .2 . . . . . . . . . .3 . . . . . . . . . .4 . . . . . . . . . .5 . . . . X O . . . .6 . . . . O X . . . .7 . . . . . . . . . .8 . . . . . . . . . .9 . . . . . . . . . .10 . . . . . . . . . .Figure 5: Initial configuration of a game board with 10 rows and 10 columnsRestrictions You cannot declare any global variables (i.e. variables declared outside any functions). But globalconstants or arrays of constants are allowed. Please use a character array to represent the board. Avoid using other container classes likevector, map, etc. available in the standard library. Avoid defining classes in this assignment. We will have other assignments on classes and objectsto come. But using struct (C/C++ structures) is still allowed if you find it fit for this assignment.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 8Sample 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 #1: (for N = 6)Enter number of pits: 19Too many pits!Enter number of pits: 7Enter position for pit 1: A1Enter position for pit 2: A2Enter position for pit 3: B1Enter position for pit 4: B2Enter position for pit 5: C3Invalid position!Enter position for pit 5: D4Invalid position!Enter position for pit 5: X10Invalid position!Enter position for pit 5: A1Invalid position!Enter position for pit 5: B5Enter position for pit 6: E5Enter position for pit 7: E2Round 1:A B C D E F1 # # . . . .2 # # . . # .3 . . X O . .4 . . O X . .5 . # . . # .6 . . . . . .Player Xs turn: E3Round 2:A B C D E F1 # # . . . .2 # # . . # .3 . . X X X .4 . . O X . .5 . # . . # .6 . . . . . .Player Os turn: C2(some rounds skipped due to space constraint)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 8Round 17:A B C D E F1 # # X O . .2 # # O O # X3 O O X O O X4 O O O O O X5 . # O O # .6 . . O . . .Player Xs turn: E1Round 18:A B C D E F1 # # X X X .2 # # O X # X3 O O X O O X4 O O O O O X5 . # O O # .6 . . O . . .Player O has no valid moves! Pass!Round 19:A B C D E F1 # # X X X .2 # # O X # X3 O O X O O X4 O O O O O X5 . # O O # .6 . . O . . .Player Xs turn: A5Round 20:A B C D E F1 # # X X X .2 # # O X # X3 O O X O O X4 O X O O O X5 X # O O # .6 . . O . . .(some rounds skipped due to space constraint)Round 24:A B C D E F1 # # X X X .2 # # O X # X3 O O X X O X4 O X O X X X5 O # X O # X6 O . O O O .Player Os turn: B6CSCI1120 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 8Round 25:A B C D E F1 # # X X X .2 # # O X # X3 O O X X O X4 O X O O X X5 O # O O # X6 O O O O O .Player X has no valid moves! Pass!Round 26:A B C D E F1 # # X X X .2 # # O X # X3 O O X X O X4 O X O O X X5 O # O O # X6 O O O O O .Player O has no valid moves! Pass!Game over:A B C D E F1 # # X X X .2 # # O X # X3 O O X X O X4 O X O O X X5 O # O O # X6 O O O O O .Player O wins!Sample Run #2: (for N = 4)(This example is an extreme case with so many pit cells that no valid moves can ever be made since the start of the game.)Enter number of pits: 9Too many pits!Enter number of pits: 8Enter position for pit 1: B1Enter position for pit 2: C1Enter position for pit 3: A2Enter position for pit 4: D2Enter position for pit 5: A3Enter position for pit 6: D3Enter position for pit 7: B4Enter position for pit 8: C4Round 1:A B C D1 . # # .2 # X O #3 # O X #4 . # # .Player X has no valid moves! Pass!CSCI1120 Introduction to Computing Using C++, Fall 2020/21Department of Computer Science and Engineering, The Chinese University of Hong KongCopyright 2020 CSE, CUHK Page 8 of 8Round 2:A B C D1 . # # .2 # X O #3 # O X #4 . # # .Player O has no valid moves! Pass!Game over:A B C D1 . # # .2 # X O #3 # O X #4 . # # .Draw game! There are many combinations of possible inputs. Please check your program correctness againstthe results produced by our sample program executable posted on Blackboard.Submission and Marking Your program source file name should be othello.cpp. Submit the file in Blackboard( httpss://blackboard.cuhk.edu.hk/). Insert Your name, student ID, and e-mail as comments at the beginning of your source 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.如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导