C++五子棋编写(控制台版_CMD命令框版)【简单五子棋游戏,适合初学者学习,代码有详细注释】
用C语言实现五子棋小游戏

⽤C语⾔实现五⼦棋⼩游戏简介本次五⼦棋使⽤的是光标控制移动,通过按空格键(键值32)来落⼦,实现游戏的。
我们额外⽤到的头⽂件有:#include<getch.h> 通过调⽤getch()函数来识别上下左右以及空格的操作。
#include<stdlib.h> 采⽤ system(“clear”);清理屏幕,实现视觉上的⾛动效果。
include<stdbool.h>stdbool.h 头⽂件对布尔类型进⾏了模拟返回 true false⼤致思路需要的数据:(全局)1、定义棋盘⼆维数组 15* 15 空位置 ' * '2、定义棋⼦⾓⾊变量⽩棋为 ' $ ' ⿊棋为 ' @ '3、定义变量⽤于记录下棋的位置业务逻辑:(实现成⼀个个函数)是否需要初始化for( ; ; ){ 1、清理屏幕、打印棋盘 2、落⼦ 坐标合法、该位置不能有棋⼦,否则继续落⼦ 3、判断是否五⼦连珠 4、交换⾓⾊}代码#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#include<getch.h>//定义棋盘char board[15][15];//⾓⾊char role='@';//落⼦坐标char key_x=7,key_y=7;//棋盘初始化void init_board(void){for(int i=0;i<15;i++){for(int j=0;j<15;j++){board[i][j]='*';}}}//打印棋盘void show_board(void){system("clear");for(int i=0;i<15;i++){for(int j=0;j<15;j++){printf(" %c",board[i][j]);printf("\n");}}//落⼦void get_key(void){for(;;){printf("\33[%d;%dH",key_x+1,2*key_y+2);//光标位置,(key_y+1)*2是因为列与列之间有空格 switch(getch()){case 183:key_x>0 && key_x--;break; //上,改变光标位置值case 184:key_x<14 && key_x++;break; //下case 185:key_y<14 && key_y++;break; //右case 186:key_y>0 && key_y--;break;//左case 32: //空格if('*'==board[key_x][key_y])//如果落⼦位置为空,落⼦{board[key_x][key_y]=role;return;}}}}//记录落⼦位置某个⽅向,连⼦的个数int count_board(int go_x,int go_y){int count=0;for(int x=key_x+go_x,y=key_y+go_y;x>=0 && y>=0 && x<15 && y<15;x+=go_x,y+=go_y){if(board[x][y]==board[key_x][key_y]){count++;}else{break;}}return count;}//判断五⼦连珠,是否胜利bool is_win(void){if(count_board(0,-1)+count_board(0,1)>=4){return true;}if(count_board(-1,-0)+count_board(1,0)>=4){return true;}if(count_board(-1,-1)+count_board(1,1)>=4){return true;}if(count_board(1,-1)+count_board(-1,1)>=4){return true;}return false;}int main(int argc,const char* argv[]){//初始化棋盘init_board();for(int i=0;i<255;i++){//清屏,打印棋盘show_board();//落⼦get_key();//是否五⼦连珠if(is_win())system("clear");show_board();printf("游戏结束,%c赢了\n",role); return 0;}//交换⾓⾊role=role=='@'?'$':'@';}}效果图以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
C语言实现简易五子棋

C语⾔实现简易五⼦棋本⽂实例为⼤家分享了C语⾔实现简易五⼦棋的具体代码,供⼤家参考,具体内容如下#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<time.h>#define ROW 5#define COL 5char g_broad[ROW][COL];void menu();void menu(){printf("-----------------------\n");printf("------1.开始游戏-------\n");printf("------0.结束游戏-------\n");printf("-----------------------\n");printf("请输⼊您的选择:");}void Init(char broad[ROW][COL]){for (int row = 0; row < ROW; ++row){for (int col = 0; col < COL; ++col){broad[row][col] = ' ';}}}void print(char broad[ROW][COL]){for (int row = 0; row < ROW; ++row){printf("| %c | %c | %c | %c | %c |\n", broad[row][0],broad[row][1], broad[row][2], broad[row][3],broad[row][4]);if (row != ROW - 1){printf(" ---|---|---|---|--- \n");}}}void playermove(char broad[ROW][COL]){printf("玩家落⼦:\n");while (1){printf("玩家请输⼊⼀组坐标:(row col)");int row = 0;int col = 0;scanf("%d %d", &row, &col);if (row < 0 || row >= ROW || col < 0 || col >= COL){printf("您输⼊的坐标⾮法,请重新输⼊:\n");continue;}else if (broad[row][col] != ' '){printf("该位置已经被占⽤,请重新输⼊:\n");continue;}else{broad[row][col] = 'x';break;}}}void computermove(char broad[ROW][COL]){printf("电脑落⼦:\n");while (1){int row = rand() % ROW;int col = rand() % COL;if (broad[row][col] != ' '){continue;}else{broad[row][col] = 'o';break;}}}int Isfull(char broad[ROW][COL]){for (int row = 0; row < ROW; ++row){for (int col = 0; col < COL; ++col){if (broad[row][col] == ' ')return 0;}}return 1;}char checkwinner(char broad[ROW][COL]) {for (int row = 0; row < ROW; ++row){if (broad[row][0] == broad[row][1]&& broad[row][0] == broad[row][2]&& broad[row][0] == broad[row][3]&& broad[row][0] == broad[row][4]&& broad[row][0] != ' '){return broad[row][0];}}for (int col = 0; col < COL; ++col){if (broad[0][col] == broad[1][col]&& broad[0][col] == broad[2][col]&& broad[0][col] == broad[3][col]&& broad[0][col] == broad[4][col]&& broad[0][col] != ' '){return broad[0][col];}}if (broad[0][0] == broad[1][1]&& broad[0][0] == broad[2][2]&& broad[0][0] == broad[3][3]&& broad[0][0] == broad[4][4]&& broad[0][0] != ' '){return broad[0][0];}if (broad[0][4] == broad[1][3]&& broad[0][4] == broad[2][2]&& broad[0][4] == broad[3][1]&& broad[0][4] == broad[4][0]&& broad[4][0] != ' '){return broad[4][0];}if (Isfull(broad)){return 'p';}elsereturn ' ';}int main(){srand((unsigned int)time(0));int input = 0;menu(g_broad); //初始化棋盘,将棋盘初始化成' 'Init(g_broad);scanf("%d", &input);char winner = ' ';while (input){//第⼀次打印棋盘print(g_broad);//玩家输⼊,提⽰玩家输⼊⼀组坐标,检查游戏是否结束 playermove(g_broad);winner = checkwinner(g_broad);if (winner != ' '){break;}//电脑输⼊,瞎下,检查游戏是否结束computermove(g_broad);winner = checkwinner(g_broad);if (winner != ' '){break;}}if (winner == 'x'){printf("玩家胜利!\n");print(g_broad);}if (winner == 'o'){printf("电脑胜利!\n");print(g_broad);}if (winner == 'p'){printf("和棋!\n");print(g_broad);}if (winner == ' '){printf("游戏结束!goodbay~\n");return 0;}return 0;}游戏运⾏结果如下:注:使⽤宏定义可以扩充棋盘更多有趣的经典⼩游戏实现专题,分享给⼤家:以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
五子棋游戏C语言

五子棋游戏程序设计(C语言实现)一、设计任务与目标设计两个人对战的五子棋游戏,游戏开始界面是显示游戏的规则,然后让用户输入命令以确定游戏是否开始,如果用户确定开始,那么要显示棋盘,接下来到了最重要的几步,两个玩家交替落子,当连续五个棋子在一条直线上时,一方赢棋,游戏结束。
其中,有些问题就是平时基本的输入输出问题,例如:游戏规则,可以直接打印。
棋盘的显示也是一般的图形输出问题,但是稍微复杂一些。
需要改进的地方和达到的目标是:1、游戏的初始界面显示的是游戏规则,当玩家确定开始的时候要清除界面来显示棋盘。
2、棋盘和棋子的显示,界面(棋子和棋盘)容易分辨,这要从颜色和图形上加以区分。
3、要求一方用‘W’(上)、‘S’(下)、‘A’(左)、‘D’(右),另一方用‘↑’、‘↓’、‘←’、‘→’来移动光标,再分别用‘Z’和‘空格’键确定落子。
4、当一方走棋时,另一方的按键应该设置为无效。
5、游戏进行时打印提示信息,当一方赢棋后,要显示赢棋的字符,并询问玩家是否继续开始。
6、可以随时退出游戏或重新开始游戏。
二、方案设计与论证首先设置游戏的初始界面,采用白色背景和红色前景,这可以调用‘conio.h’库函数实现打印游戏规则。
询问玩家是不是开始游戏,通过选择Y\N来确定。
其中会遇到这样的问题:当玩家输入的不是‘Y(y)’或者‘N(n)’时应该怎么办呢?如果采用scanf函数来接收命令,这样会显示一个不满足要求的字符,于是可以用getch函数来接收命令,判断输入的字符是否为‘Y(y)’和‘N(n)’,如果是再显示出来。
为了界面的简洁,进入游戏前先清除屏幕,调用‘system()’函数来实现。
然后打印棋盘,可以把背景设置为湖蓝色,这样棋盘和棋子更容易分辨。
游戏开始后棋盘用黑色显示,这样易于区分。
具体的思路是:由于棋盘是网格状的,所以选择一个基本图形字符串‘十’,通过循环打印而构成一张大图。
接下来确定落子的位置,这需要通过改变光标的位置来实现,考虑到是在vc6.0环境下编译文件,c语言中的有些库函数并不支持,所以选择了’gotoxy()’函数并结合‘window.h’下的函数,通过键盘按键控制达到光标移动功能。
用C语言编写的五子棋游戏

