Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that will be used as part of a game.The game is played on a board (like a chess board or a checkers

Write a program that will be used as part of a game.The game is played on a board (like a chess board or a checkers board) that has dimensions nxn, where n is even. In the picture below n = 4. The game uses tiles that are white on one side, and black on the other side (they can be flipped over to change their color ). One player plays white; the other player plays black. The picture below shows the initial board configuration, which has two white and two black tiles pre-placed in the cen ter. Observe that rows and columns are labelled with letters.

Figure 1: Starting positions on game board. A turn consists of a player laying a tile of his/her own color on a candidate empty board position, subject to the following two rules:

1. There must be a continuous straight line of tile(s) of the opponents color in at least one of the eight directions from the candidate empty position (North, South, East, West, and diagonals). 2. In the position immediately following the continuous straight line mentioned in #1 above, a tile of the players color must already be placed. After playing a tile at a position that meets the above criteria, all of the lines of the opponents tiles that meet the criteria above are flipped to the players color. In the picture below, all of the candidate positions for Whites next move are shown shaded.Figure 2: All of the candidate positions for Whites next move. If the White player decides to play at row c, column a, the Black tile at row c, column b is flipped and the board looks like this: Figure 3: White plays at row c, column a. The picture below shows the possible move positions for the Black player: Figure 4: All of the candidate positions for Blacks next move after White plays at ( c, a). If the Blac k player lays a tile at (b , a), the board appears like this: Figure 5: Black plays at (b , a). Finally, if the White player lays a tile at ( a, c ) the board appears like this: Figure 6: White responds to Black by playing at (a, c). Note that in Whites move, two lines of Black tiles were flipped: the line directly to the South, and the line to the South West. The turns alternate between the players, unless one player has no available move, in which case the only player with an available move is allowed to continue to make moves until a move becomes available for the opponent, at which point, the opponent is allowed to take a turn and the alternating turns between the players resumes. The game ends when either: 1) the entire board is full, or 2) neither player has an available move. you will write a C program that will do the following: (Note that the specific details of input and output will be given in the example runs below this section) 1.The first input to the program will be n, giving the size of the n x n board. You may assume that the size of n will be even and will never be larger than 26, and shoulddeclare a 2 dimensional array. Your program should initialize the board as shown above and print it.

2. The next sequence of inputs will describe a board configur ation, representing a situation part -way through of the game . Each line of input will consist of three characters with no spaces in between. The character will be a color : B or W ; the second character will be the row (a-z); the third character will be the column (a- z). The three characters represent a tile of the specified color placed at the specified row and column. The three -character sequence !!! ends the board configuration entry phase. Character arithmetic can be used to transl ate the rows/columns into array indices, e.g. b - a equals 1. Note: your pr ogram should not check for move legality during this phase. This phase is simply to input an intermediate board configuration.

3. Then, your program should print a list of the a vailable moves for the White player, followed by a list of the available moves for the Black player, given the board configuration input in the previous step. The available moves for each player should be printed in the order of increasing rows, then in the order of increasing columns (for available moves in the same row).

4. Next, your program should ask the user to input a move, represented in the same three - character format. Your program should check i f the move is valid, and if so, make the move, flipping the tiles correspondingly. If the move is invalid, your program should indicate so. 5. Your program should print the final board configuration and terminate. Your program must use the following characters to represent the state of each board position:

U - for unoccupied

B - occupied by black

W

- occupied by white

For example, after the entire board above is entered, it would be printed as follows:

abcd

a UUWU

b BWWU

c WWWU

d UUUU

To print the board, your program should contain a function with the following prototype: void printBoard(char board[][26], int n); where board is the 2D array representing the current board state, and n is the board dimensions. Here is an example execution of the program:

Enter the board dimension: 4

abcd

a UUUU

b UWBU

c UBWU

d UUUU

Enter board configuration:

Bba

Wca

Bac

!!!

abcd

a UUBU

b BWBU

c WBWU

d UUUU

Available moves for W:

aa

bd

db

Available moves for B:

ab

cd

da

dc

Enter a move:

Wdb

Valid move.

abcd

a UUBU

b BWBU

c WWWU

d UWUU

Here is another example execution of the program where the final move is invalid:

Enter the board dimension: 6

abcdef

a UUUUUU

b UUUUUU

c UUWBUU

d UUBWUU

e UUUUUU

f UUUUUU

Enter board configuration:

Bbd

Bad

Wde

Wcb

!!!

abcdef

a UUUBUU

b UUUBUU

c UWWBUU

d UUBWWU

e UUUUUU

f UUUUUU

Available moves for W:

ae

bc

ce

db

ec

ed

Available moves for B:

ba

bc

ca

db

df

ed

ef

Enter a move:

Bbe

Invalid move.

abcdef

a UUUBUU

b UUUBUU

c UWWBUU

d UUBWWU

e UUUUUU

f UUUUUU

Break up your program into separ ate functions, and to carefully test each function separately, before connecting it into the larger p rogram. To help with this, you are required to create the following helper functions and use them in your implementation: bool positionInBounds(char board [][26], int n, char row, char col); which checks whether the specified (row ,col) lies within the board dimensions. It is prone to write separate code to check each of the eight possible line directions that begin at a given tile. To simplify this, you are required to write and use the following function: bool checkLegalInDirection(char board[][ 26], int n, char row, char col, char color , int deltaRow, int deltaCol); which checks whether (row, col) is a legal position for a tile of color by looking in the direction specified by deltaRow and deltaCol. deltaRow and deltaCol take on values of -1, 0, and 1, with the restriction that they cannot both be 0. For example, if deltaRow = 1 and deltaCol = 0, the function searches the South line. I f deltaRow = - 1 and deltaCol = 1, the function searches the Northeast line. The idea is that, elsewhere in you r program, you will call the helper function 8 times to search the lines in the 8 possible directions.Testyour program using all the cases you can think of, according to the specification giv

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions