【小游戏】黑白棋
作者:互联网
#include <stdio.h>
#include <cstring>
struct REVERSI_BOARD {
int b[9][9];
int tot, host;
void init() {
memset(b,0,sizeof(b));
b[4][4] = b[5][5] = -1;
b[4][5] = b[5][4] = 1;
tot = 4, host = 1;
}
void put() {
printf(" 1 2 3 4 5 6 7 8\n");
printf(" -------------------------\n");
for(int i = 1;i <= 8;++i) {
printf("%d : ",i);
for(int j = 1;j <= 8;++j) {
if(b[i][j] == 1) {
printf("|○");
} else if(b[i][j] == -1) {
printf("|●");
} else {
printf("| ");
}
}
printf("|\n");
printf(" -------------------------\n");
}
}
bool check() {
return tot != 64;
}
bool arround(int x,int y) {
if(b[x+1][y] == -host)
return false;
if(b[x-1][y] == -host)
return false;
if(b[x][y+1] == -host)
return false;
if(b[x][y-1] == -host)
return false;
return true;
}
bool conduct(int x,int y) {
if(x < 0||x > 8) {
printf("illegal step.\n");
return true;
}
if(y < 0||y > 8) {
printf("illegal step.\n");
return true;
}
if(b[x][y] != 0) {
printf("illegal step.\n");
return true;
}
if(arround(x,y)) {
printf("illagal step.\n");
return true;
}
b[x][y] = host;
for(int i = 1;x+i <= 8&&b[x+i][y];++i)
if(b[x+i][y] == host) {
for(int j = x+1;j < x+i;++j)
b[j][y] = host;
break;
}
for(int i = 1;x-i >= 1&&b[x-i][y];++i)
if(b[x-i][y] == host) {
for(int j = x-i+1;j < x;++j)
b[j][y] = host;
break;
}
for(int i = 1;y-i >= 1&&b[x][y-i];++i)
if(b[x][y-i] == host) {
for(int j = y-i+1;j < y;++j)
b[x][j] = host;
break;
}
for(int i = 1;y+i <= 8&&b[x][y+i];++i)
if(b[x][y+i] == host) {
for(int j = y+1;j < y+i;++j)
b[x][j] = host;
break;
}
for(int i = 1;x+i <= 8&&y+i <= 8&&b[x+i][y+i];++i)
if(b[x+i][y+i] == host) {
for(int j = 1;j < i;++j)
b[x+j][y+j] = host;
break;
}
for(int i = 1;x-i >= 1&&y-i >= 1&&b[x-i][y-i];++i)
if(b[x-i][y-i] == host) {
for(int j = 1;j < i;++j)
b[x-j][y-j] = host;
break;
}
for(int i = 1;x-i >= 1&&y+i <= 8&&b[x-i][y+i];++i)
if(b[x-i][y+i] == host) {
for(int j = 1;j < i;++j)
b[x-j][y+j] = host;
break;
}
for(int i = 1;x+i <= 8&&y-i >= 1&&b[x+i][y-i];++i)
if(b[x+i][y-i] == host) {
for(int j = 1;j < i;++j)
b[x+j][y-j] = host;
break;
}
tot++;
host *= -1;
return false;
}
bool judge() {
int cnt_white = 0, cnt_black = 0;
for(int i = 1;i <= 8;++i)
for(int j = 1;j <= 8;++j) {
if(b[i][j] == -1)
cnt_black++;
else
cnt_white++;
}
return cnt_black-cnt_white;
}
} rb;
void Reversi() {
REVERSI_BOARD now;
now.init();
int site_x, site_y;
now.put();
while(now.check()) {
if(now.host == 1)
printf("black's turn.\n");
else
printf("white's turn.\n");
scanf("%d %d",&site_x,&site_y);
while(now.conduct(site_x,site_y))
scanf("%d %d",&site_x,&site_y);
now.put();
}
if(now.judge() > 0)
printf("black win!\n");
else if(now.judge() < 0)
printf("white win!\n");
else
printf("none.\n");
}
signed main() {
Reversi();
}
标签:黑白棋,return,int,++,host,小游戏,&&,printf 来源: https://www.cnblogs.com/bikuhiku/p/16357083.html