Project: Write a program that will be used as part of a game. The game is played
ID: 3698847 • Letter: P
Question
Project: 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 center. 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 opponent's 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 player's color must already be placed. After playing a tile at a position that meets the above criteria, all of the lines of the opponent's tiles that meet the criteria above are flipped to the player's color. In the picture below, all of the candidate positions for White's next move are shown shaded. Figure 2: All of the candidate positions for White's next move.Explanation / Answer
#include<stdio.h>
#define ROWS 8
#define COLS 8
#define EMPTY 1
#define WHITE 2
#define BLACK 3
#define WHITEKING 4
#define BLACKKING 5
#define ISWHITE(c) (c == WHITE || c == WHITEKING)
#define ISBLACK(c) (c == BLACK || c == BLACKKING))
#define ISEMPTY(c) (c == 1)
void printDisplay(int d[][COLS]);
void swapIJKL(int d[ROWS][COLS], int i, int j, int k, int l);
char value2symbol(int i);
void printDisplayBeautified(int d[][COLS]);
int Playersturn(int d[][COLS], int player,int i,int j,int k,int l);
void printDisplayBeautified(int d[][COLS])
{
int rr, cc, pp;
printf(" +---+---+---+---+---+---+---+---+ ");
for (rr=0; rr<ROWS; ++rr)
{
printf("%d |", rr+1);
for (cc=0; cc<COLS; ++cc)
{
printf(" %c |", value2symbol(d[rr][cc]) );
}
printf(" ");
printf(" +---+---+---+---+---+---+---+---+ ");
}
printf(" a b c d e f g h ");
}
void swapIJKL(int d[ROWS][COLS], int i, int j, int k, int l)
{
int temp;
printf("SWAP: %d,%d to %d,%d ", i, j, k, l);
temp = d[i][j];
d[i][j] = d[k][l];
d[k][l] = temp;
}
char value2symbol(int i)
{
switch(i)
{
case 0:
return ' ';
case 1:
return 'E';
case 2:
return '$';
case 3:
return '@';
}
return ('?');
}
int Playersturn(int d[][COLS], int player,int i,int j,int k,int l)
{
int jmp_r;
int jmp_c;
if(player == WHITE){
printf("WHITE move from %d,%d to %d,%d ", i, j, k, l);
} else {
printf("BLACK move from %d,%d to %d,%d ", i, j, k, l);
}
if(i < 0 && ROWS <= i){
printf("i is out of bounds ");
return -1;
}
if(j < 0 && COLS <= j){
printf("j is out of bound");
return -1;
}
if(k < 0 && ROWS <= k){
printf("k is out of bounds");
return -1;
}
if(l < 0 && COLS<= l){
printf("l is out of bounds ");
return -1;
}
if(player == WHITE){
if(d[i][j] != WHITE){
printf("move your own piece! ");
return -1;
}
} else {
if(d[i][j] != BLACK){
printf("move your own piece! ");
return -1;
}
}
if(d[k][l] != EMPTY){
printf("You must move to a empty location");
return -1;
}
if(player == WHITE){
if(i >= k){
printf("WHITE player must move down ");
return -1;
}
} else {
if(i <= k){
printf("BLACK player must move up ");
return -1;
}
}
if(i - k == -1 || i - k == 1){
if(j - l == 1 || j - l == -1){
swapIJKL(d,i,j,k,l);
return 0;
}
}
if(i - k == -2 || i - k == 2){
if(j - l == -2 || j - l == 2){
if(i < k){
jmp_r = i + 1;
} else {
jmp_r = i - 1;
}
if(j < l){
jmp_c = j + 1;
} else {
jmp_c = j - 1;
}
if(player==WHITE && d[jmp_r][jmp_c]!=BLACK)
{
printf("Enemeny is not Black at %d%d",jmp_r, jmp_c);
return -1;
}
if(player==BLACK && d[jmp_r][jmp_c] != WHITE){
printf("you can only jump over an enemy player ");
return -1;
}
d[jmp_r][jmp_c] = 1;
swapIJKL(d,i,j,k,l);
return 0;
}
}
printf("You can only move diagnally ");
return -1;
}
int main()
{
int r,c;
int pr, pb;
int i, k;
char j, l;
int d[ROWS][COLS]={
{0,2,0,2,0,2,0,2},
{2,0,2,0,2,0,2,0},
{0,2,0,2,0,2,0,2},
{1,0,1,0,1,0,1,0},
{0,1,0,1,0,1,0,1},
{3,0,3,0,3,0,3,0},
{0,3,0,3,0,3,0,3},
{3,0,3,0,3,0,3,0}};
for(;;){
printDisplayBeautified(d);
while(1){
printf("WHITE's turn: ");
scanf("%d%c",&i,&j);
printf("to: ");
scanf("%d%c",&k,&l);
if(Playersturn(d, WHITE, i-1,j - 'a',k-1,l - 'a') == 0)
break;
printf("Illegal move, try again ");
}
printDisplayBeautified(d);
while(1){
printf("Black's turn: ");
scanf("%d%c",&i,&j);
printf("to :");
scanf("%d%c",&k,&l);
if(Playersturn(d, BLACK, i-1,j - 'a',k-1,l - 'a') == 0)
break;
printf("Illegal move, try again ");
}
}
getchar();
getchar();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.