Question: In The C Programming Language (NOT C++) The other answers to this question have errors. In this assignment, you are asked to write a program

In The C Programming Language (NOT C++) The other answers to this question have errors.

In this assignment, you are asked to write a program that stores a chessboard in a 2D array of integers. Your program must make necessary changes on the array after each move or capture of the two players of the game.

In The C Programming Language (NOT C++) The other answers to this

question have errors. In this assignment, you are asked to write a

program that stores a chessboard in a 2D array of integers. Your

program must make necessary changes on the array after each move or

The Project should be done using this shell below

Thank you

#include

#include

#define MAX_COMMAND_TOKEN_LENGTH 6

#define WHITE 1

#define BLACK -1

#define EMPTY 0

#define ROOK 4

#define KNIGHT 2

#define BISHOP 3

#define KING 6

#define QUEEN 5

#define PAWN 1

static int board[8][8] = { {-4, -2, -3, -5, -6, -3, -2, -4},

{-1, -1, -1, -1, -1, -1, -1, -1},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{1, 1, 1, 1, 1, 1, 1, 1},

{4, 2, 3, 5, 6, 3, 2, 4}};

static int isLegalMove(int srcI, int srcJ, int trgI, int trgJ) {

int i;

int piece = board[srcI][srcJ];//moved piece...

int pieceType = abs(piece);

switch (pieceType) {

case ROOK:

break;

case PAWN:

break;

case KNIGHT://L shape

return abs((srcI - trgI) * (srcJ - trgJ)) == 2;

break;

case BISHOP:

if (abs(srcI - trgI) != abs(srcJ - trgJ))

return 0;

if (srcI

for (i = 1; i

if (board[srcI + i][srcJ + i] != EMPTY)

return 0;

else if (srcI trgJ) {//case 2

for (;;);

}

else if (srcI > trgI && srcJ

for (;;);

else //case 4

for (;;);

break;

case QUEEN:

break;

case KING:

break;

}

return 1;//legal move

}

static int isLegalCapture(int srcI, int srcJ, int trgI, int trgJ) {

return 1;//legal move

}

static int turn = WHITE;

char getCommandWord(char command[], int maxLength) {

char lastCharacter;//either space or new line

int i;

for (i = 0; (lastCharacter = getchar()) == ' '; i++);//skip leading white spaces

if (lastCharacter == ' ') {

command[0] = '\0';

return lastCharacter;

}

command[0] = lastCharacter;

for (i = 1; i

lastCharacter = command[i];

command[i] = '\0';

return lastCharacter;

}

handleMove() {

char source[MAX_COMMAND_TOKEN_LENGTH];

char target[MAX_COMMAND_TOKEN_LENGTH];

char lastCharacter;

int sourceFile, sourceRank, targetFile, targetRank, sourceI, sourceJ, targetI, targetJ;

lastCharacter = getCommandWord(source, MAX_COMMAND_TOKEN_LENGTH);

if (lastCharacter == ' ') {

printf("Too few arguments for mv command! It must be in the form of mv ai bj. ");

return;

}

lastCharacter = getCommandWord(target, MAX_COMMAND_TOKEN_LENGTH);

if (lastCharacter != ' ') {

printf("Too many arguments for mv command! It must be in the form of mv ai bj. ");

while (getCommandWord(target, MAX_COMMAND_TOKEN_LENGTH) != ' ');//skip therest of illegal command..

return;

}

sourceFile = source[0];//source = "a5", sourceFile = 'a'

targetFile = target[0];

sourceRank = source[1] - '0';//source = "a5", sourceRank = 5

targetRank = target[1] - '0';

//board[sourceI][sourceJ]: source square...

//board[targetI][targetJ]: target square...

sourceI = 8 - sourceRank;

sourceJ = sourceFile - 'a';

targetI = 8 - targetRank;

targetJ = targetFile - 'a';

if (sourceI

|| sourceI > 7 || sourceJ > 7 || targetI > 7 || targetJ > 7) {

printf("invalid mv arguments ");

return;

}

//checking the turn first

if (board[sourceI][sourceJ] * turn

printf("Turn violation, it's %s to move ", turn == 1 ? "white" : "black");

return;

}

if (board[sourceI][sourceJ] == EMPTY || board[targetI][targetJ] != EMPTY) {

printf("Invalid move: either source square is empty or target square is notempty ");

return;

}

if (!isLegalMove(sourceI, sourceJ, targetI, targetJ)) {

printf("Illegal chess move ");

return;

}

//end of error checking....

board[targetI][targetJ] = board[sourceI][sourceJ];

board[sourceI][sourceJ] = EMPTY;

turn *= -1;//WHITE --> BLACK and BLACK --> WHITE

}

handleCapture() {

char source[MAX_COMMAND_TOKEN_LENGTH];

char target[MAX_COMMAND_TOKEN_LENGTH];

char lastCharacter;

int sourceFile, sourceRank, targetFile, targetRank, sourceI, sourceJ, targetI, targetJ;

lastCharacter = getCommandWord(source, MAX_COMMAND_TOKEN_LENGTH);

if (lastCharacter == ' ') {

printf("Too few arguments for cp command! It must be in the form of cp ai bj. ");

return;

}

lastCharacter = getCommandWord(target, MAX_COMMAND_TOKEN_LENGTH);

if (lastCharacter != ' ') {

printf("Too many arguments for cp command! It must be in the form of cp ai bj. ");

while (getCommandWord(target, MAX_COMMAND_TOKEN_LENGTH) != ' ');

return;

}

sourceFile = source[0];

targetFile = target[0];

sourceRank = source[1] - '0';

targetRank = target[1] - '0';

sourceI = 8 - sourceRank;

sourceJ = sourceFile - 'a';

targetI = 8 - targetRank;

targetJ = targetFile - 'a';

if (sourceI 7 || sourceJ > 7 || targetI > 7 || targetJ > 7) {

printf("invalid mv arguments ");

return;

}

//checking the turn first

if (board[sourceI][sourceJ] * turn

printf("Turn violation, it's %s to move", turn == WHITE ? "WHITE" : "BLACK");

return;

}

if (board[sourceI][sourceJ] * board[targetI][targetJ] > 0) {

printf("Violation, %s cannot capture its own piece.", turn == WHITE ? "WHITE" : "BLACK");

return;

}

if (board[sourceI][sourceJ] == EMPTY || board[targetI][targetJ] == EMPTY) {

printf("Invalid capture: either source square is empty or target square is empty");

return;

}

if (!isLegalCapture(sourceI, sourceJ, targetI, targetJ)) {

printf("Illegal chess capture");

return;

}

//end of error checking....

board[targetI][targetJ] = board[sourceI][sourceJ];

board[sourceI][sourceJ] = 0;

turn *= -1;

}

handleShow() {

int i, j;

printf("board\t");

for (i = 0; i

printf("%c%c", 'a' + i, i == 7 ? ' ' : '\t');

for (i = 0; i

for (j = 0; j

printf("%d%c", j == 0 ? 8 - i: board[i][j - 1], j == 8 ? ' ' : '\t');

}

main() {

char command[MAX_COMMAND_TOKEN_LENGTH];

char lastCharacter;

while (1) {//infinite while loop...

printf("Please enter a new command... ");

lastCharacter = getCommandWord(command, MAX_COMMAND_TOKEN_LENGTH);

if (strcmp(command, "quit") == 0)//if command === "quit"

break;

else if (!strcmp(command, "mv"))//if command == "mv"

{

if (lastCharacter == ' ')//mv

printf("Too few arguments for mv command! It must be in the form ofmv ai bj. ");

else

handleMove();

}

else if (!strcmp(command, "cp"))//if command == "cp"

{

if (lastCharacter == ' ')//cp

printf("Too few arguments for cp command! It must be in the form ofcp ai bj. ");

else

handleCapture();

}

else if (!strcmp(command, "show"))//if command == "show"

{

if (lastCharacter != ' ') {//show x

printf("Too many arguments for show command! It must be in the formof show. ");

while (getCommandWord(command, MAX_COMMAND_TOKEN_LENGTH) != ' ');//skip the invalid show command...

}

else

handleShow();

}

else { printf("invalid command! Your command must start either with quit, mv or cp. ");

while (lastCharacter != ' ')//skip the remainder of my invalid command...

lastCharacter = getCommandWord(command, MAX_COMMAND_TOKEN_LENGTH);

}

}

}

COP-4338 System Programming Programming Assignment 3: FIU School of Computing & Info. Sciences In this assignment, you are asked to write a program that stores a chessboard in a 2D array of integers. Your program must make necessary changes on the array after each move or capture of the two players of the game. 1 Array Format A chessboard can be represented by an 8 x 8 table of integers in which every cell is corre- sponding to one of the 64 squares of the board and determines the occupant of the square. As seen in Figure 1, the table row with index 0 is labeled with "g and the table row with B q 8 7 b d f h Figure 1: Row and column labels in a chessboard. index 7 is labeled with "1". Also, the table column with index 0 is labeled with "a", while the table column with index 7 is labeled with "h". If the value of a table cell is zero, it means that the square corresponding to that cell is empty. If the value is positive, it means that a white piece occupies the corresponding square; while a negative value means that the occupying piece color is black. The absolute 1 value of the cell specifies type of piece occupying the corresponding square. Here is the general rule: O cell is empty 1 cell is occupied by a pawn 2 cell is occupied by a knight table[i][j] = 3 cell is occupied by a bishop (1) 4 cell is occupied by a rook 5 cell is occupied by a queen 6 cell is occupied by a king For the sake of simplicity, you can store this table in a static or external 2D array of integers in your program. 2 Initial Setting of Pieces on the Board The program starts by initializing the table in the following fashion: which corresponds to -4 -2 -3-5 -6-3-2-4 -1 -1-1-1 -1-1-1-1 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 1 1 1 1 1 1 1 1 4 2 3 5 6 3 2 4 the following chessboard setup: a b d e f g h | tt iii 5 3 2 5 i 11 12 a b d e f g h 2 X d Figure 2: Movement and capture of knight (left), movement (middle) and capture of pawn (right) on chessboard. 3 Program Commands Your program must perform the following commands entered by user via standard input stream: mv Qolo Q1B1: moves the piece in cell with label ao Bo to cell with label Q1B1 where ao and @ can be letters from a to h; while Bo and B, can be integers from 1 to 8. cp QoBo Q1B1: captures the piece in cell with label au with the piece in cell aoBo where go and Q1 can be letters from a to h; while Bo and B. can be integers from 1 to 8. show: prints out the content of the 2D array on the screen in the form of a table like this (the ranks 1,2,..., 8 and files a, b, ..., h must be printed as well ): board a b d e f 8 h 8 -2 -3 -5 -6 -3 -2 -4 7 -1 -1 -1 -1 0 -1 -1 -1 6 0 0 0 0 0 0 0 0 5 0 0 0 0 -1 0 0 0 4 0 0 0 0 1 0 0 0 3 0 0 0 0 0 0 0 0 2 1 1 1 1 0 1 1 1 1 4 2 3 5 6 3 2 4 Figure 2 shows how knight can move on the board and capture pieces with opposite color on the board. Also, it shows the direction in which white pawn moves and captures. The black pawn moves and captures in the opposite direction. In summary, a pawn can move only one cell forward at a time; however, if it is white and is in the second rank (or is black and is in the seventh rank), it has the option of moving two cells forward as well if there is no piece on its way. Bishop moves diagonally and rook moves vertically or horizontally. Queen can 3 move like a bishop or rook. King moves one square at a time to one of the four adjacent squares. If a 'cp' or 'my command tries to make illegal move, your program prints out an error message. 4 30% Bonus Parts 1. As the first bonus point, your program must support promotion of white/black pawn to a queen, knight, rook or bishop once it reaches to the eighth/first rank. 2. As the second bonus part, your program must support en passant move (see https://en.wikipedia.org/wiki/En passant for details) 3. As the third bonus part, your program must print a "check message once the king is in check. 5 Submissions You need to submit a .zip file compressing the following files: the C source file(s) related to the assignment (.c files). the header files (.h files) A readme file clearly explaining what parts have/haven't been implemented. 4 COP-4338 System Programming Programming Assignment 3: FIU School of Computing & Info. Sciences In this assignment, you are asked to write a program that stores a chessboard in a 2D array of integers. Your program must make necessary changes on the array after each move or capture of the two players of the game. 1 Array Format A chessboard can be represented by an 8 x 8 table of integers in which every cell is corre- sponding to one of the 64 squares of the board and determines the occupant of the square. As seen in Figure 1, the table row with index 0 is labeled with "g and the table row with B q 8 7 b d f h Figure 1: Row and column labels in a chessboard. index 7 is labeled with "1". Also, the table column with index 0 is labeled with "a", while the table column with index 7 is labeled with "h". If the value of a table cell is zero, it means that the square corresponding to that cell is empty. If the value is positive, it means that a white piece occupies the corresponding square; while a negative value means that the occupying piece color is black. The absolute 1 value of the cell specifies type of piece occupying the corresponding square. Here is the general rule: O cell is empty 1 cell is occupied by a pawn 2 cell is occupied by a knight table[i][j] = 3 cell is occupied by a bishop (1) 4 cell is occupied by a rook 5 cell is occupied by a queen 6 cell is occupied by a king For the sake of simplicity, you can store this table in a static or external 2D array of integers in your program. 2 Initial Setting of Pieces on the Board The program starts by initializing the table in the following fashion: which corresponds to -4 -2 -3-5 -6-3-2-4 -1 -1-1-1 -1-1-1-1 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 1 1 1 1 1 1 1 1 4 2 3 5 6 3 2 4 the following chessboard setup: a b d e f g h | tt iii 5 3 2 5 i 11 12 a b d e f g h 2 X d Figure 2: Movement and capture of knight (left), movement (middle) and capture of pawn (right) on chessboard. 3 Program Commands Your program must perform the following commands entered by user via standard input stream: mv Qolo Q1B1: moves the piece in cell with label ao Bo to cell with label Q1B1 where ao and @ can be letters from a to h; while Bo and B, can be integers from 1 to 8. cp QoBo Q1B1: captures the piece in cell with label au with the piece in cell aoBo where go and Q1 can be letters from a to h; while Bo and B. can be integers from 1 to 8. show: prints out the content of the 2D array on the screen in the form of a table like this (the ranks 1,2,..., 8 and files a, b, ..., h must be printed as well ): board a b d e f 8 h 8 -2 -3 -5 -6 -3 -2 -4 7 -1 -1 -1 -1 0 -1 -1 -1 6 0 0 0 0 0 0 0 0 5 0 0 0 0 -1 0 0 0 4 0 0 0 0 1 0 0 0 3 0 0 0 0 0 0 0 0 2 1 1 1 1 0 1 1 1 1 4 2 3 5 6 3 2 4 Figure 2 shows how knight can move on the board and capture pieces with opposite color on the board. Also, it shows the direction in which white pawn moves and captures. The black pawn moves and captures in the opposite direction. In summary, a pawn can move only one cell forward at a time; however, if it is white and is in the second rank (or is black and is in the seventh rank), it has the option of moving two cells forward as well if there is no piece on its way. Bishop moves diagonally and rook moves vertically or horizontally. Queen can 3 move like a bishop or rook. King moves one square at a time to one of the four adjacent squares. If a 'cp' or 'my command tries to make illegal move, your program prints out an error message. 4 30% Bonus Parts 1. As the first bonus point, your program must support promotion of white/black pawn to a queen, knight, rook or bishop once it reaches to the eighth/first rank. 2. As the second bonus part, your program must support en passant move (see https://en.wikipedia.org/wiki/En passant for details) 3. As the third bonus part, your program must print a "check message once the king is in check. 5 Submissions You need to submit a .zip file compressing the following files: the C source file(s) related to the assignment (.c files). the header files (.h files) A readme file clearly explaining what parts have/haven't been implemented. 4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!