UVA 220 Othello——模拟

网友投稿 594 2022-11-29

UVA 220 Othello——模拟

UVA 220 Othello——模拟

1的问题:

{多1少1} 是程序员经常犯的错误,写判断条件时一定要注意写i、i-1、i+1中的哪一个,<、<=、==、>、>=中的哪一个;

然后再来看这道题,算法不难,关键是细节,忽视了任何一个小细节就可能wa,注意:

1 输入问题,注意getchar吃\n;

2 边界问题,处理时保证不越界,推荐写一个函数专门判断;

3 执行M时别忘了把新的棋子放到棋盘上;

4 只有两子之间的棋子才会翻转,一个子和边界夹住的棋子不翻转,这个体现在样例2中;

5 放新子后保证其八个方向的棋子如果能翻转则都要翻转(我一开始判断时只能令一个方向的棋子翻转,因此wa了一遍);

6 执行完M后换人;

7 活用函数,不然代码会很长很乱。

#include #include #include char othello[15][15];int position[15][15];char player, rival;int flag;int yuejie(int i, int j, int x, int y){ if((i+x) >= 1 && (i+x) <= 8 && (j+y) >= 1 && (j+y) <= 8) return 1; else return 0;}void saveposition(int x,int y, int i, int j){ int a = x - i, b = y - j; for(x = x+a, y = y+b; yuejie(0,0,x,y); x+=a,y+=b){ if(othello[x][y] == player) break; else if(othello[x][y] == '-'){ position[x][y] = 1; flag++; break; } }}void change(int x, int y, int i, int j){ int judge = 0; int a = x - i, b = y - j; for(x = x+a, y = y+b; yuejie(0,0,x,y); x+=a,y+=b){ if(othello[x][y] == player){ judge = 1; break; } else if(othello[x][y] == '-') break; } if(judge == 1){ for(i = i+a, j = j+b; ; i+=a,j+=b){ if(othello[i][j] == player) break; othello[i][j] = player; } }}void L(void){ if(flag == 0) printf("No legal move."); else{ int i, j, kongge = 0; for(i = 1; i <= 8; i++){ for(j = 0; j <= 8; j++){ if(position[i][j] == 1){ if(kongge != 0) printf(" "); printf("(%d,%d)",i,j); kongge++; } } } } printf("\n");}void M(int i, int j){ char temp; if(flag == 0){//换玩家 temp = player; player = rival; rival = temp; } int x, y; for(x = -1; x <= 1; x++) for(y = -1; y <= 1; y++){ if(yuejie(i,j,x,y) && othello[i+x][j+y] == rival) change(i+x,j+y,i,j); } othello[i][j] = player; int countw = 0, countb = 0; for(i = 1; i <= 8; i++) for(j = 1; j <= 8; j++){ if(othello[i][j] == 'W') countw++; else if(othello[i][j] == 'B') countb++; } printf("Black - %2d White - %2d\n",countb,countw); temp = player; player = rival; rival = temp;//换玩家}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);/*输入*/ int konghang = 0; int T; scanf("%d",&T); getchar(); while(T--){ if(konghang != 0) printf("\n"); konghang++; memset(othello,'-',sizeof(othello)); int i,j;//输入棋盘 for(i = 1; i <= 8; i++){ for(j = 1; j <=8; j++){ othello[i][j] = getchar(); } getchar(); } player = getchar();//输入玩家 if(player == 'W') rival = 'B'; else if(player == 'B') rival = 'W'; else {printf("error!\n");return 0;} getchar();/*指令*/ char command[10]; while( gets(command) && command[0] != 'Q'){ memset(position,0,sizeof(position)); flag = 0; for(i = 1; i <= 8; i++) for(j = 1; j <= 8; j++){ if(othello[i][j] == player){ int x, y; for(x = -1; x <= 1; x++) for(y = -1; y <= 1; y++){ if(yuejie(i,j,x,y) && othello[i+x][j+y] == rival) saveposition(i+x,j+y,i,j); } } } if(command[0] == 'L') L(); else if(command[0] == 'M') M(command[1] - '0', command[2] - '0'); memset(command,0,sizeof(command)); }/*输出*/ for(i = 1; i <= 8; i++){ for(j = 1; j <=8; j++){ printf("%c",othello[i][j]); } printf("\n"); } } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:UVA 1374 Power Calculus——暴力打表
下一篇:POJ 1061 青蛙的约会——扩展欧几里得
相关文章

 发表评论

暂时没有评论,来抢沙发吧~