# 黑白棋

#include <stdio.h>
#include <graphics.h>
#define OFF 0
#define ON  1
#define isvalid(x,y) ((x>-1) && (x<8) && (y>-1) && (y<8))
#define PLAYER1 0
#define PLAYER2 1
#define BLACK 0
#define WHITE 1
#define SPACE 2
#define X(0)
#define X(k)   (X(k-1) + STEP)
#define Y(0)
#define Y(k)   (Y(k-1) + STEP)
void change(int x, int y, const int m, const int n);
int cboard[8][8];
int color1, color2;
static int i, j;
int player;
main()
{
init();
while(1) {
drawc();
rmvp(i, j);
if (player == PLAYER1)
player = PLAYER2;
else
player = PLAYER1;
key = bioskey();
switch (key) {
case W:
if (isvalid(i, --j)
mvp(X(i), Y(j);
else {
++j;
continue;
}
break;
case S:  if (isvalid(i, ++j)
mvp(X(i), Y(j);
else {
--j;
continue;
}
break;
case A: if (isvalid(--i, j)
mvp(X(i), Y(j);
else {
++i;
continue;
}
break;
case D: if (isvalid(++i, j)
mvp(X(i), Y(j);
else {
--i;
continue;
}
break;
case ESC: exit();
default:
mvp(i, j);
continue;
}
if ((check(i, j, player))
continue;
if (end())
exit();
}
drawscore();
}

int check(int x, int y, int player)
{
if (!isvalid(x,y))
return 0;
if (cboard[x][y] != SPACE)
return 0;

if (player == PLAYER1)
color1 = BLACK, color2 = WHITE;
else
color1 = WHITE, color2 = BLACK;

change(x, y, 0, 1);
change(x, y, 0, -1);
change(x, y, 1, 0);
change(x, y, -1, 0);
change(x, y, 1, 1);
change(x, y, -1, -1);
change(x, y, 1, -1);
change(x, y, -1, 1);

return 1;
}

/* ............*/
void change(int x, int y, const int m, const int n)
{
int hx, hy;
int state;

state = OFF;
for (hx = x+m, hy = y+n; isvalid(hx,hy); hx+=m, hy+=n)
if (cboard[hx][hy] == color2) {
state = ON;
continue;
} else if (cboard[hx][hy] == color1)
break;
else {
state = OFF;
break;
}
if (state == ON) {
for ( ; hx != x && hy != y; hx-=m, hy-=n)
cboard[hx][hy] = color1;
cboard[hx][hy] = color1;
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：黑白棋 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)