c语言程序设计五子棋

合集下载

五子棋(双人对战) C语言课程设计.doc

五子棋(双人对战) C语言课程设计.doc

C语言程序设计题目五子棋(双人对战)指导教师曹东燕学生姓名夏文龙于文杰邢健学号 201000802032 201000802114 201000802097专业电子信息工程教学单位物理系(盖章)目录1设计内容与设计要求 (1)1.1系统功能简介 (1)1.2系统运行环境 (1)2程序流程图 (1)3运行的运行及说明 (2)3.1系统的运行 (2)3.2运行说明 (4)4课程设计目的………………………………………………….5 程序特色 (5)6总结 (5)附件:源程序清单 (5)1设计内容与设计要求1.1系统功能简介该系统是五子棋的人人对战模式,而非平常的人机对战系统有简单的菜单界面1.2系统运行环境Vc++6.0下运行,硬件基本无要求,现在的计算机系统基本都可以运行该系统。

2程序流程图3系统的运行及说明3.1系统的运行图3-1程序的初始化界面程序初始化的界面,界面简洁明快,便于观察。

图3-2程序的运行界面图3-3程序的介绍界面3-2运行说明A向左 W向上 D向右 S向左空格键确定当结束时按Y重新开始游戏按N退出游戏4课程设计目的五子棋游戏不仅能增强人们的抽象思维能力、逻辑推理能力、空间想象力,提高人们的记忆力、心算能力等,而且深含哲理,有助于修身养性。

五子棋既有现代休闲方式所特有的特征“短、平、快” ,又有中国古典哲学所包含的高深学问“阴阳易理” ;它既有简单易学的特点,为人民群众所喜闻乐见,又有深奥的技巧;既能组织举办群众性的比赛、活动,又能组织举办高水平的国际性比赛;它的棋文化源渊流长,具有东方的神秘和西方的直观,它是中西方文化的交融点,也是中西方文化交流的一个平台。

自从计算机作为游戏对战平台以来,各种棋类游戏如雨后春笋般纷纷冒出。

五子棋是一种受大众广泛喜爱的游戏,其规则简单,变化多端,非常富有趣味性和消遣性。

同时具有简单易学、既动手又动脑的特点。

同时也为锻炼自己的编程能力。

5程序特色本程序简洁明快,采用黑白两色棋,并且本程序为双人对战。

C语言程序设计---五子棋 (1)

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语言

