永发信息网

C++ 井字棋 (注释) 一定要给注释!!!

答案:1  悬赏:0  手机版
解决时间 2021-11-10 20:07
  • 提问者网友:低吟詩仙的傷
  • 2021-11-10 10:55
C++ 井字棋 (注释) 一定要给注释!!!
最佳答案
  • 五星知识达人网友:酒者煙囻
  • 2021-11-10 11:10
#include
#include

#define SIZE 3
typedef enum {CBLANK, CBLACK, CWHITE} CHESS;
typedef enum {GM_WIN, GM_LOST, GM_UNKNOW, GM_ERROR} GAMEFLAG;

void init_board(CHESS board[SIZE][SIZE]) //初始化
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
board[i][j] = CBLANK;
}
}
}

void print_chess(CHESS board[SIZE][SIZE]) //打印棋盘
{
int i, j;

putchar(' ');
for (i=0; i < SIZE; i++)
{
printf("%2d", i+1);
}
putchar('\n');

for (i=0; i < SIZE; i++)
{
printf("%-2d", i+1);
for (j=0; j < SIZE; j++)
{
switch (board[i][j])
{
case CWHITE:
putchar('O');
break;
case CBLACK:
putchar('*');
break;
case CBLANK:
putchar('_');
break;
default:
putchar('?');
break;
}
putchar(' ');
}
putchar('\n');
}
}

void swc(CHESS chess, int *black, int *white, int *bmax, int *wmax) //判断
{
switch (chess)
{
case CBLACK:
*white = 0;
(*black)++;
break;
case CWHITE:
*black = 0;
(*white)++;
break;
case CBLANK:
*black = 0;
*white = 0;
break;
default:
break;
}

if (*black > *bmax) *bmax = *black;
if (*white > *wmax) *wmax = *white;
}

GAMEFLAG res(CHESS board[SIZE][SIZE]) //判断输赢
{
int i, j;
int win[4] = {0, 0, 0, 0};
int rblack, rwhite, cblack, cwhite,
loblack = 0, lowhite = 0,
roblack = 0, rowhite = 0,
bmax = 0, wmax = 0;

for (i=0; i < SIZE; i++)
{
rblack = 0;
rwhite = 0;
cblack = 0;
cwhite = 0;

swc(board[i][i], &loblack, &lowhite, &bmax, &wmax);
swc(board[i][SIZE-i-1], &roblack, &rowhite, &bmax, &wmax);

for (j=0; j < SIZE; j++)
{
swc(board[i][j], &rblack, &rwhite, &bmax, &wmax);
swc(board[j][i], &cblack, &cwhite, &bmax, &wmax);

}

}

if (bmax >= 3)
{
if (wmax >= 3)
{
return GM_ERROR;
}
else
{
return GM_WIN;
}
}
else
{
if (wmax >= 3)
{
return GM_LOST;
}
else
{
return GM_UNKNOW;
}
}

}

int move(CHESS board[SIZE][SIZE], CHESS chs, int x, int y)
{
int bs = 1;
if (board[x][y])
bs = 0;
else if (y >= SIZE || y < 0 || x >= SIZE || x < 0)
bs = 0;
else
board[x][y] = chs;

return bs;
}

int main()
{
CHESS b[SIZE][SIZE];
char *msg[] = {"BLACK WIN!\n", "WHITE LOST!", "NOT YET", "ERROR!!"};
char *plr[] = {"NON", "BLACK", "WHITE"};
CHESS p = CBLACK;
GAMEFLAG flg;

init_board(b);
while ((flg = res(b)) == GM_UNKNOW)
{
int x, y, bmv = 1;
system("cls");
print_chess(b);
while (bmv)
{
printf("%s回合,输入坐标:", plr[p]);
scanf("%d%d", &x, &y);
bmv = !move(b,p,x-1,y-1);
}
p = (CHESS)(CWHITE + CBLACK - p);
}

printf("%s", msg[flg]);
system("pause");

return 0;
}

有加分有注释!!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