Code1. #include <graphics.h>2. #include <conio.h>3. #include <dos.h>4. #include <bios.h>5. #include <malloc.h>6. #include <stdlib.h>7. #include <stdio.h>8. #define R 10 /*The size of mouse*/9. void init(void); /* BGI initialization */10. int cover(int);/*draw lines , set color, output the text*/11. void get_board(void);12. /*using the loop and the line function to draw the chessboard*/13. void word(int); /*input word,the color is come from the rand*/14. void getmouse(int *,int *,int *);15. /*get the location and the button of mouse,key=1 is the left button,key=2 is the rightbutton*/16. void visbilemouse(void);17. /*Display the mouse*//*after typing ,gets the x, y and mouse button,then return */18. void mouse(int *,int *,int *);19. /*drawing a mouse is to put an empty rectangular in the memory , then draw theshape of the mouse in the empty rectangular*/20. void change_word(int); /*show the black or the white*/21. void help(void); /*get playing help*/22. void prompt(int); /*the cancel or quit*/23. void game_player(void); /*how to realize the game*/24. int main()25. {26. int key;27. init();/*BGI initialization*/28. key=cover(0); /*the welcome interface*/29. while(key) /*only it is 1.it will running the loop*/30. {31. get_board(); /*draw the chessboard*/32. game_player(); /*control or play the games*/33. }34. closegraph();35. return 0;36. }37. void init() /* BGI initialization */38. {39. int graphdriver = DETECT, graphmode = 0;40. /* The same effect with gd =VGA and gm = VGAHI */41. registerbgidriver(EGAVGA_driver);/* After register the BGI driver needn't the support of running the BGI */42. initgraph(&graphdriver, &graphmode, "");43. return;44. }45. int cover(int choose)46. {47. int row,col,i;48. char answer;49. switch(choose)50. {51. case 0:52. setfillstyle(SOLID_FILL,BLUE); /*fill in the color*/53. bar(630,450,10,30);54. for(row=30;row<=180;row+=30) /*draw cross lines*/55. line(10,row,160,row);56. for(col=10;col<=180;col+=30) /*draw vertical lines*/57. line(col,30,col,180);58. setcolor(BLACK);59. settextstyle(0,0,3);60. outtextxy(200,200,"loading...");61. setfillstyle(1,BLACK);62. for(i=25;i<175;i+=30)63. {64. pieslice(i,i+20,0,360,14);65. sleep(1);66. }67. for(row=30;row<=180;row+=30)68. line(480,row,630,row);69. for(col=480;col<=630;col+=30)70. line(col,30,col,180);71. setcolor(WHITE);72. settextstyle(0,0,3);73. outtextxy(200,200,"loading...");74. setfillstyle(1,WHITE);75. for(i=495;i<=615;i+=30)76. {77. pieslice(i,660-i,0,360,14);78. sleep(1);79. }80. setcolor(BLUE);81. settextstyle(0,0,3);82. outtextxy(200,200,"loading...");83. settextstyle(0,0,5);84. /* fornt :DEFAULT_FONT, TRIPLEX_FONT,SMALL_FONT,SANSSERIF_FONT,GOTHIC_FONT /direction:lateral and vertical /size */85. for(i=1;i<=21;i++)86. {87. setcolor(i); /*the color of the text*/88. outtextxy(65,100,"FIVE IN A ROW"); /*output the text*/89. }90. setcolor(6);91. settextstyle(0,0,3);92. sleep(1);93. outtextxy(50,300,"Made by Hu yin feng");94. sleep(1);95. outtextxy(100,350," Xiao xin ran");96. sleep(1);97. outtextxy(100,400," Zheng yun");98. setcolor(7);99. settextstyle(0,0,2);100. sleep(2);101. outtextxy(20,430,"would you like to try?(Y/N:)");102. answer=getch();103. break;104. case 1:105. setfillstyle(SOLID_FILL,3);106. bar(640,400,451,220);107. setcolor(BLACK);108. settextstyle(0,0,2.5);109. outtextxy(455,280,"BLACK WIN!");110. sleep(1);111. setcolor(RED);112. settextstyle(0,0,2);113. outtextxy(451,320,"Try again?");114. answer=getch();115. break;116. case 2:117. setfillstyle(SOLID_FILL,3);118. bar(640,400,451,220);119. setcolor(WHITE);120. settextstyle(0,0,2.5);121. outtextxy(455,280,"WHITE WIN!");122. sleep(1);123. setcolor(RED);124. settextstyle(0,0,2);125. outtextxy(455,320,"Try again?");126. answer=getch();127. break;128. case 3:129. setfillstyle(SOLID_FILL,3);130. bar(640,400,451,220);131. settextstyle(0,0,2.5);132. setcolor(WHITE);133. outtextxy(455,280,"A Draw!");134. sleep(1);135. setcolor(RED);136. settextstyle(0,0,2);137. outtextxy(455,320,"Try again?");138. answer=getch();139. break;140. case 4:141. cleardevice();142. setbkcolor(GREEN);143. setfillstyle(SOLID_FILL,MAGENTA);144. bar(620,450,20,30);145. setcolor(RED);146. settextstyle(0,0,5);147. outtextxy(150,100,"Game Over!");148. sleep(2);149. break;150. }151. if(answer=='Y'||answer=='y')152. return 1;153. else154. exit(0);155. return 0;156. }157. void get_board()158. {159. int row,col;160. /*setbkcolor(YELLOW);set the color of background */ 161. setfillstyle(SOLID_FILL,YELLOW);162. bar(450,480,0,0);163. setcolor(BLACK); /*set the color of lines*/164. for(row=0;row<=450;row+=30) /*Draw lines*/165. line(0,row,450,row);166. for(col=0;col<=450;col+=30) /*Draw lines*/167. line(col,0,col,480);168. setcolor(BLACK);169. circle(90,90,2);170. circle(330,90,2); /*draw four small rounds in the chessboard */171. circle(90,330,2);172. circle(330,330,2);173. setfillstyle(SOLID_FILL,GREEN);174. bar(451,0,640,480); /*filling range*/175. return;176. }177. void word(int color)/*input word*/178. {179. settextstyle(0,0,4); /*display the characters of :‘five in a row’*/180. setcolor(color);181. outtextxy(461,5,"FIVE");182. setcolor(color+1);183. outtextxy(496,45,"IN");184. setcolor(color+4);185. outtextxy(500,80,"A");186. setcolor(color+4);187. outtextxy(540,80,"ROW");188. setcolor(YELLOW);189. settextstyle(0,0,1);190. rectangle(460,450,510,470);/* the help window */191. rectangle(590,450,630,470); /* the regret window */192. rectangle(520,450,580,470); /*the exit window */193. setcolor(BLACK);194. outtextxy(470,455,"help");195. outtextxy(525,455,"cancel");196. outtextxy(595,455,"exit");197. return;198. }199. void change_word(digit)200. {201. if(digit==0)202. {203. settextstyle(0,0,2); /*when choose white then hint the next one is black */ 204. setcolor(BLACK);205. outtextxy(459,130,"THE BLACK");206. setcolor(GREEN);207. outtextxy(459,180,"THE WHITE");208. }209. else if(digit==1)210. {211. settextstyle(0,0,2);212. setcolor(GREEN);213. outtextxy(459,130,"THE BLACK");214. setcolor(WHITE); /*when choose black then hint the next one is white*/ 215. outtextxy(459,180,"THE WHITE");216. }217. return;218. }219. void help()220. {221. setfillstyle(SOLID_FILL,BLUE);222. bar(640,480,0,0);223. setcolor(YELLOW);224. rectangle(0,0,639,479);225. settextstyle(0,0,3);226. outtextxy(50,10,"How to play the game");227. settextstyle(0,0,2);228. setcolor(WHITE);229. outtextxy(10,60,"1. Clicking the mouse in the chessboard"); 230. outtextxy(10,80," to start the game black is the first , "); 231. outtextxy(10,100," the changing word on the right will "); 232. outtextxy(10,120," remind you the next person to play ;"); 233. outtextxy(10,160,"2. The side will win who form a ");234. outtextxy(10,180," continuous five chess pieces in a "); 235. outtextxy(10,200," straight line no matter of horizontal,"); 236. outtextxy(10,220," vertical and oblique.");237. outtextxy(10,260,"3. Clicking the 'regret' on the right ");238. outtextxy(10,280," is to erase the last chess you'd just "); 239. outtextxy(10,300," played ;");240. outtextxy(10,340,"4. Clicking the 'exit' is to exit the ");241. outtextxy(10,360," game ,it'll jump out another window to "); 242. outtextxy(10,380," make sure if you decide to end the "); 243. outtextxy(10,400," game,if press'y'is closing the window "); 244. outtextxy(10,420," and press 'n' is to continue the game ;"); 245. settextstyle(0,0,2);246. setcolor(RED);247. outtextxy(100,450,"Please any key to continue!");248. getch();249. return;250. }251. void prompt(number)252. {253. if(number==1)254. {255. setcolor(RED); /*the exit window*/256. setfillstyle(SOLID_FILL,BLUE);257. bar(640,400,451,220);258. settextstyle(0,0,2.5);259. outtextxy(455,300,"quit?(Y/N)");260. }261. else if(number==0)262. {263. setcolor(RED); /* the regret window*/264. setfillstyle(SOLID_FILL,BLUE);265. bar(640,400,451,220);266. settextstyle(0,0,2);267. outtextxy(480,300,"Cancel?");268. }269. return;270. }271. /*get the location and the button of mouse,key=1 is the left button*/ 272. void getmouse(int *x,int *y,int *key)273. {274. union REGS inregs,outregs;275. inregs.x.ax=3; /*Obtain the position and the state of mouse can also use 3*/ 276. int86(0x33,&inregs,&outregs); /*Interrupt calls*/277. *x=outregs.x.cx; /*The X-axis is saved in cx register */278. *y=outregs.x.dx; /*The Y-axis is saved in dx register*/279. *key=outregs.x.bx;/*Bx registers are button states*/280. return;281. }282. void visbilemouse()283. {284. union REGS inregs,outregs;285. inregs.x.ax=0x01; /*Display the mouse*/286. int86(0x33,&inregs,&outregs);287. return;288. }/*after typing ,gets the x, y and mouse button,then return */289. void mouse(int *x,int *y,int *z)/*drawing a mouse is to put an empty rectangular in the memory , then draw shape of the mouse in the empty rectangular*/290. {291. int a=0,b=0,c=0,a_old=0,b_old=0; /*it's ok to be free of the value of a and b*/ 292. int color;293. float i=0;294. int *ball; /*define a pointer to point to the store of graphics*/295. ball=malloc(imagesize(a,b,a+R,b+R)); /*Returns the size of the rectangle*/ 296. getimage(a,b,a+R,b+R,ball);/*the first time to put graphics that save an empty rectangle into the memory */ 297. while(c==0)/*loop will be ended until typein */298. {299. getmouse(&a,&b,&c);/*a,is X-axis,b is Y-axis,c is the statement ofthe key */300. if(a<0) a=0; /*ensure the left of the mouse do not out of bound*/301. if(b<0) b=0; /*ensure the up of the mouse do not out of bound*/302. if(a>getmaxx()-R) a=getmaxx()-R;/*ensure the right of the mouse do not out of bound*//*ensure the down of the mouse do not out of bound*/303. if(b>getmaxy()-R) b=getmaxy()-R;304. if(a!=a_old || b!=b_old) /*When the mouse moves*/305. {306. putimage(a_old,b_old,ball,0); /*output the graphic in a_oldandb_old to erase originally mouse*/307. getimage(a,b,a+R,b+R,ball);/*the statement is to save the location of the graph of the mouse in the ball*/ 308. setcolor(BLACK);309. setlinestyle(0,0,1);310. line(a,b,a+R,b+R/2);311. line(a,b,a+R/2,b+R);312. line(a+R,b+R/2,a+R/2,b+R);313. line(a+R*3/4,b+R*3/4,a+R,b+R);/*draw the mouse*/314. }315. a_old=a;b_old=b;316. i++;317. if(i==200000) /*Flashing frequency*/318. {319. color=rand()%16;/*use the feature of the loop of mouse will get the randon color*/320. word(color);321. i=1;/*the value of the i return to 1*/322. }323. }324. /*end of while()*/325. *x=a;*y=b;*z=c; /*return the position of the mouse after typing*/326. putimage(a,b,ball,0);/*ease the mouse ,because it is a empty retangle of default-background save in the ball */ 327. free(ball);328. return;329. }/*the main idea is through storing the present graphic in the getimage,put image and imagesize to erase the preceding mouse's graphic, we also can use clearing parts of the screen */330. void game_player()331. {332. int x,y,z,row,col,i;333. char answer;334. int address[16][15]={NULL},count[2]={0};335. int temp=0,num=1;336. visbilemouse();337. do338. {339. mouse(&x,&y,&z);340. if(x<450)/*judge whether the location of the mouse is in the keyboard*/341. {342. col=x/30;343. row=y/30;344. x=30*col+15;345. y=30*row+15;346. if(address[row][col]==0) /*whether the position is available*/347. {348. temp++; /*only accumulate in no circumstance of the pieces*/ 349. count[0]=x;350. count[1]=y;/*save the coordinate of y in an array convenient for regret*/351. if(temp%2==1)352. {353. address[row][col]=1;354. setcolor(BLACK);355. setfillstyle(1,BLACK);356. pieslice(x,y,0,360,14);/*Using the method of painting fan-shaped to draw a circle*/357. change_word(1);358. }359. else360. {361. setcolor(WHITE);362. address[row][col]=2;363. setfillstyle(1,WHITE);364. pieslice(x,y,0,360,14);365. change_word(0);366. }367. /*Judgement of the situation in a row*/368. /*make the judgment of if there's five in a row*/369. for(i=1;i<=4;i++)370. {371. if(col+i<=14)372. {373. if(address[row][col]==address[row][col+i])374. num++;375. else376. break;377. }378. else379. break;380. }381. if(num!=5)382. for(i=1;i<=4;i++)383. {384. if(col-i>=0)385. {386. if(address[row][col]==address[row][col-i]) 387. num++;388. else if(num<5)389. {390. num=1; /*the num is reassigned to 1*/ 391. break;392. }393. else394. break;395. }396. else if(num<5)397. {398. num=1;399. break;400. }401. else402. break;403. }404. /*make the judgment of if there's five in a column*/405. for(i=1;i<=4;i++)406. {407. if(row+i<=15)408. {409. if(address[row][col]==address[row+i][col])410. num++;411. else412. break;413. }414. else415. break;416. }417. if(num!=5)418. for(i=1;i<=4;i++)419. {420. if(row-i>=0)421. {422. if(address[row][col]==address[row-i][col]) 423. num++;424. else if(num<5)425. {426. num=1;427. break;428. }429. else430. break;431. }432. else if(num<5)433. {434. num=1;435. break;436. }437. else438. break;439. }440. /*make judgment of if the main diagonal line have reached the five */ 441. for(i=1;i<=4;i++)442. { if(row-i>=0&&col+i<=14)443. {444. if(address[row][col]==address[row-i][col+i]) 445. num++;446. else447. break;448. }449. else450. break;451. }452. if(num!=5)453. for(i=1;i<=4;i++)454. {455. if(col-i>=0&&row+i<=15)456. {457. if(address[row][col]==address[row+i][col-i]) 458. num++;459. else if(num<5)460. {461. num=1;462. break;463. }464. else465. break;466. }467. else if(num<5)468. {469. num=1;470. break;471. }472. else473. break;474. }475. /*make judgment of if the main diagonal line have reached the five */ 476. for(i=1;i<=4;i++)477. {478. if(row-i>=0&&col-i>=0)479. {480. if(address[row][col]==address[row-i][col-i]) 481. num++;482. else483. break;484. }485. else486. break;487. }488. if(num!=5)489. for(i=1;i<=4;i++)490. {491. if(row+i<=16&&col+i<=14)492. {493. if(address[row][col]==address[row+i][col+i]) 494. num++;495. else if(num<5)496. {497. num=1;498. break;499. }500. else501. break;502. }503. else if(num<5)504. {505. num=1;506. break;507. }508. else509. break;510. }511. if(num>=5)512. {513. cover(address[row][col]);514. return;515. }516. else if(temp==240)517. {518. cover(3);519. return;520. }521. }522. }523. else if(x>460 && x<510&&y>450&&y<470)524. {525. help();526. get_board();527. game_player();528. }529. else if(x>590 && x<630&&y>450&&y<470)530. {531. prompt(1);532. answer=getch();533. if(answer=='Y'||answer=='y')534. {535. cover(4);536. exit(0);537. }538. else539. {540. setfillstyle(SOLID_FILL,GREEN);541. bar(640,400,451,220);542. continue;543. }544. }545. else if(x>520 && x<580&&y>450&&y<470)546. {547. prompt(0);548. answer=getch();549. setfillstyle(SOLID_FILL,GREEN);550. bar(640,400,451,220);551. if(answer=='Y'||answer=='y')552. {553. setcolor(YELLOW);554. setfillstyle(1,YELLOW);555. pieslice(count[0],count[1],0,360,14); /*only regrets once*/ 556. address[row][col]=0;557. temp--;558. }559. else560. continue;561. }562. else563. continue;564. }565. while(x<640 || x>0 || y<480 || y>0); /*the range of the mouse*/ 566. return;567. }。
c语言设计五子棋代码

