C语言程序设计之五子棋
五子棋游戏程序设计

C程序组成
C程序
源程序1 …… 源程序2 …… 源程序n
预编译命令
函数1
……
函数n
说明部分
执行部分
开发方法: 自上向下,逐步细化,模块化设计,结构化编码
五子棋游戏程序
一个综合实例的分析
五 子 棋 游 戏 程 序
一个综合实例的分析
一般来说,开发一个软件要经过以下步骤:
确定软件的功能
按航班号查询 按终点站查询
系统进入画面(静态或动画) 承办订票和退票业务(可选项)
参考设计题目三:学生选修课程系统设计
假定有n门课程,每门课程有课程编号,课程名称, 课程性质,总学时,授课学时,实验或上机学时, 学分,开课学期等信息,学生可按要求(如总学分 不得少于60)自由选课。试设计一选修课程系统, 使之能提供以下功能: 系统以菜单方式工作 课程信息录入功能(课程信息用文件保存)--输入 课程信息浏览功能--输出 查询功能:(至少一种查询方式)--算法
§2 Turbo C绘图
Turbo C支持的适配器和图形模式 图形库文件与图形头文件 适配器 模式 分辨率 颜色数 标识符 graphics.lib与graphics.h 0 4 CGAC0 320 200 CGA 图形显示的坐标与象素 4 CGAC1 (彩色图形 1 320 200
适配器) (0,0)
颜色控制函数
设调色板:setpalette() 设背景色:setbkcolor() 设绘图色:setcolor()
颜色值 0 1 2 3 4 5 6 7
颜色名
BLACK BLUE
颜色 颜色值 黑 蓝 绿 青 红 洋红 棕 浅灰 8 9
颜色名
DARKGRAY
C语言程序设计---五子棋 (1)

五子棋算法:任何一种棋类游戏其关键是对当前棋局是否有正确的评分,评分越准确则电脑的AI 越高。
五子棋游戏也是如此,但在打分之前,我们先扫描整个棋盘,把每个空位从八个方向上的棋型填入数组gStyle(2,15,15,8,2),其中第一个下标为1时表示黑棋,为2时表示白棋,第二和第三个下标表示(x,y),第四个下标表示8个方向,最后一个下标为1时表示棋子数,为2时表示空格数,如:gStyle(1,2,2,1,1)=3表示与坐标(2,2)在第1个方向上相邻的黑棋棋子数为3gstyle(1,2,2,1,2)=4表示与坐标(2,2)在第1个方向上的最近的空格数为4在定义方向时,也应该注意一定的技巧,表示两个相反的方向的数应该差4,在程序中我是这样定义的:Const DIR_UP=1Const DIR_UPRIGHT=2Const DIR_RIGHT=3Const DIR_RIGHTDOWN=4Const DIR_DOWN=5Const DIR_DOWNLEFT=6Const DIR_LEFT=7Const DIR_LEFTUP=8这样我们前四个方向可以通过加四得到另一个方向的值。
如果你还是不太明白,请看下面的图:---------------------oo-----ox*xx---------------------图中的*点从标为(4,4),(打*的位置是空位),则:gStyle(2,4,4,1,1)=1在(4,4)点相邻的上方白棋数为1gStyle(2,4,4,1,2)=2在(4,4)点的上方距上方白棋最近的空格数为2gStyle(1,4,4,3,1)=2在(4,4)点相邻的右方黑棋数为2gStyle(1,4,4,3,2)=1在(4,4)点的右方距右方黑棋最近的空格数为3...一旦把所有空点的棋型值填完,我们很容易地得出黑棋水平方向上点(4,4)的价值,由一个冲1(我把有界的棋称为冲)和活2(两边无界的棋称为活)组成的。
五子棋游戏C语言

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

