C语言 棋盘游戏 源代码

合集下载

C语言棋盘游戏源代码

C语言棋盘游戏源代码

C语言棋盘游戏源代码#include <stdio. h>#include <stdlib・h>char matrix[3j [3] ;/*定义游戏棋盘 */char check(void);void init_matrix(void): void get_player_move(void) : void get_computer_move(void); void disp_matrix(void);int main(){char done;printf (,z This is the game of Tic Tac Toe・ \n");printf (,z You will be playing against the computer・ \n");done 二'';init_matrix();do {disp_matrix();get_player_move ();done = check () ;/*检测输赢*/if(done!= ' ')break;/*赢了*/get_computer_move ();done = check () ;/*检测输赢 */}while (done ='');if(done==,X’) printf("You won!\n");else printf (^Computer won!\n,?);disp_matrix() ;/*显示棋了的位彗*/return 0;}/*初始化棋盘*/void init_matrix(void) {int i, j;for(i=0;i<3;i++)for (j=0;j<3;j++)matrixLiZ[j] = ' ; }/*下棋者下*/void get_player_move(void) {int x, y;printf("Enter coordinates for your X:"); scanf (//%d%d,,> &x, &y);x—; y—;if(matrix[xj[y] != '){printf (''Invalid move, try again. \n"); get_player_move();}else matrix[x][y]二'X' ; }/*计算机下*/void get_computer_move(void) {int i, j;for(i=0;i<3;i++){for (j=0;j〈3;j++)if(matrixEiZ[j]== ' *)break;if(matrixEiZ[j]== ' *)break; }if(i*j=9){printf (,/draw\n,/);exit(O);}elsematrixTiZ [j]二'O';}/ *在屏幕上显示棋盘* /void disp_matrix(void){int t;for(t=0;t<3;t++){printf("%c|%c 丨%c", matrix[t][0], matrix[tJ[1], matrix[t][2]); if (t !=2) printf ("\nT - | -\n");}printf("\n");}/*看谁是赢着*/char check(void)int i;/*检查横行*/for(i=0;i<3;i++)if (matrixliZ lOZ—matrix[iZ L12&&matrix[i_ _O_==matrixLi_[2])return matrix[i_ _0_;/*检查竖行*/for(i=0;i<3;i++)if (matrixLOZ lil==matrix[lZmatrix[0_ _i_—matrix[2] .i])return matrix[0_ _i];/*检查对角线*/if (matrixlOZ ZOZ~matrix[lZ [lZ&&matrix[lZ [1^—matrix[2] [2])return matrix[0_ _0];if (matrix[OJ [2]~matrix[lZ [lZ&&matrix[lL [1]二二matrix[2] [0])return matrix[0_[2];return '';}许多MM在减肥的过程中困难重重,用尽方法却瘦不下来,其实只有在数不清的技巧,推敲中找到属于跟适合自己的最佳方式,才能在健康的瘦身道路上走下去,下面一些瘦身的小技巧,一定可以帮到你,1、慢慢吃我们都曾在美味佳肴面前狼呑虎咽。

C语言实现五子棋小游戏源代码

C语言实现五子棋小游戏源代码
}
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语言华容道代码华容道是一个经典的逻辑游戏,其中包含一个11x11 的棋盘和15个大小不一的棋子。

游戏的目标是将红色棋子移到最下方的任意空位。

以下是一个简单的华容道游戏的C语言实现:注意:这只是一个非常基本的版本,不包括游戏图形界面或错误处理等高级特性。