c语言设计五子棋代码五子棋,亦称五目、五子、顺手拍子、连珠、下五子、悔棋、先手必胜等,是一种两人对弈的纯策略型棋类游戏。
通常用黑白两色的棋子在白色棋盘上进行游戏,以先把五个棋子连成一线者获胜。
实现五子棋游戏需要完成以下步骤:1. 棋盘的绘制2. 棋子的下落判断,包括判断是否胜利3. 玩家之间的交替下棋首先是棋盘的绘制。
采用二维数组来表示棋盘,用“+”、“-”、“|”等符号来绘制边框和交叉点。
同时还要用“ ”表示空白区域,用“●”和“○”代表黑白棋子。
然后是棋子的下落判断。
需要记录下棋子的位置,并判断落子是否在棋盘范围内,以及是否与已落下的棋子位置冲突。
在判断胜负时,需要遍历棋盘上所有的落子情况,检查是否存在连续的五个同色棋子。
最后是玩家之间的交替下棋。
可以使用一个计数器来记录当前下棋者,每次下完一子后,计数器加1,判断下一个下棋者,直到游戏结束为止。
下面是一份实现五子棋游戏的 C 代码:#include <stdio.h>#include <stdlib.h>#define ROW 15 // 棋盘行数#define COL 15 // 棋盘列数#define BLACK '●' // 黑色棋子#define WHITE '○' // 白色棋子#define SPACE ' ' // 空白区域char board[ROW][COL]; // 棋盘二维数组// 初始化棋盘void init_board() {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {board[i][j] = SPACE;}}}// 打印棋盘void print_board() {for (int i = 0; i < ROW; i++) {for (int j = 0; j < COL; j++) {if (j == 0) printf("%2d ", i + 1); printf("%c ", board[i][j]);}printf("\n");}for (int i = 0; i < COL; i++) {if (i == 0) printf(" ");printf("%c ", 'A' + i);}printf("\n");}// 判断是否胜利int is_win(int row, int col, char ch) {int count = 1;// 判断横向for (int i = col - 1; i >= 0; i--) { if (board[row][i] == ch) {count++;} else {break;}}for (int i = col + 1; i < COL; i++) { if (board[row][i] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断纵向count = 1;for (int i = row - 1; i >= 0; i--) { if (board[i][col] == ch) {count++;} else {break;}}for (int i = row + 1; i < ROW; i++) {if (board[i][col] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断左上到右下斜向count = 1;for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == ch) {count++;} else {break;}}for (int i = row + 1, j = col + 1; i < ROW && j < COL; i++, j++) { if (board[i][j] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}// 判断左下到右上斜向count = 1;for (int i = row + 1, j = col - 1; i < ROW && j >= 0; i++, j--) { if (board[i][j] == ch) {count++;} else {break;}}for (int i = row - 1, j = col + 1; i >= 0 && j < COL; i--, j++) { if (board[i][j] == ch) {count++;} else {break;}}if (count >= 5) {return 1;}return 0;}。
C语言五子棋代码

