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棋盘上,棋手们更容易出现失误,对局结果更加平均。
- 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);}。