c#include <stdio.h>#include <stdlib.h>#define SIZE 15typedef struct {int data[SIZE][SIZE];} board;board boardInit() {board b;for(int i=0; i<SIZE; i++) {for(int j=0; j<SIZE; j++) {b.data[i][j] = 0;}}return b;}void printBoard(board b) {for(int i=0; i<SIZE; i++) {for(int j=0; j<SIZE; j++) {printf("%d ", b.data[i][j]);}printf("\n");}}void moveUp(board *b, int x, int y) {if(x > 0 && b->data[x-1][y] == 0) {int temp = b->data[x][y];b->data[x][y] = b->data[x-1][y];b->data[x-1][y] = temp;}}void moveDown(board *b, int x, int y) {if(x < SIZE-1 && b->data[x+1][y] == 0) {int temp = b->data[x][y];b->data[x][y] = b->data[x+1][y];b->data[x+1][y] = temp;}}void moveLeft(board *b, int x, int y) {if(y > 0 && b->data[x][y-1] == 0) {int temp = b->data[x][y];b->data[x][y] = b->data[x][y-1];b->data[x][y-1] = temp;}}void moveRight(board *b, int x, int y) {if(y < SIZE-1 && b->data[x][y+1] == 0) {int temp = b->data[x][y];b->data[x][y] = b->data[x][y+1];b->data[x][y+1] = temp;}}void solve(board *b) {int visited[SIZE][SIZE];for(int i=0; i<SIZE; i++) {for(int j=0; j<SIZE; j++) {visited[i][j] = 0;}}for(int i=0; i<SIZE; i++) {for(int j=0; j<SIZE; j++) {if(b->data[i][j] == 1 && visited[i][j] == 0) { // Start from the top-left corner (red piece)visited[i][j] = 1; // Mark the current position as visited (to avoid infinite loops)if(i > 0) moveUp(b, i, j); // Move up if possible (top empty) and mark it as visited (to avoid infinite loops)if(j > 0) moveLeft(b, i, j); // Move left if possible (left empty) and mark it as visited (to avoid infinite loops)}}}} // The solution should be at the bottom of the board now (in any empty slot)。

用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语言代码#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语言-五子棋游戏-代码-源代码-解释

C语言教程:简易五子棋程序收集于网络/* 纯用字符和数组编的五子棋,棋盘也是用字符画的。

、编了1上午了,主要是算法跟按键比较烦,发现有bug-- 按键速度过快会产生延时显示,可能是算法不好。

操作:玩家1:a,s,w,d(方向)空格(落子)玩家2:上、下、左、右回车(落子)ESC:退出编译测试环境:`*/#include <>#include <>#include <>#include <>/#define CRRU 0xbf /*右上角点197*/#define CRLU 0xda /*左上角点218*/#define CRLD 0xc0 /*左下角点192*/#define CRRD 0xd9 /*右下角点217*/<#define CRL 0xc3 /*左边195*/#define CRR 0xb4 /*右边190*/#define CRU 0xc2 /*上边194*/#define CRD 0xc1 /*下边193*/#define CR 0xc5 /*十字交叉点197*/-#define size 19char a[size][size];int i,j; int x=10;int y=3; return ;}void pressct(int m){switch(m){¥case 19200: //左if(i>0) {i--;x--;gotoxy(x,y);} break;case 20480: //下if(j<size-1){j++;y++;gotoxy(x,y);} break;case 18432: //上$if(j>0) {j--;y--;gotoxy(x,y);} break;case 19712: //右if(i<size-1){i++;x++;gotoxy(x,y);} break;case 7181: //回车if(a[i][j]!=CB&&a[i][j]!=CW){a[i][j]=CW;putch(CW);gotoxy(x,y);side=1;}break; {default: break;}return ;}>int judge(int pa) //判断是否胜利,胜利则返回1,否则返回0;// 其中i,j为当前的落子位;{int m;int sum=1;for(m=1;m<=i&&m<=j;m++) {if(a[i-m][j-m]!=pa) break;sum++;}for(m=1;m<(size-i)&&m<(size-j);m++) {if(a[i+m][j+m]!=pa) break;sum++;}if(sum>=5) return 1;else sum=1;for(m=1;m<=i;m++) {if(a[i-m][j]!=pa) break;sum++;}for(m=1;m<(size-j);m++) {if(a[i+m][j]!=pa) break;sum++;}if(sum>=5) return 1;else sum=1;for(m=1;m<=j;m++) {if(a[i][j-m]!=pa) break;sum++;}for(m=1;m<(size-j);m++) {if(a[i][j+m]!=pa) break;sum++;}if(sum>=5) return 1;else sum=1;for(m=1;m<=i&&m<(size-j);m++) {if(a[i-m][j+m]!=pa) break;sum++;}for(m=1;m<(size-i)&&m<=j;m++) {if(a[i+m][j-m]!=pa) break;sum++;}if(sum>=5) return 1;else return 0;本文章来自21视频教程网C语言教程:简易五子棋程序_C语言程序设计教程原文链接:。