1. 键盘上事先设定8个按键,分作两组,每组四个,分别代表两个人用来控制棋 子的上下左右键。
2. 绘制棋盘,15条横线,15条竖线,在直线交点处下棋子(实心圆形)。 3. 黑子先行,黑白交替下子,在棋盘上设定一个与棋盘格大小边长相等的正方
形,初始状态,正方形的中心位于期盼的中心点。当一方欲走棋的时候,应 用四个按键来控制所要下棋的位置,每按一次按键,正方形都要向相应方向 移动一个格,但不能让其移出边界。当按下回车键时,应在正方形所在位置 放下一个棋子,然后此组按键不能操作,换另一个人下棋,用另一组按键, 规则同前。 4. 当任何一方有五个棋子沿着横,竖,斜连在一起时,系统自动判断赢棋,并 显示黑方或白方胜利。棋局结束后,任何一方均不能继续操作。 相关知识:图形绘制、音乐制作、键盘响应 功能扩充:(1)可以设置一个按键,用来悔棋。并且让喇叭发出某种声音,将要移
main()
screen()
InitGraph()
InitData()
DrawChessboard()
GamePlay()
closegraph()
ShowChess() Reminder()
MoveChess()
CheckWin()
GetNum()
ShowChess()
Refresh()
函数说明: 1. InitGraph():初始化图形,确认游戏能否正常运行。 2. InitData():初始化数据,设定棋盘坐标,棋子半径。 3. DrawChessboard():画棋盘,设定显示画面。 4. ShowChess()画棋子,并且填充棋子。 5. Reminder()提醒执该子棋手下棋。 6. GamePlay ()开始游戏的程序,里面包含选择退出,还是重新开始,还是继续进行。 7. MoveChess()使棋子能实现移动功能。 8. CheckWin()判断棋子能否赢的程序。 9. Refresh()更新画面,使刚下的棋子实现显示。 10.GetNum()用来数清各方向棋子数目。 11.Screen()画欢迎界面。 §2 模块划分 1.InitGraph()
用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原码/*turboc2.0下编译通过*/ #i nclude <graphics.h> #i nclude <stdlib.h>#i nclude <stdio.h>#i nclude <conio.h>#define N 15#define B 7#define STOP -10000 #define OK 1#define NO 0#define UP 328#define DOWN 336#define LEFT 331#define RIGHT 333/*定义了两个数,n为棋盘的大小。
b为背景颜色的数值*/ int a[N+1][N+1];int zx,zy;int write=1,biaoji=0;struct zn{long sum;int y;int x;}w[N+1][N+1],max,max1;void cbar(int i,int x,int y,int r);void map(int a[][]);int getkey();int key();void zuobiao(int x,int y,int i);int tu(int a[][],int write);int wtu(int a[][],int write);int zhineng(int a[][]);int zh5(int y,int x,int a[][]);long zzh5(int b[][],int i);main(){int i,j;int gdriver=DETECT;int gmode;initgraph(&gdriver,&gmode,""); zx=(N+1)/2;zy=(N+1)/2;for(i=1;i<=N;i++)for(j=1;j<=N;j++)a[i][j]=0;map(a);i=1;while(i){int k,n;k=wtu(a,write);if(k==STOP) goto end; map(a);n=zhineng(a);if(n==STOP) goto end; map(a);}end:;}/* 实现对局的程序,计算全部N*N个格中,最应该填的格子*/ int zhineng(int a[N+1][N+1]){int i,j;int k;max.sum=-1;for(i=0;i<=N;i++)for(j=0;j<+N;j++){w[i][j].sum=0;w[i][j].x=i;w[i][j].y=j;}for(i=1;i<=N-4;i++)for(j=1;j<=N-4;j++){k=zh5(i,j,a);if(k==STOP) return (STOP); }for(i=1;i<=N;i++)for(j=1;j<=N;j++){if(max.sum<w[i][j].sum) {max.sum=w[i][j].sum; max.y=i;max.x=j;}else if(max.sum==w[i][j].sum){if(((max.y-zy)*(max.y-zy)+(max.x-zx)*(max.x-zx))>((i-zy)*(i-zy)+(j-zx)*(j-zx))) max.sum=w[i][j].sum;max.y=i;max.x=j;}}if(a[max.y][max.x]==0){a[max.y][max.x]=-1;zy=max.y;zx=max.x;}}/* 转换成5*5的数组,计算出在二十五个格子中,最应该填的格*/ int zh5(int y,int x,int a[N+1][N+1]){int i,j;int b[6][6];long c[13];long d[6][6];long temp;for(i=y;i<=y+4;i++)for(j=x;j<=x+4;j++)b[i+1-y][j+1-x]=a[i][j];c[1]=b[1][1]+b[1][2]+b[1][3]+b[1][4]+b[1][5]; c[2]=b[2][1]+b[2][2]+b[2][3]+b[2][4]+b[2][5]; c[3]=b[3][1]+b[3][2]+b[3][3]+b[3][4]+b[3][5]; c[4]=b[4][1]+b[4][2]+b[4][3]+b[4][4]+b[4][5]; c[5]=b[5][1]+b[5][2]+b[5][3]+b[5][4]+b[5][5]; c[6]=b[1][1]+b[2][1]+b[3][1]+b[4][1]+b[5][1]; c[7]=b[1][2]+b[2][2]+b[3][2]+b[4][2]+b[5][2]; c[8]=b[1][3]+b[2][3]+b[3][3]+b[4][3]+b[5][3]; c[9]=b[1][4]+b[2][4]+b[3][4]+b[4][4]+b[5][4]; c[10]=b[1][5]+b[2][5]+b[3][5]+b[4][5]+b[5][5]; c[11]=b[1][1]+b[2][2]+b[3][3]+b[4][4]+b[5][5]; c[12]=b[1][5]+b[2][4]+b[3][3]+b[4][2]+b[5][1];for(i=1;i<=12;i++)switch(c[i]){case 5:biaoji=1;return(STOP); case -5:biaoji=-1;return(STOP); case -4:c[i]=100000;break; case 4:c[i]=100000;break; case -3:c[i]=150;break;case 3:c[i]=150;break;case -2:c[i]=120;break;case 2:c[i]=100;break;case -1:c[i]=1;break;case 1:c[i]=1;break;default: c[i]=0;}}for(i=1;i<=12;i++){if(c[i]==150)c[i]+=zzh5(b,i);}for(i=1;i<=5;i++)for(j=1;j<=5;j++)d[i][j]=0;for(i=1;i<=5;i++)for(j=1;j<=5;j++){if(i==j) d[i][j]+=c[11];if((i+j)==6) d[i][j]+=c[12];d[i][j]+=c[i]+c[j+5]; }for(i=1;i<=5;i++) for(j=1;j<=5;j++) {if(b[i][j]!=0)d[i][j]=-2;}max1.sum=-1; max1.y=0;max1.x=0;for(i=1;i<=5;i++) for(j=1;j<=5;j++) {if(max1.sum<d[i][j]){max1.sum=d[i][j];max1.y=i;max1.x=j;w[i+y-1][j+x-1].sum+=max1.sum;}else if(max1.sum==d[i][j]){if(((i+y-1-zy)*(i+y-1-zy)+(j+x-1-zx)*(j+x-1-zx))>((max1.y+y-1-zy)*(max1.y+y-1-zy)+(max1.x +x-1-zx)*(max1.x+x-1-zx))){max1.sum=d[i][j];max1.y=i;max1.x=j;}}}}long zzh5(int b[6][6],int n){int i,j,k,l,m;switch(n){case 1:i=b[1][1];j=b[1][2];k=b[1][3];l=b[1][4];m=b[1][5];break; case 2:i=b[2][1];j=b[2][2];k=b[2][3];l=b[2][4];m=b[2][5];break; case 3:i=b[3][1];j=b[3][2];k=b[3][3];l=b[3][4];m=b[3][5];break; case 4:i=b[4][1];j=b[4][2];k=b[4][3];l=b[4][4];m=b[4][5];break; case 5:i=b[5][1];j=b[5][2];k=b[5][3];l=b[5][4];m=b[5][5];break; case 6:i=b[1][1];j=b[2][1];k=b[3][1];l=b[4][1];m=b[5][1];break;case 7:i=b[1][2];j=b[2][2];k=b[3][2];l=b[4][2];m=b[5][2];break;case 8:i=b[1][3];j=b[2][3];k=b[3][3];l=b[4][3];m=b[5][3];break;case 9:i=b[1][4];j=b[2][4];k=b[3][4];l=b[4][4];m=b[5][4];break;case 10:i=b[1][5];j=b[2][5];k=b[3][5];l=b[4][5];m=b[5][5];break;case 11:i=b[1][1];j=b[2][2];k=b[3][3];l=b[4][4];m=b[5][5];break;case 12:i=b[1][5];j=b[2][4];k=b[3][3];l=b[4][2];m=b[5][1];break;}if((i==0&&j==1&&k==1&&l==1&&m==0))return (900);if((i==0&&j==-1&&k==-1&&l==-1&&m==0))return(1000);if((i==0&&j==0&&k==1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==0&&m==0)) return(20);if((i==0&&j==0&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==0&&m==0)) return(20);if((i==-1&&j==1&&k==1&&l==1&&m==1)||(i==1&&j==-1&&k==1&&l==1&&m==1)||(i==1&& j==1&&k==-1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==-1&&m==1)||(i==1&&j==1&&k= =1&&l==1&&m==-1))return(-60);if((i==1&&j==-1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==1&&k==-1&&l==-1&&m==-1)||(i== -1&&j==1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==1&&m==-1)||(i==-1&&j ==-1&&k==-1&&l==-1&&m==1))return(-60);}/* 循环执行坐标的选择,直到按回车,空格或ESC键*/int wtu(int a[N+1][N+1],int write){int i=1;map(a);zuobiao(zx,zy,1);while(i){int k;k=tu(a,write);if(k==OK) i=0;if(k==STOP) return (STOP); }}/*从键盘获得输入的值*/int getkey(){int key,lo,hi;key=bioskey(0);lo=key&0x00ff;hi=(key&0xff00)>>8;return((lo==0) ? hi+256:lo);}/*对获得的值进行判断*//*对应的码值分别如下*//* 上:328 下:336 左:331 右:333 */ /* 回车:13 ESC键:27 */int key(){int k;k=getkey();switch(k){case 27: return (STOP);case 13:case ' ': return (OK);case 328: return (UP); case 336: return (DOWN); case 331: return (LEFT); case 333: return (RIGHT); default: return (NO);}}/*用来显示坐标的位置*/ void zuobiao(int x,int y,int i) {int r;if(i!=0){setcolor(GREEN);for(r=1;r<=5;r++)circle(75+25*x,25+25*y,r);}else{if(a[zy][zx]==1){setcolor(8);for(r=1;r<=5;r++)circle(75+25*x,25+25*y,r); }else if(a[zy][zx]==-1){setcolor(WHITE);for(r=1;r<=5;r++)circle(75+25*x,25+25*y,r);}else{setcolor(B);for(r=1;r<=5;r++)circle(75+25*x,25+25*y,r);setcolor(RED); line(75+25*zx-5,25+25*zy,75+25*x+5,25+25*zy); line(75+25*zx,25+25*zy-5,75+25*zx,25+25*zy+5);}}}/*从键盘获得的值进行判断,反映在显示的图上*/int tu(int a[N+1][N+1],int write) {int k;re:k=key();if(k==OK){if(a[zy][zx]==0){a[zy][zx]=write;}elsegoto re;}if(k==STOP) return(STOP);if(k==NO) goto re; if(k==UP){int i,j;if(zy==1) j=zy; else j=zy-1; zuobiao(zx,zy,0); zuobiao(zx,j,1); zy=j;goto re;}if(k==DOWN) {int i,j;if(zy==N) j=zy; else j=zy+1; zuobiao(zx,zy,0); zuobiao(zx,j,1); zy=j;goto re;}if(k==LEFT) {int i,j;if(zx==1) i=zx; else i=zx-1; zuobiao(zx,zy,0); zuobiao(i,zy,1); zx=i;goto re;}if(k==RIGHT) {int i,j;if(zx==N) i=zx; else i=zx+1; zuobiao(zx,zy,0); zuobiao(i,zy,1); zx=i;goto re;}}/* 根据数组中(存储棋子位置)各位置的数,画实心圆(画出棋子)*/ void cbar(int i,int x,int y,int r){if(i!=0){if(i==1)setcolor(8);else if(i==-1)setcolor(WHITE);for(i=1;i<=r;i++){circle(x,y,i);}}}/*画出棋盘,和各个棋子*/void map(int a[N+1][N+1]){int i,j;cleardevice();setbkcolor(B);setcolor(RED);for(i=0;i<N;i++){line(100,50+25*i,75+N*25,50+25*i); line(100+25*i,50,100+25*i,25+N*25); }for(i=1;i<=N;i++)for(j=1;j<=N;j++)cbar(a[i][j],75+25*j,25+25*i,10); }。
c语言五子棋课程设计

