c语言报告五子棋
五子棋实验报告(含代码)

实验报告实验一五子棋游戏北方工业大学 2013级计算机技术米鹏一、实验原理及方法五子棋游戏开发借用Visual Studio 2012软件开发平台,选用C#语言进行编写。
整体程序主要分为三部分:界面操作部分、AI逻辑部分和棋子定点分析部分。
1、界面操作部分界面操作部分代码主要针对图像呈现、对应矩阵存储、下棋过程控制等可见的操作环节进编写。
同时负责整个程序的初始化工作。
图像呈现采用C#中Graphics进行绘制。
棋盘被划分为15行15列,每个划分出的小方格均为30*30的正方形,棋盘可操作的范围规定在(20,20)、(460,460)两点的确定的正方形区域内。
通过鼠标左击来确定下子地点。
程序会根据鼠标鼠标点击的位置进行计算,计算得到时对应矩阵的行列,之后再改变对应矩阵的内容后,在通过行列值乘以小方格边长计算得到在显示区域中的具体位置,再稍加变动后画到显示区域中。
以X点坐标为例,下面是计算X(Column)的流程图:在对应矩阵存储方面,后面AI逻辑和棋子分析所用到的矩阵都是来源这里。
同时AI 逻辑和棋子分析不能去修改对应矩阵内容。
图像呈现点的位置、重绘的根据都是来源这里。
在下棋过程控制方面采用信号亮的机制,当操作者下过后,根据信号AI会立即计算将要下点的位置同时改变信号亮变量。
当AI下过棋子后,由于信号亮的的限制就等待操作者去下棋,同时改变信号亮变量内容。
AI和操作者的所有下子、修改矩阵、显示棋子的过程都是统一的。
在每一盘游戏开始时程序会对一些重要的变量进行初始化这里包括矩阵、信号亮、第一步棋子颜色、呈现图像等内容进行初始化。
同时AI会在棋盘中央下第一子。
2、AI逻辑部分AI逻辑部分算是整个程序策略的灵魂。
其中的一些关键性判别的前后关系将影响AI 的下棋的结果。
同时加大和降低AI的难度也是这里。
下面是我设计的策略过程:从下棋者的考虑角度进行考虑,尽可能保证每一次下子都是有必要的、都是在情理当中的。
我所设计的策略并不是完整,漏洞在与没有考虑三棋子连续的情况。
C语言图形五子棋课程设计分析报告

C语言课程设计报告五子棋院系:软件学院专业:软件工程摘要五子棋是一种两人对弈的纯策略型棋类游戏,应用C语言编写程序可以在计算机上实现二人对弈五子棋功能。
二人对弈五子棋程序由图像生成、光标移动与落子、判断胜负和系统帮助等子程序构成;程序中应用了结构体、数组、全局变量、按键处理和图形编程等元素和语句。
程序通过棋盘和棋子图像生成、二人移子与落子和判断胜负等功能的实现,在计算机上实现了二人五子棋对弈。
Abstract目录第1章:需求分析五子棋背景传统五子棋的与围棋相同,分为黑白两色,棋盘为15×15,棋子放置于棋盘线上。
两人对局,各执一色,轮流下一子,先将横、竖或斜线的5个或5个以上同色棋子连成不间断的一排者为胜。
因为传统五子棋在落子后不能移动或拿掉,所以也可以用纸和笔来进行游戏。
程序设计本程序设计为人与人对弈,一方执黑棋,一方执白棋,轮流走棋,每方都试图在游戏结束前让自己的棋子五子相连,首先实现五子相连的一方获胜。
程序执行过程中,要求棋盘、棋子时时可见,并且人可以通过按键盘按键移动光标,摆放棋子。
程序需求分析根据功能需求,将程序分为图形显示、玩家控制、胜负判断和玩家计分四个模块,以下分析各模块的需求。
图形显示模块:程序开始运行时,给出欢迎及帮助界面;游戏开始后要求生成19×19的棋盘图像,并在棋盘上方提示当前落子方棋子颜色,游戏进行过程中,要求实时显示棋盘上已落下的棋子;分出胜负后,要求给出游戏结束画面。
玩家控制模块:程序开始时,需玩家确定而后开始游戏;游戏过程中,两个玩家通过不同的按键移动光标,选择落子;游戏结束时,有玩家选择是否开始新棋局。
胜负判断模块:实时监测棋盘上棋子,一旦某一色棋子出现五子连线,终止游戏程序,并着色连成一线的五子,弹出该色玩家胜出界面。
玩家计分模块:一方玩家再胜利后通过对文件的操作进行计分,并输出于计分板上。
程序流程设计根据程序需求分析结果,可以得出程序的总体结构图如图1,程序总体流程图如图2。
c语言五子棋课程设计报告

