普通

来源:互联网 发布:2010总决赛第七场数据 编辑:程序博客网 时间:2024/06/09 22:45
五子棋:

五子棋是世界智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏,是世界智力运动会竞技项目之一,通常双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成5子连线者获胜。

棋具与围棋通用,起源于中国上古时代的传统黑白棋种之一。主要流行于华人和汉字文化圈的国家以及欧美一些地区,是世界上最古老的棋。

容易上手,老少皆宜,而且趣味横生,引人入胜;不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。已在各个游戏平台有应用。

现在就请你来编写一个普通的程序,来实现五子棋这款游戏。


这里仅仅只做参考, 毕竟博主还很弱弱的对吧。

轮流来下棋,先黑棋后白棋,然后在判断五子棋时从斜、行、列三种情形进行判断。

好像代码就这么简单,哎呀不多说了,贴完代码博主就默默地溜了。(大佬勿喷)

代码:

// making by zhouzhuan #include <bits/stdc++.h>using namespace std;const int x = 20; // 棋盘行数 const int y = 20; // 棋盘列数char a[x + 5][y + 5]; // 制造棋盘// printf chessvoid print_chess(){// printf listprintf("   ");for (int i = 1; i <= x; i++)printf("%3d", i);puts("");for (int i = 1; i <= y; i++){// printf lineprintf("%3d", i);for (int j = 1; j <= x; j++)printf("%3c", a[i][j]);puts("");}return ;}// get black chessvoid black_chess(){int xx, yy;print_chess();printf("Black : Please write (x, y) : ");scanf("%d%d", &xx, &yy);if (xx < 1 || xx > x || yy < 1 || yy > y){printf("overstep the boundary !\n");//Sleep(1000);system("cls");black_chess();}else if (a[xx][yy] == 'B' || a[xx][yy] == 'W'){printf("repeat !\n");//Sleep(1000);system("cls");black_chess();}// black chesselse a[xx][yy] = 'B';//Sleep(500);system("cls");return ;}// get white chessvoid white_chess(){int xx, yy;print_chess();printf("White : Please write (x, y) : ");scanf("%d%d", &xx, &yy);if (xx < 1 || xx > x || yy < 1 || yy > y){printf("overstep the boundary !\n");//Sleep(1000);system("cls");black_chess();}else if (a[xx][yy] == 'B' || a[xx][yy] == 'W'){printf("repeat !\n");//Sleep(1000);system("cls");black_chess();}// white chesselse a[xx][yy] = 'W';//Sleep(500);system("cls");return ;}// check winbool check(){// slash checkfor (int i = 3; i <= x - 2; i++)for (int j = 3; j <= y - 2; j++)if (a[i][j] != '-' && a[i - 2][j - 2] == a[i - 1][j - 1] && a[i - 1][j - 1] == a[i][j] && a[i][j] == a[i + 1][j + 1] && a[i + 1][j + 1] == a[i + 2][j + 2]) return true;else if (a[i][j] != '-' && a[i - 2][j + 2] == a[i - 1][j + 1] && a[i - 1][j + 1] == a[i][j] && a[i][j] == a[i + 1][j - 1] && a[i + 1][j - 1] == a[i + 2][j - 2]) return true;// line checkfor (int i = 1; i <= x; i++)for (int j = 3; j <= y - 2; j++)if (a[i][j] != '-' && a[i][j - 2] == a[i][j - 1] && a[i][j - 1] == a[i][j] && a[i][j] == a[i][j + 1] && a[i][j + 1] == a[i][j + 2])return true;// list checkfor (int i = 3; i <= x - 2; i++)for (int j = 1; j <= y; j++)if (a[i][j] != '-' && a[i - 2][j] == a[i - 1][j] && a[i - 1][j] == a[i][j] && a[i][j] == a[i + 1][j] && a[i + 1][j] == a[i + 2][j]) return true;return false;}int main(){memset(a, '-', sizeof(a));while (true){// first : blackblack_chess();if (check()){printf("Black win !\n");break;}// second : whitewhite_chess();if (check()){printf("White win !\n");break;}}return 0;}
不就是才100多行嘛,习以为常了。。。