c语言五子棋课程设计一、课程目标知识目标:1. 学生能理解并掌握C语言的基本语法和编程技巧;2. 学生能运用C语言编写五子棋游戏的基本功能,包括棋盘的初始化、玩家输入、落子、判断胜负等;3. 学生能通过五子棋案例,理解并掌握数组和循环等C语言核心知识点的应用。
技能目标:1. 学生能够运用结构化的编程思想进行问题分析,将复杂问题分解为可解决的小问题;2. 学生能够独立完成五子棋游戏的编写,培养编程实践能力和解决问题的能力;3. 学生通过团队协作完成课程项目,提高沟通和协作能力。
情感态度价值观目标:1. 学生在编程实践中培养逻辑思维能力和创新意识,增强对编程的兴趣和热情;2. 学生通过五子棋游戏的设计与实现,体验编程带来的成就感,提高自信心;3. 学生在团队协作中学会相互尊重、理解和帮助,培养良好的合作精神。
二、教学内容1. C语言基础语法回顾:变量定义与使用、数据类型、运算符、表达式、控制语句(if、for、while等);2. 数组的应用:一维数组、二维数组,重点讲解二维数组在五子棋棋盘中的应用;3. 函数的定义与调用:编写功能模块,如初始化棋盘、打印棋盘、落子、判断胜负等函数;4. 指针的应用:指针与数组的关系,通过指针操作五子棋棋盘;5. 五子棋游戏设计与实现:分析游戏需求,设计游戏流程,编写代码实现游戏功能;6. 结构体的使用:定义玩家信息结构体,存储和管理玩家信息;7. 文件操作:读取和保存棋局,实现游戏进度保存与加载;8. 算法与逻辑:介绍五子棋胜负判断的算法,以及优化策略;9. 项目实践:学生分组进行五子棋游戏的开发,按照教学进度完成相应功能;10. 课堂讨论与展示:分析项目中的问题与解决方法,分享编程技巧,展示团队成果。
教学内容按照课本章节进行组织,确保学生能够将所学知识应用于实际项目中,逐步掌握C语言编程的核心技能。
三、教学方法本课程采用以下多样化的教学方法,旨在激发学生的学习兴趣,提高学生的主动性和实践能力:1. 讲授法:教师以清晰、生动的语言讲解C语言的基本概念、语法规则和五子棋游戏设计原理。
c语言课程设计五子棋