c语言五子棋课程设计报告一、前言五子棋作为一种智力游戏,在我国有着悠久的历史和广泛的群众基础。
本次设计的目的是使用C语言实现五子棋游戏,通过此次课程设计,学生将掌握C语言的编程思想和开发方法,同时了解五子棋游戏的规则和策略。
二、五子棋游戏规则五子棋游戏是在15*15的棋盘上进行,由黑白双方轮流落子,先将五个同色棋子连成一条线的一方获胜。
落子的位置必须为空,不能与已有的棋子重叠。
黑方先手,白方后手。
下图为五子棋棋盘示意图。
三、五子棋游戏实现本次课程设计采用C语言实现五子棋游戏,主要包括棋盘的绘制、落子的判断和胜负的判断等功能。
1.棋盘的绘制棋盘的绘制采用双重循环实现,将15*15的棋盘分成225个小格,其中包括14个横线和14个竖线,以及4个角上的点。
通过循环输出字符实现棋盘的绘制。
2.落子的判断落子的判断主要包括鼠标的点击和棋子的绘制两个部分。
当鼠标点击棋盘上的一个位置时,程序会根据当前轮到哪方落子来绘制相应颜色的棋子,并将该位置的状态改为已有棋子。
同时,程序会检查当前落子是否符合规则,即该位置是否为空,如果不为空则重新等待鼠标点击。
3.胜负的判断胜负的判断主要包括横向、纵向、斜向和反斜向四个方向。
通过判断当前落子位置在这四个方向上的连续棋子数是否达到五个来确定胜负。
如果达到五个,则程序会弹出相应的提示框,显示胜利方。
四、总结通过本次课程设计,学生掌握了C语言的编程思想和开发方法,同时了解了五子棋游戏的规则和策略。
本次设计主要包括棋盘的绘制、落子的判断和胜负的判断等功能。
希望本次课程设计能够增强学生的编程能力和对五子棋游戏的理解。
用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五子棋实验报告篇一:五子棋对战实验报告实验项目五子棋网络对战和聊天实验日期XX0406实验报告要求:一、实验目的:学习和使用socket编程,熟练软件开发二、实验原理:使用socket进行网络通信,java作为编程语言三、实验要求:编写五子棋程序可以实现联机网络对战,并且可以进行聊天四、实验步骤、结果(程序+注释+截图)及分析:首先拟定编程语言与开发方案,选择java语言,考虑到java可以跨平台运行,然后决定把这个程序拆分为客户端、服务器两个部分,每个部分再分成5个小的部分实现不同功能。
1、然后考虑使用java的swing包,创建ClientChessPanel类负责棋盘部分,包括判断输赢,使用数组chesses[i][j]记录棋盘上棋子的分布,对数组进行不同的赋值表示网格节点上无棋、黑棋、白棋;使用playChessHandler作为鼠标单击事件,单击事件调用Clientskt中的函数传送棋子坐标以及输赢信息。
drawChess函数画棋子,drawGrids画网格,gameOver判断棋盘棋子分布,输赢情况。
importjavax.swing.*;importjava.awt.*;;importChatOneToOneClient.Clientskt;classClientChessPanel extends JPanel{private static final long serialVersionUID = 1L;private int space=20; //网格间的距离private int grids=30; //棋盘的网格数private int radius=space/2; //棋的半径Clientsktskt;//当chesses[i][j]=0,表示网格节点(i,j)上无棋//当chesses[i][j]=1,表示网格节点(i,j)上放白棋//当chesses[i][j]=2,表示网格节点(i,j)上放黑棋privateint[][] chesses=new int[grids+1][grids+1];private intcurrColor=1; //当前棋的颜色privateMouseListenerplayChessHandler=new MouseAdapter(){public void mouseClicked(MouseEvent e){if(skt.reMouseGo()){int x=e.getX();int y=e.getY();//放一颗棋子if(x=0 && y=0)if(chesses[round(x)][round(y)]==0){chesses[round(x)][round(y)]=currColor;repaint(); //刷新图形skt.dataout("x:"+String.valueOf(round(x)));skt.dataout("y:"+String.valueOf(round(y)));skt.setMouseGo(false);if(gameOver(currColor)){skt.dataout("g:你输了");ClientMyDialog(skt.chat,"你赢了");;}currColor=currColor==1?2:1; //切换棋子的颜色}}}};public int round(float a){ //获得接近a的网格节点坐标float f=a/space;returnMath.round(f);}publicClientChessPanel(intspace,intgrids,Clientsktskt){ this.space=space;this.grids=grids;this.radius=space/2;this.skt=skt;setBackground(Color.BLUE);setSize(space*grids,space*grids);addMouseListener(playChessHandler);startChess();}public void startChess(){clearGrids(); //清空棋盘currColor=1;repaint(); //刷新图形private void clearGrids(){for(inti=0;i for(int j=0;j chesses[i][j]=0;}//画一颗棋子private void drawChess(Graphics g,intx,inty,int color){g.setColor(color==1?Color.GREEN:Color.BLACK);g.fillOval(x*space-radius,y*space-radius,radius*2,radius*2);}//画网格private void drawGrids(Graphics g){g.setColor(Color.DARK_GRAY);for(inti=0;i g.drawLine(0,i*space,grids*space,i*space);g.drawLine(i*space,0,i*space,grids*space);}}//接收对方下的棋坐标public void paintChess(intx,int y){if(x=0 && y=0){if(chesses[x][y]==0){chesses[x][y]=currColor;currColor=currColor==1?2:1; //切换棋子的颜色skt.setMouseGo(false);skt.setMouseGo(true);repaint(); //刷新图形}}}//判断游戏是否结束publicbooleangameOver(intgameOver){int five=0;//用于判断是否有连续5个子for(inti=0;i for(int j=0;j if(chesses[i][j]==gameOver){five++;for(in(本文来自:小草范文网:c五子棋实验报告)t k=1;k if(chesses[i][j+k]==gameOver){five++;if(five==5){return true;}else{five=1;k=5;}}for(int k=1;k if(chesses[i+k][j]==gameOver){ five++;if(five==5){return true;}}else{five=1;k=5;}}for(int k=1;k if(chesses[i+k][j+k]==gameOver){ five++;if(five==5){return true;}}else{five=1;k=5;}}for(int k=1;k4;k++){//左斜向比较if(chesses[i+k][j-k]==gameOver){five++;if(five==5){return true;}}else{five=1;}}}}five=0;}return false;}public void paintComponent(Graphics g){ //覆盖paintComponent()方法super.paintComponent(g); //必须先调用父类的方法drawGrids(g); //画网格for(inti=0;i for(int j=0;j if(chesses[i][j]!=0)drawChess(g,i,j,chesses[i][j]); //画棋子}}2、ClientComponentPopupMenu类主要负责聊天的部分,使用JTextField并且对其添加单击事件以及鼠标事件,可以实现文本的剪贴、复制粘贴等功能。
c 五子棋实验报告

c 五子棋实验报告
C五子棋实验报告
引言
五子棋是一种古老的策略游戏,它既考验了玩家的思维能力,又具有很高的娱乐性。
在本次实验中,我们将利用C语言编程,设计一个简单的五子棋游戏,并对其进行实验测试。
实验目的
1. 学习使用C语言进行游戏开发;
2. 设计并实现一个简单的五子棋游戏;
3. 对游戏进行功能测试和性能评估。
实验方法
1. 使用C语言编写五子棋游戏的程序代码;
2. 设计游戏界面和用户交互功能;
3. 实现游戏规则和胜负判定功能;
4. 进行功能测试和性能评估。
实验结果
经过实验,我们成功地设计并实现了一个简单的五子棋游戏。
游戏具有清晰的界面和简单的操作方式,玩家可以轻松上手。
在功能测试中,游戏能够正确判定胜负,且没有出现明显的bug。
在性能评估中,游戏在常见的操作系统上都能够流畅运行,响应速度较快。
实验结论
通过本次实验,我们学习到了使用C语言进行游戏开发的基本方法和技巧。
我
们成功地设计并实现了一个简单的五子棋游戏,并对其进行了功能测试和性能
评估。
实验结果表明,我们的游戏具有良好的稳定性和性能表现,能够满足玩
家的基本需求。
展望
在未来,我们可以进一步完善游戏的功能和界面设计,增加更多的游戏模式和
挑战性。
我们也可以考虑将游戏移植到其他平台上,以提供更广泛的游戏体验。
同时,我们还可以利用更先进的技术和算法,进一步优化游戏的性能和用户体验。
总之,我们将继续努力,不断改进和完善我们的五子棋游戏,为玩家提供
更好的游戏体验。
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 五子棋实验报告五子棋实验报告引言五子棋是一种古老而受欢迎的棋类游戏,它简单易学,却又充满了策略性。
为了更深入地了解五子棋的规则和策略,我们进行了一系列实验,以探索不同因素对游戏结果的影响。
实验一:棋手水平对游戏结果的影响我们邀请了10名棋手参与实验,他们分别具有不同水平的五子棋技能。
实验中,每个棋手与其他9名棋手进行对局,共进行了45局比赛。
结果显示,技术水平较高的棋手在对局中获胜的次数更多,而技术水平较低的棋手则相对较少。
这表明,五子棋是一种需要技巧和经验的游戏,高水平的棋手在对局中具有明显的优势。
实验二:先手与后手的优劣势为了研究先手与后手在五子棋中的优劣势,我们进行了一组对局实验。
实验中,每个棋手与其他棋手进行5局对局,分别执黑先手和白后手。
结果显示,先手方在对局中的获胜率略高于后手方。
这可能是因为先手方在棋局初期可以占据更多的关键位置,从而更容易形成有利的局面。
然而,我们也观察到,随着棋局的发展,后手方有机会通过反击和防守来扭转局势。
实验三:不同开局策略的效果比较为了研究不同开局策略对五子棋结果的影响,我们选择了三种常见的开局策略进行实验比较。
实验中,每个棋手与其他棋手进行5局对局,分别采用三种不同的开局策略。
结果显示,开局策略A相对于策略B和C,在对局中获胜的次数更多。
这可能是因为策略A更加注重中心位置的争夺,从而更容易形成有利的局面。
然而,我们也观察到,策略B和C在特定情况下也能发挥出色,这表明在五子棋中,开局策略的选择应该根据具体情况进行调整。
实验四:不同棋盘大小对游戏结果的影响为了研究不同棋盘大小对五子棋结果的影响,我们进行了一组对局实验。
实验中,每个棋手与其他棋手进行5局对局,分别在15x15、19x19和23x23的棋盘上进行。
结果显示,随着棋盘大小的增加,对局的复杂度也增加了。
在15x15棋盘上,棋手们更容易形成有利的局面,并且获胜的次数相对较多。
而在23x23棋盘上,棋手们更容易出现失误,对局结果更加平均。
五子棋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!");}。
五子棋C++实验报告

五子棋C++实验报告五子棋 C++实验报告一、实验目的本次实验的目的是使用 C++语言实现五子棋游戏,通过编写代码来模拟五子棋的游戏规则和逻辑,加深对 C++程序设计语言的理解和应用能力,同时提高解决实际问题的编程思维和技巧。
二、实验环境本次实验使用的开发环境为 Visual Studio 2019,操作系统为Windows 10。
三、实验原理1、棋盘设计采用二维数组来表示棋盘,数组的每个元素表示棋盘上的一个交叉点的状态(空、黑棋、白棋)。
2、落子判断在玩家落子时,需要判断落子位置是否合法(在棋盘范围内且该位置为空)。
3、胜负判断检查横向、纵向、正斜向和反斜向是否有连续的同色棋子达到五子相连,以确定胜负。
四、实验步骤1、棋盘类的设计定义一个`ChessBoard`类,包含棋盘的大小、棋盘状态数组以及相关的操作方法,如初始化棋盘、获取棋盘状态、判断落子位置是否合法等。
2、玩家类的设计设计`Player`类,代表游戏中的玩家,包含玩家的颜色(黑或白)和落子方法。
3、游戏流程控制在主函数中实现游戏的主要流程,包括初始化棋盘、玩家轮流落子、判断胜负等。
4、界面显示使用简单的控制台输出显示棋盘和游戏的相关信息。
五、代码实现以下是关键部分的代码示例:```cppinclude <iostream>using namespace std;//棋盘类class ChessBoard {private:int boardSize;int boardState;public:ChessBoard(int size) {boardSize = size;boardState = new int boardSize;for (int i = 0; i < boardSize; i++){boardStatei = new intboardSize;for (int j = 0; j < boardSize; j++){boardStateij = 0; // 0 表示空位}}}~ChessBoard(){for (int i = 0; i < boardSize; i++){delete boardStatei;}delete boardState;}bool isLegalMove(int x, int y) {if (x < 0 || x >= boardSize || y < 0 || y >=boardSize || boardStatexy!= 0) {return false;}return true;}void makeMove(int x, int y, int player) {boardStatexy = player;}void printBoard(){for (int i = 0; i < boardSize; i++){for (int j = 0; j < boardSize; j++){if (boardStateij == 0) {cout <<"";} else if (boardStateij == 1) {cout <<" X ";} else {cout <<" O ";}}cout << endl;}}bool checkWin(int player) {//横向检查for (int i = 0; i < boardSize; i++){for (int j = 0; j < boardSize 4; j++){if (boardStateij == player && boardStateij + 1 == player &&boardStateij + 2 == player && boardStateij + 3 == player &&boardStateij + 4 == player) {return true;}}}//纵向检查for (int i = 0; i < boardSize 4; i++){for (int j = 0; j < boardSize; j++){if (boardStateij == player && boardStatei + 1j == player &&boardStatei + 2j == player && boardStatei + 3j == player &&boardStatei + 4j == player) {return true;}}}//正斜向检查for (int i = 0; i < boardSize 4; i++){for (int j = 0; j < boardSize 4; j++){if (boardStateij == player && boardStatei + 1j + 1 == player && boardStatei + 2j + 2 == player && boardStatei + 3j + 3 ==player && boardStatei + 4j + 4 == player) {return true;}}}//反斜向检查for (int i = 4; i < boardSize; i++){for (int j = 0; j < boardSize 4; j++){if (boardStateij == player && boardStatei 1j + 1 == player &&boardStatei 2j + 2 == player && boardStatei 3j + 3 == player &&boardStatei 4j + 4 == player) {return true;}}}return false;}};//玩家类class Player {private:int color; // 1 表示黑棋,2 表示白棋public:Player(int c) {color = c;}void makeMove(ChessBoard& board) {int x, y;cout <<"请" <<(color == 1? "黑棋" :"白棋")<<"玩家输入落子位置(x y):";cin >> x >> y;while (!boardisLegalMove(x, y)){cout <<"非法落子,请重新输入: ";cin >> x >> y;}boardmakeMove(x, y, color);}};int main(){ChessBoard board(15);// 15x15 的棋盘Player blackPlayer(1);Player whitePlayer(2);int currentPlayer = 1;while (true) {boardprintBoard();if (currentPlayer == 1) {blackPlayermakeMove(board);if (boardcheckWin(1)){cout <<"黑棋胜利!"<< endl; break;}currentPlayer = 2;} else {whitePlayermakeMove(board);if (boardcheckWin(2)){cout <<"白棋胜利!"<< endl;break;}currentPlayer = 1;}}return 0;}```六、实验结果与分析经过多次测试,程序能够正确地实现五子棋的游戏规则,玩家可以顺利进行游戏。
如何用c语言做五子棋

如何用c语言做五子棋五子棋主要流行于华人和汉字文化圈的国家以及欧美一些地区,是世界上最古老的棋。
下面店铺给你介绍如何用c语言做五子棋,欢迎阅读。
用c语言做五子棋的方法五子棋游戏是一款很经典的智力游戏,只有学过编程语言的人,把五子棋的编程原理弄懂了,就能用自己熟悉的语言实现出来。
五子棋C语言代码如下#include#include#include#include#include#define CROSSRU 0xbf /*右上角点*/#define CROSSLU 0xda /*左上角点*/#define CROSSLD 0xc0 /*左下角点*/#define CROSSRD 0xd9 /*右下角点*/#define CROSSL 0xc3 /*左边*/#define CROSSR 0xb4 /*右边*/#define CROSSU 0xc2 /*上边*/#define CROSSD 0xc1 /*下边*/#define CROSS 0xc5 /*十字交叉点*//*定义棋盘左上角点在屏幕上的位置*/#define MAPXOFT 5#define MAPYOFT 2/*定义1号玩家的操作键键码*/#define PLAY1UP 0x1157/*上移--'W'*/#define PLAY1DOWN 0x1f53/*下移--'S'*/#define PLAY1LEFT 0x1e41/*左移--'A'*/#define PLAY1RIGHT 0x2044/*右移--'D'*/#define PLAY1DO 0x3920/*落子--空格键*//*定义2号玩家的操作键键码*/#define PLAY2UP 0x4800/*上移--方向键up*/#define PLAY2DOWN 0x5000/*下移--方向键down*/#define PLAY2LEFT 0x4b00/*左移--方向键left*/#define PLAY2RIGHT 0x4d00/*右移--方向键right*/#define PLAY2DO 0x1c0d/*落子--回车键Enter*//*若想在游戏中途退出, 可按 Esc 键*/#define ESCAPE 0x011b/*定义棋盘上交叉点的状态, 即该点有无棋子 *//*若有棋子, 还应能指出是哪个玩家的棋子 */#define CHESSNULL 0 /*没有棋子*/#define CHESS1 'O'/*一号玩家的棋子*/#define CHESS2 'X'/*二号玩家的棋子*//*定义按键类别*/#define KEYEXIT 0/*退出键*/#define KEYFALLCHESS 1/*落子键*/#define KEYMOVECURSOR 2/*光标移动键*/#define KEYINVALID 3/*无效键*//*定义符号常量: 真, 假 --- 真为1, 假为0 */#define TRUE 1#define FALSE 0/**********************************************************/ /* 定义数据结构 *//*棋盘交叉点坐标的数据结构*/struct point{int x,y;};/**********************************************************/ /*自定义函数原型说明 */void Init(void);int GetKey(void);int CheckKey(int press);int ChangeOrder(void);int ChessGo(int Order,struct point Cursor);void DoError(void);void DoOK(void);void DoWin(int Order);void MoveCursor(int Order,int press);void DrawCross(int x,int y);void DrawMap(void);int JudgeWin(int Order,struct point Cursor);int JudgeWinLine(int Order,struct point Cursor,int direction); void ShowOrderMsg(int Order);void EndGame(void);/**********************************************************/ /**********************************************************/ /* 定义全局变量 */int gPlayOrder; /*指示当前行棋方 */struct point gCursor; /*光标在棋盘上的位置 */char gChessBoard[19][19];/*用于记录棋盘上各点的状态*//**********************************************************/ /**********************************************************/ /*主函数*/void main(){int press;int bOutWhile=FALSE;/*退出循环标志*/printf("Welcome ");Init();/*初始化图象,数据*/while(1){press=GetKey();/*获取用户的按键值*/switch(CheckKey(press))/*判断按键类别*/{/*是退出键*/case KEYEXIT:clrscr();/*清屏*/bOutWhile = TRUE;break;/*是落子键*/case KEYFALLCHESS:if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/ DoError();/*落子错误*/else{DoOK();/*落子正确*//*如果当前行棋方赢棋*/if(JudgeWin(gPlayOrder,gCursor)==TRUE){DoWin(gPlayOrder);bOutWhile = TRUE;/*退出循环标志置为真*/}/*否则*/else/*交换行棋方*/ChangeOrder();ShowOrderMsg(gPlayOrder);}break;/*是光标移动键*/case KEYMOVECURSOR:MoveCursor(gPlayOrder,press);break;/*是无效键*/case KEYINVALID:break;}if(bOutWhile==TRUE)break;}/*游戏结束*/EndGame();}/**********************************************************/ /*界面初始化,数据初始化*/void Init(void){int i,j;char *Msg[]={"Player1 key:"," UP----w"," DOWN--s"," LEFT--a"," RIGHT-d"," DO----space","","Player2 key:"," UP----up"," DOWN--down"," LEFT--left"," RIGHT-right"," DO----ENTER","","exit game:"," ESC",NULL,};/* 先手方为1号玩家 */gPlayOrder = CHESS1;/* 棋盘数据清零, 即棋盘上各点开始的时候都没有棋子 */ for(i=0;i<19;i++)for(j=0;j<19;j++)gChessBoard[i][j]=CHESSNULL;/*光标初始位置*/gCursor.x=gCursor.y=0;/*画棋盘*/textmode(C40);DrawMap();/*显示操作键说明*/i=0;textcolor(BROWN);while(Msg[i]!=NULL){gotoxy(25,3+i);cputs(Msg[i]);i++;}/*显示当前行棋方*/ShowOrderMsg(gPlayOrder);/*光标移至棋盘的左上角点处*/gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*画棋盘*/void DrawMap(void){int i,j;clrscr();for(i=0;i<19;i++)for(j=0;j<19;j++)DrawCross(i,j);}/*画棋盘上的交叉点*/void DrawCross(int x,int y){gotoxy(x+MAPXOFT,y+MAPYOFT);/*交叉点上是一号玩家的棋子*/if(gChessBoard[x][y]==CHESS1){textcolor(LIGHTBLUE);putch(CHESS1);return;}/*交叉点上是二号玩家的棋子*/if(gChessBoard[x][y]==CHESS2){textcolor(LIGHTBLUE);putch(CHESS2); return;}textcolor(GREEN); /*左上角交叉点*/if(x==0&&y==0) {putch(CROSSLU); return;}/*左下角交叉点*/if(x==0&&y==18) {putch(CROSSLD); return;}/*右上角交叉点*/if(x==18&&y==0) {putch(CROSSRU); return;}/*右下角交叉点*/if(x==18&&y==18) {putch(CROSSRD); return;}/*左边界交叉点*/if(x==0){putch(CROSSL); return;}/*右边界交叉点*/if(x==18){putch(CROSSR); return;}/*上边界交叉点*/if(y==0){putch(CROSSU); return;}/*下边界交叉点*/if(y==18){putch(CROSSD); return;}/*棋盘中间的交叉点*/ putch(CROSS);}/*交换行棋方*/int ChangeOrder(void) {if(gPlayOrder==CHESS1) gPlayOrder=CHESS2;elsegPlayOrder=CHESS1;return(gPlayOrder);}/*获取按键值*/int GetKey(void){char lowbyte;int press;while (bioskey(1) == 0);/*如果用户没有按键,空循环*/press=bioskey(0);lowbyte=press&0xff;press=press&0xff00 + toupper(lowbyte); return(press);}/*落子错误处理*/void DoError(void){sound(1200);delay(50);nosound();}/*赢棋处理*/void DoWin(int Order){sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);nosound();textcolor(RED+BLINK);gotoxy(25,20);if(Order==CHESS1)cputs("PLAYER1 WIN!");elsecputs("PLAYER2 WIN!");gotoxy(25,21);cputs(" \\<^+^>/");getch();}/*走棋*/int ChessGo(int Order,struct point Cursor){/*判断交叉点上有无棋子*/if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL) {/*若没有棋子, 则可以落子*/gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); textcolor(LIGHTBLUE);putch(Order);gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); gChessBoard[Cursor.x][Cursor.y]=Order;return TRUE;}elsereturn FALSE;}/*判断当前行棋方落子后是否赢棋*/int JudgeWin(int Order,struct point Cursor){int i;for(i=0;i<4;i++)/*判断在指定方向上是否有连续5个行棋方的棋子*/if(JudgeWinLine(Order,Cursor,i))return TRUE;return FALSE;}/*判断在指定方向上是否有连续5个行棋方的棋子*/int JudgeWinLine(int Order,struct point Cursor,int direction) {int i;struct point pos,dpos;const int testnum = 5;int count;switch(direction){case 0:/*在水平方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y;dpos.x=1;dpos.y=0;break;case 1:/*在垂直方向*/pos.x=Cursor.x;pos.y=Cursor.y-(testnum-1);dpos.x=0;dpos.y=1;break;case 2:/*在左下至右上的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y+(testnum-1);dpos.x=1;dpos.y=-1;break;case 3:/*在左上至右下的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y-(testnum-1);dpos.x=1;dpos.y=1;break;}count=0;for(i=0;i{if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18) {if(gChessBoard[pos.x][pos.y]==Order){count++;if(count>=testnum)return TRUE;}elsecount=0;}pos.x+=dpos.x;pos.y+=dpos.y;}return FALSE;}/*移动光标*/void MoveCursor(int Order,int press) {switch(press){case PLAY1UP:if(Order==CHESS1&&gCursor.y>0) gCursor.y--;break;case PLAY1DOWN:if(Order==CHESS1&&gCursor.y<18) gCursor.y++;break;case PLAY1LEFT:if(Order==CHESS1&&gCursor.x>0) gCursor.x--;break;case PLAY1RIGHT:if(Order==CHESS1&&gCursor.x<18) gCursor.x++;break;case PLAY2UP:if(Order==CHESS2&&gCursor.y>0) gCursor.y--;break;case PLAY2DOWN:if(Order==CHESS2&&gCursor.y<18)gCursor.y++;break;case PLAY2LEFT:if(Order==CHESS2&&gCursor.x>0)gCursor.x--;break;case PLAY2RIGHT:if(Order==CHESS2&&gCursor.x<18)gCursor.x++;break;}gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*游戏结束处理*/void EndGame(void){textmode(C80);}/*显示当前行棋方*/void ShowOrderMsg(int Order){gotoxy(6,MAPYOFT+20);textcolor(LIGHTRED);if(Order==CHESS1)cputs("Player1 go!");elsecputs("Player2 go!");gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);}/*落子正确处理*/void DoOK(void){sound(500);delay(70);sound(600);delay(50);sound(1000);delay(100);nosound();}/*检查用户的按键类别*/int CheckKey(int press){if(press==ESCAPE)return KEYEXIT;/*是退出键*/elseif( ( press==PLAY1DO && gPlayOrder==CHESS1) || ( press==PLAY2DO && gPlayOrder==CHESS2) )return KEYFALLCHESS;/*是落子键*/elseif( press==PLAY1UP || press==PLAY1DOWN || press==PLAY1LEFT || press==PLAY1RIGHT || press==PLAY2UP || press==PLAY2DOWN || press==PLAY2LEFT || press==PLAY2RIGHT)return KEYMOVECURSOR;/*是光标移动键*/ elsereturn KEYINVALID;/*按键无效*/}。
五子棋实验报告

青岛理工大学《C语言程序设计》题目五子棋游戏程序设计指导教师:姓名:学号:班级:专业:地点:时间:一、内容五子棋的游戏规则对我们大家来说都很很清楚的。
只要某一方的棋子在一条直线上,棋子数先达到五或以上,则该方为胜者。
本人的五子棋小游戏程序实现了两个人对战的简单步骤和一些简单规则,能判断双方的输赢。
当一方的棋子数先达到五或以上的,则能判断这方为胜者。
而且能判别,当棋盘上的某一个位置有棋子时,能判断这个位置不能再落子了。
当某一方胜利了时可以选择是否再继续玩游戏。
在游戏还没有分出胜负时,也可以退出游戏。
进入游戏一方可以用“W”,“S”,“A”,“D”,“Z”分别来控制上下左右的方向和落子,则另一方则用“O”,“L”,“I”,“P”和空格来分别控制上下左右的方向和落子。
二、上机环境:操作系统:windows XP开发工具:Vc6.0三、函数调用图:四、各函数的说明:1.main()主函数2.void drawqipan()画棋盘3.void move gb() 移动光标4.void luozi() 下棋子5.void judgewin() 判断输赢6.void system() 系统函数五、算法描述void movegb() move luozi()V oid drawqipan() void judgewin()六程序运行效果图1、初始界面2、游戏界面3、可以中途退出七、体会这次程序设计让我感到我学习的知识是多么的有限。
所以我要更加的努力学习编程的知识。
可是这次编程的过程也让我收获不少,首先加固了我的知识点,也锻炼了我的调试能力。
其实这次也让我感觉到C语言也挺有趣的,让我对C语言的兴趣更加浓厚。
并且C语言编程也不是想象中的那么难,因而也让我的心里障碍减少了很多。
八、参考文献:谭浩强.C程序设计(第三版).清华大学出版社,2005九、程序清单#include<stdio.h>#include <conio.h> #include<windows.h> #define play1up 'w'#define play1down 's' #define play1left 'a' #define play1right 'd' #define play2up 'o'#define play2down 'l' #define play2left 'i'#define play2right 'p' #define play1lq 'z'#define play2lq ' ' #define N 20#define playnext 'q'#define exit 'e'int m=1;int p[N][N];/*记录棋盘落子情况*/char q[N][N];/*记录棋盘交叉点棋子种类*/ struct zuobiao{int x;int y;}weizhi;void gotoxy(int x, int y) /*建立坐标函数*/ {COORD c;c.X=x;c.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);}void drawqipan()/*建立棋盘*/{int i,j;for(i=0;i<N;i++){for(j=0;j<N;j++){ p[i][j]=0;q[i][j]='o';printf("十");}printf("\n");}}void movegb(char press)/*光标的移动*/{switch(press){caseplay1up:if(weizhi.y>-1)weizhi.y--;if(weizhi.y<0)weiz hi.y=19;break;/*玩家1光标向下移动*/caseplay1down:if(weizhi.y<20)weizhi.y++;if(weizhi.y>19 )weizhi.y=0;break;/*玩家1光标向上移动*/caseplay1left:if(weizhi.x>-1)weizhi.x--;if(weizhi.x<0)wei zhi.x=19;break;/*玩家1光标向左移动*/caseplay1right:if(weizhi.x<20)weizhi.x++;if(weizhi.x>19) weizhi.x=0;break;/*玩家1光标向右移动*/caseplay2up:if(weizhi.y>-1)weizhi.y--;if(weizhi.y<0)weiz hi.y=19;break;/*玩家2光标向上移动*/caseplay2down:if(weizhi.y<20)weizhi.y++;if(weizhi.y>19 )weizhi.y=0;break;/*玩家2光标向下移动*/caseplay2left:if(weizhi.x>-1)weizhi.x--;if(weizhi.x<0)wei zhi.x=19;break;/*玩家2光标向左移动*/caseplay2right:if(weizhi.x<20)weizhi.x++;if(weizhi.x>19)weizhi.x=0;break;/*玩家2光标向右移动*/}gotoxy(2*weizhi.x,weizhi.y);}void luozi(char press){static t=0;/*代表坐标交叉点有无棋子的状态*/ if(press==play1lq)if(t==0&&p[weizhi.x][weizhi.y]==0){printf("●");gotoxy(2*weizhi.x,weizhi.y);q[weizhi.x][weizhi.y]=press;p[weizhi.x][weizhi.y]=1;t=1;}if(press==play2lq)if(t==1&&p[weizhi.x][weizhi.y]==0){printf("○");gotoxy(2*weizhi.x,weizhi.y);q[weizhi.x][weizhi.y]=press;p[weizhi.x][weizhi.y]=1;t=0;}}void judgewin(char press)//判断输赢{int i,count1,count2=0,count3=0;struct zuobiao p;for(i=0;i<4;i++){for(count1=4;count1>=-4;count1--){ switch(i){case0:p.x=weizhi.x-count1;p.y=weizhi.y;break;//从左往右判断是否有五子连续在一直线上case1:p.x=weizhi.x;p.y=weizhi.y+count1;break;// 从上往下判断是否有五子连续在一直线上case2:p.x=weizhi.x-count1;p.y=weizhi.y+count1;break; // 从左上往右下判断是否有五子连续在一直线上case3:p.x=weizhi.x-count1;p.y=weizhi.y-count1;break; //从左下往右上判断是否有五子连续在一直线上}if(p.x>=0&&p.y>=0){if(q[p.x][p.y]==play1lq){ count2++;count3=0;}if(q[p.x][p.y]==play2lq){count3++;count2=0;}}}if(count2>=5){gotoxy(20,20);printf("the side of ●wins\n");printf("do you want to play again?\nplease do Q!");break;}else count2=0;if(count3>=5){gotoxy(20,20);printf("the side of ○wins\n");printf("do you want to play again?\nplease do Q!");break;}else count3=0;}}void main()//主函数{char press;system("COLOR 2f");//背景颜色和前景颜色drawqipan();gotoxy(0,0);while(m){press=getch();movegb(press);switch(press){case play1lq:case play2lq:luozi(press);judgewin(press);break; caseplaynext:system("cls");drawqipan();gotoxy(0,0);break ;case exit : m=0;break;default :break;}}}。
五子棋游戏 C语言 代码 源代码

#include <stdio.h>#include <stdlib.h>#define m 30int main (void){int count;//计数器算横纵行的结果int w,h;int u;int l;int i,size;//i声明步数。
size声明int r[m][m] = {0};//数组声明(棋子位置)int x, y;//声明落子坐标int n;//声明棋盘大小nchar a[20],b[20];printf ("请输入棋盘大小n\n");//编辑棋盘直到棋盘长度宽度大于4小于30 scanf ("%d", &n);if (n<=4 || n>m){do{printf ("输入的棋盘大小:4<n<%d\n", m);scanf ("%d", &n);}while (n<=4 || n>m);}getchar ();//声明玩家printf ("请输入玩家1姓名:\n");gets(a);printf ("请输入玩家2姓名:\n");gets(b);for ( i = 1, size = n*n;i <= size; i++)//编辑棋盘{if (i%2 == 1)//如果i能被2整除,为玩家a相关信息{do//玩家a棋子信息{printf ("%s该你下棋了,第%d个棋子\n", a, i);scanf ("%d%d", &x, &y);if (x > n || x < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>m || x<0);}if (y > n || y < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 1;for (u = 0;u < n; u++)//不同情况下判断玩家a获胜方式{for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 1 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;//直接跳转,其余代码不在运行count = 0;for (w = u, h = l; r[w][h] == 1 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w = u,h = l; r[w][h] == 1 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 1 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}}}}system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++)//画棋盘,声明两玩家棋子图片{if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●"); }printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k);}else if (i%2 == 0)//如果i不能被2整除,为玩家b相关信息{do{printf ("\n%s该你下棋了,第%d个棋子\n", b, i);scanf ("%d%d", &x, &y);if (x > n || x < 0){do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>n || x<0);}if (y >n|| y < 0){do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 2;system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");count = 0;for (u = 0;u < n; u++){for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 2 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u, h = l; r[w][h] == 2 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u,h = l; r[w][h] == 2 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 2 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}}}}}e: for (int j = n;j>=0;j--)//游戏结束界面棋盘固定重新显示{printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");printf ("\a游戏愉快,Powered by Techmessager\n");//结束语句return 0;}。
MFC程序五子棋实验报告

MFC程序五子棋实验报告
一、实验内容
本次实验主要以Visual Studio2024为开发工具,以MFC应用程序为开发平台,以五子棋为实现的对象,实现一个基于MFC编写的五子棋游戏程序。
二、实验要求
本次实验的目标是基于MFC应用程序平台,使用Visual Studio2024开发工具,实现一个能够完成基本的五子棋游戏功能的程序。
实现的功能包括:
1、游戏初始界面:实现游戏初始界面,包括游戏开始、设置(玩家姓名)、取消、选择棋子颜色按钮等;
2、逻辑控制:实现玩家双方的棋子及其状态控制,判断双方棋子的落子位置,根据五子棋的规则判断双方输赢,判断本局棋盘是否成和棋;
3、棋盘显示:棋盘支持多种背景皮肤选择,实现了棋子的真实落子位置;
4、功能支持:支持悔棋、撤销悔棋、重开等常用功能,还支持联机对战功能;
5、记录显示:显示游戏状态,如落子数,玩家双方的输赢情况。
三、实验过程
1. 使用Visual Studio2024,以MFC应用程序平台为基础,创建五子棋游戏程序。
2.编写程序界面,实现游戏初始界面,在界面中添加游戏开始、设置(玩家姓名)、取消、选择棋子颜色等按钮。
3.实现五子棋游戏需要的基础函数。
c语言五子棋课程设计报告

C语言五子棋课程设计报告一、引言五子棋是一种非常古老的棋类游戏,起源于中国。
它简单但却能激发人们的智慧和策略思维。
在本课程设计报告中,我们将使用C语言来实现一个五子棋游戏,让玩家可以在计算机上进行对战或与计算机进行对弈。
二、项目概述本项目旨在设计一个简单而实用的五子棋游戏,实现以下主要功能: 1. 人机对战:玩家可以选择与计算机进行对战,计算机作为电脑AI会给出最佳的下棋策略。
2. 双人对战:玩家可以选择与其他玩家进行对战,通过交替下棋来竞争胜利。
3. 悔棋功能:玩家可以选择悔棋,回退到上一步棋的状态。
4. 判断胜负:每一步落子后,游戏程序会自动判断是否有五子连珠,以确定胜负。
5. 棋盘显示:在游戏界面中,将显示实时的棋盘状态,供玩家观察。
三、设计思路1.游戏界面设计:使用命令行界面实现游戏的显示和操作,通过文本形式的棋盘来展示棋局状态。
2.数据结构设计:采用二维数组来表示棋盘,每个数组元素代表一个交叉点,可以存储不同的棋子状态(无子、玩家1棋子、玩家2棋子)。
3.算法设计:–下棋算法:计算机AI通过搜索当前棋盘的状态,评估每个可落子位置的得分,并选择得分最高的位置进行落子。
–判断胜负算法:在每次玩家下棋后,判断当前棋局是否出现了五子连珠的情况,如果有则宣布胜利。
4.用户交互设计:根据用户的输入进行响应,包括选择游戏模式、确定下棋位置等。
四、详细实现1. 游戏初始化在游戏开始时,初始化棋盘状态,清空棋盘上的所有棋子。
2. 游戏界面显示使用命令行界面来显示游戏界面,通过输出字符来展示棋盘状态。
3. 玩家操作玩家在游戏过程中可以通过键盘输入来选择操作: - 选择对手:可以选择与计算机对战或与其他玩家对战。
- 下棋位置:玩家可以通过输入行列坐标的方式选择下棋位置。
4. 计算机AI计算机AI通过搜索当前棋盘状态来选择最佳的下棋位置,算法包括以下步骤: 1. 遍历棋盘上的每个空位置。
2. 对于每个空位置,依次评估其在横向、纵向和对角线方向上的得分: - 得分规则:根据当前棋盘状态判断是否形成连珠,如果形成则加分,如果有对手形成连珠则减分。
C语言五子棋源代码_设计报告1

C语言五子棋源代码_设计报告1C语言五子棋源代码_设计报告1设计报告一、概述本项目是一个基于C语言的五子棋游戏,实现了双人对战的功能。
通过控制台界面显示棋盘和棋子,并进行相应的逻辑判断,以确定游戏的胜负。
二、设计思路1.棋盘的显示使用二维数组来表示棋盘,通过循环遍历数组并打印相应的字符来显示棋盘。
2.棋子的放置根据玩家的输入即坐标位置,在对应的数组下标位置放置相应的字符表示棋子。
3.游戏逻辑设计了胜利的条件判断函数,通过检查棋盘中的连续五个相同的字符来判断是否已经获胜。
4.玩家输入的处理使用循环来等待玩家输入,输入合法的坐标后进行相应的处理,包括棋盘上棋子的放置、胜利判断以及游戏结束的处理。
5.游戏结束的判断游戏结束时,根据胜利的条件判断结果进行相应的处理,可以继续游戏或退出游戏。
三、关键函数说明1. void displayBoard(char board[ROW][COL])该函数用于显示棋盘,根据棋盘数组的值打印相应的字符来显示棋盘。
2. int isWin(char board[ROW][COL], int x, int y)该函数用于判断当前位置的棋子是否连成五子线,如果是胜利则返回1,否则返回0。
3. void playerMove(char board[ROW][COL], int player)该函数用于玩家输入坐标,并将相应的棋子放置在棋盘上,同时进行胜利判断。
4. void playGame该函数用于游戏的整体流程,循环进行玩家的输入和处理,直到出现胜利或平局。
四、总结通过本项目的设计和实现,我进一步熟悉了C语言的编程技巧和逻辑思维,学会了如何通过编写函数来组织代码和实现功能。
同时,我也了解了五子棋游戏的规则和胜利判断的逻辑。
通过不断调试和优化代码,我成功地实现了一个简单但完整的五子棋游戏,提升了自己的编程能力和解决问题的能力。
这个项目的完成使我对C语言的应用有了更加深入的理解,也锻炼了我的团队合作和解决问题的能力。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
青岛理工大学《C语言程序设计》题目游戏五子棋指导教师:李兰姓名:王鹏学号:200907127班级:计算094专业:计算机科学与技术地点:二号实验楼第六机房时间: 6.28 至7.1一、内容游戏五子棋在这个小游戏中,实现了两人对战的游戏模式,没有添加禁手规则,另外考虑到落子时的可能失误,就添加了悔棋工具,游戏过程中可以进行游戏存档,进入悔棋和存档的方法为将光标移动到已有子的位置,敲一下落子键即可。
下次游戏时候可以进入存档界面然后选择读取游戏存档即可继续玩上次的游戏。
在游戏时自动计算双方各胜几局,平几局。
按键控制一号玩家:上:w 下:s 左: a 右 : d 落子: f二号玩家:上:I 下:k 左: j 右: l 落子: h二、上机环境操作系统:Windows 98/XP/Win 7开发工具:VC++6.0三、函数调用关系图四、各函数功能说明对每个函数功能加以说明,说明该函数主要实现了什么内容。
1 int main() 主函数2 system(“color 5a”) 控制台前景、背景颜色设置3 system(“cls”) 清屏4 Printsjm(void) 输出游戏首界面5 start(void) 界面初始化6 print(void) 界面刷新7 check(void) 检测是否有一方赢棋8 checkheqi(void) 检测是否和棋9 zq(int,char (*p)[],char (*p)[]) 走棋落子10 huiqi(char (*p)[]) 悔棋11 jilu(char (*p)[]) 记录上次落子前的界面12 cundang(void) 存储游戏进度13 duqu(void) 读取游戏进度14 fuzhi(void) 保存临时界面15 huifu(void) 恢复临时界面以实现光标移动16 newdata(void) 数据刷新五、算法描述或流程图system(“color 5a”)和system(“cls”) 只需要调用<stdlib.h>头文件,并在使用前将要声明的变量声明好即可。
/*输出游戏首页*//*原样输出即可*/void Printsjm(){printf(" * * * * * * * * * * * * * * * 游戏名称* * * * * * * * * * * * * * **\n");printf(" * * * * * * * * *\n");printf(" * * * * * * 五子棋* * * * *\n");printf(" * * * * * * * * *\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" \n");printf(" * * * * * * * * * * * * * * * 游戏规则* * * * * * * * * * * * * * * *\n");printf(" * * * * * * * *\n");printf(" * * * * 游戏悔棋时不允许连续悔棋* * * *\n");printf(" * * * * * * * *\n");printf(" ************ 任一方五子相连即获胜,一局游戏也就结束***********\n");printf(" * * * * * * * *\n");printf(" ********* 请遵守游戏规则谢谢!!!********\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" \n");printf(" * * * * * * * * * * * * * * * 游戏制作* * * * * * * * * * * * * * * *\n");printf(" * * * * * * * *\n");printf(" *** *** 计算机工程学院094班*** ***\n");printf(" * * * * * * * *\n");printf(" * * * * * * * 王鹏* * * * * * *\n");printf(" * * * * * * * 2010.6.28 * * * * * * *\n");printf(" *********************************************************************\n"); /*恢复*/void huifu(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)qipan[i][j]=linshi[i][j]; /*临时界面恢复到实际界面*/}void fuzhi(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)linshi[i][j]=qipan[i][j];/*记录临时界面*/}void cundang(){FILE *fp;int i,j;if((fp=fopen(filename,"w"))==NULL) /*打开存档文件*/{printf(" 存档失败!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();fclose(fp);}else{for(i=0;i<20;i++){for(j=0;j<20;j++){cd.cun[i][j]=linshi[i][j]; /*数据恢复*/cd.jx=jx;cd.jy=jy;cd.leiji=leiji;}}fwrite(&cd,sizeof(struct cund),1,fp);printf(" 存档成功!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();fclose(fp);system("cls");}}void duqu() /*读取游戏进度*/{FILE *fpp;int i,j;if((fpp=fopen(filename,"r"))==NULL){printf(" 你还没有存档,读取存档失败!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();fclose(fpp);}else{fread(&cd,sizeof(struct cund),1,fpp);fclose(fpp);jx=cd.jx;jy=cd.jy;leiji=cd.leiji;for(i=0;i<20;i++)for(j=0;j<20;j++)qipan[i][j]=cd.cun[i][j];system("cls");printf(" 读取存档成功,请继续游戏!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();}}void jilu(char (*p)[20])/*记录悔棋前棋盘个子的位置以及悔棋后该谁落子*/{int i,j;for(i=0;i<20;i++){for(j=0;j<20;j++){*(*(p+i)+j)=qipan[i][j];}}}/*悔棋到上次你的落子权*/void huiqi(char (*p)[20]){int i,j;for(i=0;i<20;i++){for(j=0;j<20;j++){qipan[i][j]=*(*(p+i)+j);}}}/*输出界面,每次走完一步棋都要刷新界面*/void print()/*start()函数也是同样道理*/{int i=0,j=0,k=0;/*显示列坐标*/printf("000|001|002|003|004|005|006|007|008|009|010|011|012|013|014|\n");for(i=1;i<15;i++){ /*棋盘的上边界*/printf("---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");for(j=0;j<15;j++){if(j==0){if(i<10) printf("00%d|",i);/*显示可控制行坐标以及左边界*/else printf("0%d|",i);}elseprintf(" %c |",qipan[i][j]); /*显示棋盘上的字符以及由边界*/ }printf("\n");/*显示棋盘下边界*/printf("---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");if(leiji%2==1)printf("\n \n\n 一号玩家落子:\n\n\n");elseprintf("\n \n\n 二号玩家落子:\n\n\n");}/*检测棋盘上是否有一方赢棋*/check() /*调用时对棋盘上的一号二号玩家分别进行行检测、列检测、左斜线检测、右斜线检测*/if((qipan[i][j]==black)&&(qipan[i][j+1]==black)&&(qipan[i][j+2]==black)&&(qipan[i][j+3]==bla ck)&&(qipan[i][j+4]==black))/* 一号玩家行检测*/if((qipan[i][j]==white)&&(qipan[i][j+1]==white)&&(qipan[i][j+2]==white)&&(qipan[i][j+3]==white)&&(qi pan[i][j+4]==white)) /*二号玩家列检测*/if(qipan[i][j]==black&&qipan[i+1][j-1]==black&&qipan[i+2][j-2]==black&&qipan[i+3][j-3]==black&&qip an[i+4][j-4]==black) /*右斜线检测*//*检测双方是否和棋并在棋盘满的时候自动判和棋*/int checkheqi(){char ch;if((leiji-1)==186) /*统计数初始值为1,现在减一来判断棋子数是否快落满棋盘*/{printf(" 真是棋逢敌手啊,棋已至此仍未能分出胜负...........\n");printf(" 但现在棋盘即将落满,请问两位棋士是否选择和棋,改日再战?Y / N ?\n");ch=getchar();while(ch!='Y'||ch!='N')/*显示询问界面,提示是否和棋*/{printf("请选择是或否Y / N ");ch=getchar();}if(ch=='Y') /*选择是,如下输出,并返回主函数所需的控制值*/{printf("哈哈...来日方长...两位光然有棋士风范,拿得起放得下...改日再战...\n");return(3);}if(ch=='N') /*选择否,如下输出,并返回主函数所需的控制值*/{printf(" 哈哈,看来两位都不愿和棋...... \n");printf(" 那就请继续下完此局,但是不要忘了,棋盘落满子的时候自动判为和棋.....\n");return(0);}}if((leiji-1)==196)/*当棋子数等于棋盘空格数时自动判和棋,并返回主函数所需的控制值*/{printf(" \n\n棋盘已满,系统自动判为和棋,请两位棋士改日再战...........\n\n\n\n");return(5);}return(0);/*否则返回零继续游戏*/}/*一下语句块为zq()、huiqi()、jilu(),fuzhi(),huifu()五个函数的调用*//*走棋函数*/void zq(char player,char (*p1)[20],char (*p2)[20]){int mn=0,i=1,p,q;char key;key=_getch();if(player==white){while(1){/*实现光标移动的关键算法*/switch(key){case 'j': if(ly>1) {huifu();ly--;qipan[lx][ly]=95;}system("cls");print();break;case 'l': if(ly<14){huifu();ly++;qipan[lx][ly]=95;}system("cls");print();break;case 'k': if(lx<14){huifu();lx++;qipan[lx][ly]=95;}system("cls");print();break;case 'i': if(lx>1) {huifu();lx--;qipan[lx][ly]=95;}system("cls");print();break;}key=_getch();if(key=='h'){if(linshi[lx][ly]==black||linshi[lx][ly]==white){system("cls");printf("请选择:\n");printf(" 【1】进入游戏界面存档\n");printf(" 【2】进入游戏悔棋界面\n");printf(" ");scanf("%d",&mn);if(mn==1){mn=0;printf("请选择:\n");printf(" 【1】保存游戏进度\n");printf(" 【2】读取游戏进度\n");printf(" ");scanf("%d",&mn);if(mn==1){mn=0;printf(" 你选择了保存游戏进度!!!\n");cundang();system("cls");print();}else if(mn==2){mn=0;printf(" 你选择了读取游戏进度!!!\n");duqu();fuzhi();system("cls");print();}}else if(mn==2){mn=0;printf(" 你选择了悔棋!!!\n");if(kongzhi==player1){if(leiji%2==0){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}else huiqi(p2);leiji-=2;jx--;jy--;fuzhi();system("cls");print();}else{if(leiji%2==1){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}else huiqi(p2);leiji-=2;jx--;jy--;fuzhi();system("cls");print();}}}else if(linshi[lx][ly]!=black&&linshi[lx][ly]!=white){for(p=0;p<20;p++){for(q=0;q<20;q++){if(qipan[p][q]!=black&&qipan[p][q]!=white)qipan[p][q]=32;}}if(kongzhi==player1){if(2*jx==leiji){jilu(p1);jx++;}else if((2*jy-1)==leiji){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break;}else{if(2*jx==(leiji-1)){jilu(p1);jx++;}else if((2*jy-1)==(leiji-1)){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break; } } }}}else if(player==black){while(1){switch(key){case 'a': if(ly>1) {huifu();ly--;qipan[lx][ly]=95;}system("cls");print();break;case 'd': if(ly<14){huifu();ly++;qipan[lx][ly]=95;}system("cls");print();break;case 's': if(lx<14){huifu();lx++;qipan[lx][ly]=95;}system("cls");print();break;case 'w': if(lx>1) {huifu();lx--;qipan[lx][ly]=95;}system("cls");print();break;}key=_getch();if(key=='f'){if(linshi[lx][ly]==black||linshi[lx][ly]==white){system("cls");printf("\n 请选择:\n");printf(" 【1】进入游戏界面存档\n");printf(" 【2】进入游戏悔棋界面\n");printf(" "); scanf("%d",&mn);if(mn==1){mn=0;printf("\n 请选择:\n");printf(" 【1】保存游戏进度\n");printf(" 【2】读取游戏进度\n");printf(" ");scanf("%d",&mn);if(mn==1){mn=0;printf(" 你选择了保存游戏进度!!!\n");cundang();}else if(mn==2){mn=0;printf(" 你选择了读取游戏进度!!!\n");duqu();fuzhi();system("cls");print();}}else if(mn==2){printf(" 你选择了悔棋!!!\n");if(kongzhi==player1){if(leiji%2==0){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}else{huiqi(p2);}leiji-=2;jx--;jy--;fuzhi();system("cls");print();}else{if(leiji%2==1){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}else{huiqi(p2);}leiji-=2;jx--;jy--;fuzhi();system("cls");print();} }}}else if(linshi[lx][ly]!=black&&linshi[lx][ly]!=white){for(p=0;p<20;p++){for(q=0;q<20;q++){if(qipan[p][q]!=black&&qipan[p][q]!=white)qipan[p][q]=32;}}if(kongzhi==player1){if(2*jx==leiji){jilu(p1);jx++;}else if((2*jy-1)==leiji){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break;}else{if(2*jx==(leiji-1)){jilu(p1);jx++;}else if((2*jy-1)==(leiji-1)){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break;} } }}} } /*数据初始化*/void newdata(){int i,j;lx=ly=jx=jy=jushu=1;leiji=sh1=sh2=he=0;for(i=0;i<20;i++)for(j=0;j<20;j++)qipan[i][j]=linshi[i][j]=32;}六、程序运行效果图/*游戏首界面如下*//*以下为选择谁先落子的界面*//*存档时的界面*//*存档成功的界面*//*走棋时的界面,也是悔棋前的界面*//*悔棋时的界面*//*悔棋后的界面*//*以下为赢棋的界面*/七、体会经过这次课程设计,我发现在很多地方的知识都不足,很多函数和技巧都要从网络上或其他书籍上找,可能学计算机要看看很多很多书,尤其在大学,很多情况下都需要自学,我想在以后的学习和实践工作中,会多读多看多练的,努力让自己的实力得到提高,在这个小游戏中,不能调用鼠标,只能通过输入坐标点位置,这是一个很大的不足,以后学习了一定要修改的更好!八、参考文献[1] 湛为芳.C语言程序设计技术.清华大学出版社,2006[2] 谭浩强.C程序设计(第三版).清华大学出版社,2005[3] Kennth A.reek. C和指针(徐波译)[4] 另外还使用了百度中文搜索引擎九、程序清单#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <ctype.h>#define player1 1 /*一号玩家控制*/#define player2 2 /*二号玩家控制*/#define black 1 /*黑色棋子1*/#define white 2 /*白色棋子2*/struct cund /*用来保存棋局*/{char cun[20][20];int jx;int jy;int leiji;}cd;char kz=32;char qipan[20][20]={32};char linshi[20][20]={32};char filename[20]={"c:\\五子棋存档.dat"};int kongzhi;int lx=1,ly=1,leiji=0,jushu=1,sh1=0,sh2=0,he=0; int jx=1,jy=1;void huifu(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)qipan[i][j]=linshi[i][j];}void fuzhi(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)linshi[i][j]=qipan[i][j];}void cundang(){FILE *fp;int i,j;if((fp=fopen(filename,"w"))==NULL){printf(" 存档失败!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();fclose(fp);}else{for(i=0;i<20;i++){for(j=0;j<20;j++){cd.cun[i][j]=linshi[i][j];cd.jx=jx;cd.jy=jy;cd.leiji=leiji;}}fwrite(&cd,sizeof(struct cund),1,fp);printf(" 存档成功!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();fclose(fp);system("cls");}}void duqu(){FILE *fpp;int i,j;if((fpp=fopen(filename,"r"))==NULL){printf(" 你还没有存档,读取存档失败!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();fclose(fpp);}else{fread(&cd,sizeof(struct cund),1,fpp);fclose(fpp);jx=cd.jx;jy=cd.jy;leiji=cd.leiji;for(i=0;i<20;i++)for(j=0;j<20;j++)qipan[i][j]=cd.cun[i][j];system("cls");printf(" 读取存档成功,请继续游戏!!!\n");printf(" 按任意键返回游戏界面!!!");i=_getch();}}void jilu(char (*p)[20])/*记录悔棋前棋盘个子的位置以及悔棋后该谁落子*/{int i,j;for(i=0;i<20;i++){for(j=0;j<20;j++){*(*(p+i)+j)=qipan[i][j];}}}/*悔棋到上你次你的落子权*/void huiqi(char (*p)[20]){int i,j;for(i=0;i<20;i++){for(j=0;j<20;j++){qipan[i][j]=*(*(p+i)+j);}}}/*界面初始化*/void start(){int i,j,k=0;for(i=0;i<15;i++)qipan[i][0]=qipan[0][i]=i;printf("000|001|002|003|004|005|006|007|008|00 9|010|011|012|013|014|\n");for(i=1;i<15;i++){printf("---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");for(j=0;j<15;j++){if(j==0){if(i<10) printf("00%d|",i);else printf("0%d|",i);}elseprintf(" %c |",qipan[i][j]);}printf("\n");}printf("---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");}/*输出界面,每次走完一步棋都要刷新界面*/ void print(){int i=0,j=0,k=0;printf(" 第%d局\n",jushu);printf("一号玩家胜%d局平%d局二号玩家胜%d局\n",sh1,he,sh2);printf("000|001|002|003|004|005|006|007|008|00 9|010|011|012|013|014|\n");for(i=1;i<15;i++){printf("---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");for(j=0;j<15;j++){if(j==0){if(i<10) printf("00%d|",i);else printf("0%d|",i);}elseprintf(" %c |",qipan[i][j]);}printf("\n");}printf("---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");if(leiji%2==1)printf("\n \n\n 一号玩家落子:\n\n\n");elseprintf("\n \n\n 二号玩家落子:\n\n\n");}/*走棋函数*/void zq(char player,char (*p1)[20],char (*p2)[20]) {int mn=0,i=1,p,q;char key;key=_getch();if(player==white){while(1){switch(key){case 'j': if(ly>1) {huifu();ly--;qipan[lx][ly]=95;}system("cls");print();b reak;case 'l': if(ly<14){huifu();ly++;qipan[lx][ly]=95;}system("cls" );print();break;case 'k': if(lx<14){huifu();lx++;qipan[lx][ly]=95;}system("cls" );print();break;case 'i': if(lx>1) {huifu();lx--;qipan[lx][ly]=95;}system("cls");print();b reak;}key=_getch();if(key=='h'){if(linshi[lx][ly]==black||linshi[lx][ly]==white){system("cls");printf("请选择:\n");printf("【1】进入游戏界面存档\n");printf("【2】进入游戏悔棋界面\n");printf("");scanf("%d",&mn);if(mn==1){mn=0;printf("请选择:\n");printf("【1】保存游戏进度\n");printf("【2】读取游戏进度\n");printf("");scanf("%d",&mn);if(mn==1){mn=0;printf(" 你选择了保存游戏进度!!!\n");cundang();system("cls");print();}else if(mn==2){mn=0;printf("你选择了读取游戏进度!!!\n");duqu();fuzhi();system("cls");print();}}else if(mn==2){mn=0;printf(" 你选择了悔棋!!!\n");if(kongzhi==player1){if(leiji%2==0){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}elsehuiqi(p2);leiji-=2;jx--;jy--;fuzhi();system("cls");print();}else{if(leiji%2==1){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}elsehuiqi(p2);leiji-=2;jx--;jy--;fuzhi();system("cls");print();}}}elseif(linshi[lx][ly]!=black&&linshi[lx][ly]!=white){for(p=0;p<20;p++){for(q=0;q<20;q++){if(qipan[p][q]!=black&&qipan[p][q]!=white)qipan[p][q]=32;}}if(kongzhi==player1){if(2*jx==leiji){jilu(p1);jx++;}else if((2*jy-1)==leiji){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break;}else{if(2*jx==(leiji-1)){jilu(p1);jx++;}elseif((2*jy-1)==(leiji-1)){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break;}}}}}else if(player==black){while(1){switch(key){case 'a': if(ly>1) {huifu();ly--;qipan[lx][ly]=95;}system("cls");print();b reak;case 'd': if(ly<14){huifu();ly++;qipan[lx][ly]=95;}system("cls" );print();break;case 's': if(lx<14){huifu();lx++;qipan[lx][ly]=95;}system("cls" );print();break;case 'w': if(lx>1) {huifu();lx--;qipan[lx][ly]=95;}system("cls");print();b reak;}key=_getch();if(key=='f'){if(linshi[lx][ly]==black||linshi[lx][ly]==white){system("cls");printf("\n 请选择:\n");printf("【1】进入游戏界面存档\n");printf("【2】进入游戏悔棋界面\n");printf("");scanf("%d",&mn);if(mn==1){mn=0;printf("\n 请选择:\n");printf("【1】保存游戏进度\n");printf("【2】读取游戏进度\n");printf("");scanf("%d",&mn);if(mn==1){mn=0;printf(" 你选择了保存游戏进度!!!\n");cundang();}else if(mn==2){mn=0;printf("你选择了读取游戏进度!!!\n");duqu();fuzhi();system("cls");print();}}else if(mn==2){printf(" 你选择了悔棋!!!\n");if(kongzhi==player1){if(leiji%2==0){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}else{huiqi(p2);}leiji-=2;jx--;jy--;fuzhi();system("cls");print();}else{if(leiji%2==1){huiqi(p2); /*先走棋的玩家悔棋时的n为偶数*/}else{huiqi(p2);}leiji-=2;jx--;jy--;fuzhi();system("cls");print();}}}elseif(linshi[lx][ly]!=black&&linshi[lx][ly]!=white){for(p=0;p<20;p++){for(q=0;q<20;q++){if(qipan[p][q]!=black&&qipan[p][q]!=white)qipan[p][q]=32;}}if(kongzhi==player1){if(2*jx==leiji){jilu(p1);jx++;}elseif((2*jy-1)==leiji){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break;}else{if(2*jx==(leiji-1)){jilu(p1);jx++;}elseif((2*jy-1)==(leiji-1)){jilu(p2);jy++;}qipan[lx][ly]=player;fuzhi();system("cls");print();break;}}}}}}int checkheqi(){char ch;if((leiji-1)==186){printf(" 真是棋逢敌手啊,棋已至此仍未能分出胜负...........\n");printf(" 但现在棋盘即将落满,请问两位棋士是否选择和棋,改日再战?Y / N ?\n");ch=getchar();while(ch!='Y'||ch!='N'){printf("请选择是或否Y / N ");ch=getchar();}if(ch=='Y'){printf("哈哈...来日方长...两位光然有棋士风范,拿得起放得下...改日再战...\n");return(3);}if(ch=='N'){printf(" 哈哈,看来两位都不愿和棋...... \n");printf(" 那就请继续下完此局,但是不要忘了,棋盘落满子的时候自动判为和棋.....\n");return(0);}}if((leiji-1)==196){printf(" \n\n棋盘已满,系统自动判为和棋,请两位棋士改日再战...........\n\n\n\n");return(5);}return(0);}/*判断是否有一方赢棋,并结束游戏*/int check(){int i,j;for(i=1;i<11;i++){for(j=1;j<11;j++){/*对一号玩家行检测*/if((qipan[i][j]==black)&&(qipan[i][j+1]==black )&&(qipan[i][j+2]==black)&&(qipan[i][j+3]==black) &&(qipan[i][j+4]==black)){printf("\n一号玩家胜出!二号玩家不要灰心,再接再厉!\n");return(1);}/*对一号玩家列检测*/if((qipan[i][j]==black)&&(qipan[i+1][j]==black )&&(qipan[i+2][j]==black)&&(qipan[i+3][j]==black) &&(qipan[i+4][j]==black)){printf("\n一号玩家胜出!二号玩家不要灰心,再接再厉!\n");return(1);}/*对二号玩家行检测*/if((qipan[i][j]==white)&&(qipan[i][j+1]==white )&&(qipan[i][j+2]==white)&&(qipan[i][j+3]==white)&&(qipan[i][j+4]==white)){printf("\n二号玩家胜出!一号玩家不要灰心,再接再厉!\n");return(2);}/*对二号玩家列检测*/if((qipan[i][j]==white)&&(qipan[i+1][j]==white)&&( qipan[i+2][j]==white)&&(qipan[i+3][j]==white)&&( qipan[i+4][j]==white)){printf("\n二号玩家胜出!一号玩家不要灰心,再接再厉!\n");return(2);}}}for(i=1;i<1;i++){for(j=14;j>4;j--){/*对一号玩家右斜线检测*/if(qipan[i][j]==black&&qipan[i+1][j-1]==black&&qi pan[i+2][j-2]==black&&qipan[i+3][j-3]==black&&qi pan[i+4][j-4]==black){printf("\n一号玩家胜出!二号玩家不要灰心,再接再厉!\n");return(1);}/*对二号玩家右斜线检测*/if(qipan[i][j]==white&&qipan[i+1][j-1]==white &&qipan[i+2][j-2]==white&&qipan[i+3][j-3]==white&&qipan[i+4][j-4]==white){printf("\n二号玩家胜出!一号玩家不要灰心,再接再厉!\n");return(2);}}}for(i=1;i<11;i++){for(j=1;j<11;j++){/*对一号玩家左斜线检测*/if(qipan[i][j]==black&&qipan[i+1][j+1]==black &&qipan[i+2][j+2]==black&&qipan[i+3][j+3]==black&&qipan[i+4][j+4]==black){printf("\n一号玩家胜出!二号玩家不要灰心,再接再厉!\n");return(1);}/*对二号玩家左斜线坚测*/if(qipan[i][j]==white&&qipan[i+1][j+1]==white&&qi pan[i+2][j+2]==white&&qipan[i+3][j+3]==white&& qipan[i+4][j+4]==white){printf("\n二号玩家胜出!一号玩家不要灰心,再接再厉!\n");return(1);} }}return(0);}/*输出游戏首页*/void Printsjm(){printf(" * * * * * * * * * * * * * * * 游戏名称* * * * * * * * * * * * * * **\n");printf(" * * * * * * * * *\n");printf(" * * * * * * 五子棋* * * * *\n");printf(" * * * * * * * * *\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf("\n");printf(" * * * * * * * * * * * * * * * 游戏规则* * * * * * * * * * * * * * * *\n");printf(" * * * * * * * *\n");printf(" * * * * 游戏悔棋时不允许连续悔棋* * * *\n");printf(" * * * * * * * *\n");printf(" ************ 任一方五子相连即获胜,一局游戏也就结束***********\n");printf(" * * * * * * * *\n");printf(" ********* 请遵守游戏规则谢谢!!!********\n");printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf("\n");printf(" * * * * * * * * * * * * * * * 游戏制作* * * * * * * * * * * * * * * *\n");printf(" * * * * * * * *\n");printf(" *** *** 计算机工程学院094班*** ***\n");printf(" * * * * * * * *\n");printf(" * * * * * * * 王鹏* * * * * * *\n");printf(" * * * * * * * 2010.6.28 * * * * * * *\n");printf("******************************************* **************************\n");}/*数据初始化*/void newdata(){int i,j;lx=ly=jx=jy=jushu=1;leiji=sh1=sh2=he=0;for(i=0;i<20;i++)for(j=0;j<20;j++)qipan[i][j]=linshi[i][j]=32;}/* 主函数*/int main(){char ch,ch1='Y';char hq1[20][20]={0},hq2[20][20]={0};char (*p1)[20]=hq1,(*p2)[20]=hq2;int m=0,n=0;int i,j;system("color 5a");Printsjm();printf("\n\n\n\n 现在就开始游戏吗?(Y / N) "); /*确定开始游戏还是退出*/ch=getchar();while(ch!='Y'&&ch!='N'&&ch!='y'&&ch!='n'){printf("您的输入有错误,请输入Y / N :");ch=getchar();}system("cls");while(ch1=='Y'||ch1=='y'){system("cls");if(ch=='Y'||ch=='y'){/*是就开始新游戏*/printf("第%d局游戏开始",jushu);}else if(ch=='N'||ch=='n')/*否则结束*/return(0);printf("\n\n\n 请选择一号玩家先落子还是二号玩家先落子?(1 / 2)");/*确定哪个玩家先落子*/scanf("%d",&kongzhi);newdata();if(kongzhi==player1){leiji=0;printf(" 一号玩家胜%d局平%d局二号玩家胜%d局\n",sh1,he,sh2);printf(" 本局游戏一号玩家先落子,现在游戏开始了!!! \n");}else if(kongzhi==player2){leiji=1;printf("一号玩家胜%d局平%d局二号玩家胜%d局\n",sh1,he,sh2);printf(" 本局游戏二号玩家先落子,现在游戏开始了!!! \n");}start(); /*窗口初始化,游戏开始*/do{if(leiji%2==0){kz=black;}else if(leiji%2==1){kz=white;}leiji++;n++;zq(kz,p1,p2);/*调用走棋函数*/system("cls");print();m=checkheqi();/*没有选择和棋则继续游戏*/if(m==0) ;/*选择和棋则结束游戏*/if(m==3) {he++; break;}/*如果没有选择和棋,并且棋盘已满,则强制结束游戏,判为和棋*/if(m==5) {he++; break;}m=check();if(m==1) sh1++;else if(m==2) sh2++;}while(m==0);for(i=0;i<20;i++){for(j=0;j<20;j++){qipan[i][j]=0;hq1[i][j]=0;hq2[i][j]=0;}}jushu+=1;printf(" 是否进行第%d局?Y / N ",jushu);for(i=0;i<20;i++)for(j=0;j<20;j++){qipan[i][j]=0;linshi[i][j]=0;}ch1=getchar();ch1=getchar();}return(0);}。