
Patrick B. answered 04/28/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BLACK (1)
#define WHITE (0)
#define EMPTY (-1)
int board[8][8];
Board_Init(int board[8][8])
{
int i,j;
for (i=0; i<8; i++)
{
for (j=0; j<8; j++)
{
board[i][j] = EMPTY;
}
}
board[3][3]=BLACK;
board[4][4]=BLACK ;
board[3][4]=WHITE;
board[4][3]=WHITE;
}
BoardShow(int board[8][8])
{
int i,j;
for (i=0; i<8; i++) { printf(" %d ",i+1); } printf("\n");
printf("-------------------------------------------------------\n");
for (i=0; i<8; i++)
{
printf("%c ",char(65+i));
for (j=0; j<8; j++)
{
if (board[i][j]==WHITE)
{
printf("w ");
}
else if (board[i][j]==BLACK)
{
printf("b ");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
int GetPlayerMove( int playerColor, int * x, int * y, int move_flip_flag)
{
char ch;
char strPlayer[6];
char strMoveFlip[5];
int columnNum,rowNum;
int iReturn=0;
if (playerColor==WHITE) { strcpy(strPlayer,"WHITE"); } else { strcpy(strPlayer,"BLACK"); }
if (move_flip_flag==1) { strcpy(strMoveFlip,"move"); } else { strcpy(strMoveFlip,"flip"); }
printf(" Player %s please input %s or P to PASS \n",strPlayer,strMoveFlip);
printf(" row letter :>"); scanf("%c",&ch);
if (ch=='P'|| ch=='p')
{
iReturn=-1;
*x=-1; *y=-1;
}
else
{
printf(" column # :>"); scanf("%d",&columnNum);
rowNum = toupper(ch)-65;
*x=rowNum; *y=columnNum;
}
fflush(stdin);
return(iReturn);
}
GetWinner(int board[8][8])
{
int i,j;
int nWhite=0, nBlack=0;
for (i=0; i<8; i++)
{
for (j=0; j<8; j++)
{
if (board[i][j]==WHITE) { nWhite++; }
if (board[i][j]==BLACK) { nBlack++; }
}
}
printf(" White: %d \n",nWhite);
printf(" Black: %d \n",nBlack);
if (nWhite>nBlack)
{
printf(" White wins \n");
}
else if (nWhite<nBlack)
{
printf(" Black wins \n");
}
else
{
printf("tie");
}
}
main()
{
Board_Init(board);
BoardShow(board);
int iLoop;
int iRowIndex,iColumnIndex;
int player=BLACK;
for (iLoop=0; iLoop<16; iLoop++)
{
player=BLACK;
GetPlayerMove(player,&iRowIndex,&iColumnIndex,1);
board[iRowIndex][iColumnIndex]=player;
BoardShow(board);
if (GetPlayerMove(player,&iRowIndex,&iColumnIndex,0)==0)
{
board[iRowIndex][iColumnIndex]=player;
BoardShow(board);
}
player=WHITE;
GetPlayerMove(player,&iRowIndex,&iColumnIndex,1);
board[iRowIndex][iColumnIndex]=player;
BoardShow(board);
if (GetPlayerMove(player,&iRowIndex,&iColumnIndex,0)==0)
{
board[iRowIndex][iColumnIndex]=player;
BoardShow(board);
}
}
GetWinner(board);
}