基于C语言编写的五子棋游戏
五子棋游戏C语言
![五子棋游戏C语言](https://img.taocdn.com/s3/m/ed2a1eec7cd184254a35351a.png)
五子棋游戏程序设计(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语言实现五子棋小游戏源代码](https://img.taocdn.com/s3/m/e93de33a43323968011c92d2.png)
break;
case ESC:
break;
case SPACE:
if(chessx>=1&&chessx<=19&&chessy>=1&&chessy<=19)
{
if(chess[chessx][chessy]==0)
}
if(flag==2)
{
cleardevice();
setfont(36, 0, "宋体", 900, 900, 0, false, false, false);
outtextxy(80,200,"RED win!");
getch();
closegraph();
exit(0);
}
}
if(flag==1)
flag=2;
else
flag=1;
break;
}
void draw_pixel(int x,int y,int color)
{
x=(x+2)*20;
y=(y+2)*20;
{
putpixel(x+8,y,color);
putpixel(x,y-8,color);
putpixel(x+8,y+8,color);
{
chess[chessx][chessy]=flag;
if(result(chessx,chessy)==1)
{
if(flag==1)
{
cleardevice();
n1=0;
n2=0;
C#课件:基于C#的五子棋游戏的设计与实现
![C#课件:基于C#的五子棋游戏的设计与实现](https://img.taocdn.com/s3/m/19805ef6b4daa58da1114a79.png)
系统功能模块图
游戏界面
初始化操作界面
登记玩家姓名
换方界面
悔棋界面
电脑攻守测试
电脑获胜界面
玩家获胜界面
历史排行
总结
系统已经通过基本的测试。基本上完成了本次设计的预期 目的。通过编写这个程序,本人体会最为深刻的一点是系 统架构和设计模式的重要性。本人在此程序中加入了音效、 还设置了工具栏,方便玩家使用。不过由于本人能力有限。 此程序也存在一些不足:
系统开发环境
操作系统: Windows XP或Windows 7 开发工具: Visual studio 2010
系统功能模块图
系统功能模块设计
1)界面模块包括两个子模块——绘制棋盘模块和窗体设计模 块。绘制棋盘模块主要介绍棋盘的绘制,窗体设计模块的 功能主要是设计了菜单快捷键,方便玩家进行游戏,增加 游戏的可玩性和趣味性。
基于C#的五子棋游戏的设计与实现
学 生: 指导老师:
课题背景
五子棋相传起源于四千多年前的尧帝时期,比围棋的历史还要悠久,可能早在 “尧造围棋”之前,民间就已有五子棋游戏。五子棋,日文亦有“连五子、五 子连、串珠、五目、五目碰、五格、五石、五法、五联、京棋”等多种称谓, 英文则称之为“FIR (Five In A Row的缩写)、Gomoku(日语“五目”的罗马拼 音)、Gobang、connect 5、mo-rphion”。 五子棋专用盘为 15*15 。棋盘正中一点称为“天元”。棋盘两端的横线称端线。 最边的纵线称边线。从两条端线和两条边线向正中发展而纵横交叉在第四条线 形成的四个点称为“星”。棋盘上可见的横纵直线叫做阳线。棋盘上无实线连 接的隐形斜线叫做阴线。 黑先走对局的第一个子俗称“黑先白后”,黑白双方依次落子。棋盘上形黑白双方先在棋 盘上形成五连的一方为胜。对局双方均认为不可能形成五连为和棋。 由于禁手规则太复杂,这里笔者设计的是无禁手的五子棋人机对战游戏。
用C语言编写的五子棋游戏
![用C语言编写的五子棋游戏](https://img.taocdn.com/s3/m/421c878c6529647d27285218.png)
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语言实现的五子棋算法](https://img.taocdn.com/s3/m/4244881a227916888486d7a4.png)
五子棋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); }。
在linux下ubuntu的五子棋游戏c语言代码
![在linux下ubuntu的五子棋游戏c语言代码](https://img.taocdn.com/s3/m/384376d681eb6294dd88d0d233d4b14e85243e07.png)
#include <stdio.h>#include <stdlib.h>//#include <linu*/fb.h>#include <sys/types.h>#include <sys/ioctl.h>#include <ftl.h>#include <sys/mman.h>#define FB_W 1024#define FB_H 768#define CHESSBOARD1 10#define CHESSBOARD2 10#define *_OFFSET 400#define Y_OFFSET 200#define *_CAPASITY 10#define Y_CAPASITY 10#define SPACE 40#define RED 0*00ff0000#define BLUE 0*0000ff00#define WHITE 0*ffffffff#define BLACK 0*ff000000#define NOKEY 0#define RADIO 19#define C_H 17#define C_W 10#define T___ 0*00ffffff#define *___ 0*ff00ffff#define BORD 0*eeff0000#define u32_t unsigned intint array[CHESSBOARD1][CHESSBOARD2][1];int board[CHESSBOARD1][CHESSBOARD2];int play1[CHESSBOARD1][CHESSBOARD2][4];int [CHESSBOARD1][CHESSBOARD2][4];int cursor_store[C_H][C_W];struct fb_info{int w;int h;int bpp;int *mem;}fb;static u32_t cursor_pi*el[C_H][C_W]={{BORD,T___,T___,T___,T___,T___,T___,T___,T___,T___}, {BORD,BORD,T___,T___,T___,T___,T___,T___,T___,T___}, {BORD,*___,BORD,T___,T___,T___,T___,T___,T___,T___},{BORD,*___,*___,BORD,T___,T___,T___,T___,T___,T___},{BORD,*___,*___,*___,BORD,T___,T___,T___,T___,T___},{BORD,*___,*___,*___,*___,BORD,T___,T___,T___,T___},{BORD,*___,*___,*___,*___,*___,BORD,T___,T___,T___},{BORD,*___,*___,*___,*___,*___,*___,BORD,T___,T___},{BORD,*___,*___,*___,*___,*___,*___,*___,BORD,T___},{BORD,*___,*___,*___,*___,*___,*___,*___,*___,BORD},{BORD,*___,*___,*___,*___,*___,BORD,BORD,BORD,BORD},{BORD,*___,*___,BORD,*___,*___,BORD,T___,T___,T___},{BORD,*___,BORD,T___,BORD,*___,*___,BORD,T___,T___},{BORD,BORD,T___,T___,BORD,*___,*___,BORD,T___,T___},{T___,T___,T___,T___,T___,BORD,*___,*___,BORD,T___},{T___,T___,T___,T___,T___,BORD,*___,*___,BORD,T___},{T___,T___,T___,T___,T___,T___,BORD,BORD,T___,T___}};int fb_open(){int fd;int *mem;fd = open("/dev/fb0",O_RDWR);if(fd < 0){printf("open file error!\n");return 1;}struct fb_var_screeninfo fb_var;ioctl(fd,FBIOGET_VSCREENINFO,&fb_var);fb.w = fb_var.*res;fb.h = fb_var.yres;fb.bpp = fb_var.bits_per_pi*el;printf("w:%d h:%d bpp:%d\n",fb_var.*res,fb_var.yres,fb_var.bits_per_pi*el);fb.mem = mmap(NULL,FB_H*FB_W*fb_var.bits_per_pi*el/8,PROT_READ|PROT_WRITE,MAP_SHARE D,fd,0);//printf("%d\n", mem);return 0;}void fb_pi*el(int *,int y,int color){*(fb.mem + y*FB_W+*) = color;}void fb_drawline(int *0,int *1,int y,int color){int i;for(i = *0;i<*1;i++){fb_pi*el(i,y,color);}}void fb_drawretangle(int *0,int y0,int *1,int y1,int color) {int i,j;for(i = *0;i<*1;i++)for(j = y0;j<y1;j++){fb_pi*el(i,j,color);}}void fb_drawline2(int *,int y0,int y1,int color){int i;for(i = y0;i<y1;i++)fb_pi*el(*,i,color);}void fb_drawcycle(int *,int y,int r,int color){int i,j;for(i = -r;i<r;i++)for(j = -r;j<r;j++){if(i*i+j*j <= r*r){fb_pi*el(i+*,j+y,color);}}}int get_mouse_info(int fd,char *buf){int n;n = read(fd,buf,8);return n;}int save_cursor(int *,int y){int i,j;for(i = 0;i < C_H;i++){for(j = 0;j<C_W;j++)cursor_store[i][j] = *(fb.mem + j + *+(i +y)*FB_W);}return 0;}int restore_cursor(int *,int y){int i,j;for(i = 0;i<C_H;i++){for(j = 0;j < C_W;j++)*(fb.mem+j+*+(i+y)*FB_W)=cursor_store[i][j];}return 0;}void drawmouse(int *,int y){int i,j;for(i = 0;i<C_H;i++){for(j = 0;j<C_W;j++){if(cursor_pi*el[i][j] != T___)fb_pi*el(*+j,y+i,cursor_pi*el[i][j]);//printf("kdjfk%d\n",cursor_pi*el[i][j]);}}}int printboard(){int i;fb_drawretangle(*_OFFSET - SPACE,Y_OFFSET - SPACE,*_OFFSET + SPACE *Y_CAPASITY,Y_OFFSET + SPACE * *_CAPASITY,BLUE);for(i = 0;i < Y_CAPASITY;i++){fb_drawline(*_OFFSET,*_OFFSET+SPACE*(Y_CAPASITY - 1),i * SPACE + Y_OFFSET,RED);}for(i = 0;i < *_CAPASITY;i++){fb_drawline2(i*SPACE+*_OFFSET,Y_OFFSET,Y_OFFSET+SPACE*(*_CAPASITY-1),RED); }}void printqizi(int row,int col, int color){int *,y;* = *_OFFSET + SPACE*col;y = Y_OFFSET + SPACE*row;fb_drawcycle(*,y,RADIO,color);}struct node_t{int *;int y;int z;};//print the chessboardvoid print(int board[CHESSBOARD1][CHESSBOARD2]){int i, j;printf("\n");printf(" ");for (j = 0; j < CHESSBOARD2; j++)printf(" %d", j);printf("\n");printf(" -");for (j = 0; j < CHESSBOARD2; j++)printf(" -");printf("\n");for (i = 0; i < CHESSBOARD1; i++) {printf("%d| ", i);for (j = 0; j < CHESSBOARD2; j++) {printf("%d ", board[i][j]);}printf("\n");}printf("\n");}// 鍒濆鍖栨鐩"void init(int map[CHESSBOARD1][CHESSBOARD2]) {int i,j;for(i=0;i<CHESSBOARD1;i++)for(j=0;j<CHESSBOARD2;j++)map[i][j] = 0;}void init2(int map[CHESSBOARD1][CHESSBOARD2][4]){int i,j,k;for(i=0;i<CHESSBOARD1;i++)for(j=0;j<CHESSBOARD2;j++)for(k =0;k<4;k++)map[i][j][k] = 0;}void init3(int map[CHESSBOARD1][CHESSBOARD2][1]){int i , j;for(i = 0;i<CHESSBOARD1;i++)for(j = 0;j<CHESSBOARD2;j++)map[i][j][1] = 0;}int detection(int map[CHESSBOARD1][CHESSBOARD2][4],int *,int y,int player) {int i,j,k,n*,ny,counter = 0;int dir*[4] = {0,1,1,1};int diry[4] = {1,0,1,-1};if(board[*][y]){return 0;}for(i =0;i<4;i++){n* = *;ny = y;while(board[n*+=dir*[i]][ny+=diry[i]] == player)counter++;n* =*;ny =y;while(board[n*-=dir*[i]][ny-=diry[i]] == player)counter++;switch(counter){case 0:map[*][y][i] = 0;break;case 1:map[*][y][i] = 5;break;case 2:map[*][y][i] = 10;break;case 3:map[*][y][i] = 15;break;case 4:map[*][y][i] = 20;break;}//printf("%d,%d,%d ",*,y,map[*][y][i]);counter = 0;}printf("\n");return 0;}int get_ma*(int map[CHESSBOARD1][CHESSBOARD2][4]){int i,j,k;int ma* = 0;for(i = 0;i<CHESSBOARD2;i++)for(j = 0;j<CHESSBOARD1;j++)for(k = 0;k<4;k++){if(map[i][j][k] >ma*)ma* = map[i][j][k];}return ma*;}struct node_t get_ma*2(int map[CHESSBOARD1][CHESSBOARD2][4]) {int i,j,k;struct node_t p;int ma* = 0,cur;for(i = 0;i<CHESSBOARD2;i++){for(j = 0;j<CHESSBOARD1;j++){for(k = 0;k<4;k++){cur = map[i][j][k];if(cur >ma*){ma* = cur;p.* = i;p.y = j;p.z = k;}}}}return p;}struct node_t get_ma*3(int map[CHESSBOARD1][CHESSBOARD2][4]) {int i,j,k;struct node_t p;int ma* = 0,cur;for(i = 0;i<CHESSBOARD2;i++){for(j = 0;j<CHESSBOARD1;j++){for(k = 0;k<4;k++){cur = map[i][j][k];if(cur >= ma*){ma* = cur;p.* = i;p.y = j;p.z = k;}}}}return p;}struct node_t get_ma*4(int map[CHESSBOARD1][CHESSBOARD2][1]) {int i,j;struct node_t p;int ma* = 0,cur;for(i = 0;i<CHESSBOARD2;i++){for(j = 0;j<CHESSBOARD1;j++){cur = map[i][j][1];if(cur >= ma*){ma* = cur;p.* = i;p.y = j;p.z = 1;}}}return p;}//鏌ョ湅鍧愭爣* y鏄惁鑳借蛋int if_done(int *,int y){if(*>=CHESSBOARD1||*<0)return 0;if(y>=CHESSBOARD2||y<0)return 0;return 1;}void puter(){int i,j,k;init2(play1);init2();for(i = 0;i<CHESSBOARD2;i++){for(j = 0;j<CHESSBOARD1;j++){detection(play1,i,j,1);detection(,i,j,2);}}}//鏌ョ湅妫嬬洏鐨勬瀛愭儏鍐垫湁鍑犱釜杩炰竴璧"int check(int *,int y,int board[CHESSBOARD1][CHESSBOARD2]){int i = 0,j = 0,ne*t*,ne*ty;int counter = 1;int player;int direction*[4] = {0,1,1,1};int directiony[4] = {1,0,1,-1};player = board[*][y];if(player == 0)return 0;for(i = 0;i<4;i++){ne*t* = *;ne*ty = y;counter = 1;for(j = 1;j<5;j++){ne*t* += direction*[i];ne*ty +=directiony[i];if(board[ne*t*][ne*ty] == player)counter++;}if(counter == 5){return 1;}}return 0;}void drawbg(int row,int col){int *,y;* = *_OFFSET + SPACE*col;y = Y_OFFSET + SPACE*row;fb_drawcycle(*,y,RADIO,BLUE);fb_drawline(*-RADIO,*+RADIO,y,RED);fb_drawline2(*,y-RADIO,y+RADIO,RED);}int if_gameover(int board[CHESSBOARD1][CHESSBOARD2]) {int i,j;for(i = 0;i<CHESSBOARD2;i++)for(j = 0;j<CHESSBOARD1;j++){if(check(i,j,board))return 1;}return 0;}void mouse_doing(){int fd,step = 1;int row,col,flag1=1;int ma*1,ma*2;char buf[8];struct node_t p;int m*,my,a,b,c,d,e;m* = FB_W/2-100;my = FB_H/2;int n;//drawmouse(m*,my);fd = open("/dev/input/mice",O_RDWR|O_NONBLOCK); if(fd < 0){printf("open mouse error!\n");}save_cursor(m*,my);drawmouse(m*,my);while(flag1){if(read(fd,buf,8) >0){e = (buf[0]&0*07);c = 2*buf[1];d = 2*buf[2];if(e == NOKEY){restore_cursor(m*,my);m* += c;my -= d;if(m* >= FB_W-17)m*=FB_W-17;if(m* < 0)m* = 0;if(my > FB_H-17)my = FB_H-17;if(my < 0)my = 0;save_cursor(m*,my);drawmouse(m*,my);}else if(e == 1){if(step%2==1){// printf("play1 done!\n");col =(int) (m*-*_OFFSET+SPACE/2)/SPACE;row = (int)(my-Y_OFFSET+SPACE/2)/SPACE;if(!(if_done(row,col))||board[row][col]){//printf("you can't done here !\n");col =(int) (m*-*_OFFSET)/SPACE;row = (int)(my-Y_OFFSET)/SPACE;continue;}board[row][col] = 1;restore_cursor(m*,my);printqizi(row,col,BLACK);//鐢婚粦鑹茬殑妫嬪瓙save_cursor(m*, my);array[row][col][1] = step;step ++;puter();//鏇存柊浼樺厛绾ц〃// printf("flag2 %d\n",flag2);//flag2 = 1;}else {// printf("play2 done!\n");ma*1 = get_ma*(play1);ma*2 = get_ma*();if(ma*1 >= ma*2){p = get_ma*2(play1);}else{p = get_ma*2();}row = p.*;col = p.y;if(!(if_done(row,col))||board[row][col]){// printf("you can't done here !\n");ma*1 = get_ma*(play1);ma*2 = get_ma*();if(ma*1 >= ma*2){p = get_ma*3(play1);}else{p = get_ma*3();}row = p.*;col = p.y;continue;}board[row][col] = 2;puter(board);restore_cursor(m*,my);printqizi(row,col,WHITE); save_cursor(m*, my);array[row][col][1] = step;step++;//flag2 = 0;}if(if_gameover(board))flag1=0;}else if(e == 2){if(step != 1){p = get_ma*4(array);row = p.*;col = p.y;board[row][col] = 0;array[row][col][1] = 0;puter(board);drawbg(row,col);//restore_cursor(m*,my);step--;}}}}if(board[row][col] ==1)printf("GAME OVER! play1 win!\n");elseprintf("GAME OVER! play2 win!\n");}void mouse_doing2(){int fd,step = 1;int row,col,flag1=1;int ma*1,ma*2;char buf[8];struct node_t p;int m*,my,a,b,c,d,e;m* = FB_W/2-100;my = FB_H/2;int n;//drawmouse(m*,my);fd = open("/dev/input/mice",O_RDWR|O_NONBLOCK); if(fd < 0){printf("open mouse error!\n");}save_cursor(m*,my);drawmouse(m*,my);while(flag1){if(read(fd,buf,8) >0){e = (buf[0]&0*07);c = 2*buf[1];d = 2*buf[2];if(e == NOKEY){restore_cursor(m*,my);m* += c;my -= d;if(m* >= FB_W-17)m*=FB_W-17;if(m* < 0)m* = 0;if(my > FB_H-17)my = FB_H-17;if(my < 0)my = 0;save_cursor(m*,my);drawmouse(m*,my);}else if(e == 1){if(step%2==1){// printf("play1 done!\n");col =(int) (m*-*_OFFSET+SPACE/2)/SPACE;row = (int)(my-Y_OFFSET+SPACE/2)/SPACE;if(!(if_done(row,col))||board[row][col]){//printf("you can't done here !\n");col =(int) (m*-*_OFFSET)/SPACE;row = (int)(my-Y_OFFSET)/SPACE;continue;}board[row][col] = 1;restore_cursor(m*,my);printqizi(row,col,BLACK);//鐢婚粦鑹茬殑妫嬪瓙save_cursor(m*, my);array[row][col][1] = step;step ++;//puter();//鏇存柊浼樺厛绾ц〃// printf("flag2 %d\n",flag2);//flag2 = 1;}else {// printf("play2 done!\n");col =(int) (m*-*_OFFSET+SPACE/2)/SPACE;row = (int)(my-Y_OFFSET+SPACE/2)/SPACE;if(!(if_done(row,col))||board[row][col]){//printf("you can't done here !\n");col =(int) (m*-*_OFFSET)/SPACE;row = (int)(my-Y_OFFSET)/SPACE;continue;}board[row][col] = 2;//puter(board);restore_cursor(m*,my);printqizi(row,col,WHITE);save_cursor(m*, my);array[row][col][1] = step;step++;//flag2 = 0;}if(if_gameover(board))flag1=0;}else if(e == 2){if(step != 1){p = get_ma*4(array);row = p.*;col = p.y;board[row][col] = 0;array[row][col][1] = 0;puter(board);drawbg(row,col);//restore_cursor(m*,my);step--;}}}}if(board[row][col] ==1)printf("GAME OVER! play1 win!\n");elseprintf("GAME OVER! play2 win!\n"); }int main(int argc, const char *argv[]){fb_open();int row,col;int m;init(board);init2(play1);printf("qing *uan ze you *i mo shi:\n"); printf("1 dan ren you *i\n");printf("2 shuang ren you *i\n");scanf("%d",&m);if(m == 1){printboard();mouse_doing();}if(m == 2){printboard();mouse_doing2();}return 0;}。
五子棋代码C语言版
![五子棋代码C语言版](https://img.taocdn.com/s3/m/46e8887baf1ffc4ffe47ace4.png)
#include <stdlib.h>#include <stdio.h>#include <conio.h>#include <string.h>#include <malloc.h>struct rcd;//声明节点结构typedef struct rcd* Record;//节点指针别名typedef struct rcd record;//节点别名#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;//回合数int Putable;//指示当前是否可以走棋int Exiting;//1为当场上无子并按ESC时询问是否退出程序的状态,2为非此状态int ExiRep;//1为当回放到最后一回合并按向后时询问是否退出回放的状态,2为非此状态Record RecBeg,RecNow;//记录的开始节点和当前节点struct rcd//记录节点结构,双链表形式{int X;//此记录走棋的X坐标int Y;//此记录走棋的Y坐标Record Next;//前一个记录Record Back;//后一个记录};Record newRecord()//记录节点构造函数{Record r=(Record)malloc(sizeof(record));//申请一个节点对象r->Next=NULL;//给予前后节点初值NULLr->Back=NULL;return r;}void Exit()//检查退出程序{int input;if(Exiting)//如果是第二次按下ESC{exit(0);}else//如果是第一次按下ESC则询问是否退出程序{showText="是否退出?再次按下ESC退出,其他键返回";Exiting=1;//指示已经按下过ESC}}void ExitRep()//检查退出回放{int input;if(ExiRep)//如果是第二次后移{ExiRep=3;}else//如果是第一次后移则询问是否退出回放{showText="是否退出?再次后移退出回放,其他键返回";ExiRep=1;//指示已经按下过后移}}void AddRecord()//添加记录{RecNow->X=Cx;//记录坐标RecNow->Y=Cy;RecNow->Next=newRecord();//创建下一个记录节点RecNow->Next->Back=RecNow;//完成双链表RecNow=RecNow->Next;//当前记录推至下一个记录节点}int DelRecord()//删除当前记录节点,1为删除成功,0为删除失败{Record b;//上一个节点if(RecNow->Back!=NULL)//越界检查{b=RecNow->Back;//缓存上一个节点free(RecNow);//释放当前节点RecNow=b;//当前记录回至上一个记录节点return 1;}else{return 0;//没有节点可删除时}}void CleanRecord()//清理所有记录{Record n;//下一个节点while(RecBeg->Next!=NULL)//删除所有记录,直到越界前为止{n=RecBeg->Next;//记下下一个节点free(RecBeg);//释放当前节点RecBeg=n;//当前记录推至下一个记录节点}}char* Copy(char* strDest,const char* strSrc)//修改过的字符串复制函数,会忽略末端的\0 {char* strDestCopy = strDest;while (*strSrc!='\0'){*strDest++=*strSrc++;}return strDestCopy;}void Initialize()//初始化一个对局函数{int i,j;//循环变量system("title 对局中(按方向键控制光标,空格走子),Esc撤销");showText="";//重置显示信息count=0;//回合数归零RecNow=RecBeg=newRecord();Exiting=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(Putable)//可走棋时光标为粗线{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 "┛";}}else//不可走棋时光标为虚线{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(Putable){p[Cx][Cy]=Now;//改变该位置数据AddRecord();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表示还没有玩家达成胜利}void ReplayMode(){int i,j;//循环变量system("title 回放中(按左键后退,右键或空格前进),Esc退出");showText="";//重置显示信息count=0;//回合数归零Putable=0;//不可走棋状态RecBeg->Back=newRecord();RecBeg->Back->Next=RecBeg;RecBeg=RecBeg->Back;for(i=0;i<MAXIMUS;i++)//重置对局数据{for(j=0;j<MAXIMUS;j++){p[i][j]=0;}}Now=1;//重置当前为黑方}void RepForward()//回放模式前进{if(RecNow->Next->Next!=NULL)//越界检查{RecNow=RecNow->Next;//当前节点推至下一个记录节点p[RecNow->X][RecNow->Y]=Now;//按照记录还原一个回合Cx=RecNow->X;//设置光标位置Cy=RecNow->Y;Now=3-Now;//转换当前的黑白方}else//若已达到最后则询问退出{ExitRep();}}void RepBackward()//回放模式后退{if(RecNow->Back!=NULL)//越界检查{p[RecNow->X][RecNow->Y]=0;//按照记录撤销一个回合if(RecNow->Back->Back==NULL)//在整个棋盘没有棋子时隐藏光标{Cx=-2;Cy=-2;}else if(RecNow->Back==NULL)//在只有一个棋子时移动光标到这个棋子的位置{Cx=RecNow->X;Cy=RecNow->Y;}else//正常情况下移动光标到上一回合的位置{Cx=RecNow->Back->X;Cy=RecNow->Back->Y;}RecNow=RecNow->Back;//当前节点后退至上一个记录节点Now=3-Now;//转换当前的黑白方}}void ShowReplay(){int input;//输入变量ReplayMode();//初始化回放模式RecNow=RecBeg;//当前观察从头开始RepForward();//显示第一次走棋while(1)//开始无限回合的死循环,直到Esc退出{if(ExiRep==3){ExiRep=0;break;}Print();//打印棋盘input=getch();//等待键盘按下一个字符if(input==27)//如果是ESC则退出回放{return;}else if(input==0x20)//如果是空格则前进{RepForward();continue;}else if(input==0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键{input=getch();//获得第二次输入信息switch(input)//判断方向键方向并移动光标位置{case 0x4B:RepBackward();//向左后退break;case 0x4D:RepForward();//向右前进continue;}}ExiRep=0;//未再次按后移则不准备退出showText="";}}void Regret()//悔棋撤销,如果棋盘上没有子即为退出{if(DelRecord()){//尝试删除当前节点,如果有节点可以删除则p[RecNow->X][RecNow->Y]=0;//撤除当前回合if(RecNow->Back==NULL)//如果删除的是第一颗子则将光标移动到第一颗子原来的位置上{Cx=RecNow->X;Cy=RecNow->Y;}else//否则将光标移动到上一颗子上{Cx=RecNow->Back->X;Cy=RecNow->Back->Y;}Now=3-Now;//反转当前黑白方}else{Exit();//如果没有棋子可以撤销,则询问退出}}int RunGame()//进行整个对局,返回赢家信息(虽然有用上){int input;//输入变量int victor;//赢家信息Initialize();//初始化对局while(1){//开始无限回合的死循环,直到出现胜利跳出Putable=p[Cx][Cy]==0;Print();//打印棋盘input=getch();//等待键盘按下一个字符if(input==27)//如果是ESC则悔棋或退出{Regret();Print();continue;}else if(input==0x20)//如果是空格则开始走子{if(Put())//如果走子成功则判断胜负{victor=Check();Now=3-Now;//轮换当前走子玩家count++;if(victor==1)//如果黑方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText="黑方胜利!按R查看回放,按其他键重新开局";Print();input=getch();if(input==0xE0){getch();}else if(input=='R'||input=='r'){ShowReplay();}return Now;}else if(victor==2)//如果白方达到胜利,显示提示文字并等待一次按键,返回胜利信息{showText="白方胜利!按R查看回放,按其他键重新开局";Print();input=getch();if(input==0xE0)getch();}else if(input=='R'||input=='r'){ShowReplay();}return Now;}else if(count==MAXIMUS*MAXIMUS)//如果回合数达到了棋盘总量,即棋盘充满,即为平局{showText="平局!按R查看回放,按其他键重新开局";Print();input=getch();if(input==0xE0){getch();}else if(input=='R'||input=='r'){ShowReplay();}CleanRecord();return 0;}}}else if(input==0xE0)//如果按下的是方向键,会填充两次输入,第一次为0xE0表示按下的是控制键{input=getch();//获得第二次输入信息switch(input)//判断方向键方向并移动光标位置{case 0x4B://Cx--;break;case 0x48:Cy--;break;case 0x4D:Cx++;break;case 0x50:Cy++;}if(Cx<0)Cx=MAXIMUS-1;//如果光标位置越界则移动到对侧if(Cy<0)Cy=MAXIMUS-1;if(Cx>MAXIMUS-1)Cx=0;if(Cy>MAXIMUS-1)Cy=0;}Exiting=0;//未再次按下ESC则不准备退出showText="";}}int main()//主函数{system("mode con cols=63 lines=32");//设置窗口大小system("color E0");//设置颜色while(1){//循环执行游戏RunGame();}}。
C语言五子棋代码
![C语言五子棋代码](https://img.taocdn.com/s3/m/e69b0eddad51f01dc381f107.png)
//五子棋小游戏纯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语言程序](https://img.taocdn.com/s3/m/5091a3cc960590c69fc37628.png)
# include<stdio.h># include<string.h># include<stdlib.h># define SPA 0# define MAN 1# define COM 2 /* 空位置设为0 ,玩家下的位置设为1 ,电脑下的位置设为2 */int qipan[15][15]; /* 15*15的棋盘*/int a,b,c,d,x; /* a b为玩家下子坐标,c d为电脑下子坐标x为剩余空位置*/void start(); /* 程序的主要控制函数*/void draw(); /* 画棋盘*/int win(int p,int q); /* 判断胜利p q为判断点坐标*/void AI(int *p,int *q); /* 电脑下子p q返回下子坐标*/int value(int p,int q); /* 计算空点p q的价值*/int qixing(int n,int p,int q); /* 返回空点p q在n方向上的棋型n为1-8方向从右顺时针开始数*/void yiwei(int n,int *i,int *j); /* 在n方向上对坐标i j 移位n为1-8方向从右顺时针开始数*/void main(){char k;do{x=225;start();printf("还要再来一把吗?输入y或n:"); getchar(); scanf("%c",&k);while(k!='y'&&k!='n'){ printf("输入错误,请重新输入\n"); scanf("%c",&k); }system("cls");}while(k=='y');printf("谢谢使用!\n");}void start(){int i,j,a1,b1,c1,d1,choice; /* a1 b1储存玩家上手坐标c1 d1储存电脑上手坐标*/char ch;printf("\t╔═══════════════════════════════╗\n");printf("\t║║\n");printf("\t║ 欢迎使用五子棋对战程序祝您玩的愉快挑战无极限║\n");printf("\t║║\n");printf("\t║ ._______________________. ║\n");printf("\t║ | _____________________ | ║\n");printf("\t║ | I I | ║\n");printf("\t║ | I 五子棋I | ║\n");printf("\t║ | I I | ║\n");printf("\t║ | I made by 晓之蓬I | ║\n");printf("\t║ | I___________________I | ║\n");printf("\t║ !_______________________! ║\n");printf("\t║ ._[__________]_. ║\n");printf("\t║ .___|_______________|___. ║\n");printf("\t║ |::: ____ | ║\n");printf("\t║ | ~~~~ [CD-ROM] | ║\n");printf("\t║ !_____________________! ║\n");printf("\t║║\n");printf("\t║║\n");printf("\t║ 寒星溪月疏星首,花残二月并白莲。
用C开发一个迷你的五子棋游戏
![用C开发一个迷你的五子棋游戏](https://img.taocdn.com/s3/m/21a44e327ed5360cba1aa8114431b90d6c858918.png)
用C开发一个迷你的五子棋游戏迷你五子棋游戏是一种简化版的五子棋游戏,它具有简洁的界面和简单易懂的规则,非常适合初学者。
本文将介绍如何使用C语言开发一个迷你的五子棋游戏,以供大家学习参考。
一、游戏规则迷你五子棋游戏的规则非常简单,玩家分别执黑子和白子,轮流在棋盘上落子。
当任一玩家在横、竖、斜四个方向上连成五个自己的棋子时,即可获胜。
若棋盘填满且无一方连成五子,则为平局。
二、游戏实现为了实现迷你五子棋游戏,我们需要使用C语言和一些基本的图形库函数。
在开始编写代码之前,我们先引入必要的头文件,并定义一些常量和全局变量。
#include <stdio.h>#include <conio.h>#include <graphics.h>#define ROW 10 // 棋盘行数#define COL 10 // 棋盘列数#define SIZE 40 // 每个格子的大小#define WIDTH COL*SIZE // 棋盘的宽度#define HEIGHT ROW*SIZE // 棋盘的高度int chessboard[ROW][COL]; // 棋盘数组,用来保存棋盘中每个位置的棋子信息int currentPlayer; // 当前玩家,0代表黑子,1代表白子接下来,我们需要进行一些初始化操作,包括创建图形窗口、初始化棋盘和设置玩家初始状态。
void init() {int driver, mode;driver = VGA;mode = VGAHI;initgraph(&driver, &mode, "");setbkcolor(WHITE);cleardevice();currentPlayer = 0;memset(chessboard, 0, sizeof(chessboard));}在绘制棋盘的函数中,我们使用for循环和线段绘制函数line()来绘制出棋盘的网格。
五子棋C语言代码
![五子棋C语言代码](https://img.taocdn.com/s3/m/d1929dd7e45c3b3566ec8b9f.png)
#include "graphics.h" /*图形系统头文件*/#define LEFT 0x4b00 /*光标左键值*/#define RIGHT 0x4d00 /*光标右键值*/#define DOWN 0x5000 /*光标下键值*/#define UP 0x4800 /*光标上键值*/#define ESC 0x011b /* ESC键值*/#define ENTER 0x1c0d /* 回车键值*/int a[8][8]={0},key,score1,score2;/*具体分数以及按键与存放棋子的变量*/ char playone[3],playtwo[3];/*两个人的得分转换成字符串输出*/void playtoplay(void);/*人人对战函数*/void DrawQp(void);/*画棋盘函数*/void SetPlayColor(int x);/*设置棋子第一次的颜色*/void MoveColor(int x,int y);/*恢复原来棋盘状态*/int QpChange(int x,int y,int z);/*判断棋盘的变化*/void DoScore(void);/*处理分数*/void PrintScore(int n);/*输出成绩*/void playWin(void);/*输出胜利者信息*//******主函数*********/void main(void){int gd=DETECT,gr;initgraph(&gd,&gr,"c:\\tc"); /*初始化图形系统*/DrawQp();/*画棋盘*/playtoplay();/*人人对战*/getch();closegraph();/*关闭图形系统*/}void DrawQp()/*画棋盘*/{int i,j;score1=score2=0;/*棋手一开始得分都为0*/setbkcolor(BLUE);for(i=100;i<=420;i+=40){line(100,i,420,i);/*画水平线*/line(i,100,i,420); /*画垂直线*/}setcolor(0);/*取消圆周围的一圈东西*/setfillstyle(SOLID_FILL,15);/*白色实体填充模式*/fillellipse(500,200,15,15); /*在显示得分的位置画棋*/setfillstyle(SOLID_FILL,8); /*黑色实体填充模式*/fillellipse(500,300,15,15);a[3][3]=a[4][4]=1;/*初始两个黑棋*/a[3][4]=a[4][3]=2;/*初始两个白棋*/setfillstyle(SOLID_FILL,WHITE);fillellipse(120+3*40,120+3*40,15,15);fillellipse(120+4*40,120+4*40,15,15);setfillstyle(SOLID_FILL,8);fillellipse(120+3*40,120+4*40,15,15);fillellipse(120+4*40,120+3*40,15,15);score1=score2=2; /*有棋后改变分数*/DoScore();/*输出开始分数*/}void playtoplay()/*人人对战*/{int x,y,t=1,i,j,cc=0;while(1)/*换棋手走棋*/{x=120,y=80;/*每次棋子一开始出来的坐标,x为行坐标,y为列坐标*/ while(1) /*具体一个棋手走棋的过程*/{PrintScore(1);/*输出棋手1的成绩*/PrintScore(2);/*输出棋手2的成绩*/SetPlayColor(t);/*t变量是用来判断棋手所执棋子的颜色*/fillellipse(x,y,15,15);key=bioskey(0);/*接收按键*/if(key==ESC)/*跳出游戏*/break;elseif(key==ENTER)/*如果按键确定就可以跳出循环*/{if(y!=80&&a[(x-120)/40][(y-120)/40]!=1&&a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置没有棋子*/{if(t%2==1)/*如果是棋手1移动*/a[(x-120)/40][(y-120)/40]=1;else/*否则棋手2移动*/a[(x-120)/40][(y-120)/40]=2;if(!QpChange(x,y,t))/*落子后判断棋盘的变化*/{a[(x-120)/40][(y-120)/40]=0;/*恢复空格状态*/cc++;/*开始统计尝试次数*/if(cc>=64-score1-score2) /*如果尝试超过空格数则停步*/{MoveColor(x,y);fillellipse(x,y,15,15);break;}elsecontinue;/*如果按键无效*/}DoScore();/*分数的改变*/break;/*棋盘变化了,则轮对方走棋*/ }else/*已经有棋子就继续按键*/continue;}else /*四个方向按键的判断*/if(key==LEFT&&x>120)/*左方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x-=40;fillellipse(x,y,15,15);}elseif(key==RIGHT&&x<400&&y>80)/*右方向键*/ {MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x+=40;fillellipse(x,y,15,15);}elseif(key==UP&&y>120)/*上方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);y-=40;fillellipse(x,y,15,15);}elseif(key==DOWN&&y<400)/*下方向键*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);y+=40;fillellipse(x,y,15,15);}}if(key==ESC)/*结束游戏*/break;if((score1+score2)==64||score1==0||score2==0)/*格子已经占满或一方棋子为0判断胜负*/{playWin();/*输出最后结果*/break;}t=t%2+1; /*一方走后,改变棋子颜色即轮对方走*/cc=0; /*计数值恢复为0*/} /*endwhile*/}void SetPlayColor(int t)/*设置棋子颜色*/{if(t%2==1)setfillstyle(SOLID_FILL,15);/*白色*/elsesetfillstyle(SOLID_FILL,8);/*灰色*/}void MoveColor(int x,int y)/*走了一步后恢复原来格子的状态*/{if(y<100)/*如果是从起点出发就恢复蓝色*/setfillstyle(SOLID_FILL,BLUE);else/*其他情况如果是1就恢复白色棋子,2恢复黑色棋子,或恢复蓝色棋盘*/ switch(a[(x-120)/40][(y-120)/40]){case 1:setfillstyle(SOLID_FILL,15);break; /*白色*/case 2:setfillstyle(SOLID_FILL,8);break; /*黑色*/default:setfillstyle(SOLID_FILL,BLUE); /*蓝色*/}}int QpChange(int x,int y,int t)/*判断棋盘的变化*/{int i,j,k,kk,ii,jj,yes;yes=0;i=(x-120)/40; /*计算数组元素的行下标*/j=(y-120)/40; /*计算数组元素的列下标*/SetPlayColor(t);/*设置棋子变化的颜色*//*开始往8个方向判断变化*/if(j<6)/*往右边*/{for(k=j+1;k<8;k++)if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格结束*/ break;if(a[i][k]!=0&&k<8){for(kk=j+1;kk<k&&k<8;kk++)/*判断右边*/{a[i][kk]=a[i][j]; /*改变棋子颜色*/fillellipse(120+i*40,120+kk*40,15,15);}if(kk!=j+1) /*条件成立则有棋子改变过颜色*/yes=1;}}if(j>1)/*判断左边*/{for(k=j-1;k>=0;k--)if(a[i][k]==a[i][j]||!a[i][k])break;if(a[i][k]!=0&&k>=0){for(kk=j-1;kk>k&&k>=0;kk--){a[i][kk]=a[i][j];fillellipse(120+i*40,120+kk*40,15,15);}if(kk!=j-1)yes=1;}}if(i<6)/*判断下边*/{for(k=i+1;k<8;k++)if(a[k][j]==a[i][j]||!a[k][j])break;if(a[k][j]!=0&&k<8){for(kk=i+1;kk<k&&k<8;kk++){a[kk][j]=a[i][j];fillellipse(120+kk*40,120+j*40,15,15);}if(kk!=i+1)yes=1;}}if(i>1)/*判断上边*/{for(k=i-1;k>=0;k--)if(a[k][j]==a[i][j]||!a[k][j])break;if(a[k][j]!=0&&k>=0){for(kk=i-1;kk>k&&k>=0;kk--){a[kk][j]=a[i][j];fillellipse(120+kk*40,120+j*40,15,15); }if(kk!=i-1)yes=1;}}if(i>1&&j<6)/*右上*/{for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++) if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]&&k>=0&&kk<8){for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15); }if(ii!=i-1)yes=1;}}if(i<6&&j>1)/*左下*/{for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--) if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&k<8&&kk>=0){for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i+1)yes=1;}}if(i>1&&j>1)/*左上*/{for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&k>=0&&kk>=0){for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i-1)yes=1;}}if(i<6&&j<6)/* 右下*/{for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&kk<8&&k<8){for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i+1)yes=1;}}return yes;/*返回是否改变过棋子颜色的标记*/ }void DoScore()/*处理分数*/{int i,j;score1=score2=0;/*重新开始计分数*/for(i=0;i<8;i++)for(j=0;j<8;j++)if(a[i][j]==1)/*分别统计两个人的分数*/score1++;elseif(a[i][j]==2)score2++;}void PrintScore(int playnum)/*输出成绩*/{if(playnum==1)/*清除以前的成绩*/{setfillstyle(SOLID_FILL,BLUE);bar(550,100,640,400);}setcolor(RED);settextstyle(0,0,4);/*设置文本输出样式*/if(playnum==1)/*判断输出哪个棋手的分,在不同的位置输出*/ {sprintf(playone,"%d",score1);outtextxy(550,200,playone);}else{sprintf(playtwo,"%d",score2);outtextxy(550,300,playtwo);}setcolor(0);}void playWin()/*输出最后的胜利者结果*/{settextstyle(0,0,4);setcolor(12);if(score2>score1)/*开始判断最后的结果*/outtextxy(100,50,"black win!");elseif(score2<score1)outtextxy(100,50,"white win!");elseouttextxy(60,50,"you all win!");}。
C语言实现五子棋游戏
![C语言实现五子棋游戏](https://img.taocdn.com/s3/m/3c0d66205b8102d276a20029bd64783e09127d2f.png)
C语⾔实现五⼦棋游戏本⽂实例为⼤家分享了C语⾔实现五⼦棋的具体代码,供⼤家参考,具体内容如下#include <stdio.h>#include <bios.h>#include <ctype.h>#include <conio.h>#include <dos.h>#define CROSSRU 0xbf /*右上⾓点*/#define CROSSLU 0xda /*左上⾓点*/#define CROSSLD 0xc0 /*左下⾓点*/#define CROSSRD 0xd9 /*右下⾓点*/#define CROSSL 0xc3 /*左边*/#define CROSSR 0xb4 /*右边*/#define CROSSU 0xc2 /*上边*/#define CROSSD 0xc1 /*下边*/#define CROSS 0xc5 /*⼗字交叉点*//*定义棋盘左上⾓点在屏幕上的位置*/#define MAPXOFT 5#define MAPYOFT 2/*定义1号玩家的操作键键码*/#define PLAY1UP 0x1157/*上移--'W'*/#define PLAY1DOWN 0x1f53/*下移--'S'*/#define PLAY1LEFT 0x1e41/*左移--'A'*/#define PLAY1RIGHT 0x2044/*右移--'D'*/#define PLAY1DO 0x3920/*落⼦--空格键*//*定义2号玩家的操作键键码*/#define PLAY2UP 0x4800/*上移--⽅向键up*/#define PLAY2DOWN 0x5000/*下移--⽅向键down*/#define PLAY2LEFT 0x4b00/*左移--⽅向键left*/#define PLAY2RIGHT 0x4d00/*右移--⽅向键right*/#define PLAY2DO 0x1c0d/*落⼦--回车键Enter*//*若想在游戏中途退出, 可按 Esc 键*/#define ESCAPE 0x011b/*定义棋盘上交叉点的状态, 即该点有⽆棋⼦ *//*若有棋⼦, 还应能指出是哪个玩家的棋⼦ */#define CHESSNULL 0 /*没有棋⼦*/#define CHESS1 'O'/*⼀号玩家的棋⼦*/#define CHESS2 'X'/*⼆号玩家的棋⼦*//*定义按键类别*/#define KEYEXIT 0/*退出键*/#define KEYFALLCHESS 1/*落⼦键*/#define KEYMOVECURSOR 2/*光标移动键*/#define KEYINVALID 3/*⽆效键*//*定义符号常量: 真, 假 --- 真为1, 假为0 */#define TRUE 1#define FALSE 0/**********************************************************//* 定义数据结构 *//*棋盘交叉点坐标的数据结构*/struct point{int x,y;};/**********************************************************//*⾃定义函数原型说明 */void Init(void);int GetKey(void);int CheckKey(int press);int ChangeOrder(void);int ChessGo(int Order,struct point Cursor);void DoError(void);void DoOK(void);void DoWin(int Order);void MoveCursor(int Order,int press);void DrawCross(int x,int y);void DrawMap(void);int JudgeWin(int Order,struct point Cursor);int JudgeWinLine(int Order,struct point Cursor,int direction); void ShowOrderMsg(int Order);void EndGame(void);/**********************************************************//**********************************************************//* 定义全局变量 */int gPlayOrder; /*指⽰当前⾏棋⽅ */struct point gCursor; /*光标在棋盘上的位置 */char gChessBoard[19][19];/*⽤于记录棋盘上各点的状态*/ /**********************************************************//**********************************************************//*主函数*/void main(){int press;int bOutWhile=FALSE;/*退出循环标志*/printf("Welcome [url][/url]");Init();/*初始化图象,数据*/while(1){press=GetKey();/*获取⽤户的按键值*/switch(CheckKey(press))/*判断按键类别*/{/*是退出键*/case KEYEXIT:clrscr();/*清屏*/bOutWhile = TRUE;break;/*是落⼦键*/case KEYFALLCHESS:if(ChessGo(gPlayOrder,gCursor)==FALSE)/*⾛棋*/ DoError();/*落⼦错误*/else{DoOK();/*落⼦正确*//*如果当前⾏棋⽅赢棋*/if(JudgeWin(gPlayOrder,gCursor)==TRUE){DoWin(gPlayOrder);bOutWhile = TRUE;/*退出循环标志置为真*/}/*否则*/else/*交换⾏棋⽅*/ChangeOrder();ShowOrderMsg(gPlayOrder);}break;/*是光标移动键*/case KEYMOVECURSOR:MoveCursor(gPlayOrder,press);break;/*是⽆效键*/case KEYINVALID:break;}if(bOutWhile==TRUE)break;}/*游戏结束*/EndGame();}/**********************************************************//*界⾯初始化,数据初始化*/void Init(void){int i,j;char *Msg[]={"Player1 key:"," UP----w"," DOWN--s"," LEFT--a"," RIGHT-d"," DO----space","","Player2 key:"," UP----up"," DOWN--down"," LEFT--left"," RIGHT-right"," DO----ENTER","","exit game:"," ESC",NULL,};/* 先⼿⽅为1号玩家 */gPlayOrder = CHESS1;/* 棋盘数据清零, 即棋盘上各点开始的时候都没有棋⼦ */ for(i=0;i<19;i++)for(j=0;j<19;j++)gChessBoard[i][j]=CHESSNULL;/*光标初始位置*/gCursor.x=gCursor.y=0;/*画棋盘*/textmode(C40);DrawMap();/*显⽰操作键说明*/i=0;textcolor(BROWN);while(Msg[i]!=NULL){gotoxy(25,3+i);cputs(Msg[i]);i++;}/*显⽰当前⾏棋⽅*/ShowOrderMsg(gPlayOrder);/*光标移⾄棋盘的左上⾓点处*/gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*画棋盘*/void DrawMap(void){int i,j;clrscr();for(i=0;i<19;i++)for(j=0;j<19;j++)DrawCross(i,j);}/*画棋盘上的交叉点*/void DrawCross(int x,int y){gotoxy(x+MAPXOFT,y+MAPYOFT);/*交叉点上是⼀号玩家的棋⼦*/if(gChessBoard[x][y]==CHESS1){textcolor(LIGHTBLUE);putch(CHESS1);return;}/*交叉点上是⼆号玩家的棋⼦*/if(gChessBoard[x][y]==CHESS2) {textcolor(LIGHTBLUE);putch(CHESS2);return;}textcolor(GREEN);/*左上⾓交叉点*/if(x==0&&y==0){putch(CROSSLU);return;}/*左下⾓交叉点*/if(x==0&&y==18){putch(CROSSLD);return;}/*右上⾓交叉点*/if(x==18&&y==0){putch(CROSSRU);return;}/*右下⾓交叉点*/if(x==18&&y==18){putch(CROSSRD);return;}/*左边界交叉点*/if(x==0){putch(CROSSL);return;}/*右边界交叉点*/if(x==18){putch(CROSSR);return;}/*上边界交叉点*/if(y==0){putch(CROSSU);return;}/*下边界交叉点*/if(y==18){putch(CROSSD);return;}/*棋盘中间的交叉点*/putch(CROSS);}/*交换⾏棋⽅*/int ChangeOrder(void)if(gPlayOrder==CHESS1)gPlayOrder=CHESS2;elsegPlayOrder=CHESS1;return(gPlayOrder);}/*获取按键值*/int GetKey(void){char lowbyte;int press;while (bioskey(1) == 0);/*如果⽤户没有按键,空循环*/press=bioskey(0);lowbyte=press&0xff;press=press&0xff00 + toupper(lowbyte);return(press);}/*落⼦错误处理*/void DoError(void){sound(1200);delay(50);nosound();}/*赢棋处理*/void DoWin(int Order){sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);nosound();textcolor(RED+BLINK);gotoxy(25,20);if(Order==CHESS1)cputs("PLAYER1 WIN!");elsecputs("PLAYER2 WIN!");gotoxy(25,21);cputs(" \\<^+^>/");getch();}/*⾛棋*/int ChessGo(int Order,struct point Cursor){/*判断交叉点上有⽆棋⼦*/if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL) {/*若没有棋⼦, 则可以落⼦*/gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); textcolor(LIGHTBLUE);putch(Order);gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); gChessBoard[Cursor.x][Cursor.y]=Order;return TRUE;}elsereturn FALSE;}/*判断当前⾏棋⽅落⼦后是否赢棋*/int JudgeWin(int Order,struct point Cursor)int i;for(i=0;i<4;i++)/*判断在指定⽅向上是否有连续5个⾏棋⽅的棋⼦*/if(JudgeWinLine(Order,Cursor,i))return TRUE;return FALSE;}/*判断在指定⽅向上是否有连续5个⾏棋⽅的棋⼦*/int JudgeWinLine(int Order,struct point Cursor,int direction) {int i;struct point pos,dpos;const int testnum = 5;int count;switch(direction){case 0:/*在⽔平⽅向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y;dpos.x=1;dpos.y=0;break;case 1:/*在垂直⽅向*/pos.x=Cursor.x;pos.y=Cursor.y-(testnum-1);dpos.x=0;dpos.y=1;break;case 2:/*在左下⾄右上的斜⽅向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y+(testnum-1);dpos.x=1;dpos.y=-1;break;case 3:/*在左上⾄右下的斜⽅向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y-(testnum-1);dpos.x=1;dpos.y=1;break;}count=0;for(i=0;i<testnum*2+1;i++)/*i<testnum*2-1*/{if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18){if(gChessBoard[pos.x][pos.y]==Order){count++;if(count>=testnum)return TRUE;}elsecount=0;}pos.x+=dpos.x;pos.y+=dpos.y;}return FALSE;}/*移动光标*/void MoveCursor(int Order,int press){switch(press){case PLAY1UP:if(Order==CHESS1&&gCursor.y>0)gCursor.y--;break;case PLAY1DOWN:if(Order==CHESS1&&gCursor.y<18)gCursor.y++;break;case PLAY1LEFT:if(Order==CHESS1&&gCursor.x>0)gCursor.x--;break;case PLAY1RIGHT:if(Order==CHESS1&&gCursor.x<18)gCursor.x++;break;case PLAY2UP:if(Order==CHESS2&&gCursor.y>0)gCursor.y--;break;case PLAY2DOWN:if(Order==CHESS2&&gCursor.y<18)gCursor.y++;break;case PLAY2LEFT:if(Order==CHESS2&&gCursor.x>0)gCursor.x--;break;case PLAY2RIGHT:if(Order==CHESS2&&gCursor.x<18)gCursor.x++;break;}gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*游戏结束处理*/void EndGame(void){textmode(C80);}/*显⽰当前⾏棋⽅*/void ShowOrderMsg(int Order){gotoxy(6,MAPYOFT+20);textcolor(LIGHTRED);if(Order==CHESS1)cputs("Player1 go!");elsecputs("Player2 go!");gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*落⼦正确处理*/void DoOK(void){sound(500);delay(70);sound(600);delay(50);sound(1000);delay(100);nosound();}/*检查⽤户的按键类别*/int CheckKey(int press){if(press==ESCAPE)return KEYEXIT;/*是退出键*/elseif( ( press==PLAY1DO && gPlayOrder==CHESS1) || ( press==PLAY2DO && gPlayOrder==CHESS2))return KEYFALLCHESS;/*是落⼦键*/elseif( press==PLAY1UP || press==PLAY1DOWN ||press==PLAY1LEFT || press==PLAY1RIGHT ||press==PLAY2UP || press==PLAY2DOWN ||press==PLAY2LEFT || press==PLAY2RIGHT)return KEYMOVECURSOR;/*是光标移动键*/elsereturn KEYINVALID;/*按键⽆效*/}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
C语言编写五子棋游戏
![C语言编写五子棋游戏](https://img.taocdn.com/s3/m/29feaa2442323968011ca300a6c30c225901f04c.png)
C语⾔编写五⼦棋游戏本⽂实例为⼤家分享了C语⾔编写五⼦棋游戏的具体代码,供⼤家参考,具体内容如下⼀、构建棋盘⾸先可以⽤⼀个⼆维数组来构建棋盘,下标表⽰位置,内容表⽰⿊⼦⽩⼦或者空位。
当数组内容为1时,该位置为⽩字,当数组为0时,该位置为⽩⼦,空位输出+int w[11][11], flag = 0;int a, b;for (int k = 0; k < 11; k++)printf("第%d列\t", k);printf("\n");for (int i = 0; i < 11; i++) {for (int j = 0; j < 11; j++) {if (w[i][j] == 0) printf("⿊\t");else if (w[i][j] == 1) printf("⽩\t");else printf("+\t");}printf("第%d⾏\n\n", i);}⼆、判断位置超范围与错误输⼊当输⼊的棋⼦坐标超出范围或者该位置已经存在棋⼦,则再次输⼊棋⼦坐标。
其中flag作为标志作为判别⿊⼿⽩⼿的标志。
if (flag == 0) {printf("⿊⾊下棋\n");scanf("%d %d", &a, &b);while (a < 0 || a>10 || b < 0 || b>10) {printf("此位置超出范围,请重新输⼊:");scanf("%d %d", &a, &b);}while (w[a][b] == 0 || w[a][b] == 1){printf("此位置已有棋⼦,请重新输⼊:");scanf("%d %d", &a, &b);}flag = 1;w[a][b] = 0;}else {printf("⽩⾊下棋\n");scanf("%d %d", &a, &b);while (a < 0 || a>10 || b < 0 || b>10) {printf("此位置超出范围,请重新输⼊:");scanf("%d %d", &a, &b);}while (w[a][b] == 0 || w[a][b] == 1){printf("此位置已有棋⼦,请重新输⼊:");scanf("%d %d", &a, &b);}三、判断胜负在五⼦棋中,出现五个棋⼦排成⼀排或者⼀列或者斜排即可判为胜出。
五子棋游戏程序(c)
![五子棋游戏程序(c)](https://img.taocdn.com/s3/m/9ffcb122a5e9856a561260cc.png)
closegraph(); //按Esc键结束
}
char Inputbox(void)
{
char a;
a=getch(); //读入用户输入
if(a!='0')
return a;
else
return '0';
}
////////////////////////////////
//判断五子连珠
////////////////////////////////
{
m++;
n=0;
if(m>=5)
{
drawtext("play1 win!", &r, DT_LEFT);
break;
}
}
else if(*(*(p+i*20)+i*20)=='W')
{
n++;
m=0;
if(n>=5)
{
drawtext("play2 win!", &r, DT_LEFT);
break;
//开始游戏模块
/////////////////////////////////
void start(char (*p)[400])
{
void win(char (*p)[400]);
int x,y,m,n,flag=0,e=1;
char a;
char Inputbox(void);
char s6[]="Input error!";
五子棋游戏程序
规则:1.执黑先行
2.最先在棋盘横向、纵向或斜向形成连续的
c语言五子棋代码
![c语言五子棋代码](https://img.taocdn.com/s3/m/154936e75a8102d277a22f06.png)
c语言五子棋代码#include <windows.h>#define MAX 20//绘制20X20的棋盘#define TextWidth 200//棋盘右边的宽度#define ERROR 0#define NO 0#define OK 1#define DEFAULT 0#define ICO_CUR 0x1000 //预定义光标的idint leng=1;HDC hdc,hdc1,hdc2;int xw,yw;int iGame[MAX][MAX];POINT point;//鼠标点击位置enum {Default,Player1,Player2}play; enum {Stop,Play,Paush}plays;void Init(HWND hwnd);void paint(int play,int x,int y); void chagePlayer();int Look(int x,int y,int play); void over(HWND hwnd,int play); LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);char szClassName[] = "超简单的五子棋";int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)//主函数{HWND hwnd;MSG messages;WNDCLASSEX wincl;wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; wincl.style = CS_DBLCLKS;wincl.cbSize = sizeof (WNDCLASSEX);wincl.hIcon = LoadIcon(hThisInstance,MAKEINTRESOURCE(ICO_CUR));wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);wincl.hCursor = LoadCursor (NULL, IDC_ARROW);wincl.lpszMenuName = NULL; wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl)) return 0;hwnd = CreateWindowEx (1, szClassName,szClassName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,800,600,HWND_DESKTOP,NULL,hThisInstance,NULL);ShowWindow (hwnd, nFunsterStil);//该函数设置指定窗口的显示状态 while (GetMessage (&messages, NULL, 0, 0)){TranslateMessage(&messages); DispatchMessage(&messages); }return messages.wParam; }LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam) //处理窗口事件{PAINTSTRUCT ps;static HPEN hpen,hpen1,hpen2; //设备static HBRUSH hbrush,hbrush1,hbrush2; //画刷的句柄int x,y;switch (message) //消息{case WM_KEYDOWN:switch (wParam){case VK_F2: //F2重新开始游戏Init(hwnd); //初始绘制游戏界面Rectangle(hdc,0,0,xw,yw); for(x=0;x<MAX;x++) //绘制棋盘for(y=0;y<MAX;y++) {Rectangle(hdc,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;iGame[x][y]=Default; }leng=1;plays=Play;break;case VK_F1:leng=1;plays=Play;break;}break;case WM_CREATE: //初始化窗体时调用plays=Stop;play=Player1;break;case WM_SIZE:xw=LOWORD(lParam); //获取窗体的宽度 yw=HIWORD(lParam); //获取窗体的高度 //TCHAR xx[20];//wsprintf(xx,"%d",xw); //MessageBox(hwnd,xx,xx,MB_OK);xw-=TextWidth; //xw保存棋盘的宽度InvalidateRect(hwnd,NULL,TRUE);//向窗体绘制一个矩形,支持重新绘制break;case WM_LBUTTONDOWN: //响应用户鼠标左键 //获取但前鼠标坐标point.x=LOWORD(lParam); point.y=HIWORD(lParam); //初始化设备DC Init(hwnd);//鼠标坐标换为数组坐标x=(point.x)/(xw/MAX); //定位到小狂格中去y=(point.y)/(yw/MAX);if(plays==Stop)break;if(x<MAX&&y<MAX) //未超出棋盘{if(iGame[x][y]==Default&&plays==Play)//判断但前位置是否有棋子覆盖是否是下棋模式 {leng=1;paint(play,x,y);if(Look(x,y,play))over(hwnd,play);chagePlayer();}}break;case WM_PAINT://开始绘制{hdc=BeginPaint(hwnd,&ps);Init(hwnd);Rectangle(hdc,0,0,xw,yw);for(x=0;x<MAX;x++)for(y=0;y<MAX;y++){Rectangle(hdc,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;paint(iGame[x][y],x,y);}EndPaint(hwnd,&ps);}break;case WM_DESTROY: //当用户关闭游戏PostQuitMessage (0); // 发送一个消息给message queue break;default: //什么都不做return DefWindowProc (hwnd, message, wParam, lParam);}return 0;}void Init(HWND hwnd) //初始化方法{hdc1=GetDC(hwnd);hdc2=GetDC(hwnd);//初始背景 (游戏的界面)SelectObject(hdc,CreatePen(0,1,RGB(0,0,0))); //小矩形框的边框的颜色SelectObject(hdc,CreateSolidBrush(RGB(255,218,185))); //填充的颜色 //初始玩家一图形SelectObject(hdc1,CreatePen(0,1,RGB(255,255,255))); //玩家一的棋子颜色 SelectObject(hdc1,CreateSolidBrush(RGB(255,255,255))); //填充白色 //初始玩家二图形SelectObject(hdc2,CreatePen(0,1,RGB(255,255,255))); //玩家二的棋子颜色 SelectObject(hdc2,CreateSolidBrush(RGB(0,0,0))); //填充黑色 //该背景分为两层背景,一层为游戏界面(hdc),一层为玩家的棋子(hdc1,hdc2) //SetBkMode(hdc,0);该方法好像没什么用//绘制右边操作提示区域Rectangle(hdc,xw,29,xw+200,30);//从xw开始绘制到xw+200结束(绘制长200宽为30-29的矩形)的线条TextOut(hdc,xw+45,13,"超简单的五子棋",14); //从坐标(xw+45)(13)开始绘制“超简单的五子棋”宽为14TextOut(hdc,xw+45,40,"F1 - 开始游戏",13);TextOut(hdc,xw+45,60,"F2 -重新开始游戏",16);Rectangle(hdc,xw,80,xw+200,81); TextOut(hdc,xw+45,150,"白棋为第二玩家",14);TextOut(hdc,xw+45,250,"黑棋为第二玩家",14);Rectangle(hdc,xw,330,xw+200,331); }void paint(int play,int x,int y) //画棋子{switch (play){case Player1:if(iGame[x][y]!=Default){//该地方已经有棋子了}else{Ellipse(hdc1,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;//Ellipse (圆心x坐标,圆心y坐标,起始角度,终止角度,x轴半径,y轴半径) }//绘制玩家player1的棋子iGame[x][y]=Player1; //将该地方标准已经有棋子break;case Player2:if(iGame[x][y]!=Default){}else{Ellipse(hdc2,x*xw/MAX,y*yw/MAX,(x+1)*xw/MAX,(y+1)*yw/MAX) ;}//绘制玩家player2的棋子iGame[x][y]=Player2; //将该地方标准已经有棋子break;}}void chagePlayer() //改变玩家{if(play==Player1) play=Player2; elseplay=Player1;}int Look(int x,int y,int play) // 检查模块{int i,tempx,tempy;tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempy--,i++); if(i>=5) return OK; //向上核对(y轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempy++,i++); if(i>=5) return OK; //向下核对(y轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx--,i++); if(i>=5) return OK; //向左核对(x轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx++,i++); if(i>=5) return OK; //向右核对(y轴)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx--,tempy--,i++); if(i>=5) return OK; //向左上核对(x,y轴的对角线向上\)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx++,tempy--,i++); if(i>=5) return OK; //向右上核对(x,y轴的对角线向下\)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx--,tempy++,i++); if(i>=5) return OK; //向左下核对(x,y轴的对角线向上/)tempx=x;tempy=y;for(i=0;i<5&&iGame[tempx][tempy]==play;tempx++,tempy++,i++); if(i>=5) return OK; //向右下核对(x,y轴的对角线向上/)return NO;}void over(HWND hwnd,int play) //判断胜负{switch(play){case Player1:MessageBox(hwnd,"恭喜白棋玩家获得胜利","胜利",0);SendMessage(hwnd,WM_KEYDOWN,VK_F2,NULL); //重新开始游戏 plays=Stop;break;case Player2:MessageBox(hwnd,"恭喜黑棋玩家获得胜利","胜利",0);SendMessage(hwnd,WM_KEYDOWN,VK_F2,NULL);plays=Stop;break;}}最终效果图:。
C语言实现简易五子棋小游戏
![C语言实现简易五子棋小游戏](https://img.taocdn.com/s3/m/4d88a9f0534de518964bcf84b9d528ea81c72f34.png)
C语⾔实现简易五⼦棋⼩游戏本⽂实例为⼤家分享了C语⾔实现简单五⼦棋⼩游戏的具体代码,供⼤家参考,具体内容如下效果图如下:设计思路:棋盘设计为15×15格,初始状态光标在棋盘的中央,⽩棋先⾛,轮流落⼦,当⼀⽅连成五⼦或下满棋盘时,游戏结束(连成五⼦的⼀⽅获胜,下满棋盘为和棋)。
当游戏⼀⽅胜利后显⽰胜利信息,提⽰信息利⽤汉字点阵输出。
程序游戏是⼀个⼆维平⾯图,可⽤⼆维数组来实现,数组两个下标可以表⽰棋盘上的位置,数组元素的值代表棋格上的状态,共有三种情况,分别是0代表空格,1代表⽩棋,2代表⿊棋。
程序的主要⼯作是接收棋⼿按键操作,棋⼿1⽤设定四个键控制光标移动,回车键表⽰落⼦。
棋⼿2⽤另四个键控制光标移动,空格键表⽰落⼦。
接收到回车键或空格键,说明棋⼿落⼦,先判断是否是有效位置,即有棋⼦的位置不能重叠落⼦。
落⼦成功后,马上判断以该位置为中⼼的⼋个⽅向:上、下、左、右、左上、左下、右上、右下是否有相同颜⾊的棋⼦连成五⼦,如果连成五⼦,则游戏结束。
#include<stdio.h>#include<stdlib.h>#include<windows.h>#pragma comment(lib, "WINMM.LIB")#include <mmsystem.h>#include<conio.h>#include<time.h>#define width 32 //棋盘总宽度#define high 31 //棋盘总⾼度#define MAX_X 15 //棋盘横向格⼦数#define MAX_Y 15 //棋盘纵向格⼦数#define WIDTH (16+width) //游戏总⾼度#define HIGH (high+4) //游戏总⾼度#define player1 1 //⽩⼦玩家#define player2 2 //⿊⼦玩家#define emptyPlayer 0//⽆⼦#define Unplayer -2 //中途退出游戏,⽆玩家获胜typedef struct Stack{//记录下每次落⼦的坐标int x[MAX_X*MAX_Y];int y[MAX_X*MAX_Y];//相当于栈顶指针int top;}Stack;int pos[MAX_X][MAX_Y];//存储棋盘上各位置处的状态⽐如有⽩⼦为1,有⿊⼦为2,⽆⼦为0int px,py; //光标位置int player = 1;//记录当前玩家默认玩家从⽩⽅开始int flag1 = 0;//标志游戏开始int gameOver_player = -1;//判断结束的标志int pre_px = -1, pre_py = -1;//记录下上⼀次的坐标位置void gotoxy(int x,int y);//设置CMD窗⼝光标位置void hide_cursor(); //隐藏CMD窗⼝光标void map();//打印地图void game_Description();//打印动态游戏说明void initMapState();//初始化游戏位置数据void mapState(int qizi);//数组记录下对应位置的状态int isGoPlay();//判断是否可以落⼦int hasGoPlay(int Player);//以落⼦处为中⼼,判断已经落⼦后的棋盘是否五⼦相连void goPlay(int Player, Stack* p);//落⼦ Player 1 2 0void yiDongKuang();//移动框void player1_move();//玩家1_移动void player2_move();//玩家2_移动int gameOver();//判断游戏是否结束Stack* createStack();//创建空栈void push(Stack* p, int x, int y);//⼊栈void color(const unsigned short textColor);//⾃定义函根据参数改变颜⾊//void setColor(unsigned short backColor);//设置游戏背景颜⾊void gotoxy(int x,int y)//设置CMD窗⼝光标位置{COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}void hide_cursor() //隐藏CMD窗⼝光标{CONSOLE_CURSOR_INFO cci;cci.bVisible = FALSE;cci.dwSize = sizeof(cci);HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorInfo(handle, &cci);}void color(const unsigned short textColor) //⾃定义函根据参数改变颜⾊{if(textColor>0 && textColor<=15) //参数在0-15的范围颜⾊SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textColor); //⽤⼀个参数,改变字体颜⾊ else //默认的字体颜⾊是⽩⾊SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);}//打印地图void map(){int x, y;color(02);/*开始打印棋盘格⼦*///打印横向图格for(y = 2; y < high - 1; y+=2){for(x = 1; x < width - 2; x+=2){gotoxy(x , y);printf("-+");}}//打印竖向图格for(y = 1; y < high; y += 2) {for(x = 2; x < width-2; x += 2){gotoxy(x , y);printf("|");}}/*打印棋盘格⼦结束*//*开始打印图框*///打印棋盘图框for (y = 0; y < high; y++){for (x = 0; x < width; x+=2){if (x == 0 || x == width - 2){gotoxy(x, y);printf("|");}if(y == 0 || y == high - 1 ){gotoxy(x, y);printf("--");}}}//打印右图框for(y = 0; y < high; y++){for(x = width; x < WIDTH; x+=2){if (x == WIDTH - 2){gotoxy(x, y);printf("|");}if(y == 0 || y == high - 1 ){gotoxy(x, y);printf("--");}}}//打印下图框 y->high ~ HiGH-1 x->0 ~ WIDTH-2 for(y = high; y < HIGH; y++){for(x = 0; x < WIDTH; x+=2){if (x == 0 || x == WIDTH - 2){gotoxy(x, y);printf("|");}if(y == high || y == HIGH - 1 ){gotoxy(x, y);printf("--");}}}//打印下图框内容gotoxy( 1, high+1);printf(" 欢迎来到五⼦棋游戏!");//打印右图框内容gotoxy( width-1, 1);printf("游戏说明:");gotoxy( width-1, 3);printf("1)X表⽰玩家⼀棋");gotoxy( width-1, 4);printf("⼦,⽽O表⽰玩家");gotoxy( width-1, 5);printf("⼆棋⼦");gotoxy( width-1, 7);printf("2)X先、O后,交替");gotoxy( width-1, 8);printf("下⼦,每次只能下");gotoxy( width-1, 9);printf("⼀⼦");gotoxy( width-1, 11);printf("3)棋⼦下在棋盘");gotoxy( width-1, 12);printf("的格⼦内,棋⼦下");gotoxy( width-1, 13);printf("定后,不得向其它");gotoxy( width-1, 14);printf("点移动");gotoxy( width-1, 16);printf("4)最先连成五⼦");gotoxy( width-1, 17);printf("的⼀⽅即为获胜");gotoxy( width-1, 19);printf("玩家⼀移动键:");gotoxy( width-1, 20);printf("w上 s下 a左 d右");gotoxy( width-1, 21);printf("下⼦: 空格键");gotoxy( width-1, 23);printf("玩家⼆移动键:");gotoxy( width-1, 24);printf("i上 k下 j左 l右");gotoxy( width-1, 25);printf("下⼦:回车键");gotoxy( width+1, 27);color(4);printf("退出键: Y");gotoxy( width+1, 29);color(6);printf("悔棋键: G");/*打印图框结束*/color(02);/*打印棋盘上的四个标记点*/gotoxy( 3*2+2 , 3*2+2);printf("*");gotoxy( (MAX_X-4)*2 , 3*2+2);printf("*");gotoxy( 3*2+2 , (MAX_Y-4)*2);printf("*");gotoxy( (MAX_X-4)*2 , (MAX_Y-4)*2); printf("*");/*打印结束*//*开始修边*//*gotoxy(width - 1, 0);printf(" ");gotoxy(width - 1, high - 1);printf(" ");*//*修边结束*//*打印地图完成*/}//打印动态游戏说明void game_Description(){//打印下图框内容gotoxy( 1, high+1);printf(" ");if(player == player1){gotoxy( 1, high+1);color(2);printf(" 玩家⼀下棋中..."); }else if(player == player2){gotoxy( 1, high+1);color(2);printf(" 玩家⼆下棋中..."); }}//初始化游戏位置数据void initMapState(){for(int i = 0 ; i < MAX_Y; i++){for(int j = 0; j < MAX_X; j++){pos[i][j] = 0;//初始状态全为空}}//注意光标的位置和存储在数组中的位置是不同的px = 7;py = 7;gotoxy(py*2+1,px*2+1);//初始位置}//数组记录下对应位置的状态void mapState(int qizi){ //2*px+1 = x //2*py+1 = y //px->0~14 //py->0~14if(px >= MAX_X || px < 0 || py >= MAX_Y || py < 0) return;//其他情况不可以记录状态pos[px][py] = qizi;}//判断是否可以落⼦int isGoPlay(){if(px >= MAX_X || px < 0 || py >= MAX_Y || py < 0)return 0;//其他情况不可以记录状态if(pos[px][py] == emptyPlayer){//说明⽆⼦return 1;}else{//说明有⼦return 0;}}//以落⼦处为中⼼,判断已经落⼦后的棋盘是否五⼦相连int hasGoPlay(int Player){ //分为两部分,先记录⼀部分的相同Player的个数//再记录下另余部分的个数,相加为相连棋⼦总个数 int port1 = 0, port2 = 0;int x, y, count;//上下查找x = px, y = py-1;while(pos[x][y]==Player && y >= 0){++port1;//上部分个数--y;//上移}y = py+1;while(pos[x][y]==Player && y < MAX_Y){++port2;//下部分个数++y;//下移}//计算总数count = port1 + port2 + 1;if(count >= 5) return 1;//左右查找port1 = 0, port2 = 0;x = px-1, y = py;while(pos[x][y]==Player && x >= 0){++port1;//上部分个数--x;//左移}x = px+1;while(pos[x][y]==Player && x < MAX_X){++port2;//下部分个数++x;//右移}//计算总数count = port1 + port2 + 1;if(count >= 5) return 1;//左上右下查找port1 = 0, port2 = 0;x = px-1, y = py-1;while(pos[x][y]==Player && x >= 0 && y >= 0){++port1;//上部分个数--x;//左移--y;//上移}x = px+1, y = py+1;while(pos[x][y]==Player && x < MAX_X && y < MAX_Y){++port2;//下部分个数++x;//右移++y;//下移}//计算总数count = port1 + port2 + 1;if(count >= 5) return 1;//右上左下查找port1 = 0, port2 = 0;x = px+1, y = py-1;while(pos[x][y]==Player && x < MAX_X && y >= 0){++port1;//上部分个数++x;//左移--y;//上移}x = px-1, y = py+1;while(pos[x][y]==Player && x >= 0 && y < MAX_Y){++port2;//下部分个数--x;//右移++y;//下移}//计算总数count = port1 + port2 + 1;if(count >= 5) return 1;return 0;}//落⼦ Player 1 2 0void goPlay(int Player, Stack* p){if(isGoPlay()){//说明可以落⼦mapState(Player);//将对应位置的情况记录在数组中if(hasGoPlay(Player)){//如果五⼦相连,则 gameovergameOver_player = Player; //记录此刻胜利玩家,结束游戏 }/*⼊栈*/push(p, px, py);/*⾓⾊切换*/if(Player == player1){player = player2;//切换成玩家1gotoxy(px*2+1, py*2+1 );//将光标移动到对应位置color(07);printf("X");//打印玩家1game_Description();// 动态说明}else if(Player == player2){player = player1;//切换成另⼀个玩家2gotoxy( px*2+1, py*2+1);//将光标移动到对应位置 color(07);printf("O");//打印玩家2game_Description();// 动态说明}}}//⼊栈void push(Stack* p, int x, int y){//将此刻的坐标⼊栈int top = p->top;++p->top;//移动栈针p->x[top] = x;p->y[top] = y;return;}//出栈void pop(Stack* p){int x, y;//出栈,移动栈顶指针//如果栈为空,则不弹出if(p->top <= 0) return;--p->top;int top = p->top;//记录下弹出的位置x = p->x[top];y = p->y[top];//在弹出位置打印空格gotoxy(x*2+1, y*2+1);printf(" ");//抹除记录pos[x][y] = 0;}//移动框void yiDongKuang(){//打印移动框gotoxy(px*2, py*2+1);color(11);printf("[");gotoxy(px*2+2, py*2+1);printf("]");//打印移动框结束if(pre_px != -1 && pre_py != -1){if(pre_px > px && pre_py == py){//当向左移动时//将上⼀个位置的右边保持原样gotoxy(pre_px*2+2, pre_py*2+1);color(2);printf("|");}else if(pre_px < px && pre_py == py){//当向右移动时//将上⼀个位置的左边保持原样gotoxy(pre_px*2, pre_py*2+1);color(2);printf("|");}else{//当向上下移动时//将上⼀个位置的左右边保持原样gotoxy(pre_px*2+2, pre_py*2+1);color(2);printf("|");gotoxy(pre_px*2, pre_py*2+1);color(2);printf("|");}}pre_px = px;pre_py = py;}//玩家1 移动void player1_move(Stack* p){char key;if (_kbhit())//检测是否按键{fflush(stdin);key = _getch();//保存按键game_Description();//动态说明}switch(key){case 'w': py--;yiDongKuang();break;//上case 'a': px--;yiDongKuang();break;//左case 'd': px++;yiDongKuang();break;//右case 's': py++;yiDongKuang();break;//下case ' ': goPlay(player1, p);break;//落⼦case 'y': gameOver_player = -2; gameOver();//退出游戏 case 'Y': gameOver_player = -2; gameOver();//退出游戏case 'g': pop(p); pop(p); break;//悔棋case 'G': pop(p); pop(p); break;//悔棋default: break;}//限制光标范围if(py < 0) py = MAX_Y-1;else if(py >= MAX_Y) py = 0;else if(px < 0) px = MAX_X-1;else if(px >= MAX_X) px = 0;gotoxy(2*py+1, 2*px+1);}//玩家2 移动void player2_move(Stack* p){char key;if (_kbhit())//检测是否按键{fflush(stdin);key = _getch();//保存按键game_Description();//动态说明}switch(key){case 'i': py--;yiDongKuang();break;//上case 'j': px--;yiDongKuang();break;//左case 'l': px++;yiDongKuang();break;//右case 'k': py++;yiDongKuang();break;//下case '\r': goPlay(player2, p);break;//落⼦case 'y': gameOver_player = -2; gameOver();//退出游戏case 'Y': gameOver_player = -2; gameOver();//退出游戏case 'g': pop(p); pop(p); break;//悔棋case 'G': pop(p); pop(p); break;//悔棋default: break;}//限制光标范围if(py < 0) py = MAX_Y-1;else if(py >= MAX_Y) py = 0;else if(px < 0) px = MAX_X-1;else if(px >= MAX_X) px = 0;gotoxy(2*px+1, 2*py+1);}//创建空栈Stack* createStack(){//申请空间Stack* p = (Stack* )malloc(sizeof(Stack));//如果未申请到空间if(p==NULL) return NULL;p->top = 0;//初始化栈顶return p;}//判断游戏是否结束int gameOver(){ //gamerOver_player -1 表⽰继续游戏 1 表⽰⽩⽅胜利 2 表⽰⿊⽅胜利 0 表⽰平局 //五⼦相连⼀⽅取胜 y->high ~ HiGH-1 x->0 ~ WIDTH-2if(gameOver_player == -1){return 1;}else if(gameOver_player == player1){//⽩⽅胜利gotoxy( 1, high+1);printf("玩家1胜利");return 0;}else if(gameOver_player == player2){//⿊⽅胜利gotoxy( 1, high+1);printf("玩家2胜利");return 0;}else if(gameOver_player == emptyPlayer){//棋盘满棋平局gotoxy( 1, high+1);printf("平局");return 0;}else if(gameOver_player == Unplayer){//中途退出gotoxy( 1, high+1);printf("正在退出游戏中...");exit(1);}return 1;}int main(){//调整游戏框system("mode con cols=48 lines=35");//打印地图map();//初始化游戏位置数据initMapState();//创建空栈Stack* p = createStack();//隐藏光标hide_cursor();//游戏循环控制移动while(gameOver()){//不断调换⼈物if(player == player1)player1_move(p);// 切换玩家1else if(player == player2)player2_move(p);// 切换玩家2}free(p);}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
五子棋游戏 C语言 代码 源代码
![五子棋游戏 C语言 代码 源代码](https://img.taocdn.com/s3/m/13a0137031b765ce050814bf.png)
#include <stdio.h>#include <stdlib.h>#define m 30int main (void){int count;//计数器算横纵行的结果int w,h;int u;int l;int i,size;//i声明步数。
size声明int r[m][m] = {0};//数组声明(棋子位置)int x, y;//声明落子坐标int n;//声明棋盘大小nchar a[20],b[20];printf ("请输入棋盘大小n\n");//编辑棋盘直到棋盘长度宽度大于4小于30 scanf ("%d", &n);if (n<=4 || n>m){do{printf ("输入的棋盘大小:4<n<%d\n", m);scanf ("%d", &n);}while (n<=4 || n>m);}getchar ();//声明玩家printf ("请输入玩家1姓名:\n");gets(a);printf ("请输入玩家2姓名:\n");gets(b);for ( i = 1, size = n*n;i <= size; i++)//编辑棋盘{if (i%2 == 1)//如果i能被2整除,为玩家a相关信息{do//玩家a棋子信息{printf ("%s该你下棋了,第%d个棋子\n", a, i);scanf ("%d%d", &x, &y);if (x > n || x < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>m || x<0);}if (y > n || y < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 1;for (u = 0;u < n; u++)//不同情况下判断玩家a获胜方式{for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 1 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;//直接跳转,其余代码不在运行count = 0;for (w = u, h = l; r[w][h] == 1 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w = u,h = l; r[w][h] == 1 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 1 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}}}}system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++)//画棋盘,声明两玩家棋子图片{if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●"); }printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k);}else if (i%2 == 0)//如果i不能被2整除,为玩家b相关信息{do{printf ("\n%s该你下棋了,第%d个棋子\n", b, i);scanf ("%d%d", &x, &y);if (x > n || x < 0){do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>n || x<0);}if (y >n|| y < 0){do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 2;system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");count = 0;for (u = 0;u < n; u++){for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 2 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u, h = l; r[w][h] == 2 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u,h = l; r[w][h] == 2 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 2 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}}}}}e: for (int j = n;j>=0;j--)//游戏结束界面棋盘固定重新显示{printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");printf ("\a游戏愉快,Powered by Techmessager\n");//结束语句return 0;}。
C语言五子棋游戏源代码
![C语言五子棋游戏源代码](https://img.taocdn.com/s3/m/3989779068dc5022aaea998fcc22bcd126ff42c2.png)
i n c l u d e<> define N 10void welcome;void initqipan;void showqiint i;void saveint p;void panduanint p;void heqi;void over;int zouqihang;int zouqilie;/结构体/struct zuobiao{int xNN;int yNN;}weizhiNN;/主函数/void main{int p=0;welcome;initqipan;forp=1;p<=NN;p++{weizhip.xp=zouqihang; weizhip.yp=zouqilie; savep;showqip;panduanp;}ifp==NNheqi;over;}/建立棋盘/void initqipan{int i,j;fori=0;i<N;i++{printf"%d",i;printf" ";}printf"\n";fori=1;i<N;i++{forj=0;j<N;j++{ifj==0printf"%d",i; elseprintf"·";}printf"\n";}}/显示棋子/void showqiint p{int i,j,k,m;int aNN,bNN;FILE fp;fp=fopen"wuzi_list","rb";fori=1;i<=NN;i++{fread&weizhii,sizeofstruct zuobiao,1,fp; ai=weizhii.xi;bi=weizhii.yi;}form=1;m<p;m++{whileweizhip.xp==am&&weizhip.yp==bm{printf"error\n";weizhip.xp=zouqihang;weizhip.yp=zouqilie;m=1;}}fori=0;i<N;i++{printf"%d",i;printf" ";}printf"\n";fori=1;i<N;i++{forj=1;j<N;j++{ifj==1printf"%d",i;fork=1;k<=p;k++{ifi==weizhik.xk&&j==weizhik.yk {ifk%2==1{printf"○";break;}else ifk%2==0{printf"●";break;}}}ifk>pprintf"·";else continue;}printf"\n";}}/走棋行/int zouqihang{int x;printf"请输入要走棋子所在行数\n";printf"x=";scanf"%d",&x;whilex>N-1||x<1{printf"error\n";printf"请输入要走棋子所在行数\n"; printf"x=";scanf"%d",&x;}return x;}/走棋列/int zouqilie{int y;printf"请输入要走棋子所在列数\n";printf"y=";scanf"%d",&y;whiley>N-1||y<1{printf"error\n";printf"请输入要走棋子所在列数\n";printf"y=";scanf"%d",&y;}return y;}/文件保存/void saveint i{FILE fp;fp=fopen"wuzi_list","wb";fwrite&weizhii,sizeofstruct zuobiao,1,fp; }/判断输赢/void panduanint p{int i,j,k8={1,1,1,1,1,1,1,1,};int aNN,bNN;FILE fp;fp=fopen"wuzi_list","rb";fori=1;i<=p;i++{fread&weizhii,sizeofstruct zuobiao,1,fp; ai=weizhii.xi;bi=weizhii.yi;}/判断行/fori=1;i<=p;i++{ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj&&bi==bj-1{k0++;continue;}else ifai==aj&&bi==bj-2{k0++;continue;}else ifai==aj&&bi==bj-3{k0++;continue;}else ifai==aj&&bi==bj-4{k0++;continue;}else ifk0==5{printf"Player 1 wins\n"; }elsecontinue;}ifk0==5break;k0=1;}else ifk0==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj&&bi==bj-1{k1++;continue;}else ifai==aj&&bi==bj-2 {k1++;continue;}else ifai==aj&&bi==bj-3 {k1++;continue;}else ifai==aj&&bi==bj-4{k1++;continue;}else ifk1==5{printf"Player 2 wins\n"; }elsecontinue;}ifk1==5break;k1=1;}}/判断列/fori=1;i<=p;i++{ifk0==5||k1==5break;else ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj-1&&bi==bj{k2++;continue;}else ifai==aj-2&&bi==bj {k2++;continue;}else ifai==aj-3&&bi==bj {k2++;continue;}else ifai==aj-4&&bi==bj {k2++;continue;else ifk2==5{printf"Player 1 wins\n"; }elsecontinue;}ifk2==5break;k2=1;}else ifk2==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj-1&&bi==bj{k3++;continue;else ifai==aj-2&&bi==bj{k3++;continue;}else ifai==aj-3&&bi==bj{k3++;continue;}else ifai==aj-4&&bi==bj{k3++;continue;}else ifk3==5{printf"Player 2 wins\n"; }elsecontinue;}ifk3==5break;k3=1;}}/判断对角左上-右下/fori=1;i<=p;i++{ifk0==5||k1==5||k2==5||k3==5break;else ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj-1&&bi==bj-1{k4++;continue;}else ifai==aj-2&&bi==bj-2 {k4++;continue;}else ifai==aj-3&&bi==bj-3{k4++;continue;}else ifai==aj-4&&bi==bj-4{k4++;continue;}else ifk4==5{printf"Player 1 wins\n"; }elsecontinue;}ifk4==5break;}else ifk2==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj-1&&bi==bj-1{k5++;continue;}else ifai==aj-2&&bi==bj-2 {k5++;continue;}else ifai==aj-3&&bi==bj-3 {k5++;continue;else ifai==aj-4&&bi==bj-4{k5++;continue;}else ifk5==5{printf"Player 2 wins\n";}elsecontinue;}ifk5==5break;k5=1;}}/判断对角左下-右上/fori=1;i<=p;i++{ifk0==5||k1==5||k2==5||k3==5||k4==5||k5==5else ifi%2==1{forj=1;j<=p;j=j+2{ifai==aj+1&&bi==bj-1{k6++;continue;}else ifai==aj+2&&bi==bj-2 {k6++;continue;}else ifai==aj+3&&bi==bj-3 {k6++;continue;}else ifai==aj+4&&bi==bj-4 {k6++;continue;}else ifk6==5{printf"Player 1 wins\n"; }elsecontinue;}ifk6==5break;k6=1;}else ifk6==5break;else ifi%2==0{forj=2;j<=p;j=j+2{ifai==aj+1&&bi==bj-1{k7++;continue;}else ifai==aj+2&&bi==bj-2{k7++;continue;}else ifai==aj+3&&bi==bj-3{k7++;continue;}else ifai==aj+4&&bi==bj-4{k7++;continue;}else ifk7==5{printf"Player 2 wins\n"; }elsecontinue;}ifk7==5break;k7=1;}}}/和棋/void heqi{printf"\n";printf" Tie\n";printf"\n";}/游戏结束/void over{printf"\n";printf" game over\n"; printf"\n";}/游戏开始/void welcome{printf"\n";printf" Welcome\n"; printf"\n";}。