联系方式

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

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

日期:2020-11-05 11:31

CSCI1120 Introduction to Computing Using C++, Fall 2020/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2020 CSE, CUHK Page 1 of 8

Assignment 4: Othello

Due: 20:00, Sat 7 Nov 2020 File name: othello.cpp Full marks: 100

Introduction

The objective of this assignment is to let you practice the use of 2-D arrays with loops in C++. You are

to 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 empty

cell in the board, the character 'X' to denote a black piece, and the character 'O' to denote a white

piece. To denote a position in a game board, we use a coordinate system similar to a spreadsheet

program, e.g. "A2" refers to the cell in the first column and the second row. Suppose that a C++ array

arr is used to represent the game board, cell address "A2" will be mapped to arr[1][0].

Figure 1: Initial configuration of the game board

The two players, namely "Black" and "White" make moves in turns by putting a disc of their color on

an empty cell of the board. Black plays the first move. A move cannot be made arbitrarily to any of

the empty cells. As a valid move, the newly placed disc must be in a position such that it forms at

least one straight (horizontal, vertical, or diagonal) occupied line between the new piece and another

piece 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 more

than 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/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2020 CSE, CUHK Page 2 of 8

Figure 2: An example move by White ('O') at position D7, forming a vertical occupied line from D7 to

D2. 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 having

more pieces of his/her color than the opponent wins the game. If both players have the same

number of pieces, the game draws.

To add more fun and challenges to the game, this assignment will extend the original Othello game

with an additional feature: some “pits” can be added to the game board, preoccupying some cells so

that both players cannot place a disc onto such positions. Pits are added before the game begins. For

example, Figure 4 shows a board with cells A1, A2, B1, B2, G5, G6, G7 and F7 initialized as pits. Let’s

call them “pit cells”. Then players cannot make a move to such positions. Apart from fewer choices

for making a move and greater likelihood that the players run out of valid moves before the board

becomes 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) added

CSCI1120 Introduction to Computing Using C++, Fall 2020/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2020 CSE, CUHK Page 3 of 8

Program Specification

Game board

You 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 avoid

declaring 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 be

defined as a global constant near the top of your code to make it easily scalable.

const int N = 8; // board size

When grading, we may change N to a different value for more thorough testing of your code. Please

follow the above constant definition exactly, i.e. declaring a constant of int type and naming it with

a 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 will

prompt the user to enter the number of pit cells to add to the board. It can be zero or up to half

of the board’s area at most. For example, if N (board size) = 8, the maximum number of pit cells

allowed would be (8 * 8) / 2 = 32. This ensures that the board still has enough empty cells for

making 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 board’s size range is

illegal too. If the user enters an exceedingly value for N or any illegal cell addresses, the program

repeats prompting until valid inputs are received.

• Then the game starts with round 1 on the board initialized with the first four discs as shown in

Figure 1 and optionally with pit cells. Black moves first.

• Black and White make moves alternately. For each move, the program should prompt the player

to enter the cell address of the intended move, e.g. "D3", and it will convert the characters into

two integer values (x, y) that represent the column and row indexes, respectively, to access the

corresponding element of the array board[y][x].

• You should check whether a player’s 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 position

cannot flip any opponent’s 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 a

player to make a valid move. Thus, your program needs to check whether there still exists

at least one empty cell that allows the current player to make a valid move before

prompting him/her for entering a cell address. This involves scanning the whole game

board for any cells for a valid move per round before prompting.

o In case of no possible valid moves, the player’s 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/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 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 have

no valid moves to make). If Black wins, the message “Player X wins!” should be printed; if White

wins, the message “Player O wins!” gets printed; and if the game is a draw, “Draw game!” should

be printed.

• Your program should be reasonably decomposed into at least four functions (including main) for

various tasks such as printing the game board, initializing the board, adding pits, checking if a

move is valid, and updating the board, etc. The game board array and other data like the input

coordinate 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 up

to Z only. And you need not spend too much effort on testing a big game board.

• N is always even, so we won’t have 5x5, 7x7, 9x9, … boards but 4x4, 6x6, 8x8, … boards only.

• The row index markers on the left of the printed board are always right adjusted at a fixed width

of 2 characters, i.e. for single-digit row indexes, there is a single space ahead of them. For

example, 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 one

integer (1-26). Lowercase inputs like "a1", "h10", etc. will be accepted as normal. You need NOT

