主题:[原创]黑白棋 游戏
/*
黑白棋 1.0 文本界面
程序 经TC2.0 运行通过
*/
#define WHITE 1
#define BLACK -1
#define NULL 0
#define PASS 1
#define OK 0
#define DEBUG 0 /*设置为 1时,可以看见包裹线 用于调试程序*/
struct {
int color;
int value; /*优先级 0, 1 -> 9 1的优先级最高 9最低 0为边界值*/
int index; /*包裹线 坐标在 dstak 中的次序*/
}room[10][10]={
{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
{{0,0,0},{0,1,0},{0,8,0},{0,2,0},{0,4,0},{0,4,0},{0,2,0},{0,8,0},{0,1,0},{0,0,0}},
{{0,0,0},{0,8,0},{0,9,0},{0,7,0},{0,6,0},{0,6,0},{0,7,0},{0,9,0},{0,8,0},{0,0,0}},
{{0,0,0},{0,2,0},{0,7,0},{0,3,1},{0,5,8},{0,5,7},{0,3,4},{0,7,0},{0,2,0},{0,0,0}},
{{0,0,0},{0,4,0},{0,6,0},{0,5,1},{0,0,0},{0,0,0},{0,5,6},{0,6,0},{0,4,0},{0,0,0}},
{{0,0,0},{0,4,0},{0,6,0},{0,5,2},{0,0,0},{0,0,0},{0,5,5},{0,6,0},{0,4,0},{0,0,0}},
{{0,0,0},{0,2,0},{0,7,0},{0,3,2},{0,5,3},{0,5,4},{0,3,3},{0,7,0},{0,2,0},{0,0,0}},
{{0,0,0},{0,8,0},{0,9,0},{0,7,0},{0,6,0},{0,6,0},{0,7,0},{0,9,0},{0,8,0},{0,0,0}},
{{0,0,0},{0,1,0},{0,8,0},{0,2,0},{0,4,0},{0,4,0},{0,2,0},{0,8,0},{0,1,0},{0,0,0}},
{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}
};
/*dstak[value][index] 存储包裹线 坐标*/
struct {
int x;
int y;
}dstak[9][8]={
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{3,3},{6,3},{6,6},{3,6},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{4,3},{5,3},{6,4},{6,5},{5,6},{4,6},{3,5},{3,4}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}
};
int tos[9]={0,0,4,0,8,0,0,0,0};
int com=2,you=2;
void updatescr();
int check(int x, int y, int stdc, int draw);
int search(int *px, int *py, int stdc);
void push(int x, int y, int value);
void del(int value, int index);
main()
{
int x, y, dx, dy, count, state=OK, stdc=BLACK;
room[4][4].color=room[5][5].color=BLACK;
room[4][5].color=room[5][4].color=WHITE;
system("cls");
while(1){
updatescr();
if( (count=search(&x, &y, stdc)) > 0){
if(stdc==BLACK){
do{
if(count==0) printf("Input again!\n");
scanf("%d%d",&x,&y);
printf("\n");
if(x<1 || x>8 || y<1 || y>8){
printf("x,y must be in 1~8\n");
continue;
}
updatescr();
}while((count=check(x,y,stdc,0))==0);
printf("You:%2d%2d\n",x,y);
count=check(x,y,stdc,1);
you+=count;
com-=count-1;
}else{ /*WHITE*/
printf("Com:%2d%2d\n",x,y);
count=check(x,y,stdc,1);
com+=count;
you-=count-1;
}
/*扩展包裹线 */
for(dx=-1; dx<=1; dx++){
for(dy=-1; dy<=1; dy++){
if(dx==0 && dy==0) continue;
if(room[x+dx][y+dy].color==NULL && room[x+dx][y+dy].value>0 && room[x+dx][y+dy].index==0){
push(x+dx,y+dy,room[x+dx][y+dy].value);
}
}
}
del(room[x][y].value,room[x][y].index);
state=OK;
}else if(state==OK){
state=PASS;
printf("%d PASS\n",stdc);
}else if(state==PASS){
break;
}
stdc=-stdc;
}
if(you>com){
printf("You win\n");
}else if(you<com){
printf("Com win\n");
}else{
printf("Tie\n");
}
getch();
}
黑白棋 1.0 文本界面
程序 经TC2.0 运行通过
*/
#define WHITE 1
#define BLACK -1
#define NULL 0
#define PASS 1
#define OK 0
#define DEBUG 0 /*设置为 1时,可以看见包裹线 用于调试程序*/
struct {
int color;
int value; /*优先级 0, 1 -> 9 1的优先级最高 9最低 0为边界值*/
int index; /*包裹线 坐标在 dstak 中的次序*/
}room[10][10]={
{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
{{0,0,0},{0,1,0},{0,8,0},{0,2,0},{0,4,0},{0,4,0},{0,2,0},{0,8,0},{0,1,0},{0,0,0}},
{{0,0,0},{0,8,0},{0,9,0},{0,7,0},{0,6,0},{0,6,0},{0,7,0},{0,9,0},{0,8,0},{0,0,0}},
{{0,0,0},{0,2,0},{0,7,0},{0,3,1},{0,5,8},{0,5,7},{0,3,4},{0,7,0},{0,2,0},{0,0,0}},
{{0,0,0},{0,4,0},{0,6,0},{0,5,1},{0,0,0},{0,0,0},{0,5,6},{0,6,0},{0,4,0},{0,0,0}},
{{0,0,0},{0,4,0},{0,6,0},{0,5,2},{0,0,0},{0,0,0},{0,5,5},{0,6,0},{0,4,0},{0,0,0}},
{{0,0,0},{0,2,0},{0,7,0},{0,3,2},{0,5,3},{0,5,4},{0,3,3},{0,7,0},{0,2,0},{0,0,0}},
{{0,0,0},{0,8,0},{0,9,0},{0,7,0},{0,6,0},{0,6,0},{0,7,0},{0,9,0},{0,8,0},{0,0,0}},
{{0,0,0},{0,1,0},{0,8,0},{0,2,0},{0,4,0},{0,4,0},{0,2,0},{0,8,0},{0,1,0},{0,0,0}},
{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}
};
/*dstak[value][index] 存储包裹线 坐标*/
struct {
int x;
int y;
}dstak[9][8]={
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{3,3},{6,3},{6,6},{3,6},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{4,3},{5,3},{6,4},{6,5},{5,6},{4,6},{3,5},{3,4}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}
};
int tos[9]={0,0,4,0,8,0,0,0,0};
int com=2,you=2;
void updatescr();
int check(int x, int y, int stdc, int draw);
int search(int *px, int *py, int stdc);
void push(int x, int y, int value);
void del(int value, int index);
main()
{
int x, y, dx, dy, count, state=OK, stdc=BLACK;
room[4][4].color=room[5][5].color=BLACK;
room[4][5].color=room[5][4].color=WHITE;
system("cls");
while(1){
updatescr();
if( (count=search(&x, &y, stdc)) > 0){
if(stdc==BLACK){
do{
if(count==0) printf("Input again!\n");
scanf("%d%d",&x,&y);
printf("\n");
if(x<1 || x>8 || y<1 || y>8){
printf("x,y must be in 1~8\n");
continue;
}
updatescr();
}while((count=check(x,y,stdc,0))==0);
printf("You:%2d%2d\n",x,y);
count=check(x,y,stdc,1);
you+=count;
com-=count-1;
}else{ /*WHITE*/
printf("Com:%2d%2d\n",x,y);
count=check(x,y,stdc,1);
com+=count;
you-=count-1;
}
/*扩展包裹线 */
for(dx=-1; dx<=1; dx++){
for(dy=-1; dy<=1; dy++){
if(dx==0 && dy==0) continue;
if(room[x+dx][y+dy].color==NULL && room[x+dx][y+dy].value>0 && room[x+dx][y+dy].index==0){
push(x+dx,y+dy,room[x+dx][y+dy].value);
}
}
}
del(room[x][y].value,room[x][y].index);
state=OK;
}else if(state==OK){
state=PASS;
printf("%d PASS\n",stdc);
}else if(state==PASS){
break;
}
stdc=-stdc;
}
if(you>com){
printf("You win\n");
}else if(you<com){
printf("Com win\n");
}else{
printf("Tie\n");
}
getch();
}