//五子棋小游戏纯C语言代码#include <stdio.h>#define N 14char state[N][N];void init(void);void printState(void);bool isWin(bool isBlack,int x,int y);bool isLevelWin(bool isBlack,int x,int y);bool isVerticalWin(bool isBlack,int x,int y);bool isLeftInclinedWin(bool isBlack,int x,int y);bool isRightObliqueWin(bool isBlack,int x,int y);bool isWin(bool isBlack,int x,int y)//是否有获胜{return isLevelWin(isBlack,x,y)||isVerticalWin(isBlack,x,y)||isLeftInclinedWin(isBlack,x,y)||isRightObliqueWin(isBlack,x,y);}bool isLevelWin(bool isBlack,int x,int y)//确定水平直线上是否有五子连珠{char c = isBlack ? '@':'O';int count;while(y>0 && state[x][y] == c){y--;}count =0;if(state[x][y] == c) count = 1;y++;while(y < N && state[x][y] == c){count++;if(count == 5){return true;}y++;}return false;}bool isVerticalWin(bool isBlack,int x,int y)//确定竖直直线是否有五子连珠{char c = isBlack ? '@':'O';int count;while(x>0 && state[x][y] == c){x--;}count =0;if(state[x][y] == c) count = 1;x++;while(x < N && state[x][y] == c){count++;if(count == 5){return true;}x++;}return false;}bool isLeftInclinedWin(bool isBlack,int x,int y)//确定左斜线是否有五子连珠{char c = isBlack ? '@':'O';int count;while(x>0 && y>0 && state[x][y] == c){y--;x--;}count =0;if(state[x][y] == c) count = 1;x++;y++;while(x < N && y < N && state[x][y] == c){count++;if(count == 5){return true;}x++;y++;}return false;}bool isRightObliqueWin(bool isBlack,int x,int y)//确定右斜线是否有五子连珠{char c = isBlack ? '@':'O';int count;while(x>0 && y<N && state[x][y] == c){y++;x--;}count =0;if(state[x][y] == c) count = 1;x++;y--;while(x < N && y >= 0 && state[x][y] == c){count++;if(count == 5){return true;}x++;y--;}return false;}void init(void)//开局初始化数组{int i,j;for(i=0;i<N;i++){for(j=0;j<N;j++){state[i][j] = '*';}}}void printState(void)//打印棋盘{int i,j;printf("%3c",' ');for(i=0;i<N;i++)printf("%3d",i);printf("\n");for(i=0;i<N;i++){printf("%3d",i);for(j=0;j<N;j++){printf("%3c",state[i][j]);}printf("\n");}}int main(void){int x,y;bool isBlack = true;init();printf("五子棋小游戏\n\n@代表黑子,0代表白子,*代表棋盘空白\n");printf("------------------------------------------------------\n");printState();while(1){printf("请%s 方走棋:\n",(isBlack?"黑":"白"));//请黑(白)方走棋的说明printf("输入所下位置坐标,如: 1-2\n");//走棋方法示例scanf("%d-%d",&x,&y);if(state[x][y]=='@' || state[x][y]=='O')//若此点已经存在棋子,则重新下一步棋在别处{printf("this position to have pieces\n");continue;}state[x][y] = (isBlack?'@':'O');//规定@代表黑子,0代表白子printState();//打印棋盘情况if(isWin(isBlack,x,y))//每下一步棋,判断一次是否有人获胜{printf("%s 方胜利\n",(isBlack?"黑":"白"));break;}isBlack = !isBlack;}}。
用C开发一个迷你的五子棋游戏

用C开发一个迷你的五子棋游戏迷你五子棋游戏是一种简化版的五子棋游戏,它具有简洁的界面和简单易懂的规则,非常适合初学者。
本文将介绍如何使用C语言开发一个迷你的五子棋游戏,以供大家学习参考。
一、游戏规则迷你五子棋游戏的规则非常简单,玩家分别执黑子和白子,轮流在棋盘上落子。
当任一玩家在横、竖、斜四个方向上连成五个自己的棋子时,即可获胜。
若棋盘填满且无一方连成五子,则为平局。
二、游戏实现为了实现迷你五子棋游戏,我们需要使用C语言和一些基本的图形库函数。
在开始编写代码之前,我们先引入必要的头文件,并定义一些常量和全局变量。
#include <stdio.h>#include <conio.h>#include <graphics.h>#define ROW 10 // 棋盘行数#define COL 10 // 棋盘列数#define SIZE 40 // 每个格子的大小#define WIDTH COL*SIZE // 棋盘的宽度#define HEIGHT ROW*SIZE // 棋盘的高度int chessboard[ROW][COL]; // 棋盘数组,用来保存棋盘中每个位置的棋子信息int currentPlayer; // 当前玩家,0代表黑子,1代表白子接下来,我们需要进行一些初始化操作,包括创建图形窗口、初始化棋盘和设置玩家初始状态。
void init() {int driver, mode;driver = VGA;mode = VGAHI;initgraph(&driver, &mode, "");setbkcolor(WHITE);cleardevice();currentPlayer = 0;memset(chessboard, 0, sizeof(chessboard));}在绘制棋盘的函数中,我们使用for循环和线段绘制函数line()来绘制出棋盘的网格。
五子棋C语言代码

