简单的Tic-Tac-Toe

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的Tic-Tac-Toe相关的知识,希望对你有一定的参考价值。

Not my code - see header in source
  1. /*
  2. ****************************************
  3. **********KRIZCI IN KROZCI**************
  4. ******Tic Tac Toe-English version*******
  5. **********(C) Matic Miklavcic***********
  6. *******[email protected]*********
  7. *+*+*+*+*You are free to mod it*+*+*+*+*
  8. ****************************************
  9. Programmed in DevC++, 10. OCT 2009 *****
  10. */
  11. #include <cstdlib>//libraries
  12. #include <iostream>
  13. #include <string>
  14. #include <windows.h>//-------
  15. using namespace std;
  16. char A,B,C;
  17. char D,E,F;
  18. char G,H,I;
  19. bool Player1win,Player2win;
  20. string ime1,ime2;
  21. //Maximise the console, if player chooses so
  22. BOOL NT_SetConsoleDisplayMode(HANDLE hOutputHandle, DWORD dwNewMode)
  23. {
  24. typedef BOOL (WINAPI *SCDMProc_t) (HANDLE, DWORD, LPDWORD);
  25. SCDMProc_t SetConsoleDisplayMode;
  26. HMODULE hKernel32;
  27. BOOL bFreeLib = FALSE, ret;
  28. const char KERNEL32_NAME[] = "kernel32.dll";
  29.  
  30. hKernel32 = GetModuleHandleA(KERNEL32_NAME);
  31. if (hKernel32 == NULL)
  32. {
  33. hKernel32 = LoadLibraryA(KERNEL32_NAME);
  34. if (hKernel32 == NULL)
  35. return FALSE;
  36.  
  37. bFreeLib = true;
  38. }
  39.  
  40. SetConsoleDisplayMode =
  41. (SCDMProc_t)GetProcAddress(hKernel32, "SetConsoleDisplayMode");
  42. if (SetConsoleDisplayMode == NULL)
  43. {
  44. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  45. ret = FALSE;
  46. }
  47. else
  48. {
  49. DWORD dummy;
  50. ret = SetConsoleDisplayMode(hOutputHandle, dwNewMode, &dummy);
  51. }
  52.  
  53. if (bFreeLib)
  54. FreeLibrary(hKernel32);
  55.  
  56. return ret;
  57. }
  58. void KonecIgre()//play the sad melody :((
  59. {
  60. Beep(784,500);
  61. Beep(738,500);
  62. Beep(684,500);
  63. Beep(644,1000);
  64. }
  65. void Pesmica()//play the victorious melody :))
  66. {
  67. Beep(523,200);//use Beep(frequency,time); to play a sound in internal speaker
  68. Sleep(1);//stop the program for (time in ms)
  69. Beep(523,200);
  70. Sleep(1);
  71. Beep(523,200);
  72. Sleep(1);
  73. Beep(659,700);
  74. Beep(784,500);
  75. Beep(523,200);
  76. Sleep(1);
  77. Beep(523,200);
  78. Sleep(1);
  79. Beep(523,200);
  80. Sleep(1);
  81. Beep(659,700);
  82. Beep(784,500);
  83. Sleep(800);
  84. Beep(784,400);
  85. Beep(884,200);
  86. Beep(784,200);
  87. Beep(687,200);
  88. Beep(659,200);
  89. Beep(579,200);
  90. Beep(519,400);
  91. }
  92.  
  93.  
  94. //write out help
  95. int Help()
  96. {
  97. system("CLS");
  98. cout<<"Help ----- Each player must enter letters from A-G, until someone is" +
  99. "victorious or all the fields are filled. ------- By Mickey (this screen" +
  100. "vanishes in 7 secs)";
  101. }
  102.  
  103.  
  104. //write out Menu
  105. int Menu()
  106. {
  107. zacetek:
  108. int izbira;
  109. cout<<"Hello! ----- 1.Start the game 2.Help 3.Exit >> ";
  110. cin>>izbira;
  111. switch(izbira)
  112. {
  113. case 1:
  114. goto point;
  115. break;
  116. case 2:
  117. Help();
  118. Sleep(7000);
  119. system("CLS");
  120. goto zacetek;
  121. break;
  122. case 3:
  123. exit(1);
  124. break;
  125. point:
  126. system("CLS");
  127. }
  128. }
  129.  
  130.  
  131.  
  132. //display the table
  133. void Izrisi()
  134. {
  135. system("CLS");
  136. A=toupper(A);
  137. B=toupper(B);
  138. C=toupper(C);
  139. D=toupper(D);
  140. E=toupper(E);
  141. F=toupper(F);
  142. G=toupper(G);
  143. H=toupper(H);
  144. I=toupper(I);
  145. cout<<"|---|---|---|"<<endl
  146. <<"| "<<A<<" | "<<B<<" | "<<C<<" |"<<endl
  147. <<"|---|---|---|"<<endl
  148. <<"| "<<D<<" | "<<E<<" | "<<F<<" |"<<endl
  149. <<"|---|---|---|"<<endl
  150. <<"| "<<G<<" | "<<H<<" | "<<I<<" |"<<endl
  151. <<"|---|---|---|"<<endl<<endl;
  152. }
  153. //display the stars when victorious
  154. void Zvezdice()
  155. {
  156. cout<<" * * * * * *"
  157. <<" * * * * * *"
  158. <<" * * * * * *"
  159. <<" * * * * * * * * * *"
  160. <<"* * * ** * * * ";
  161. }
  162.  
  163.  
  164. //write out the winner
  165. void Zmaga(int pNumb=0)
  166. {
  167. switch(pNumb)
  168. {
  169. case 1://if player 1 won
  170. cout<<ime1<<" WINS!!!!!! Congrats! ";
  171. Zvezdice();
  172. Pesmica();
  173. break;
  174. case 2://if player 2 won
  175. cout<<ime2<<" WINS!!!!!! Congrats! ";
  176. Zvezdice();
  177. Pesmica();
  178. break;
  179. }
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. //check for winner
  187. int PreveriZmag()
  188. {
  189. //check all X
  190. //Horizontal
  191. if(A=='X'&&B=='X'&&C=='X')
  192. {
  193. Player1win=true;
  194. }
  195. if(D=='X'&&E=='X'&&F=='X')
  196. {
  197. Player1win=true;
  198. }
  199. if(G=='X'&&H=='X'&&I=='X')
  200. {
  201. Player1win=true;
  202. }
  203. //Vertical
  204. if(A=='X'&&D=='X'&&G=='X')
  205. {
  206. Player1win=true;
  207. }
  208. if(B=='X'&&E=='X'&&H=='X')
  209. {
  210. Player1win=true;
  211. }
  212. if(C=='X'&&F=='X'&&I=='X')
  213. {
  214. Player1win=true;
  215. }
  216. //X-wise
  217. if(A=='X'&&E=='X'&&I=='X')
  218. {
  219. Player1win=true;
  220. }
  221. if(G=='X'&&E=='X'&&C=='X')
  222. {
  223. Player1win=true;
  224. }
  225. /* start of O-s
  226.   check it all over again
  227.   _-_-_-_-_-_-_-_-_-_-_-*/
  228. //Horisontal
  229. if(A=='O'&&B=='O'&&C=='O')
  230. {
  231. Player2win=true;
  232. }
  233. if(D=='O'&&E=='O'&&F=='O')
  234. {
  235. Player2win=true;
  236. }
  237. if(G=='O'&&H=='O'&&I=='O')
  238. {
  239. Player2win=true;
  240. }
  241. //Vertical
  242. if(A=='O'&&D=='O'&&G=='O')
  243. {
  244. Player2win=true;
  245. }
  246. if(B=='O'&&E=='O'&&H=='O')
  247. {
  248. Player2win=true;
  249. }
  250. if(C=='O'&&F=='O'&&I=='O')
  251. {
  252. Player2win=true;
  253. }
  254. //X-wise
  255. if(A=='O'&&E=='O'&&I=='O')
  256. {
  257. Player2win=true;
  258. }
  259. if(G=='O'&&E=='O'&&C=='O')
  260. {
  261. Player2win=true;
  262. }
  263. /*check both bool-s ->TRUE,
  264.   if yes, continue, if no,
  265.   cout the winner*/
  266.  
  267.  
  268. if (Player1win==true)
  269. {//player 1 wins
  270. system("CLS");
  271. Zmaga(1);
  272. return 1;
  273.  
  274.  
  275.  
  276. }
  277. else
  278. {//if player 1 didnt won, check the player 2
  279. if (Player2win==true)//if not, continue the game
  280. {
  281. system("CLS");
  282. Zmaga(2);
  283.  
  284. return 1;
  285. }
  286. }
  287.  
  288.  
  289.  
  290. }
  291.  
  292.  
  293.  
  294.  
  295.  
  296. //wrong X or O location, try again
  297. void Napaka()
  298. {
  299. system("CLS");
  300. cout<<"Oops, there's allready an X or O. Try again!";
  301. Sleep(2000);
  302. system("CLS");
  303. Izrisi();
  304. }
  305. //player1 - play!
  306. void Igralec1()
  307. {//main function of player 1!!!!
  308. char choice;
  309. start:
  310. cout<<ime1<<" (X): ";
  311. cin>>choice;
  312. choice=toupper(choice);
  313. switch(choice)
  314. {
  315. case 'A':
  316. if(A!='O')A='X';
  317. else
  318. {
  319. Napaka();
  320. goto start;
  321. }
  322. break;
  323. case 'B':
  324. if(B!='O')B='X';
  325. else
  326. {
  327. Napaka();
  328. goto start;
  329. }
  330. break;
  331. case 'C':
  332. if(C!='O')C='X';
  333. else
  334. {
  335. Napaka();
  336. goto start;
  337. }
  338. break;
  339. case 'D':
  340. if(D!='O')D='X';
  341. else
  342. {
  343. Napaka();
  344. goto start;
  345. }
  346. break;
  347. case 'E':
  348. if(E!='O')E='X';
  349. else
  350. {
  351. Napaka();
  352. goto start;
  353. }
  354. break;
  355. case 'F':
  356. if(F!='O')F='X';
  357. else
  358. {
  359. Napaka();
  360. goto start;
  361. }
  362. break;
  363. case 'G':
  364. if(G!='O')G='X';
  365. else
  366. {
  367. Napaka();
  368. goto start;
  369. }
  370. break;
  371. case 'H':
  372. if(H!='O')H='X';
  373. else
  374. {
  375. Napaka();
  376. goto start;
  377. }
  378. break;
  379. case 'I':
  380. if(I!='O')I='X';
  381. else
  382. {
  383. Napaka();
  384. goto start;
  385. }
  386. break;
  387. default:
  388. system("CLS");
  389. cout<<"Oops, wrong letter. Try again. ";
  390. Sleep(2000);
  391. Izrisi();
  392. goto start;
  393. break;
  394. }
  395. Izrisi();
  396. }
  397. //player 2- play!
  398. void Igralec2()
  399. {//main function of player 2!
  400. char choice;
  401. start:
  402. cout<<ime2<<" (O): ";
  403. cin>>choice;
  404. choice=toupper(choice);
  405. switch(choice)
  406. {
  407. case 'A':
  408. if(A!='X')A='O';
  409. else
  410. {
  411. Napaka();
  412. goto start;
  413. }
  414. break;
  415. case 'B':
  416. if(B!='X')B='O';
  417. else
  418. {
  419. Napaka();
  420. goto start;
  421. }
  422. break;
  423. case 'C':
  424. if(C!='X')C='O';
  425. else
  426. {
  427. Napaka();
  428. goto start;
  429. }
  430. break;
  431. case 'D':
  432. if(D!='X')D='O';
  433. else
  434. {
  435. Napaka();
  436. goto start;
  437. }
  438. break;
  439. case 'E':
  440. if(E!='X')E='O';
  441. else
  442. {
  443. Napaka();
  444. goto start;
  445. }
  446. break;
  447. case 'F':
  448. if(F!='X')F='O';
  449. else
  450. {
  451. Napaka();
  452. goto start;
  453. }
  454. break;
  455. case 'G':
  456. if(G!='X')G='O';
  457. else
  458. {
  459. Napaka();
  460. goto start;
  461. }
  462. break;
  463. case 'H':
  464. if(H!='X')H='O';
  465. else
  466. {
  467. Napaka();
  468. goto start;
  469. }
  470. break;
  471. case 'I':
  472. if(I!='X')I='O';
  473. else
  474. {
  475. Napaka();
  476. goto start;
  477. }
  478. break;
  479. default://if player 1 enters the wrong letter,warn him and try again
  480. system("CLS");
  481. cout<<"Oops, wrong letter. Try again! ";
  482. Sleep(2000);
  483. Izrisi();
  484. goto start;
  485. break;
  486. }
  487. Izrisi();
  488. }
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498. //Main function- combines all subfunctions!
  499. int main()
  500. {
  501. int celZas;
  502. cout<<"Start in full screen? 1.Yes 2.No >>";
  503. cin>>celZas;
  504. if(celZas==1)
  505. {//Full screen-yes
  506. NT_SetConsoleDisplayMode( GetStdHandle( STD_OUTPUT_HANDLE ), 1 );
  507. }
  508. system("CLS");
  509. cout<<"Tic Tac Toe ";
  510. Menu();
  511. cout<<"Name of player 1: ";
  512. cin>>ime1;
  513. cout<<"Name of player 2: ";
  514. cin>>ime2;
  515. int retPreveri;
  516. Player1win=false;
  517. Player2win=false;
  518. char choice;
  519. A='A';//set all the variables to their default value
  520. B='B';
  521. C='C';
  522. D='D';
  523. E='E';
  524. F='F';
  525. G='G';
  526. H='H';
  527. I='I';
  528. Izrisi();
  529. Igralec1();//Game starts!!!!
  530. retPreveri=PreveriZmag();
  531. if(retPreveri==1)goto end;
  532. Igralec2();
  533. retPreveri=PreveriZmag();
  534. if(retPreveri==1)goto end;
  535. Igralec1();
  536. retPreveri=PreveriZmag();
  537. if(retPreveri==1)goto end;
  538. Igralec2();
  539. retPreveri=PreveriZmag();
  540. if(retPreveri==1)goto end;
  541. Igralec1();
  542. retPreveri=PreveriZmag();
  543. if(retPreveri==1)goto end;
  544. Igralec2();
  545. retPreveri=PreveriZmag();
  546. if(retPreveri==1)goto end;
  547. Igralec1();
  548. retPreveri=PreveriZmag();
  549. if(retPreveri==1)goto end;
  550. Igralec2();
  551. retPreveri=PreveriZmag();//------
  552. if(retPreveri==1)goto end;
  553. system("CLS");
  554. cout<<ime1<<", "<<ime2<<",
  555. game is over!!! No-one won! Better luck next time."<<endl<<endl;//if noone won
  556. KonecIgre();//play the sad song, as no-one won :((
  557. end://end
  558. system("PAUSE");
  559. }
  560. /*
  561. Here are the translations of some functions, as they were written in slovenian language
  562. (don't feel like retyping the whole game ;)
  563. ime->name
  564. KonecIgre->end of the game
  565. Igralec(1,2)->player
  566. PreveriZmag->check for winner
  567. Izrisi->draw
  568. Zvezdice->stars
  569. Napaka->Error
  570. Zmaga->victory
  571. -------------
  572. Have fun with the game,
  573. your Mickey ("/(^_^)
  574. */

以上是关于简单的Tic-Tac-Toe的主要内容,如果未能解决你的问题,请参考以下文章

[CodeForces754B]Ilya and tic-tac-toe game

bfs codeforces 754B Ilya and tic-tac-toe game

Tic-tac-toe使用python tkinter

python tic-tac-toe.py

将识别列表中的空字符串并在那里打印标记/符号的函数(Python Tic-Tac-Toe)

Design Tic-Tac-Toe 解答