五子棋游戏程序设计(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语言课程设计案例-五子棋.

c语言课程设计案例-五子棋.

使用图形方式时先进行图形系统初始化, 检测或设置指定的图形模式
图形系统的初始化

图形系统初始化函数
void initgraph(int *driver,int *mode,char *pathtodriver) 其中; driver——显示适配器驱动程序的枚举变量 mode——图形模式(模式号或模式标识符) pathtodriver——适配器驱动程序的寻找路径,“” 表示在当前目录下(*.BGI) enum graphics_driver{DETECT,CGA,MCGA,EGA,EGA64,…… …,VGA,PC3270}; DETECT——自动检测所用显示适配器的类型,将相应 的驱动程序装入,并将其最高显示模式作为当前显示模 式
颜色控制函数

设调色板:setpalette() 设背景色:setbkcolor() 设绘图色:setcolor()
图形设计
§1 显示系统
显示器:CRT(阴极射线管)与控制电路组成 适配器(图形卡):CPU与显示器的接口,由 寄存器组、存储器和控制电路组成
适配器 主机
图形 显示 显示 控制器 存储器 控制器
CRT
将要显示的字符和图形以数字形式存储在卡上的视频RAM中, 再转换成视频模拟信号传送给相应的显示器
显示模式
文本模式:显示缓冲区中存放显示字符的代码 (ASCII码或汉字机内码),及字符的属性,显示屏 幕按字符分成若干行、列,如80列25行 图形模式:显示缓冲区中存放显示屏幕上每点(象 素)的颜色或灰度值,显示屏幕按象素分成若干行、 列,如640480
一般开机后的模式都是80 ×25的文本模式
§2 Turbo C绘图
图形程序的独立运行问题
为使图形方式的C源程序能够脱离TC环境下运行,应对 EGAVGA显示器的驱动程序.BGI转换成.OBJ文件: C:\TC\BGIOBJ EGAVGA

用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语言五子棋课程设计

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语言做的键盘操作的五子棋

为了帮助初学者理解,注释非常详细,希望对初学者有所启发!#include <stdlib.h>#include <stdio.h>#include <conio.h>#include <string.h>#define MAXIMUS 15 //定义棋盘大小int p[MAXIMUS][MAXIMUS];//存储对局信息char buff[MAXIMUS*2+1][MAXIMUS*4+3];//输出缓冲器int Cx,Cy;//当前光标位置int Now;//当前走子的玩家,1代表黑,2代表白int wl,wp;//当前写入缓冲器的列数和行数位置char* showText;//在棋盘中央显示的文字信息int count;//回合数char* Copy(char* strDest,const char* strSrc)//修改过的字符串复制函数,会忽略末端的\0{char* strDestCopy = strDest;while (*strSrc!='\0'){*strDest++=*strSrc++;}return strDestCopy;}void Initialize()//初始化一个对局函数{int i,j;//循环变量showText="";//重置显示信息count=0;//回合数归零for(i=0;i<MAXIMUS;i++)//重置对局数据{for(j=0;j<MAXIMUS;j++){p[i][j]=0;}}Cx=Cy=MAXIMUS/2;//重置光标到中央Now=1;//重置当前为黑方}char* getStyle(int i,int j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘{if(p[i][j]==1)//1为黑子return "●";else if(p[i][j]==2)//2为白子return "○";else if(i==0&&j==0)//以下为边缘棋盘样式return "┏";else if(i==MAXIMUS-1&&j==0)return "┓";else if(i==MAXIMUS-1&&j==MAXIMUS-1)return "┛";else if(i==0&&j==MAXIMUS-1)return "┗";else if(i==0)return "┠";else if(i==MAXIMUS-1)return "┨";else if(j==0)return "┯";else if(j==MAXIMUS-1)return "┷";return "┼";//中间的空位}char* getCurse(int i,int j){//获得指定坐标交点位置左上格的样式,通过制表符来模拟光标的显示if(i==Cx){if(j==Cy)return "┏";else if (j==Cy+1)return "┗";}else if(i==Cx+1){if(j==Cy)return "┓";else if (j==Cy+1)return "┛";}return " ";//如果不在光标附近则为空}void write(char* c)//向缓冲器写入字符串{Copy(buff[wl]+wp,c);wp+=strlen(c);}void ln()//缓冲器写入位置提行{wl+=1;wp=0;}void Display()//将缓冲器内容输出到屏幕{int i,l=strlen(showText);//循环变量,中间文字信息的长度int Offset=MAXIMUS*2+2-l/2;//算出中间文字信息居中显示所在的横坐标位置if(Offset%2==1)//如果位置为奇数,则移动到偶数,避免混乱{Offset--;}Copy(buff[MAXIMUS]+Offset,showText);//讲中间文字信息复制到缓冲器if(l%2==1)//如果中间文字长度为半角奇数,则补上空格,避免混乱{*(buff[MAXIMUS]+Offset+l)=0x20;}system("cls");//清理屏幕,准备写入for(i=0;i<MAXIMUS*2+1;i++){//循环写入每一行printf("%s",buff[i]);if(i<MAXIMUS*2)//写入完每一行需要换行printf("\n");}}void Print()//将整个棋盘算出并储存到缓冲器,然后调用Display函数显示出来{int i,j;//循环变量wl=0;wp=0;for(j=0;j<=MAXIMUS;j++)//写入出交点左上角的字符,因为需要打印棋盘右下角,所以很以横纵各多一次循环{for(i=0;i<=MAXIMUS;i++){write(getCurse(i,j));//写入左上角字符if(j==0||j==MAXIMUS)//如果是棋上下盘边缘则没有连接的竖线,用空格填充位置{if(i!=MAXIMUS)write(" ");}else//如果在棋盘中间则用竖线承接上下{if(i==0||i==MAXIMUS-1)//左右边缘的竖线更粗write("┃");else if(i!=MAXIMUS)//中间的竖线write("│");}}if(j==MAXIMUS)//如果是最后一次循环,则只需要处理边侧字符,交点要少一排{break;}ln();//提行开始打印交点内容write(" ");//用空位补齐位置for(i=0;i<MAXIMUS;i++)//按横坐标循环正常的次数{write(getStyle(i,j));//写入交点字符if(i!=MAXIMUS-1)//如果不在最右侧则补充一个横线承接左右{if(j==0||j==MAXIMUS-1){write("━");//上下边缘的横线更粗}else{write("—");//中间的横线}}}ln();//写完一行后提行}Display();//将缓冲器内容输出到屏幕}int Put(){//在当前光标位置走子,如果非空,则返回0表示失败if(p[Cx][Cy]==0){p[Cx][Cy]=Now;//改变该位置数据return 1;//返回1表示成功}else{return 0;}}int Check()//胜负检查,即判断当前走子位置有没有造成五连珠的情况{int w=1,x=1,y=1,z=1,i;//累计横竖正斜反邪四个方向的连续相同棋子数目for(i=1;i<5;i++)if(Cy+i<MAXIMUS&&p[Cx][Cy+i]==Now)w++;else break;//向下检查for(i=1;i<5;i++)if(Cy-i>0&&p[Cx][Cy-i]==Now)w++;else break;//向上检查if(w>=5)return Now;//若果达到5个则判断当前走子玩家为赢家for(i=1;i<5;i++)if(Cx+i<MAXIMUS&&p[Cx+i][Cy]==Now)x++;else break;//向右检查for(i=1;i<5;i++)if(Cx-i>0&&p[Cx-i][Cy]==Now)x++;else break;//向左检查if(x>=5)return Now;//若果达到5个则判断当前走子玩家为赢家for(i=1;i<5;i++)if(Cx+i<MAXIMUS&&Cy+i<MAXIMUS&&p[Cx+i][Cy+i]==Now)y++; else break;//向右下检查for(i=1;i<5;i++)if(Cx-i>0&&Cy-i>0&&p[Cx-i][Cy-i]==Now)y++;else break; //向左上检查if(y>=5)return Now;//若果达到5个则判断当前走子玩家为赢家for(i=1;i<5;i++)if(Cx+i<MAXIMUS&&Cy-i>0&&p[Cx+i][Cy-i]==Now)z++;else break;//向右上检查for(i=1;i<5;i++)if(Cx-i>0&&Cy+i<MAXIMUS&&p[Cx-i][Cy+i]==Now)z++;else break;//向左下检查if(z>=5)return Now;//若果达到5个则判断当前走子玩家为赢家return 0;//若没有检查到五连珠,则返回0表示还没有玩家达成胜利}int RunGame()//进行整个对局,返回赢家信息(虽然有用上){int input;//输入变量int victor;//赢家信息Initialize();//初始化对局while(1){//开始无限回合的死循环,直到出现胜利跳出Print();//打印棋盘input=getch();//等待键盘按下一个字符if(input==27)//如果是ESC则退出程序{exit(0);}else if(input==0x20)//如果是空格则开始走子{if(Put())//如果走子成功则判断胜负{victor=Check();Now=3-Now;//轮换当前走子玩家count++;if(victor==1)//如果黑方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText="黑方获得了胜利!";Print();if(getch()==0xE0){getch();}return Now;}else if(victor==2)//如果白方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText="白方获得了胜利!";Display();if(getch()==0xE0)getch();}return Now;}else if(count==MAXIMUS*MAXIMUS)//如果回合数达到了棋盘总量,即棋盘充满,即为平局{showText="平局!";Display();if(getch()==0xE0){getch();}return 0;}}}else if(input==0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE 0表示按下的是控制键{input=getch();//获得第二次输入信息switch(input)//判断方向键方向并移动光标位置{case 0x4B://Cx--;break;case 0x48:Cy--;break;case 0x4D:Cx++;break;case 0x50:Cy++;break;}if(Cx<0)Cx=MAXIMUS-1;//如果光标位置越界则移动到对侧if(Cy<0)Cy=MAXIMUS-1;if(Cx>MAXIMUS-1)Cx=0;if(Cy>MAXIMUS-1)Cy=0;}}}int main()//主函数system("title 简易五子棋——Etsnarl制作");//设置标题system("mode con cols=63 lines=32");//设置窗口大小system("color E0");//设置颜色while(1){//循环执行游戏RunGame();}}。

用C语言编写五子棋

用C语言编写五子棋

#include<stdio.h>#define N 10char a[N][N];int i,j;void print(){for(i=0;i<(2*N+2);i++){for(j=0;j<(2*N+2);j++){if(i%2==0){if(j%2==0)printf(" ");else if(j!=(2*N+1)) printf("---");}else if(i==(2*N+1)){if(j%2==0) printf(" ");else if(j!=(2*N+1)) printf(" %d ",j/2);}else{if(j%2==0) printf("|");else if(j==(2*N+1)){if(i%2==0) printf(" ");else if(i!=(2*N+1)) printf(" %d ",i/2);}else printf(" %c ",a[i/2][j/2]);}}printf("\n");}}int win(){int k=0;for(i=0;i<N-4;i++)for(j=0;j<N-4;j++){if(a[i][j]=='W'&&a[i+1][j+1]=='W'&&a[i+2][j+2]=='W'&&a[i+3][j+3]=='W '&&a[i+4][j+4]=='W') k=1;else if (a[i][j]=='B'&&a[i+1][j+1]=='B'&&a[i+2][j+2]=='B'&&a[i+3][j+3]=='B'&& a[i+4][j+4]=='B') k=2;}for(i=0;i<N;i++)for(j=0;j<N-4;j++){if(a[i][j]=='W'&&a[i][j+1]=='W'&&a[i][j+2]=='W'&&a[i][j+3]=='W'&&a[i ][j+4]=='W') k=1;else if (a[i][j]=='B'&&a[i][j+1]=='B'&&a[i][j+2]=='B'&&a[i][j+3]=='B'&&a[i][j +4]=='B') k=2;}for(i=0;i<N-4;i++)for(j=0;j<N;j++){if(a[i][j]=='W'&&a[i+1][j]=='W'&&a[i+2][j]=='W'&&a[i+3][j]=='W'&&a[i +4][j]=='W') k=1;else if (a[i][j]=='B'&&a[i+1][j]=='B'&&a[i+2][j]=='B'&&a[i+3][j]=='B'&&a[i+4] [j]=='B') k=2;}return k;}void main(){int k=1;for(i=0;i<N;i++)for(j=0;j<N;j++) a[i][j]=000; //初始化棋盘print();do{if(k%2!=0){printf("请输入黑子位置:");scanf("%d %d",&i,&j);a[i][j]='B';}else{printf("请输入白子位置:");scanf("%d %d",&i,&j);a[i][j]='W';}k++;system("CLS");print();}while(!win());if(win()==1)printf("白棋胜\n");else printf("黑棋胜\n");}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

伊犁师范学院高级语言课程设计报告学院:电子与信息工程学院学生姓名:学号:专业届别:指导教师:完成时间:五子棋游戏1 设计目的及要求目的:(1)学习综合运用C语言课程和其他相关课程的知识,提高分析解决实际问题的能力,进一步巩固、加深和拓宽所学的知识,进一步加强结构化程序设计的思想,加深对高级语言基本语言要素和控制结构的理解。

(2)全面系统的学习面向对象程序设计的基本概念、基本语法和编程方法,与同学合作设计整套程序,并且独立完成一定工作量的程序设计.(3)掌握程序设计中的常用算法和C语言的程序技巧,培养良好的程序设计风格。

(4)运用所学的知识了解五子棋游戏的制作结构,将所学的知识运用于实践中。

要求:(1)独自设计、编制、调试出一款五子棋游戏。

(2)要求该游戏具有趣味性和可操作性,能够正常运行。

其中用到C语言中的各种语句、结构。

(如其中需要注意全局变量和局部变量的申明,数组的使用等)(3)五子棋游戏要求能够进入欢迎界面,当有五颗相同颜色的棋子连续连成五颗,就胜出。

任务:五子棋是一种传统的棋盘游戏,是我国古代传统的黑白棋种之一发展于日本,流行于欧洲任务内容就是设计一款老少皆宜、能够锻炼个人策略谋划能力棋。

有助于孩子开发智力,或者闲暇时打发时间。

原则:(1)系统性。

系统作为统一整体而存在,因此,在设计中,要从系统的角度考虑。

系统的代码要统一,传递语言要尽可能一致。

(2)可靠性。

可靠性是指系统抵御外界干扰的能力及外界干扰时的恢复能力。

(3)规范性。

游戏的运用以吸引玩家的乐趣为前提,一方面,系统设计应尽量避免不必要的复杂化,另一方面模块应尽量简洁,以缩短处理流程。

2 开发工具C语言3 应用平台Windows xp4 功能设计游戏分析:五子棋是两人之间的竞技游戏,该五子棋程序基本上实现了五子棋游戏的功能,有双方下棋之前的欢迎界面及下棋过程中的棋盘界面,还有最终会判断出输赢。

同时,该游戏采用了二维坐标实现的,明了易懂,方便玩家在游戏过程中的基本操作,使游戏更加简便。

细节方面,该程序有实时储存功能,随时记录。

另外考虑到游戏挺简单的,就没有设置悔棋的相关模块。

游戏介绍:该游戏是简单的双人五子棋,所实现的功能是能够显示欢迎界面,选择是否进行游戏,进入游戏界面以后,玩家开始下棋。

玩家输入坐标则会在棋盘上显示棋子,(若是输入的坐标超出棋盘或者该位子已经有子则会提示玩家重新输入坐标位子),若输入则退出游戏。

4.1 算法设计程序说明:五子棋是一款经典的益智类游戏,通常是黑棋先行;五子棋专用盘为15*15方格,五个子的连接方向为横、竖、斜,当任一方的五个子在各方向上连成一线时,则该方获胜。

程序要求:按照程序说明编写五子棋程序,要求可以进行双人之间的游戏。

程序输出:输出棋盘、棋子,并能够进行相应的操作。

4.2流程图模块图1:程序整体设计思路流程图图2:绘图过程的算法流程否5 程序调试及运行5.1 程序运行结果图1 初始化界面图2 运行游戏的界面5.2 程序代码#include “stdio.h”#include”graphics.h”#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011b#define ENTER 0x1c0dint x=-1,y=0;int max_x,max_y;int board_flag[15][15];void draw_board();void show_init(int pat);void hide();void show(int pat);void move();int is_win(int pat);void main(){int driver=DETECT,mode;int i,j;for(i=0;i<15;i++)for(j=0;j<15;j++){board_flag[i][j]=0;}initgraph(&driver,&mode,””);draw_board();show_init(0);move();getch();closegraph();}void draw_board(){int i;max_x=getmaxx();max_y=getmaxy();setfillstyle(1,BLUE);bar(0,0,max_x,max_y);for(i=-7;i<=8;i++){line(max_x/2-7*26,max_y/2+i*26,max_x/2+8*26,max_y/2+i*26); line(max_x/2+i*26,max_y/2-7*26,max_x/2+i*26,max_y/2+8*26); }}void show_init(int pat){setcolor(BLUE);if(pat==0){setfillstyle(1,BLACK);}else{setfillstyle(1,WHITE);}x=-1;y=0;fillellipse(max_x/2-8*26+13,max_y/2-7*26+23,12,12);void hide(){int m;m=board_flag[x][y];switch(m){case 0:setfillstyle(1,BLUE);break;case 1:setfillstyle(1,BLACK);break;case 2:Setfillstyle(1,WHITE);break;default:break;}fillellipse(max_x/2+(x-7)*26+13,max_y/2+(y-7)*26+13,12,12); }void show(int pat){switch(pat){case 0:setfillstyle(1,BLACK);break;case 1:setfillstyle(1,WHITE);break;default:break;}fillellipse(max_x/2+(x-7)*26+13,max_y/2+(y-7)*26+13,12,12); }void move(){int key;int pat=0;for(;;)key=bioskey(0);switch(key){ case LEFT:if(x==-1){break;}hide();x--;x=x<0?0:x;show(pat);break;case RIGHT:if(x==-1){setfillstyle(1,BLUE);fillellipse(max_x/2+(x-7)*26+13,max_y/2+(y-7)*26+13,12,12); }else{hide();}x++;x=x>14?14:x;show(pat);break;case DOWN:if(x==-1){break;}hide();y++;y=y>14?14:y;show(pat);break;case UP:if(x==-1){break;}hide();y--;y=y<0?0:y;show(pat);break;case ENTER:if(board_flag[x][y]!=0){break;}show(pat);board_flag[x][y]=pat+1;if(is_win(pat)){settextstyle(0,0,3);setcolor(RED);if(pat==0){outtextxy(max_x/2-80,max_y/2,”Black Win!”); }else{outtextxy(max_x/2-80,max_y/2,”White Win!”); }setcolor(BLUE);getch();exit(1);}if(pat==0){pat=1;}else{pat=0;}show_init(pat);break;case ESC:exit(1);break;default:break;}}}int is_win(int pat){int i,j;int cnt=0;cnt=0;i=x;while(i<15){if(board_flag[i][y]==pat+1) {cnt++;}else{break;}i++;}i=x-1;while(i>=0){if(board_flag[i][y]==pat+1) {cnt++;}else{break;}i--;}if(cnt>=5){return 1;}cnt=0;i=x;j=y;while(i<15){{break;}if(board_flag[i][j]==pat+1) {cnt++;}else{break;}i++;j--;}i=x-1;j=y+1;while(i>=0){if(j>14){break;}if(board_flag[i][j]==pat+1) {cnt++;}else{break;}i--;j++;}if(cnt>=5){return 1;}cnt=0;j=y;while(j>=0){if(board_flag[x][j]==pat+1) {}else{break;}j--;}j=y+1;while(j<15){if(board_flag[x][j]==pat+1) {cnt++;}else{break;}j++;}if(cnt>=5){return 1;}cnt=0;i=x;j=y;while(i>=0){if(j<0){break;}if(board_flag[i][j]==pat+1) {cnt++;}else{break;}i--;j--;}i=x+1;j=y+1;while(i<15){if(j>14){break;}if(board_flag[i][j]==pat+1){cnt++;}else{break;}i++;j++;}if(cnt>=5){return 1;}return 0;}6 结束语在本次课程设计中,不仅让我认识到自己学习上的不足,也通过请教其他同学和老师,让我对程序进行了修改并运行,还让自己复习了编程的知识,对遇见的众多问题,通过思考,耐心动手解决问题。

在设计五子棋游戏的过程中也让我明白合作的力量,设计在课程设计期间我也学到了课本中学不到的知识,明白了只有通过自己不断地动手实践才能找出不足、找出问题和提高自己的能力,发现课程设计是我学习和掌握专业知识的好方法,它可以让自己变得活跃,也会看明白程序问题,使自己受益匪浅。

才能让自己的能力不断提高。

最后,感谢孙晓杰老师的悉心指导,教给我的程序设计的思想和知识。

相关文档
最新文档