#include "graphics.h" /*图形系统头文件*/#define LEFT 0x4b00 /*光标左键值*/#define RIGHT 0x4d00 /*光标右键值*/#define DOWN 0x5000 /*光标下键值*/#define UP 0x4800 /*光标上键值*/#define ESC 0x011b /* ESC键值*/#define ENTER 0x1c0d /* 回车键值*/int a[8][8]={0},key,score1,score2;/*具体分数以及按键与存放棋子的变量*/ char playone[3],playtwo[3];/*两个人的得分转换成字符串输出*/void playtoplay(void);/*人人对战函数*/void DrawQp(void);/*画棋盘函数*/void SetPlayColor(int x);/*设置棋子第一次的颜色*/void MoveColor(int x,int y);/*恢复原来棋盘状态*/int QpChange(int x,int y,int z);/*判断棋盘的变化*/void DoScore(void);/*处理分数*/void PrintScore(int n);/*输出成绩*/void playWin(void);/*输出胜利者信息*//******主函数*********/void main(void){int gd=DETECT,gr;initgraph(&gd,&gr,"c:\\tc"); /*初始化图形系统*/DrawQp();/*画棋盘*/playtoplay();/*人人对战*/getch();closegraph();/*关闭图形系统*/}void DrawQp()/*画棋盘*/{int i,j;score1=score2=0;/*棋手一开始得分都为0*/setbkcolor(BLUE);for(i=100;i<=420;i+=40){line(100,i,420,i);/*画水平线*/line(i,100,i,420); /*画垂直线*/}setcolor(0);/*取消圆周围的一圈东西*/setfillstyle(SOLID_FILL,15);/*白色实体填充模式*/fillellipse(500,200,15,15); /*在显示得分的位置画棋*/setfillstyle(SOLID_FILL,8); /*黑色实体填充模式*/fillellipse(500,300,15,15);a[3][3]=a[4][4]=1;/*初始两个黑棋*/a[3][4]=a[4][3]=2;/*初始两个白棋*/setfillstyle(SOLID_FILL,WHITE);fillellipse(120+3*40,120+3*40,15,15);fillellipse(120+4*40,120+4*40,15,15);setfillstyle(SOLID_FILL,8);fillellipse(120+3*40,120+4*40,15,15);fillellipse(120+4*40,120+3*40,15,15);score1=score2=2; /*有棋后改变分数*/DoScore();/*输出开始分数*/}void playtoplay()/*人人对战*/{int x,y,t=1,i,j,cc=0;while(1)/*换棋手走棋*/{x=120,y=80;/*每次棋子一开始出来的坐标,x为行坐标,y为列坐标*/ while(1) /*具体一个棋手走棋的过程*/{PrintScore(1);/*输出棋手1的成绩*/PrintScore(2);/*输出棋手2的成绩*/SetPlayColor(t);/*t变量是用来判断棋手所执棋子的颜色*/fillellipse(x,y,15,15);key=bioskey(0);/*接收按键*/if(key==ESC)/*跳出游戏*/break;elseif(key==ENTER)/*如果按键确定就可以跳出循环*/{if(y!=80&&a[(x-120)/40][(y-120)/40]!=1&&a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置没有棋子*/{if(t%2==1)/*如果是棋手1移动*/a[(x-120)/40][(y-120)/40]=1;else/*否则棋手2移动*/a[(x-120)/40][(y-120)/40]=2;if(!QpChange(x,y,t))/*落子后判断棋盘的变化*/{a[(x-120)/40][(y-120)/40]=0;/*恢复空格状态*/cc++;/*开始统计尝试次数*/if(cc>=64-score1-score2) /*如果尝试超过空格数则停步*/{MoveColor(x,y);fillellipse(x,y,15,15);break;}elsecontinue;/*如果按键无效*/}DoScore();/*分数的改变*/break;/*棋盘变化了,则轮对方走棋*/ }else/*已经有棋子就继续按键*/continue;}else /*四个方向按键的判断*/if(key==LEFT&&x>120)/*左方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x-=40;fillellipse(x,y,15,15);}elseif(key==RIGHT&&x<400&&y>80)/*右方向键*/ {MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x+=40;fillellipse(x,y,15,15);}elseif(key==UP&&y>120)/*上方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);y-=40;fillellipse(x,y,15,15);}elseif(key==DOWN&&y<400)/*下方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);y+=40;fillellipse(x,y,15,15);}}if(key==ESC)/*结束游戏*/break;if((score1+score2)==64||score1==0||score2==0)/*格子已经占满或一方棋子为0判断胜负*/{playWin();/*输出最后结果*/break;}t=t%2+1; /*一方走后,改变棋子颜色即轮对方走*/cc=0; /*计数值恢复为0*/} /*endwhile*/}void SetPlayColor(int t)/*设置棋子颜色*/{if(t%2==1)setfillstyle(SOLID_FILL,15);/*白色*/elsesetfillstyle(SOLID_FILL,8);/*灰色*/}void MoveColor(int x,int y)/*走了一步后恢复原来格子的状态*/{if(y<100)/*如果是从起点出发就恢复蓝色*/setfillstyle(SOLID_FILL,BLUE);else/*其他情况如果是1就恢复白色棋子,2恢复黑色棋子,或恢复蓝色棋盘*/ switch(a[(x-120)/40][(y-120)/40]){case 1:setfillstyle(SOLID_FILL,15);break; /*白色*/case 2:setfillstyle(SOLID_FILL,8);break; /*黑色*/default:setfillstyle(SOLID_FILL,BLUE); /*蓝色*/}}int QpChange(int x,int y,int t)/*判断棋盘的变化*/{int i,j,k,kk,ii,jj,yes;yes=0;i=(x-120)/40; /*计算数组元素的行下标*/j=(y-120)/40; /*计算数组元素的列下标*/SetPlayColor(t);/*设置棋子变化的颜色*//*开始往8个方向判断变化*/if(j<6)/*往右边*/{for(k=j+1;k<8;k++)if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格结束*/ break;if(a[i][k]!=0&&k<8){for(kk=j+1;kk<k&&k<8;kk++)/*判断右边*/{a[i][kk]=a[i][j]; /*改变棋子颜色*/fillellipse(120+i*40,120+kk*40,15,15);}if(kk!=j+1) /*条件成立则有棋子改变过颜色*/yes=1;}}if(j>1)/*判断左边*/{for(k=j-1;k>=0;k--)if(a[i][k]==a[i][j]||!a[i][k])break;if(a[i][k]!=0&&k>=0){for(kk=j-1;kk>k&&k>=0;kk--){a[i][kk]=a[i][j];fillellipse(120+i*40,120+kk*40,15,15);}if(kk!=j-1)yes=1;}}if(i<6)/*判断下边*/{for(k=i+1;k<8;k++)if(a[k][j]==a[i][j]||!a[k][j])break;if(a[k][j]!=0&&k<8){for(kk=i+1;kk<k&&k<8;kk++){a[kk][j]=a[i][j];fillellipse(120+kk*40,120+j*40,15,15);}if(kk!=i+1)yes=1;}}if(i>1)/*判断上边*/{for(k=i-1;k>=0;k--)if(a[k][j]==a[i][j]||!a[k][j])break;if(a[k][j]!=0&&k>=0){for(kk=i-1;kk>k&&k>=0;kk--){a[kk][j]=a[i][j];fillellipse(120+kk*40,120+j*40,15,15); }if(kk!=i-1)yes=1;}}if(i>1&&j<6)/*右上*/{for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++) if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]&&k>=0&&kk<8){for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15); }if(ii!=i-1)yes=1;}}if(i<6&&j>1)/*左下*/{for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--) if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&k<8&&kk>=0){for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i+1)yes=1;}}if(i>1&&j>1)/*左上*/{for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&k>=0&&kk>=0){for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i-1)yes=1;}}if(i<6&&j<6)/* 右下*/{for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&kk<8&&k<8){for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i+1)yes=1;}}return yes;/*返回是否改变过棋子颜色的标记*/ }void DoScore()/*处理分数*/{int i,j;score1=score2=0;/*重新开始计分数*/for(i=0;i<8;i++)for(j=0;j<8;j++)if(a[i][j]==1)/*分别统计两个人的分数*/score1++;elseif(a[i][j]==2)score2++;}void PrintScore(int playnum)/*输出成绩*/{if(playnum==1)/*清除以前的成绩*/{setfillstyle(SOLID_FILL,BLUE);bar(550,100,640,400);}setcolor(RED);settextstyle(0,0,4);/*设置文本输出样式*/if(playnum==1)/*判断输出哪个棋手的分,在不同的位置输出*/ {sprintf(playone,"%d",score1);outtextxy(550,200,playone);}else{sprintf(playtwo,"%d",score2);outtextxy(550,300,playtwo);}setcolor(0);}void playWin()/*输出最后的胜利者结果*/{settextstyle(0,0,4);setcolor(12);if(score2>score1)/*开始判断最后的结果*/outtextxy(100,50,"black win!");elseif(score2<score1)outtextxy(100,50,"white win!");elseouttextxy(60,50,"you all win!");}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C++五子棋Ver2.1程序+代码[带AI、悔棋等]【控制台版】这是用C++编写的一个简单五子棋游戏,带AI(电脑)、悔棋等功能。
支持单人游戏(与电脑对战)和双人对战。
使用的C++知识也很简单,只需要懂基本的语法,不需要用到API。
这个游戏不是图形界面的。
是DOS版或者说控制台版的。
游戏的运行界面如下:这其实是第三版,添加了悔棋,修复AI[电脑]的几个BUG,以及悔棋的一个小问题。
当然了,若是还有什么BUG可以指出,我们的QQ:775904764,有问题也可以找我。
代码如下://--------------------------------------------------------------------------//// 简单五子棋(控制台版)代码[Ver2.1]// 修复了AI越界的问题,简化评分函数,修复输赢判断函数的BUG。
// AI评分函数规则有部分改变,代码也更加简练。
// 新增悔棋功能,仅与AI对战时可用,新增重新开始功能。
// 修复悔棋的一个小BUG,修复AI随机落子部分的定义错误。
// 作者:落叶化尘 QQ:775904764//--------------------------------------------------------------------------//#include "五子棋类.h"#include <time.h>#include <iostream>using namespace std;static const char ch[11][4]={"┌","┬","┐","├","┼","┤","└","┴","┘","○","●"}; //可供选用的字符,输出棋盘或者棋子用static int Record[15][15]={0}; //记录棋子static int Score[15][15]={0}; //评分static int Sum=0,Renum=0;int main(){int Default[15][15]={ 0,1,1,1,1,1,1,1,1,1,1,1,1,1,2, //空棋盘3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,3,4,4,4,4,4,4,4,4,4,4,4,4,4,5,6,7,7,7,7,7,7,7,7,7,7,7,7,7,8 };int value[15][15]={0};FiveChess Start;do{Start.StartGame(value,Default); //开始游戏}while (Start.Continue); //若是要退出就修改这个值return 0;}//------------------------------开始游戏------------------------------------------// void FiveChess::StartGame(int value[][15],int Default[][15]){Empty(value,Default); //初始化棋盘adExWhile=true;while(ExWhile){Choice=MainMenu(); //调用主菜单,并把用户选择的值储存在Choice里switch(Choice){case 1:flag=1;ExWhile=false;break; //把标志设置为1,与AI对战case 2:flag=2;ExWhile=false;break; //把标志设置为2,双人对战case 3:Select=Introduce();ExWhile=Select;Continue=false;break; //游戏说明函数case 4:Select=Coder();ExWhile=Select;Continue=false;break; //作者介绍函数case 5:Exit();ExWhile=false;Continue=false;break; //退出游戏函数default:cin.fail();cin.clear();cin.sync();cout<<"输出错误,请重新输入!\n";system("pause>nul 2>nul");ExWhile=true;break; //其它}}if (flag==1) //如果标志等于1,说明为单人游戏{Single(value);}else if (flag==2) //如果标志等于2,说明为双人游戏{Double(value);}flag=0;}//------------------------清空棋盘或初始化棋盘-----------------------------------// void FiveChess::Empty(int value[][15],int Default[][15]) //清空棋盘{for (int i=0;i<15;i++){for (int j=0;j<15;j++){value[i][j]=Default[i][j];}}}void FiveChess::Exit() //退出游戏{system("cls");cout<<"┌—————————————————————————┐\n";cout<<"│感谢您使用:简单五子棋 Ver2.1 │\n";cout<<"││\n";cout<<"│[游戏制作]:落叶化尘│\n";cout<<"│[联系Q Q ]: 775904764 │\n";cout<<"│[联系邮箱]:***************│\n";cout<<"│[简单说明]:游戏结束界面,谢谢您的使用!│\n";cout<<"└—————————————————————————┘\n";cout<<"感谢您的使用,谢谢!按任意键退出游戏...\n";system("pause>nul 2>nul");}bool FiveChess::Introduce() //游戏介绍{system("cls");cout<<"┌—————————————————————————┐\n";cout<<"│简单五子棋 Ver2.1[游戏介绍界面] │\n";cout<<"││\n";cout<<"│[单人游戏]:玩家和AI(人工智能)对战,AI智商不高。
│\n";cout<<"││\n";cout<<"│[双人游戏]: 可以两个玩家一起对战,可能不是很方便。
│\n";cout<<"││\n";cout<<"│[游戏方法]:输入X Y坐标落子,输入双-1悔棋。
│\n";cout<<"││\n";cout<<"│[特别提示]:若游戏中想突然退出,按Ctrl+C即可中断。
│\n";cout<<"││\n";cout<<"└—————————————————————————┘\n";cout<<"是否返回?[Y-返回 N-退出]:";char choice;cin>>choice;if (choice=='N'||choice=='n'){cout<<"感谢您的使用,谢谢!按任意键退出游戏...\n";system("pause>nul 2>nul");return false;}else{return true;}}bool FiveChess::Coder() //作者介绍{system("cls");cout<<"┌—————————————————————————┐\n";cout<<"│简单五子棋 Ver2.1[作者介绍界面] │\n";cout<<"││\n";cout<<"│[游戏制作]:落叶化尘│\n";cout<<"│[联系Q Q ]: 775904764 │\n";cout<<"│[联系邮箱]:***************│\n";cout<<"│[简单介绍]:游戏制作简陋,还请见谅^_^!│\n"; cout<<"│[附加说明]:作者正在努力学习C++中,嘿嘿~ │\n";cout<<"└—————————————————————————┘\n";cout<<"是否返回?[Y-返回 N-退出]:";char choice;cin>>choice;if (choice=='N'||choice=='n'){cout<<"感谢您的使用,谢谢!按任意键退出游戏...\n";system("pause>nul 2>nul");return false;}else{return true;}}//-------------------------------选择主菜单-----------------------------//int FiveChess::MainMenu(){system("cls"); //清屏system("title 五子棋游戏 By--落叶化尘 QQ:775904764"); //修改窗口标题system("color 3f"); //定义窗口颜色system ("mode con cols=56 lines=20"); //设置窗口大小cout<<"┌—————————————————————————┐\n";cout<<"│简单五子棋 Ver2.1 │\n";cout<<"│[1]、单人游戏│\n";cout<<"││\n";cout<<"│[2]、双人游戏│\n";cout<<"││\n";cout<<"│[3]、游戏介绍│\n";cout<<"││\n";cout<<"│[4]、关于作者│\n";cout<<"││\n";cout<<"│[5]、退出│\n";cout<<"└—————————————————————————┘\n";cout<<"请选择:";int select;cin>>select;return select; //返回用户选择的值}//--------------------------------胜利界面--------------------------------// void FiveChess::Victory(int Num){system ("mode con cols=56 lines=20"); //设置窗口大小system("cls");if (Num==1){system("color 1f");cout<<"┌—————————————————————————┐\n";cout<<"│恭喜玩家1[黑棋]获得胜利!│\n";cout<<"││\n";cout<<"│[胜利玩家]:玩家1 │\n";cout<<"│[所属棋子]: 黑色│\n";cout<<"│[祝福话语]:不错哦^_^!恭喜您获得了胜利!│\n";cout<<"││\n";cout<<"└—————————————————————————┘\n"; }else if (Num==2){system("color 2f");cout<<"┌—————————————————————————┐\n";cout<<"│恭喜玩家2[白棋]获得胜利!│\n";cout<<"││\n";cout<<"│[胜利玩家]:玩家2 │\n";cout<<"│[所属棋子]: 白色│\n";cout<<"│[祝福话语]:恭喜您获得了胜利,再接再厉哈^_^!│\n";cout<<"││\n";cout<<"└—————————————————————————┘\n"; }else if(Num==3){system("color 3f");cout<<"┌—————————————————————————┐\n";cout<<"│恭喜您,获得了胜利│\n";cout<<"││\n";cout<<"│[胜利玩家]:自己│\n";cout<<"│[所属棋子]: 黑色│\n";cout<<"│[祝福话语]:不错哦,打败了AI,恭喜!│\n";cout<<"││\n";cout<<"└—————————————————————————┘\n"; }else if(Num==4){system("color 5f");cout<<"┌—————————————————————————┐\n";cout<<"│非常遗憾,您输了! │\n";cout<<"││\n";cout<<"│[胜利玩家]:AI(电脑)│\n";cout<<"│[所属棋子]: 白色│\n";cout<<"│[祝福话语]:加油吧,下次一定要把AI打败哦! │\n";cout<<"││\n";cout<<"└—————————————————————————┘\n"; }else{system("color 5f");cout<<"┌—————————————————————————┐\n";cout<<"│势均力敌——平局│\n";cout<<"││\n";cout<<"│[胜利玩家]:无[平局] │\n";cout<<"│[所属棋子]: 黑色或白色│\n";cout<<"│[祝福话语]:棋盘居然被走满了,不错不错~!│\n";cout<<"││\n";cout<<"└—————————————————————————┘\n";}cout<<"是否返回主界面?[Y-返回 N-退出]:";char choice;cin>>choice;if (choice=='N'||choice=='n'){cout<<"感谢您的使用,谢谢!按任意键退出游戏...\n";system("pause>nul 2>nul");Continue=false;}else{Continue=true;}}//----------------------------悔棋-------------------------------//void FiveChess::Retract(int &x1,int &y1,int &x2,int &y2,int value[][15]) {if (Sum>=1){Sum--;}if ((x1==-1)&&(y1==-1)&&(x2==-1)&&(y2==-1)){cout<<"当前无法悔棋,棋盘上没有棋子!";system("pause>nul");}else if (Renum>=6){cout<<"悔棋超过6次,为了公平,您不能再次悔棋!";system("pause>nul");}else{Renum++;if (x1==0&&y1==0){value[x1][y1]=0;Record[x1][y1]=0;}else if (x1==0&&y1==14){value[x1][y1]=2;Record[x1][y1]=0;}else if (x1==14&&y1==0){value[x1][y1]=6;Record[x1][y1]=0;}else if (x1==14&&y1==14){value[x1][y1]=8;Record[x1][y1]=0;}else if (x1==14&&(y1>0&&y1<14)){value[x1][y1]=7;Record[x1][y1]=0;}else if (y1==14&&(x1>0&&x1<14)){value[x1][y1]=5;Record[x1][y1]=0;}else if (x1==0&&(y1>0&&y1<14)){value[x1][y1]=1;Record[x1][y1]=0;}else if (y1==0&&(x1>0&&x1<14)){value[x1][y1]=3;Record[x1][y1]=0;}else {value[x1][y1]=4;Record[x1][y1]=0;}if (x2==0&&y2==0){value[x2][y2]=0;Record[x2][y2]=0;}else if (x2==0&&y2==14){value[x2][y2]=2;Record[x2][y2]=0;}else if (x2==14&&y2==0){value[x2][y2]=6;Record[x2][y2]=0;}else if (x2==14&&y2==14){value[x2][y2]=8;Record[x2][y2]=0;}else if (x2==14&&(y2>0&&y2<14)){value[x2][y2]=7;Record[x2][y2]=0;}else if (y2==14&&(x2>0&&x2<14)){value[x1][y2]=5;Record[x2][y2]=0;}else if (x2==0&&(y2>0&&y2<14)){value[x2][y2]=1;Record[x2][y2]=0;}else if (y2==0&&(x2>0&&x2<14)){value[x2][y2]=3;Record[x2][y2]=0;}else {value[x2][y2]=4;Record[x2][y2]=0;}cout<<"每盘最多允许悔棋6次,且最多只能回到上一步!";system("pause>nul");}}//-----------------------------判断用户输入的坐标合法性-------------------------// int FiveChess::Position(int *x,int *y,int value[][15]){if (cin.fail()||*x<0||*x>=15||*y<0||*y>=15){cin.clear();cin.sync();return 1; //返回1表示不在范围内或者输入的不是数字}else if (value[*x][*y]==9||value[*x][*y]==10){return 2; //存在棋子}else{return 0; //返回 0 表示坐标正常}}//---------------------------判断该哪个玩家先走----------------------------//int FiveChess::NextPlayer(int value[][15]){int i,j;int player1=0,player2=0;for (i=0;i<15;i++){for (j=0;j<15;j++){if (value[i][j]==10){player1++; //统计棋盘上面黑子的个数}else if (value[i][j]==9){player2++; //统计棋盘上白子的个数}}}if (player1>player2||player1<player2){return 2; //该玩家2走了(白子)}else{return 1; //该玩家1走了(黑子)}}//---------------------------判断棋子落在这点,是否胜利或者平局-----------------------------------------//int FiveChess::Win(int *x,int *y,int ChessNum,int value[][15]){int i,j,k; //返回-1:平局返回0:没有谁胜利返回1:传递过来ChessNum号的那一方胜利int number=0;Sumchess=0;for (i=-1;i<=1;i++){for (j=-1;j<=1;j++) //循环8次,分别计算这个点的8个方向是否构成5连{if (i!=0 || j!=0) //i或者j等于0,不就代表的是ChessNum的值嘛{for (k=1;k<5;k++) //循环4次{ //这是不越界判断//存在棋子数目+1if(*x+k*i>=0 &&*x+k*i<=14 && *y+k*j>=0 &&*y+k*j<=14 &&value[*x+k*i][*y+k*j]==ChessNum){number++;} //如果这个点有棋子else {break;} //没有棋子就跳出}for (k=-1;k>-5;k--) //与上面相对的方向的判断{if(*x+k*i>=0 &&*x+k*i<=14 && *y+k*j>=0 &&*y+k*j<=14 && value[*x+k*i][*y+k*j]==ChessNum){number++;}else {break;} //没有棋子就跳出}if(number>=4) //如果>=4就说明5连了,返回ChessNum表示传递过来的这个玩家得下子点能构成5个,胜利了{return 1;}else{number=0; //清空统计}}}}for (i=0;i<15;i++) //棋盘是否满了{for(j=0;j<15;j++){if (value[i][j]==9||value[i][j]==10){Sumchess++; //统计黑子和白子的总个数}}}if (Sumchess>=225){return -1; //棋盘满了}else{return 0; //没胜利也没和棋}}//---------------------------------绘制棋盘、落子函数-----------------------------------------//void FiveChess::PrintBord(int *x,int *y,int value[][15],int player){system("cls");int i,j;if (player==1){value[*x][*y]=9; //黑棋}else if (player==2){value[*x][*y]=10; //白棋}cout<<" 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4\n"; //输出参考坐标for (i=0;i<15;i++){if(i!=0){cout<<" "<<i%10;}else{cout<<" 0";}for (j=0;j<15;j++){cout<<ch[value[i][j]];}cout<<"\n";}}//------------------------------------双人对战-----------------------------// void FiveChess::Double(int value[][15]){cout<<"您选择了双人对战模式,请按任意键继续!";system("pause>nul 2>nul");system("cls");system ("mode con cols=43 lines=20");system("color 3f");system("title 五子棋-双人对战模式");PrintBord(&x,&y,value,-1); //先绘制个空棋盘ExWhile=true; //退出循环用的memset(Record,0,sizeof(Record)); //先把记录落子的数组清0while(ExWhile){int play=NextPlayer(value); //先得到该哪个玩家走棋了cout<<"请(玩家"<<play<<")输入坐标[用空格隔开]:";cin>>x>>y;Check=Position(&x,&y,value); //检测坐标合法性if (Check==0) //坐标没问题{PrintBord(&x,&y,value,play); //再绘制落子棋盘Record[x][y]=play; //记录这个点,1表示黑子,2表示白子cout<<"玩家["<<play<<"]的棋子落在了:[X:"<<x<<"] [Y:"<<y<<"]\n";Check1=Win(&x,&y,play,Record); //检测是否有一方胜利或者和棋if (Check1==-1) //和棋{cout<<"游戏棋盘满啦,还没分出胜负,都很厉害哈!\n";cout<<"本次游戏结束,按任意键继续...";system("pause>nul 2>nul");Victory(5); //跳转胜利界面ExWhile=false;}else if(Check1==1) //当前玩家paly胜利{cout<<"恭喜[玩家"<<play<<"]获得胜利!\n";cout<<"本次游戏结束,按任意键继续...";system("pause>nul 2>nul");Victory(play); //跳转胜利界面ExWhile=false;}else //没谁胜利{ExWhile=true; //不退出循环}}else if(Check==1){cout<<"输入范围错误,请重新输入!\n";ExWhile=true;}else{cout<<"您输入的坐标已经存在棋子,请重新输入!\n";ExWhile=true;}}}//-------------------------------与AI对战------------------------------//void FiveChess::Single(int value[][15]){cout<<"您选择了与AI对战的模式,请按任意键继续!";system("pause>nul 2>nul");system("cls");system ("mode con cols=43 lines=20");system("color 3f");system("title 五子棋-与AI的对战模式");PrintBord(&x,&y,value,-1); //先绘制个空棋盘ExWhile=true; //退出循环用的Sum=0,Renum=0;memset(Record,0,sizeof(Record)); //先把记录落子的数组清0int x1=-1,y1=-1,x2=-1,y2=-1;while(ExWhile){ExWhile1=true;cout<<"请[玩家]输入坐标[双(-1)为悔棋]:";cin>>x>>y;Check=Position(&x,&y,value);if ((x==-1)&&(y==-1)) //悔棋{Retract(x1,y1,x2,y2,value);PrintBord(&x,&y,value,-1); //绘制落子棋盘ExWhile=true;}else if (Check==0) //坐标没问题{value[x][y]=9;Record[x][y]=1; //记录这个点,1表示黑子,2表示白子x1=x;y1=y; //记录当前坐标悔棋用Sum++;if (Win(&x,&y,1,Record)==0){Robot(&x,&y,&Sum,value); //调用AI函数PrintBord(&x,&y,value,2); //绘制落子棋盘x2=x;y2=y; //记录当前坐标悔棋用cout<<"玩家棋子数:"<<Sum<<" 电脑的棋子落在了[X:"<<x<<"] [Y:"<<y<<"]\n";//cout<<"玩家坐标:"<<x1<<" "<<y1<<"电脑坐标:"<<x2<<" "<<y2<<endl;if (Win(&x,&y,2,Record)==1){cout<<"非常遗憾,您输了,AI获得了胜利!\n";cout<<"本次游戏结束,按任意键继续...";system("pause>nul 2>nul");Victory(4); //跳转胜利界面ExWhile=false;}else if(Win(&x,&y,2,Record)==-1){cout<<"游戏棋盘满啦,还没分出胜负,都很厉害哈!\n";cout<<"本次游戏结束,按任意键继续...";system("pause>nul 2>nul");Victory(5); //跳转平局界面ExWhile=false;}else {ExWhile=true;}}else if(Win(&x,&y,1,Record)==1){PrintBord(&x,&y,value,-1); //绘制棋盘cout<<"恭喜[玩家]获得胜利!\n";cout<<"本次游戏结束,按任意键继续...";system("pause>nul 2>nul");Victory(3); //跳转胜利界面ExWhile=false;}else if(Win(&x,&y,1,Record)==-1){PrintBord(&x,&y,value,-1); //绘制棋盘cout<<"游戏棋盘满啦,还没分出胜负,都很厉害哈!\n";cout<<"本次游戏结束,按任意键继续...";system("pause>nul 2>nul");Victory(5); //跳转平局界面ExWhile=false;}}else if(Check==1){cout<<"输入范围错误,请重新输入!\n";}else{cout<<"您输入的坐标已经存在棋子,请重新输入!\n";}}}//-----------------------------电脑落子函数-------------------------------// void FiveChess::Robot(int *x,int *y,int *Sum,int value[][15]){ExWhile1=true;if (*Sum==1){while(ExWhile1){ChessOne(*x,*y,value);if (Position(x,y,value)==0){ExWhile1=false;}}Record[*x][*y]=2; //记录这个点}else //从第2步开始,使用评分系统{Findscore(*x,*y);}}//-----------------------------查找评分最高的坐标-------------------------// void FiveChess::Findscore(int &x,int &y){srand( (unsigned)time( NULL ) );int i,j,x1,x2,y1,y2,lx;int Max=0;ChessScore(); //调用评分函数for (i=0;i<15;i++){for (j=0;j<15;j++){if(Score[i][j]>Max){Max=Score[i][j]; //获取所有点中,评分最高的x1=i;y1=j;}}}x2=x1;y2=y1;for (i=0;i<15;i++) //可能的话,有评分相同的多个点{for (j=0;j<15;j++){if(Score[i][j]==Max&&i!=x2&&j!=y2) //在这么多个相同分数的点中,随机找一个{lx=rand()%10;if (lx<5){x2=i,y2=j;break;}}}}if (x2!=x1 || y2!=y1) //棋盘上有2个最高分{lx=rand()%10; //随机一个^_^if (lx>6){x=x1,y=y1;}else{x=x2,y=y2;}}else //棋盘上只有一个最高分{x=x1,y=y1;}Max=0; //清空最大值Record[x][y]=2; //记录这个点}//------------------------------玩家走第1步时的落子-------------------------------// inline void FiveChess::ChessOne(int &x,int &y,int value[][15]){int i,j;srand( (unsigned)time( NULL ) ); //随机数随着时间的改变而改变for (i=0;i<15;i++){for(j=0;j<15;j++){if (value[i][j]==9) //如果找到了玩家的棋子,在它的8个方的任意一点落子{int lx=rand()%7;if(lx==0){x=i+1;y=j+1;if (Position(&x,&y,value)==0){break;}}else if(lx==1){x=i+1;y=j-1;if (Position(&x,&y,value)==0){break;}}else if(lx==2){x=i-1;y=j-1;if (Position(&x,&y,value)==0){break;}}else if(lx==3){x=i-1;y=j+1;if (Position(&x,&y,value)==0){break;}}else if(lx==4){x=i-1;y=j; //上if (Position(&x,&y,value)==0){break;}}else if(lx==5){x=i;y=j-1; //左if (Position(&x,&y,value)==0){break;}}else if(lx==6){x=i;y=j+1; //右if (Position(&x,&y,value)==0){break;}}else{x=i+1;y=j; //下if (Position(&x,&y,value)==0){break;}}}}}}//-----------------------------------AI评分函数---------------------------------------//void FiveChess::ChessScore(){int x,y,i,j,k; //循环变量int number1=0,number2=0; //number用来统计玩家或电脑棋子连成个数int empty=0; //empty用来统计空点个数memset(Score,0,sizeof(Score)); //把评分数组先清零for(x=0;x<15;x++){for(y=0;y<15;y++){if(Record[x][y]==0) //如果这个点为空{for(i=-1;i<=1;i++){for(j=-1;j<=1;j++) //判断8个方向{if(i!=0 || j!=0) //若是都为0的话,那不就是原坐标嘛{//-------------------------------------对玩家落点评分---------------------------------------//for(k=1;i<=4;k++) //循环4次{ //这点没越界//且这点存在黑子(玩家)if(x+k*i>=0 && x+k*i<=14 && y+k*j>=0 && y+k*j<=14 && Record[x+k*i][y+k*j]==1 ){number1++;}else if(Record[x+k*i][y+k*j]==0){empty++;break;} //这点是个空点,+1后退出else {break;} //否则是墙或者对方的棋子了}for(k=-1;k>=-4;k--) //向它的相反方向判断{ //这点没越界 //且这点存在黑子(玩家)if(x+k*i>=0 && x+k*i<=14 && y+k*j>=0 && y+k*j<=14 && Record[x+k*i][y+k*j]==1 ){number1++;}else if(Record[x+k*i][y+k*j]==0){empty++;break;} //同上条else {break;}}if(number2==1) {Score[x][y]+=1;} //2个棋子else if(number1==2) //3个棋子{if(empty==1) {Score[x][y]+=5;} //有一个空点+5分 //死3else if(empty==2) {Score[x][y]+=10;} //有两个空点+10分 //活3}else if(number1==3) //4个棋子{if(empty==1) {Score[x][y]+=20;} //有一个空点+20分 //死4else if(empty==2) {Score[x][y]+=100;} //有2个空点+100分 //活4}else if(number1>=4) {Score[x][y]+=1000;} //对方有5个棋子,分数要高点,先堵empty=0; //统计空点个数的变量清零//---------------------------------------对电脑落点评分---------------------------------------//for(k=1;i<=4;k++) //循环4次{ //这点没越界 //且这点存在白子(电脑)if(x+k*i>=0 && x+k*i<=14 && y+k*j>=0 && y+k*j<=14 && Record[x+k*i][y+k*j]==2 ){number2++;}else if(Record[x+k*i][y+k*j]==0){empty++;break;} //空点else {break;}}for(k=-1;k>=-4;k--) //向它的相反方向判断{if(x+k*i>=0 && x+k*i<=14 && y+k*j>=0 && y+k*j<=14 && Record[x+k*i][y+k*j]==2 ){number2++;}else if(Record[x+k*i][y+k*j]==0){empty++;break;}else {break;} //这里顶上判断玩家的都做解释了,不再介绍}if (number2==0) {Score[x][y]+=1;} //1个棋子else if(number2==1) {Score[x][y]+=2;} //2个棋子else if(number2==2) //3个棋子{if(empty==1) {Score[x][y]+=8;} //死3else if(empty==2) {Score[x][y]+=30;} //活3}else if(number2==3) //4个棋子{if(empty==1) {Score[x][y]+=50;} //死4else if(empty==2) {Score[x][y]+=200;} //活4}else if(number2>=4) {Score[x][y]+=10000;} //自己落在这点能形成5个,也就能胜利了,分数最高number1=0;number2=0; //清零,以便下次重新统计empty=0;}}}}}}}下载后,文件内容如下:这个游戏用到了C++基本的语法知识,包括:类class、指针、循环语句、引用、多维数组。