Marcos_Carvalho

jogo da velha

Apr 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.93 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include <stdio.h>
  3. #include<conio.h>
  4.  
  5. char vel[][4] = { {' ',' ',' '},{' ',' ',' '}, {' ',' ',' '} };
  6. int k, z;
  7. int j1;
  8. int j2;
  9. int you=0;
  10. int pc=0;
  11.  
  12. int imprime(void);
  13. int lerp(void);
  14. int troca(int s, int t);
  15. int xwin(void);
  16. int owin(void);
  17. int jogapc(void);
  18. int resetar(void);
  19.  
  20. int imprime() {
  21. printf("\n");
  22. printf(" Voc\x88: %d / Computador: %d \n\n", you, pc);
  23. printf(" C1 C2 C3\n\n");
  24. for(z=0; z<3; z++) {
  25. if(z==0) printf("L1");
  26. if(z==1) printf("L2");
  27. if(z==2) printf("L3");
  28. for(k=0; k<3; k++) {
  29. if(k==0) printf(" %c ", vel[z][k]);
  30. else printf(" %c ", vel[z][k]);
  31. if(k<2) printf("|");
  32. }
  33. if(z<2) printf("\n -----------\n");
  34. }
  35. printf("\n\n");
  36. }
  37.  
  38.  
  39.  
  40. int troca(int s, int t) {
  41.  
  42. if(vel[s-49][t-49]=='x' || vel[s-49][t-49]=='o')
  43. {
  44. printf("\n\nEspa\x87o j\xA0 preenchido\n");
  45. lerp();
  46. }
  47. if(vel[s-49][t-49]==' ') vel[s-49][t-49]='x';
  48.  
  49. }
  50.  
  51.  
  52.  
  53. int lerp() {
  54. int i, j;
  55.  
  56. printf("\nDigite as coordenadas de sua jogada:\n\nL:");
  57.  
  58. i=getche();
  59.  
  60. printf("\nC:");
  61. j=getche();
  62.  
  63. if(i>=49 && i<52 && j>=49 && j<52) troca(i,j);
  64. else {
  65. printf("\n\nERRO!\n\nCoordenada inv\xA0lida.\n\n");
  66. lerp();
  67. }
  68.  
  69. }
  70.  
  71.  
  72.  
  73. int xwin() {
  74. printf("\nVoc\x88 GANHOU!\n");
  75. you++;
  76. resetar();
  77. }
  78.  
  79.  
  80. int owin() {
  81. printf("\nVoc\x88 PERDEU!\n");
  82. pc++;
  83. resetar();
  84. }
  85.  
  86.  
  87.  
  88. int jogapc() {
  89.  
  90. int x=1;
  91.  
  92. if(vel[2][2]=='x' && vel[0][2]=='x')
  93. if(vel[1][2]==' ' && x)
  94. {
  95. vel[1][2]='o';
  96. x=0;
  97. }
  98.  
  99. if(vel[2][0]=='x' && vel[0][2]=='x')
  100. if(vel[1][0]==' ' && x)
  101. {
  102. vel[1][0]='o';
  103. x=0;
  104. }
  105.  
  106. /*ATAQUES*/
  107.  
  108. /* ataques => diagonal secundária */
  109.  
  110. if(vel[2][0]=='o' && vel[1][1]=='o')
  111. if(vel[0][2]==' ' && x)
  112. {
  113. vel[0][2]='o';
  114. x=0;
  115. }
  116. if(vel[0][2]=='o' && vel[2][2]=='o')
  117. if(vel[2][0]==' ' && x)
  118. {
  119. vel[2][0]='o';
  120. x=0;
  121. }
  122. if(vel[2][0]=='o' && vel[0][2]=='o')
  123. if(vel[1][1]==' ' && x)
  124. {
  125. vel[1][1]='o';
  126. x=0;
  127. }
  128.  
  129. /* ataques => diagonal principal */
  130.  
  131. if(vel[0][0]=='o' && vel[1][1]=='o')
  132. if(vel[2][2]==' ' && x)
  133. {
  134. vel[2][2]='o';
  135. x=0;
  136. }
  137. if(vel[1][1]=='o' && vel[2][2]=='o')
  138. if(vel[0][0]==' ' && x)
  139. {
  140. vel[0][0]='o';
  141. x=0;
  142. }
  143. if(vel[0][0]=='o' && vel[2][2]=='o')
  144. if(vel[1][1]==' ' && x)
  145. {
  146. vel[1][1]='o';
  147. x=0;
  148. }
  149.  
  150. /* ataques => coluna 1: */
  151.  
  152. if(vel[0][0]=='o' && vel[1][0]=='o')
  153. if(vel[2][0]==' ' && x)
  154. {
  155. vel[2][0]='o';
  156. x=0;
  157. }
  158. if(vel[1][0]=='o' && vel[2][0]=='o')
  159. if(vel[0][0]==' ' && x)
  160. {
  161. vel[0][0]='o';
  162. x=0;
  163. }
  164. if(vel[0][0]=='o' && vel[2][0]=='o')
  165. if(vel[1][0]==' ' && x)
  166. {
  167. vel[1][0]='o';
  168. x=0;
  169. }
  170.  
  171. /* ataques => coluna 2: */
  172.  
  173. if(vel[0][1]=='o' && vel[1][1]=='o')
  174. if(vel[2][1]==' ' && x)
  175. {
  176. vel[2][1]='o';
  177. x=0;
  178. }
  179. if(vel[1][1]=='o' && vel[2][1]=='o')
  180. if(vel[0][1]==' ' && x)
  181. {
  182. vel[0][1]='o';
  183. x=0;
  184. }
  185. if(vel[0][1]=='o' && vel[2][1]=='o')
  186. if(vel[1][1]==' ' && x)
  187. {
  188. vel[1][1]='o';
  189. x=0;
  190. }
  191.  
  192. /* ataques => coluna 3: */
  193.  
  194. if(vel[0][2]=='o' && vel[1][2]=='o')
  195. if(vel[2][2]==' ' && x)
  196. {
  197. vel[2][2]='o';
  198. x=0;
  199. }
  200. if(vel[1][2]=='o' && vel[2][2]=='o')
  201. if(vel[0][2]==' ' && x)
  202. {
  203. vel[0][2]='o';
  204. x=0;
  205. }
  206. if(vel[0][2]=='o' && vel[2][2]=='o')
  207. if(vel[1][2]==' ' && x)
  208. {
  209. vel[1][2]='o';
  210. x=0;
  211. }
  212.  
  213. /* ataques => linha 1: */
  214.  
  215. if(vel[0][0]=='o' && vel[0][1]=='o')
  216. if(vel[0][2]==' ' && x)
  217. {
  218. vel[0][2]='o';
  219. x=0;
  220. }
  221. if(vel[0][1]=='o' && vel[0][2]=='o')
  222. if(vel[0][0]==' ' && x)
  223. {
  224. vel[0][0]='o';
  225. x=0;
  226. }
  227. if(vel[0][0]=='o' && vel[0][2]=='o')
  228. if(vel[0][1]==' ' && x)
  229. {
  230. vel[0][1]='o';
  231. x=0;
  232. }
  233.  
  234. /* ataques => linha 2: */
  235.  
  236. if(vel[1][0]=='o' && vel[1][1]=='o')
  237. if(vel[1][2]==' ' && x)
  238. {
  239. vel[1][2]='o';
  240. x=0;
  241. }
  242. if(vel[1][1]=='o' && vel[1][2]=='o')
  243. if(vel[1][0]==' ' && x)
  244. {
  245. vel[1][0]='o';
  246. x=0;
  247. }
  248. if(vel[1][0]=='o' && vel[1][2]=='o')
  249. if(vel[1][1]==' ' && x)
  250. {
  251. vel[1][1]='o';
  252. x=0;
  253. }
  254.  
  255. /* ataues => linha 3: */
  256.  
  257. if(vel[2][0]=='o' && vel[2][1]=='o')
  258. if(vel[2][2]==' ' && x)
  259. {
  260. vel[2][2]='o';
  261. x=0;
  262. }
  263. if(vel[2][1]=='o' && vel[2][2]=='o')
  264. if(vel[2][0]==' ' && x)
  265. {
  266. vel[2][0]='o';
  267. x=0;
  268. }
  269. if(vel[2][0]=='o' && vel[2][2]=='o')
  270. if(vel[2][1]==' ' && x)
  271. {
  272. vel[2][1]='o';
  273. x=0;
  274. }
  275.  
  276. /* JOGADA 1 */
  277.  
  278. if(j1 && x)
  279. {
  280. if(vel[0][0]=='x')
  281. {
  282. vel[1][1]='o';
  283. x=0;
  284. j1=0;
  285. }
  286. if(vel[1][1]=='x')
  287. {
  288. vel[2][0]='o';
  289. x=0;
  290. j1=0;
  291. }
  292. if(vel[2][2]=='x')
  293. {
  294. vel[1][1]='o';
  295. x=0;
  296. j1=0;
  297. }
  298. if(vel[0][1]=='x')
  299. {
  300. vel[0][2]='o';
  301. x=0;
  302. j1=0;
  303. }
  304. if(vel[0][2]=='x')
  305. {
  306. vel[1][1]='o';
  307. x=0;
  308. j1=0;
  309. }
  310. if(vel[1][0]=='x')
  311. {
  312. vel[0][2]='o';
  313. x=0;
  314. j1=0;
  315. }
  316. if(vel[1][2]=='x')
  317. {
  318. vel[2][0]='o';
  319. x=0;
  320. j1=0;
  321. }
  322. if(vel[2][0]=='x')
  323. {
  324. vel[1][1]='o';
  325. x=0;
  326. j1=0;
  327. }
  328. if(vel[2][1]=='x')
  329. {
  330. vel[0][0]='o';
  331. x=0;
  332. j1=0;
  333. }
  334. }
  335.  
  336. /* DEFESAS */
  337.  
  338. /* defesas => diagonal secundária */
  339.  
  340. if(vel[2][0]=='x' && vel[1][1]=='x')
  341. if(vel[0][2]==' ' && x)
  342. {
  343. vel[0][2]='o';
  344. x=0;
  345. }
  346. if(vel[0][2]=='x' && vel[2][2]=='x')
  347. if(vel[2][0]==' ' && x)
  348. {
  349. vel[2][0]='o';
  350. x=0;
  351. }
  352. if(vel[2][0]=='x' && vel[0][2]=='x')
  353. if(vel[1][1]==' ' && x)
  354. {
  355. vel[1][1]='o';
  356. x=0;
  357. }
  358.  
  359. /* defesas => diagonal principal */
  360.  
  361. if(vel[0][0]=='x' && vel[1][1]=='x')
  362. if(vel[2][2]==' ' && x)
  363. {
  364. vel[2][2]='o';
  365. x=0;
  366. }
  367. if(vel[1][1]=='x' && vel[2][2]=='x')
  368. if(vel[0][0]==' ' && x)
  369. {
  370. vel[0][0]='o';
  371. x=0;
  372. }
  373. if(vel[0][0]=='x' && vel[2][2]=='x')
  374. if(vel[1][1]==' ' && x)
  375. {
  376. vel[1][1]='o';
  377. x=0;
  378. }
  379.  
  380. /* defesas => coluna 1: */
  381.  
  382. if(vel[0][0]=='x' && vel[1][0]=='x')
  383. if(vel[2][0]==' ' && x)
  384. {
  385. vel[2][0]='o';
  386. x=0;
  387. }
  388. if(vel[1][0]=='x' && vel[2][0]=='x')
  389. if(vel[0][0]==' ' && x)
  390. {
  391. vel[0][0]='o';
  392. x=0;
  393. }
  394. if(vel[0][0]=='x' && vel[2][0]=='x')
  395. if(vel[1][0]==' ' && x)
  396. {
  397. vel[1][0]='o';
  398. x=0;
  399. }
  400.  
  401. /* defesas => coluna 2: */
  402.  
  403. if(vel[0][1]=='x' && vel[1][1]=='x')
  404. if(vel[2][1]==' ' && x)
  405. {
  406. vel[2][1]='o';
  407. x=0;
  408. }
  409. if(vel[1][1]=='x' && vel[2][1]=='x')
  410. if(vel[0][1]==' ' && x)
  411. {
  412. vel[0][1]='o';
  413. x=0;
  414. }
  415. if(vel[0][1]=='x' && vel[2][1]=='x')
  416. if(vel[1][1]==' ' && x)
  417. {
  418. vel[1][1]='o';
  419. x=0;
  420. }
  421.  
  422. /* defesas => coluna 3: */
  423.  
  424. if(vel[0][2]=='x' && vel[1][2]=='x')
  425. if(vel[2][2]==' ' && x)
  426. {
  427. vel[2][2]='o';
  428. x=0;
  429. }
  430. if(vel[1][2]=='x' && vel[2][2]=='x')
  431. if(vel[0][2]==' ' && x)
  432. {
  433. vel[0][2]='o';
  434. x=0;
  435. }
  436. if(vel[0][2]=='x' && vel[2][2]=='x')
  437. if(vel[1][2]==' ' && x)
  438. {
  439. vel[1][2]='o';
  440. x=0;
  441. }
  442.  
  443. /* defesas => linha 1: */
  444.  
  445. if(vel[0][0]=='x' && vel[0][1]=='x')
  446. if(vel[0][2]==' ' && x)
  447. {
  448. vel[0][2]='o';
  449. x=0;
  450. }
  451. if(vel[0][1]=='x' && vel[0][2]=='x')
  452. if(vel[0][0]==' ' && x)
  453. {
  454. vel[0][0]='o';
  455. x=0;
  456. }
  457. if(vel[0][0]=='x' && vel[0][2]=='x')
  458. if(vel[0][1]==' ' && x)
  459. {
  460. vel[0][1]='o';
  461. x=0;
  462. }
  463.  
  464. /* defesas => linha 2: */
  465.  
  466. if(vel[1][0]=='x' && vel[1][1]=='x')
  467. if(vel[1][2]==' ' && x)
  468. {
  469. vel[1][2]='o';
  470. x=0;
  471. }
  472. if(vel[1][1]=='x' && vel[1][2]=='x')
  473. if(vel[1][0]==' ' && x)
  474. {
  475. vel[1][0]='o';
  476. x=0;
  477. }
  478. if(vel[1][0]=='x' && vel[1][2]=='x')
  479. if(vel[1][1]==' ' && x)
  480. {
  481. vel[1][1]='o';
  482. x=0;
  483. }
  484.  
  485. /* defesas => linha 3: */
  486.  
  487. if(vel[2][0]=='x' && vel[2][1]=='x')
  488. if(vel[2][2]==' ' && x)
  489. {
  490. vel[2][2]='o';
  491. x=0;
  492. }
  493. if(vel[2][1]=='x' && vel[2][2]=='x')
  494. if(vel[2][0]==' ' && x)
  495. {
  496. vel[2][0]='o';
  497. x=0;
  498. }
  499. if(vel[2][0]=='x' && vel[2][2]=='x')
  500. if(vel[2][1]==' ' && x)
  501. {
  502. vel[2][1]='o';
  503. x=0;
  504. }
  505.  
  506. /* JOGADA 2 */
  507.  
  508. if(vel[0][0]=='x' && vel[1][1]=='x' && vel[2][2]=='o')
  509. {
  510. if(x)
  511. {
  512. if(vel[2][0]==' ')
  513. {
  514. vel[2][0]='o';
  515. x=0;
  516. }
  517. else
  518. {
  519. if(vel[0][2]==' ')
  520. {
  521. vel[0][2]='o';
  522. x=0;
  523. }
  524. }
  525. }
  526. }
  527.  
  528. if(vel[0][0]=='x' && vel[2][1]=='x')
  529. {
  530. if(x && j2)
  531. {
  532. if(vel[2][0]==' ')
  533. {
  534. vel[2][0]='o';
  535. x=0;
  536. j2=0;
  537. }
  538. else
  539. {
  540. if(vel[1][0]==' ')
  541. {
  542. vel[1][0]='o';
  543. x=0;
  544. j2=0;
  545. }
  546. }
  547. }
  548. }
  549.  
  550. if(vel[0][0]=='x' && vel[1][2]=='x')
  551. {
  552. if(x && j2)
  553. {
  554. if(vel[0][2]==' ')
  555. {
  556. vel[0][2]='o';
  557. x=0;
  558. j2=0;
  559. }
  560. else
  561. {
  562. if(vel[0][1]==' ')
  563. {
  564. vel[0][1]='o';
  565. x=0;
  566. j2=0;
  567. }
  568. }
  569. }
  570. }
  571.  
  572. if(vel[0][1]=='x' && vel[1][0]=='x')
  573. {
  574. if(x && j2)
  575. {
  576. if(vel[0][0]==' ')
  577. {
  578. vel[0][0]='o';
  579. x=0;
  580. j2=0;
  581. }
  582. else
  583. {
  584. if(vel[1][1]==' ')
  585. {
  586. vel[1][1]='o';
  587. x=0;
  588. j2=0;
  589. }
  590. }
  591. }
  592. }
  593.  
  594. if(vel[0][1]=='x' && vel[2][0]=='x')
  595. {
  596. if(x && j2)
  597. {
  598. if(vel[1][1]==' ')
  599. {
  600. vel[1][1]='o';
  601. x=0;
  602. j2=0;
  603. }
  604. else
  605. {
  606. if(vel[0][0]==' ')
  607. {
  608. vel[0][0]='o';
  609. x=0;
  610. j2=0;
  611. }
  612. }
  613. }
  614. }
  615.  
  616. if(vel[0][1]=='x' && vel[1][2]=='x')
  617. {
  618. if(x && j2)
  619. {
  620. if(vel[0][2]==' ')
  621. {
  622. vel[0][2]='o';
  623. x=0;
  624. j2=0;
  625. }
  626. else
  627. {
  628. if(vel[1][1]==' ')
  629. {
  630. vel[1][1]='o';
  631. x=0;
  632. j2=0;
  633. }
  634. }
  635. }
  636. }
  637.  
  638. if(vel[0][1]=='x' && vel[2][2]=='x')
  639. {
  640. if(x && j2)
  641. {
  642. if(vel[0][2]==' ')
  643. {
  644. vel[0][2]='o';
  645. x=0;
  646. j2=0;
  647. }
  648. else
  649. {
  650. if(vel[0][0]==' ')
  651. {
  652. vel[0][0]='o';
  653. x=0;
  654. j2=0;
  655. }
  656. }
  657. }
  658. }
  659.  
  660. if(vel[0][2]=='x' && vel[1][0]=='x')
  661. {
  662. if(x && j2)
  663. {
  664. if(vel[0][0]==' ')
  665. {
  666. vel[0][0]='o';
  667. x=0;
  668. j2=0;
  669. }
  670. else
  671. {
  672. if(vel[1][1]==' ')
  673. {
  674. vel[1][1]='o';
  675. x=0;
  676. j2=0;
  677. }
  678. }
  679. }
  680. }
  681.  
  682. if(vel[0][2]=='x' && vel[2][1]=='x')
  683. {
  684. if(x && j2)
  685. {
  686. if(vel[2][2]==' ')
  687. {
  688. vel[2][2]='o';
  689. x=0;
  690. j2=0;
  691. }
  692. else
  693. {
  694. if(vel[1][1]==' ')
  695. {
  696. vel[1][1]='o';
  697. x=0;
  698. j2=0;
  699. }
  700. }
  701. }
  702. }
  703.  
  704. if(x)
  705. {
  706. int d=0;
  707. int f=0;
  708.  
  709. for(d=0; d<3; d++)
  710. {
  711. for(f=0; f<3; f++)
  712. {
  713. if(vel[d][f]==' ')
  714. {
  715. vel[d][f]='o';
  716. x=0;
  717. }
  718. if(x==0) break;
  719. }
  720. if(x==0) break;
  721. }
  722.  
  723. if(x)
  724. {
  725. printf("\nEMPATE.\n");
  726. resetar();
  727. }
  728.  
  729.  
  730.  
  731. }
  732.  
  733.  
  734. }
  735.  
  736.  
  737.  
  738. int resetar()
  739. {
  740. int d, f;
  741. for(d=0; d<3; d++)
  742. {
  743. for(f=0; f<3; f++)
  744. {
  745. vel[d][f]=' ';
  746. }
  747. }
  748.  
  749. }
  750.  
  751.  
  752. int main() {
  753. int t;
  754.  
  755. imprime();
  756.  
  757. j1=1;
  758. j2=1;
  759.  
  760. do{
  761.  
  762. lerp();
  763.  
  764. system("cls");
  765. imprime();
  766. for(t=0; t<99000000; t++) ;
  767. jogapc();
  768. system("cls");
  769. imprime();
  770.  
  771. if(vel[0][0]=='x' && vel[0][1]=='x' && vel[0][2]=='x' ||
  772. vel[1][0]=='x' && vel[1][1]=='x' && vel[1][2]=='x' ||
  773. vel[2][0]=='x' && vel[2][1]=='x' && vel[2][2]=='x' ||
  774.  
  775. vel[0][0]=='x' && vel[1][0]=='x' && vel[2][0]=='x' ||
  776. vel[0][1]=='x' && vel[1][1]=='x' && vel[2][1]=='x' ||
  777. vel[0][2]=='x' && vel[1][2]=='x' && vel[2][2]=='x' ||
  778.  
  779. vel[0][0]=='x' && vel[1][1]=='x' && vel[2][2]=='x' ||
  780. vel[2][0]=='x' && vel[1][1]=='x' && vel[0][2]=='x') xwin();
  781.  
  782. if(vel[0][0]=='o' && vel[0][1]=='o' && vel[0][2]=='o' ||
  783. vel[1][0]=='o' && vel[1][1]=='o' && vel[1][2]=='o' ||
  784. vel[2][0]=='o' && vel[2][1]=='o' && vel[2][2]=='o' ||
  785.  
  786. vel[0][0]=='o' && vel[1][0]=='o' && vel[2][0]=='o' ||
  787. vel[0][1]=='o' && vel[1][1]=='o' && vel[2][1]=='o' ||
  788. vel[0][2]=='o' && vel[1][2]=='o' && vel[2][2]=='o' ||
  789.  
  790. vel[0][0]=='o' && vel[1][1]=='o' && vel[2][2]=='o' ||
  791. vel[2][0]=='o' && vel[1][1]=='o' && vel[0][2]=='o') owin();
  792. }while(1);
  793.  
  794. system("pause");
  795.  
  796. }
Advertisement
Add Comment
Please, Sign In to add comment