c做的五子棋源码

c做的五子棋源码

using System;using;namespace wuziqi{class Program{public static void Main(string[] args){int maxsize = 19;//棋盘行列最大值int x, y;//用于定位下棋位置string x1, y1;//临时变量获取棋手输入的坐标值(此刻座标值属于字符串类型)int col_up = 0, col_down = 0;//用于统计输入座标值的上下相同棋子的个数int row_left = 0, row_right = 0;//用于统计输入座标值的左右相同棋子的个数int dia_left_up = 0, dia_left_down = 0, dia_right_up = 0, dia_right_down = 0;//对角线的英文拼写(diagonal line)用于统计对角线上相同的棋子数量int m;//临时变量,用于操纵判定相同棋子比较的次数int judgement;//用于获取判定是不是要终止棋局或继续棋局的变量string[,] qipan = new string[24, 24];//概念二维棋盘//初始化棋盘label0:{();//五子棋规那么("===============================================");("五子棋规那么说明如下:");("1、下棋坐标是从1-15");("2、下棋坐标值为整型数据类型");("3、下棋两边先组成五子连线者胜");("===============================================");for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){qipan[i, j] = "╋";}}}//输出棋盘for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){(qipan[i, j]);}("\n");}//走棋label2:{//甲方走棋("===============================================");("请甲方走棋(棋盘为15x15)");("x=");x1 = ();//判定输入的坐标值是不是为字母foreach(char ch in x1){if ((ch >'a'&&ch <'z')||(ch >'A'&&ch <'Z')){("你输入的坐标值非法!请正确输入你的坐标值");goto label2;}}//判定输入的棋子坐标是不是为空if (x1 == "" || x1 == null){("===============================================");("你输入的棋盘坐标为空!");goto label2;}("y=");y1 = ();//判定输入的坐标值是不是为字母foreach (char ch in y1){if ((ch > 'a' && ch < 'z') || (ch > 'A' && ch < 'Z')){("你输入的坐标值非法!请正确输入你的坐标值");goto label2;}}//判定输入的棋子坐标是不是为空if (y1 == "" || y1 == null){("===============================================");("你输入的棋盘坐标为空!");goto label2;}x = (x1);y = (y1);if (x >= 16 || y >= 16){("===============================================");("你输入的坐标超出了范围!");goto label2;}if (qipan[x + 4, y + 4] == "╋"){qipan[x + 4, y + 4] = "●";//清屏();//输出棋走后棋盘//五子棋规那么("===============================================");("五子棋规那么说明如下:");("1、下棋坐标是从1-15");("2、下棋坐标值为整型数据类型");("3、下棋两边先组成五子连线者胜");("===============================================");for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){(qipan[i, j]);}("\n");}}else{("===============================================");("此处已经有棋子了");goto label2;}// goto label;//判定是不是能够连成五子//判定上下col_down = 0;col_up = 0;row_left = 0;row_right = 0;dia_left_down = 0;dia_left_up = 0;dia_right_down = 0;dia_right_up = 0;for (m = 1; m <= 4; m++){//判定上下if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){col_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){col_down++;}//判定左右if (qipan[x + 4, y + 4] == qipan[x + 4, y - m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){row_left++;}if (qipan[x + 4, y + 4] == qipan[x + 4, y + m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){row_right++;}//判定左对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y - m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_left_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_left_down++;}//判定右对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_right_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y - m + 4] && qipan[x - m + 4, y + 4] != "○" && qipan[x - m + 4, y + 4] != "╋"){dia_right_down++;}}//判定甲方是不是有组成五子if(dia_left_down == 4 || dia_left_up == 4 || col_up == 4 || col_down == 4 || row_right == 4 || row_left == 4 || dia_right_up == 4 || dia_right_down == 4){("甲方先组成五子连线,恭喜甲获胜!");("请选择:");("1、继续下一局");("2、终止棋局");judgement = ());switch (judgement){case 1: goto label0;default: goto label3;}}if (((col_down + col_up) == 4) || ((row_left + row_right == 4)) || (dia_left_down + dia_left_up == 4)|| (dia_right_down + dia_right_up == 4)){("甲方先组成五子连线,恭喜甲获胜!");("请选择:");("1、继续下一局");("2、终止棋局");judgement = ());switch (judgement){case 1: goto label0;default: goto label3;}}//乙方走棋label1:{("===============================================");("请已方走棋(棋盘为15x15)");//判定坐标值是不是为空("x=");x1 = ();//判定输入的坐标值是不是为字母foreach (char ch in x1){if ((ch > 'a' && ch < 'z') || (ch > 'A' && ch < 'Z')){("你输入的坐标值非法!请正确输入你的坐标值");goto label1;}}if (x1 == "" || x1 == null){("你输入的棋盘坐标值为空!");}x = (x1);("y=");y1 = ();//判定输入的坐标值是不是为字母foreach (char ch in y1){if ((ch > 'a' && ch < 'z') || (ch > 'A' && ch < 'Z')){("你输入的坐标值非法!请正确输入你的坐标值");goto label1;}}//判定是不是为空if (y1 == "" || y1 == null){("你输入的棋盘坐标值为空!");goto label1;}y = (y1);//对输入的棋子进行判定是不是合法if (x >= 16 || y >= 16){("你输入的坐标超出了范围!");goto label1;}if (qipan[x + 4, y + 4] == "╋"){qipan[x + 4, y + 4] = "○";//输出棋走后棋盘//清屏();("===============================================");("五子棋规那么说明如下:");("1、下棋坐标是从1-15");("2、下棋坐标值为整型数据类型");("3、下棋两边先组成五子连线者胜");("===============================================");for (int i = 5; i <= maxsize; i++){for (int j = 5; j <= maxsize; j++){(qipan[i, j]);}("\n");}}else{("此处已经有棋子了");goto label1;}//判定是不是能够连成五子col_down = 0;col_up = 0;row_left = 0;row_right = 0;dia_left_down = 0;dia_left_up = 0;dia_right_down = 0;dia_right_up = 0;for (m = 1; m <= 4; m++){//判定上下if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + 4] && qipan[x - m + 4, y + 4] != "●" && qipan[x - m + 4, y + 4] != "╋"){col_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + 4] && qipan[x + m + 4, y + 4] != "●" && qipan[x + m + 4, y + 4] != "╋"){col_down++;}//判定左右if (qipan[x + 4, y + 4] == qipan[x + 4, y - m + 4] && qipan[x + 4, y - m + 4] != "●" && qipan[x + 4, y - m + 4] != "╋"){row_left++;}if (qipan[x + 4, y + 4] == qipan[x + 4, y + m + 4] && qipan[x + 4, y + m + 4] != "●" && qipan[x + 4, y + m + 4] != "╋"){row_right++;}//判定左对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y - m + 4] && qipan[x - m + 4, y - m + 4] != "●" && qipan[x - m + 4, y - m + 4] != "╋"){dia_left_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y + m + 4] && qipan[x + m + 4, y + m + 4] != "●" && qipan[x + m + 4, y + m + 4] != "╋"){dia_left_down++;}//判定右对角线if (qipan[x + 4, y + 4] == qipan[x - m + 4, y + m + 4] && qipan[x - m + 4, y + m + 4] != "●" && qipan[x - m + 4, y + m + 4] != "╋"){dia_right_up++;}if (qipan[x + 4, y + 4] == qipan[x + m + 4, y - m + 4] && qipan[x + m + 4, y - m + 4] != "●" && qipan[x + m + 4, y - m + 4] != "╋"){dia_right_down++;}}//判定甲方是不是有组成五子if (dia_left_down == 4 || dia_left_up == 4 || col_up == 4 || col_down == 4 || row_right == 4 || row_left == 4 || dia_right_up == 4 || dia_right_down == 4){("===============================================");("乙方先组成五子连线,恭喜乙获胜!");("请选择:");("1、继续下一局");("2、终止棋局");judgement = ());switch (judgement){case 1: goto label0;default: goto label3;}}if (((col_down + col_up) == 4) || ((row_left + row_right == 4)) || (dia_left_down + dia_left_up == 4) || (dia_right_down + dia_right_up == 4)){("===============================================");("乙方先组成五子连线,恭喜乙获胜!");("请选择:");("1、继续下一局");("2、终止棋局");judgement = ());switch (judgement){case 1: goto label0;default: goto label3;}}goto label2;}}//用于终止棋局label3:{}}}}。

五子棋游戏 C语言 代码 源代码

五子棋游戏 C语言 代码 源代码

#include <stdio.h>#include <stdlib.h>#define m 30int main (void){int count;//计数器算横纵行的结果int w,h;int u;int l;int i,size;//i声明步数。

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

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
return matrix[0][2];
return ' ';
}
{
int i;
/*检查横行*/
for(i=0;i<3;i++)
if(matrix[i][0]==matrix[i][1]&&
matrix[i][0]==matrix[i][2])return matrix[i][0];
/*检查竖行*/
for(i=0;i<3;i++)
if(matrix[0][i]==matrix[1][i]&&
if(matrix[i][j]== ' ')break;
if(matrix[i][j]== ' ')break;
}
if(i*j==9)
{
printf("draw\n");
exit(0);
}
else
ma}
/*在屏幕上显示棋盘*/
void disp_matrix(void)
if(done=='X') printf("You won!\n");
else printf("Computer won!\n");
disp_matrix();/*显示棋子的位置*/
return 0;
}
/*初始化棋盘*/
void init_matrix(void)
{
int i,j;
for(i=0;i<3;i++)
for (j=0;j<3;j++)
matrix[i][j] = ' ';
}
/*下棋者下*/
void get_player_move(void)
{
int x,y;
printf("Enter coordinates for your X:");
scanf("%d%d",&x,&y);
x--;y--;
if(matrix[x][y] != ' ')
init_matrix();
do{
disp_matrix();
get_player_move();
done = check();/*检测输赢*/
if(done!= ' ')break;/*赢了*/
get_computer_move();
done = check();/*检测输赢*/
}while (done == ' ');
# include <stdio.h>
# include <stdlib.h>
char matrix[3][3];/*定义游戏棋盘*/
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int main()
{
char done;
printf("This is the game of Tic Tac Toe.\n");
printf("You will be playing against the computer.\n");
done = ' ';
{
printf("Invalid move,try again.\n");
get_player_move();
}
else matrix[x][y]='X';
}
/*计算机下*/
void get_computer_move(void)
{
int i,j;
for(i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
int t;
for(t=0;t<3;t++)
{
printf("%c|%c|%c",matrix[t][0],matrix[t][1],matrix[t][2]);
if(t!=2)printf("\n-|-|-\n");
}
printf("\n");
}
/*看谁是赢着*/
char check(void)
matrix[0][i]==matrix[2][i])return matrix[0][i];
/*检查对角线*/
if(matrix[0][0]==matrix[1][1]&&matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1]&&matrix[1][1]==matrix[2][0])
相关文档
最新文档