handle 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 J

1 . . . . . . . . . .

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 columns

Restrictions

• You cannot declare any global variables (i.e. variables declared outside any functions). But global

constants or arrays of constants are allowed.

• Please use a character array to represent the board. Avoid using other container classes like

vector, map, etc. available in the standard library.

• Avoid defining classes in this assignment. We will have other assignments on classes and objects

to 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/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2020 CSE, CUHK Page 5 of 8

Sample Runs

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

Sample Run #1: (for N = 6)

Enter number of pits: 19↵

Too many pits!

Enter number of pits: 7↵

Enter position for pit 1: A1↵

Enter position for pit 2: A2↵

Enter position for pit 3: B1↵

Enter position for pit 4: B2↵

Enter position for pit 5: C3↵

Invalid position!

Enter position for pit 5: D4↵

Invalid position!

Enter position for pit 5: X10↵

Invalid position!

Enter position for pit 5: A1↵

Invalid position!

Enter position for pit 5: B5↵

Enter position for pit 6: E5↵

Enter position for pit 7: E2↵

Round 1:

A B C D E F

1 # # . . . .

2 # # . . # .

3 . . X O . .

4 . . O X . .

5 . # . . # .

6 . . . . . .

Player X's turn: E3↵

Round 2:

A B C D E F

1 # # . . . .

2 # # . . # .

3 . . X X X .

4 . . O X . .

5 . # . . # .

6 . . . . . .

Player O's turn: C2↵

(some rounds skipped due to space constraint)

CSCI1120 Introduction to Computing Using C++, Fall 2020/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2020 CSE, CUHK Page 6 of 8

Round 17:

A B C D E F

1 # # X O . .

2 # # O O # X

3 O O X O O X

4 O O O O O X

5 . # O O # .

6 . . O . . .

Player X's turn: E1↵

Round 18:

A B C D E F

1 # # X X X .

2 # # O X # X

3 O O X O O X

4 O O O O O X

5 . # O O # .

6 . . O . . .

Player O has no valid moves! Pass!

Round 19:

A B C D E F

1 # # X X X .

2 # # O X # X

3 O O X O O X

4 O O O O O X

5 . # O O # .

6 . . O . . .

Player X's turn: A5↵

Round 20:

A B C D E F

1 # # X X X .

2 # # O X # X

3 O O X O O X

4 O X O O O X

5 X # O O # .

6 . . O . . .

(some rounds skipped due to space constraint)

Round 24:

A B C D E F

1 # # X X X .

2 # # O X # X

3 O O X X O X

4 O X O X X X

5 O # X O # X

6 O . O O O .

Player O's turn: B6↵

CSCI1120 Introduction to Computing Using C++, Fall 2020/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2020 CSE, CUHK Page 7 of 8

Round 25:

A B C D E F

1 # # X X X .

2 # # O X # X

3 O O X X O X

4 O X O O X X

5 O # O O # X

6 O O O O O .

Player X has no valid moves! Pass!

Round 26:

A B C D E F

1 # # X X X .

2 # # O X # X

3 O O X X O X

4 O X O O X X

5 O # O O # X

6 O O O O O .

Player O has no valid moves! Pass!

Game over:

A B C D E F

1 # # X X X .

2 # # O X # X

3 O O X X O X

4 O X O O X X

5 O # O O # X

6 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: 9↵

Too many pits!

Enter number of pits: 8↵

Enter position for pit 1: B1↵

Enter position for pit 2: C1↵

Enter position for pit 3: A2↵

Enter position for pit 4: D2↵

Enter position for pit 5: A3↵

Enter position for pit 6: D3↵

Enter position for pit 7: B4↵

Enter position for pit 8: C4↵

Round 1:

A B C D

1 . # # .

2 # X O #

3 # O X #

4 . # # .

Player X has no valid moves! Pass!

CSCI1120 Introduction to Computing Using C++, Fall 2020/21

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Copyright © 2020 CSE, CUHK Page 8 of 8

Round 2:

A B C D

1 . # # .

2 # X O #

3 # O X #

4 . # # .

Player O has no valid moves! Pass!

Game over:

A B C D

1 . # # .

2 # X O #

3 # O X #

4 . # # .

Draw game!

• There are many combinations of possible inputs. Please check your program correctness against

the 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

(https://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.


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

python代写
微信客服:codinghelp