c语言课程设计五子棋一、教学目标本课程的教学目标是使学生掌握C语言编程基础,能够运用C语言设计并实现一个简单的五子棋游戏。
通过本课程的学习,学生将能够理解C语言的基本语法、数据类型、运算符、控制结构等基础知识,并能够运用这些知识解决实际问题。
同时,通过设计五子棋游戏的过程,培养学生的编程思维、逻辑思维和团队合作能力。
具体的学习目标包括:1.知识目标:–掌握C语言的基本语法和编程规范。
–理解数据类型、变量、运算符和控制结构的概念及使用方法。
–学会使用函数进行模块化编程。
–了解五子棋游戏的规则和算法。
2.技能目标:–能够使用C语言编写简单的程序,解决实际问题。
–能够运用循环、条件语句等控制结构编写复杂的程序。
–能够使用函数进行模块化编程,提高代码的可读性和可维护性。
–能够设计并实现一个简单的五子棋游戏,掌握游戏算法和逻辑。
3.情感态度价值观目标:–培养学生的编程兴趣,激发学习编程的积极性。
–培养学生的团队合作意识,学会与他人共同解决问题。
–培养学生的创新思维,勇于尝试和解决问题。
二、教学内容本课程的教学内容主要包括C语言的基本语法、数据类型、运算符、控制结构等基础知识,以及五子棋游戏的规则和算法。
具体的教学大纲如下:1.C语言基本语法和编程规范。
2.数据类型、变量和运算符的概念及使用方法。
3.控制结构(循环、条件语句)的使用和编程实践。
4.函数的定义和调用,模块化编程的优点和实践。
5.五子棋游戏的规则和算法分析。
6.五子棋游戏的界面设计和实现。
7.五子棋游戏的逻辑设计和实现。
三、教学方法本课程的教学方法包括讲授法、案例分析法、实验法和讨论法。
1.讲授法:通过讲解C语言的基本语法、数据类型、运算符、控制结构等基础知识,使学生掌握基本的编程概念和技巧。
2.案例分析法:通过分析典型的五子棋游戏案例,使学生理解五子棋游戏的规则和算法。
3.实验法:让学生通过编写代码和调试程序,实践C语言的基本语法和编程技巧,培养学生的动手能力。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
安徽大学
电子信息工程学院
C语言程序设计
指导老师:梁栋
小组成员:高丽蓉高雪朱楠丁昊刘佳慧阮超
C语言程序设计之五子棋
摘要:本程序是一种两人对弈的纯策略型棋类游戏,规则简单,变化多端,非常富有趣味性和消遣性。
主要应用了Visual C++编程来完成这个游戏的设计的。
本文主要介绍了本游戏的开发环境Microsoft Visual C++6.0,使用当前优秀的开发工具VC++编程基于MFC的基本对话框,编写了一个五子棋的游戏软件。
包含了五子棋程序的棋盘初始化、游戏规则、胜负判断方法。
关键词:五子棋游戏应用程序、初始化、规则、图形函数、判断
1.问题描述
在Visual C++ 6.0编译器的基础上,基于MFC的基本对话框,编写一个五子棋的游戏软件。
该游戏应该符合以下要求:
1.1游戏模式:人先下棋,电脑在人下子后会随后下棋。
1.2人机对弈:完成人和计算机的对弈,用黑色棋子表示人,白色棋子表示电脑,电脑要具有一定的人工智能,能够可以与人对弈一段时间。
1.3棋盘:要求棋盘绘制美观耐看。
1.4判断输赢:当一方有五个相邻的棋子连成一条线时,计算机能判定输赢,并能结束本次游戏并重新开始。
1.5存储功能:要求五子棋有存储功能,可以将进行中的游戏存储在棋盘上,也可以读取已存盘的游戏。
1.6弹窗功能:要求制作按钮,实现弹框功能,可以弹出指导老师与小组成员的信息。
2.详细设计
2.1总体设计
程序流程图如下图所示:
2.1.1 新建工程,选MFC AppWizard(exe),添上工程名,确定。
2.1.2选基本对话框,完成,确定
2.1.3插入位图
我们选择的方式从网上搜寻一些图片,然后直接贴图。
先插入位图(BMP格式)。
位图插入后会自动赋予ID值,我们将其修改一下:分别修改为BLACK BOARD WHITE CLEAR
2.1.4添加DRAW函数
添加成员函数Draw:
然后添加所编的程序代码。
2.1.5在OnPaint函数中加入代码画图
在OnPaint函数中加入以下代码:
CDC *pDC;
pDC=GetDC();
Draw(0,0,IDB_CLEAR,pDC);
Draw(13,13,IDB_BOARD,pDC);
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
chess[i][j]=0;
2.1.6添加OnLButtonUp 函数
添加消息处理函数:
然后添加所编的程序代码。
2.1.7添加iswin函数
然后添加所编的程序代码
2.1.8重复步骤(7),依次添加search,searchvalue,getsorce,AIplay 函数。
2.1.9复制按钮确定,并粘贴两次,并改名为“读档”“保存”“指导老师”“小组成员”
2.1.10给这四个按钮建立类向导
添加关联函数
然后添加所编的程序代码。
2.1.11在OnInitDialog函数中加入以下代码
MoveWindow(0,0,515,580);
CenterWindow();
GetDlgItem(IDC_SAVE)->MoveWindow(10,515,55,24);
GetDlgItem(IDC_OPEN)->MoveWindow(70,515,55,24);
GetDlgItem(IDC_teacher)->MoveWindow(345,515,55,24);
GetDlgItem(IDC_students)->MoveWindow(405,515,55,24);
2.1.12更改icon图标
2.2数据结构设计
2.3函数功能描述
OnInitDialog():通过这个函数,我们定义了窗口的大小、窗口所处的位置、保存按钮的放置、读档按钮的放置、指导老师按钮的放置以及小组成员按钮的放置。
OnPaint():通过这个函数,我们在窗口上画出了棋盘。
Draw(int x, int y, UINT bitmap, CDC *pDC):通过这个函数,我们可以画出各种图片。
OnLButtonUp(UINT nFlags, CPoint point):通过这个函数,我们可以通过鼠标控制在窗口上下子。
iswin(int i, int j):通过这个函数,我们可以判断胜负。
search(int i, int j, int m, int n):通过这个函数,我们可以判断棋子周围的情况。
AIplay():通过这个函数,我们设计了一个人工智能来与人对弈。
searchvalue(int &best_i, int &best_j, int color):寻找棋子的有利位置getscore(int i, int j, int color):计算这个函数位置的分数
OnSave():通过这个函数,我们实现了存储的功能。
OnOpen():通过这个函数,我们实现了打开存储的功能。
Onteacher():通过这个函数,我们实现了”指导老师“的弹出窗口。
Onstudents():通过这个函数,我们实现了”小组成员“的弹出窗口。
3.总结
本作品是一个简单的双人五子棋游戏,运行程序后直接进入棋盘界面,最终会判断出输赢。
主要设计内容概括如下:
3.1在初期,本小组认真的对五子棋进行了研究分析,参考了可供参考的资料和程序,确定了运行步骤和流程,并分配了各自的任务。
要求每位组员细致了解部分MFC函数,以便实现五子棋游戏的编译。
如定义好窗口大小及所处位置、保存按钮的放置、读档按钮的放置、指导老师按钮的放置以及小组成员按钮的放置等。
通过各种函数实现画出棋盘、用鼠标控制落子、判断棋子周围的情况、判断胜负、实现存储、打开存储等功能。
搜寻图片,找到适合用于五子棋游戏中棋盘、棋子的图片。
分配任务给各个小组成员,分别编写五子棋程序所需要的函数。
3.2期间程序需要添加很多成员函数及消息处理函数,如Draw函数、OnPaint函数、iswn函数等。
3.3运行五子棋并进行测试,每位组员负责的程序部分写好之后,再将大家的程序汇总,但发现有些人的模块编写有误,导致程序无法运行,因此大家在一起讨论与修改并回去思考。
经过多次测试并改进程序部分函数,程序已经可以运行
了。
考虑到五子棋游戏比较简单,就没有设置悔棋环节。
3.3在做C语言程序设计之前,编辑游戏在组里的每个成员看来,都是遥不可及的。
但通过这次设计我明白了并不是如此,只要团队合作也可以搞定,我多学了许多课上没有的知识,比如怎么用C程序画图、写出文本等。
通过C语言课程设计,我们对语言这门课程有了更深一步的了解。
它是计算机程序设计的重要理论技术基础,在我们电子专业的学习中占据着十分重要的地位。
同时也使我们知道,要学好这门课程,仅学习书本上的知识是不够的,还要有较强的实践能力。
只要多实践,多编写程序,才能更好的掌握书本上的东西。
参考文献:
梁栋、李新华等,C语言程序设计,中国电力出版社,2009年2月谭浩强,C程序设计(第二版),清华大学出版社,2005年
邓莉,C++程序设计(第四版),清华大学出版社,2010年
侯俊杰,深入浅出MFC(第二版),华中科技大学出版社,2001年
葛亮,Visual C++从入门到实践,清华大学出版社,2007。