Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.86 KB | None | 0 0
  1. /* Universidade de Brasilia
  2. Instituto de Ciencias Exatas
  3. Departamento de Ciencia da Computacao
  4. Algoritmos e Programacao de Computadores 2/2018
  5. Aluno(a): Murilo Ferreira Pires
  6. Matricula: 180128361
  7. Turma: A
  8. Versao do compilador: gcc version 7.3.1 20180323
  9. Descricao: Um programa que tem como objetivo implementar um jogo em um grid 2D representado por uma matriz similar ao jogo Puyo Puyo,
  10. porem em versao DIY (Do it Yourself, ou seja, Faca voce mesmo). O jogo possui diversos tijolos aleatorios, que cairao de posicoes
  11. randomicas a partir do topo do tabuleiro. A partir do momento em que o tijolo esta caindo, o jogador pode controla-lo horizontalmente,
  12. inverter a ordem das pecas, vira-los em posicao vertical e faze-los descer rapido. O jogador tem como objetivo conseguir o maior numero
  13. de pontos antes do fim do jogo, que acaba no momento em que os tijolos alcancam o topo do grid.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21.  
  22.  
  23. #define GREEN "\x1b[32m"
  24. #define YELLOW "\x1b[33m"
  25. #define BLUE "\x1b[34m"
  26. #define MAGENTA "\x1b[35m"
  27. #define CYAN "\x1b[36m"
  28. #define RESET "\x1b[0m"
  29. #define RED "\x1b[31m"
  30.  
  31. /* o codigo abaixo inclui as funcoes kbhit e getch se o sistema operacional nao for windows e define um macro para limpar a tela independente do sistema operacional */
  32.  
  33. #ifdef _WIN32
  34. #define CLEAR "cls"
  35. #else
  36. #define CLEAR "clear"
  37. #endif
  38.  
  39. #ifndef _WIN32
  40. #include <termios.h>
  41. int kbhit(){
  42. struct termios oldt, newt;
  43. int ch, oldf;
  44. tcgetattr(STDIN_FILENO,&oldt);
  45. newt = oldt;
  46. newt.c_lflag &= ~(ICANON | ECHO);
  47. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  48. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  49. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  50. ch = getchar();
  51. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  52. fcntl(STDIN_FILENO, F_SETFL, oldf);
  53. if(ch != EOF){
  54. ungetc(ch,stdin);
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. int getch(void) {
  60. int ch;
  61. struct termios oldt;
  62. struct termios newt;
  63. tcgetattr(STDIN_FILENO,&oldt);
  64. newt = oldt;
  65. newt.c_lflag &= ~(ICANON | ECHO);
  66. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  67. ch = getchar();
  68. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  69. return ch;
  70. }
  71. #else
  72. #include <conio.h>
  73. #endif
  74.  
  75. /* Variaveis globais */
  76. int al = 13; /* altura */
  77. int la = 8; /* largura */
  78. int x1 = 1, y1 = 0, x2 = 2, y2 = 0; /* cordenadas das pecas 1 e 2 */
  79. char tabu[100][100]; /* tabuleiro */
  80. int state = 1; /* tipo da peca (muda quando gira) */
  81. int pg = 1; /* variavel pra indicar se pode girar */
  82. char p1, p2; /* pecas 1 e 2 */
  83. int velocidade = 720000;
  84. int pontos;
  85. int combo;
  86. int qtdp = 5;
  87. int pontuacao = 4;
  88. int replay = 0; /* define se o replay esta ativado */
  89. char nomereplay[100]; /* nome do replay a ser lido/escrito */
  90. char preplay[2001]; /* string com todas as pecas do replay (maximo de 2000) */
  91. int creplay = 0; /* contador auxiliar para identificar a peca a ser lida na string do replay */
  92. int tamreplay = 0; /* numero de pecas salvas no replay */
  93. int gravar = 0; /* grava ou nao o jogo */
  94.  
  95.  
  96.  
  97. /* funcao para a tela inicial */
  98. int inicial(){
  99. printf (CYAN ".########..##.....##.##....##..#######.....########..##.....##.##....##..#######.\n.##.....##.##.....##..##..##..##.....##....##.....##.##.....##..##..##..##.....##\n.##.....##.##.....##...####...##.....##....##.....##.##.....##...####...##.....##\n.########..##.....##....##....##.....##....########..##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##.........#######.....##.....#######.....##.........#######.....##.....#######." RESET "\n\n\n\n\n Pressione <enter> para jogar!");
  100. getch();
  101. return 1;
  102. }
  103.  
  104. /*funcao para criar as pecas compostas */
  105. void criachar(){
  106. if (replay == 1 && creplay < tamreplay){
  107. p1 = preplay[creplay];
  108. creplay++;
  109. p2 = preplay[creplay];
  110. creplay++;
  111. }
  112. else{
  113. p1 = 'A' + rand() % qtdp;
  114. p2 = 'A' + rand() % qtdp;
  115. if (gravar == 1){
  116. FILE*df;
  117. df = fopen(nomereplay, "a");
  118. fprintf(df,"%c%c",p1,p2);
  119. fclose(df);
  120. }
  121. }
  122. }
  123.  
  124. /* zerar o tabuleiro com # e ' ' */
  125. void zeratabu(void){
  126. int x;
  127. int y;
  128. for (y=0; y < al; y++){
  129. for(x=0; x < la; x++){
  130. if ((y == al-1) || (x == la-1) || (x == 0)){
  131. tabu[x][y] = '#';
  132. }
  133. else{
  134. tabu[x][y] = ' ';
  135. }
  136. }
  137.  
  138. }
  139. }
  140.  
  141. /* atualiza a tela */
  142. void attframe(void){
  143. system(CLEAR);
  144. int x, y;
  145. for (y = 0; y < al; y++){
  146. for (x=0; x < la; x++){
  147. if (tabu[x][y] == 'A'){
  148. printf(GREEN "%c" RESET, tabu[x][y]);
  149. }
  150. else if (tabu[x][y] == 'B'){
  151. printf(YELLOW "%c" RESET, tabu[x][y]);
  152. }
  153. else if (tabu[x][y] == 'C'){
  154. printf(BLUE "%c" RESET, tabu[x][y]);
  155. }
  156. else if (tabu[x][y] == 'D'){
  157. printf(MAGENTA "%c" RESET, tabu[x][y]);
  158. }
  159. else if (tabu[x][y] == 'E'){
  160. printf(CYAN "%c" RESET, tabu[x][y]);
  161. }
  162. else if (tabu[x][y] == '*'){
  163. printf(RED "%c" RESET, tabu[x][y]);
  164. }
  165. else {printf("%c", tabu[x][y]);
  166. }
  167. }
  168. printf("\n");
  169. }
  170. printf("\nPontos: %d \n", pontos);
  171. }
  172.  
  173. /* salva as pecas no tabuleiro */
  174. void saveframe(int x){
  175. if (x == 1 && p1 != '0'){
  176. tabu[x1][y1] = p1;
  177. }
  178. if (x == 2 && p2 != '0'){
  179. tabu[x2][y2] = p2;
  180. }
  181. if (x == 3 && p1 != '0' && p2 != '0'){
  182. tabu[x1][y1] = p1;
  183. tabu[x2][y2] = p2;
  184. }
  185. }
  186.  
  187. /* funcao que gira as pecas */
  188. void girar(){
  189. if (pg == 0){
  190. return;
  191. }
  192. if (state == 1 && tabu[x2][y2+1] == ' '){
  193. tabu[x1][y1] = ' ';
  194. tabu[x2][y2] = p1;
  195. x1 = x2;
  196. y2 = y1 + 1;
  197. saveframe(3);
  198. attframe();
  199. state++;
  200. return;
  201. }
  202. if (state == 2 && tabu[x1-1][y1] == ' ' && tabu[x2+1][y2] == ' '){
  203. tabu[x2][y2] = ' ';
  204. x2--;
  205. y2--;
  206. saveframe(2);
  207. attframe();
  208. state++;
  209. return;
  210. }
  211. if (state == 3 && tabu[x1][y1+1] == ' '){
  212. tabu[x2][y2] = ' ';
  213. x2++;
  214. y1++;
  215. saveframe(3);
  216. attframe();
  217. state++;
  218. return;
  219. }
  220. if(state == 4 && tabu[x2-1][y2] == ' '){
  221. tabu[x1][y1] = ' ';
  222. x1--;
  223. y1--;
  224. saveframe(1);
  225. attframe();
  226. state = 1;
  227. return;
  228. }
  229. return;
  230. }
  231.  
  232. /*movimento para baixo automatico */
  233. void smove(void){
  234. if (p1 == '0'){
  235. tabu[x2][y2] = ' ';
  236. y2++;
  237. saveframe(2);
  238. attframe();
  239. return;
  240. }
  241. if (p2 == '0'){
  242. tabu[x1][y1] = ' ';
  243. y1++;
  244. saveframe(1);
  245. attframe();
  246. return;
  247. }
  248. tabu[x1][y1] = ' ';
  249. tabu[x2][y2] = ' ';
  250. y2++;
  251. y1++;
  252. saveframe(3);
  253. attframe();
  254. }
  255.  
  256. /* deleta as pecas marcadas com * e desce as que tao em cima */
  257. void score(){
  258. int x, y;
  259. for (y=0; y < al; y++){
  260. for(x=0; x < la; x++){
  261. if(tabu[x][y] == '*'){
  262. if (combo > 1){
  263. pontos = pontos + combo;
  264. }
  265. else{
  266. pontos++;
  267. }
  268. tabu[x][y] = tabu[x][y-1];
  269. tabu[x][y-1] = ' ';
  270. x = 0;
  271. y = 0;
  272. }
  273. while (tabu[x][y+1] == ' ' && tabu[x][y] != ' ' && tabu[x][y] != '#'){
  274. tabu[x][y+1] = tabu[x][y];
  275. tabu[x][y] = ' ';
  276. }
  277. }
  278. }
  279. }
  280.  
  281. /* checa se da pra fazer ponto e substitui as pecas que fazem por * */
  282. int check(){
  283. /*essa funcao utiliza uma funcao auxiliar para identificar quais pecas ja foram testadas*/
  284. char aux[la][al];
  285. int x, y, c = 1, i = 0, k = 0, j = 0, e = 0, a = 0, b = 0;
  286. /*dentro do for, eh construida uma cadeia de celulas que estao ligadas, utilizando um vetor de struct para salvar as coordenadas das celulas
  287. se a cadeia for maior que 4, as celulas sao substituidas por * */
  288. typedef struct {
  289. int x;
  290. int y;
  291. } coord;
  292. coord pecas[1000];
  293. for (a=0; a < al; a++){
  294. for(b=0; b < la; b++){
  295. aux[b][a] = '0';
  296. }
  297. }
  298. for (x = 1; x < la - 1; x++){
  299. for (y=0; y < al - 1; y++){
  300. if(tabu[x][y] != ' ' && tabu[x][y] != '*' && tabu[x][y] != '#'){
  301. if ( (tabu[x][y] == tabu[x+1][y] && aux[x+1][y] == '0') || (tabu[x][y] == tabu[x-1][y] && aux[x-1][y] == '0')
  302. || (tabu[x][y] == tabu[x][y+1] && aux[x][y+1] == '0') || (y != 0 && tabu[x][y] == tabu[x][y-1] && aux[x][y-1] == '0') ){
  303. pecas[i].x = x;
  304. pecas[i].y = y;
  305. for(j = 0; j <= i && aux[pecas[j].x][pecas[j].y] != '1';j++){
  306. if(tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x+1][pecas[j].y] && aux[pecas[j].x+1][pecas[j].y] != '1'){
  307. aux[pecas[j].x][pecas[j].y] = '1';
  308. i++;
  309. pecas[i].x = pecas[j].x + 1;
  310. pecas[i].y = pecas[j].y;
  311. c++;
  312. }
  313. if(tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x-1][pecas[j].y] && aux[pecas[j].x-1][pecas[j].y] != '1'){
  314. aux[pecas[j].x][pecas[j].y] = '1';
  315. i++;
  316. pecas[i].x = pecas[j].x - 1;
  317. pecas[i].y = pecas[j].y;
  318. c++;
  319. }
  320. if(tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x][pecas[j].y+1] && aux[pecas[j].x][pecas[j].y+1] != '1'){
  321. aux[pecas[j].x][pecas[j].y] = '1';
  322. i++;
  323. pecas[i].x = pecas[j].x;
  324. pecas[i].y = pecas[j].y + 1;
  325. c++;
  326. }
  327. if(y != 0 && tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x][pecas[j].y-1] && aux[pecas[j].x][pecas[j].y-1] != '1'){
  328. aux[pecas[j].x][pecas[j].y] = '1';
  329. i++;
  330. pecas[i].x = pecas[j].x;
  331. pecas[i].y = pecas[j].y - 1;
  332. c++;
  333. }
  334. aux[pecas[j].x][pecas[j].y] = '1';
  335. }
  336. }
  337. if (c >= pontuacao){
  338. combo++;
  339. for (k=0; k <= i; k++){
  340. tabu[pecas[k].x][pecas[k].y] = '*';
  341. }
  342. attframe();
  343. score();
  344. e++;
  345. }
  346. i = 0;
  347. c = 1;
  348. }
  349. }
  350. }
  351. if (e == 0){
  352. return 0;
  353. }
  354. return 1;
  355. }
  356.  
  357. /* ve se alguma das pecas colidiu com alguma coisa */
  358. int colisao(){
  359. if (p2 != '0' && p1 != '0'){
  360. if (state == 1 || state == 3){
  361. if (tabu[x1][y1+1] != ' ' && tabu[x2][y2+1] != ' '){
  362. return 3;
  363. }
  364. }
  365. if (state == 2){
  366. if (tabu[x2][y2+1] != ' '){
  367. return 3;
  368. }
  369. return 0;
  370. }
  371. if (state == 4){
  372. if (tabu[x1][y1+1] != ' '){
  373. return 3;
  374. }
  375. return 0;
  376. }
  377. }
  378. if (tabu[x2][y2+1] != ' ' && p2 != '0'){
  379. return 2;
  380. }
  381. if (tabu[x1][y1+1] != ' ' && p1 != '0'){
  382. return 1;
  383. }
  384. return 0;
  385. }
  386.  
  387. /* move a(s) peca(s) para a esquerda */
  388. void mleft(void){
  389. if (x2 < x1 && p2 != '0' && p1 != '0'){
  390. if(tabu[x2-1][y2] == ' '){
  391. tabu[x2][y2] = ' ';
  392. x2--;
  393. saveframe(2);
  394. if (p1 != '0' && tabu[x1+1][y1] == ' '){
  395. tabu[x1][y1] = ' ';
  396. x1--;
  397. saveframe(1);
  398. }
  399. }
  400. }
  401. if (x1 < x2 && p1 != '0' && p2 != '0'){
  402. if(tabu[x1-1][y1] == ' '){
  403. tabu[x1][y1] = ' ';
  404. x1--;
  405. saveframe(1);
  406. if (p2 != '0' && tabu[x2-1][y2] == ' '){
  407. tabu[x2][y2] = ' ';
  408. x2--;
  409. saveframe(2);
  410. }
  411. }
  412. }
  413. if (x2 == x1 && p1 != '0' && p2 != '0'){
  414. if(tabu[x2-1][y2] == ' ' && p2 != '0'){
  415. tabu[x2][y2] = ' ';
  416. x2--;
  417. saveframe(2);
  418. }
  419. if (p1 != '0' && tabu[x1-1][y1] == ' '){
  420. tabu[x1][y1] = ' ';
  421. x1--;
  422. saveframe(1);
  423. }
  424. }
  425. }
  426.  
  427. /* move a(s) peca(s) para a direita */
  428. void mright(void){
  429. if (x2 > x1 && p2 != '0' && p1 != '0'){
  430. if(tabu[x2+1][y2] == ' '){
  431. tabu[x2][y2] = ' ';
  432. x2++;
  433. saveframe(2);
  434. if (p1 != '0' && tabu[x1+1][y1] == ' '){
  435. tabu[x1][y1] = ' ';
  436. x1++;
  437. saveframe(1);
  438. }
  439. }
  440. }
  441. if (x1 > x2 && p2 != '0' && p1 != '0'){
  442. if(tabu[x1+1][y1] == ' '){
  443. tabu[x1][y1] = ' ';
  444. x1 = x1 + 1;
  445. saveframe(1);
  446. if (p2 != '0' && tabu[x2+1][y2] == ' '){
  447. tabu[x2][y2] = ' ';
  448. x2 = x2 + 1;
  449. saveframe(2);
  450. }
  451. }
  452. }
  453. if (x2 == x1 && p2 != '0' && p1 != '0'){
  454. if(tabu[x2+1][y2] == ' ' && p2 != '0'){
  455. tabu[x2][y2] = ' ';
  456. x2 = x2 + 1;
  457. saveframe(2);
  458. }
  459. if (p1 != '0' && tabu[x1+1][y1] == ' '){
  460. tabu[x1][y1] = ' ';
  461. x1 = x1 + 1;
  462. saveframe(1);
  463. }
  464. }
  465. }
  466.  
  467. /*inverte as 2 pecas */
  468. void inverter(void){
  469. int auxx, auxy;
  470. if (state == 1 || state == 2){
  471. state = state +2;
  472. }
  473. else if (state == 3){
  474. state = 1;
  475. }
  476. else if (state == 4){
  477. state = 2;
  478. }
  479. if(p1 != '0' && p2 != '0'){
  480. auxx = x1;
  481. auxy = y1;
  482. x1 = x2;
  483. y1 = y2;
  484. x2 = auxx;
  485. y2 = auxy;
  486. saveframe(3);
  487. attframe();
  488. }
  489.  
  490. }
  491.  
  492. /*controla as inputs do jogador */
  493. void acao(){
  494. char c;
  495. c = '0';
  496. while ((c != ' ') && (c != 'a') && (c != 's') && (c != 'd') && (c != 'w')){
  497. c = getch();
  498. }
  499. if(c == ' '){
  500. if (pg == 1){
  501. girar();
  502. attframe();
  503. }
  504. }
  505. if (c == 'a'){
  506. mleft();
  507. attframe();
  508. }
  509. if (c == 'd'){
  510. mright();
  511. attframe();
  512. }
  513. if (c == 's'){
  514. while (colisao() == 0){
  515. smove();
  516. attframe();
  517. }
  518. }
  519. if (c == 'w'){
  520. inverter();
  521. attframe();
  522. }
  523. }
  524.  
  525. /*essa funcao ve se alguma peca chegou ao topo da grid */
  526. int gameon(){
  527. int x;
  528. for (x = 1; x < la - 1; x++){
  529. if (tabu[x][0] != ' ' && tabu[x][1] != ' '){
  530. return 0;
  531. }
  532. }
  533. return 1;
  534. }
  535.  
  536. /* funcao que repete toda vez que uma peca nova eh liberada */
  537. int game(){
  538. attframe();
  539. criachar();
  540. x1 = 1; y1 = 0; x2 = x1 + 1; y2 = y1;
  541. pg = 1;
  542. state = 1;
  543. saveframe(3);
  544. attframe();
  545. while (gameon()){
  546. while(colisao() == 0){
  547. while(kbhit()){
  548. acao();
  549. saveframe(3);
  550. attframe();
  551. }
  552. if(colisao() == 0){
  553. usleep(velocidade);
  554. smove();
  555. }
  556. }
  557. if (colisao() == 3){ /* as 2 pecas colidiram */
  558. saveframe(3);
  559. p1 = '0';
  560. p2 = '0';
  561. }
  562. if (colisao() == 2){ /* p2 colidiu */
  563. pg = 0;
  564. saveframe(2);
  565. p2 = '0';
  566. }
  567. if (colisao() == 1){ /* p1 colidiu */
  568. pg = 0;
  569. saveframe(1);
  570. p1 = '0';
  571. }
  572. saveframe(3);
  573. attframe();
  574. while(p1 == '0' && p2 == '0' && check()){
  575. attframe();
  576. }
  577. combo = 0;
  578. if(p1 == '0' && p2 == '0' && gameon != 0){
  579. return 1;
  580. }
  581. }
  582. return 0;
  583. }
  584.  
  585. /*tela de fim de jogo */
  586. int gameover(){
  587. attframe();
  588. usleep(720000);
  589. system(CLEAR);
  590. printf("\n\nGAME OVER! OH NAO, VOCE PERDEU!");
  591. printf("\n\n\n\n Aperte qualquer tecla para voltar para o menu");
  592. getch();
  593. return 1;
  594. }
  595.  
  596. /* A funcao do jogo em si */
  597. int jogar(){
  598. x1 = 1;
  599. y1 = 1;
  600. x2 = x1 + 1;
  601. y2 = y1;
  602. zeratabu();
  603. pontos = 0;
  604. while(game()){}
  605. if (gameover()){
  606. return 1;
  607. }
  608. return 0; /*retornar 0 sai do jogo, retornar 1 volta pro menu */
  609. }
  610.  
  611. /* funcao para instrucoes com a opcao de voltar para o menu ou sair do jogo */
  612. int instrucoes(){
  613. char c;
  614. c = '0';
  615. system(CLEAR);
  616. printf(" +-+-+-+-+-+-+-+-+-+-+\n |I|n|s|t|r|u|c|o|e|s|\n +-+-+-+-+-+-+-+-+-+-+\n\n\n");
  617. printf("Como fazer pontos?\nBasta conectar 4 ou mais pecas iguais.\n\nCOMANDOS:\nA - Movimento para a esquerda\nD - Movimento para a direita\nS - Cai a peca mais rapido\nSPACE - Gira no sentido horario\nW - Inverte as pecas.\n\nAperte 1 para voltar para o menu e 2 para sair do jogo.");
  618. while ((c != 49) && (c != 50)){
  619. c = getch();
  620. }
  621. if (c == 49){
  622. return 1;
  623. }
  624. return 0;
  625. }
  626.  
  627. /* funcao para controlar o replay */
  628. int modoreplay(){
  629. system(CLEAR);
  630. FILE* fd;
  631. char u;
  632. printf("1 - Gravar o proximo jogo\n2 - Carregar o jogo a partir de um arquivo ja salvo\n3 - Voltar\n\nDigite o numero correspondente a opcao desejada: ");
  633. scanf("\n%c", &u);
  634. while ((u != '1') && (u != '2') && (u != '3')){
  635. printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", u);
  636. scanf("\n%c", &u);
  637. }
  638. if (u == '1'){
  639. printf("\nDigite o nome do arquivo em que o jogo sera salvo (com .txt): ");
  640. scanf("%s", nomereplay);
  641. printf("\nAs pecas do proximo jogo serao gravadas, pressione qualquer tecla para continuar.");
  642. gravar = 1;
  643. getch();
  644. return 0;
  645. }
  646. if (u == '2'){
  647. printf("\n Digite o nome do arquivo (com .txt): ");
  648. scanf("%s", nomereplay);
  649. fd = fopen(nomereplay, "r+");
  650. if(fd == NULL){
  651. printf("\nO arquivo informado nao existe. Pressione qualquer tecla para voltar.");
  652. getch();
  653. return modoreplay();
  654. }
  655. else{
  656. replay = 1;
  657. fscanf(fd, "%s", preplay);
  658. for (tamreplay=0; preplay[tamreplay] != '\0'; tamreplay++){
  659. }
  660. return 0;
  661. }
  662. }
  663. if (u == 3){
  664. return jogar();
  665. }
  666. }
  667.  
  668. /* funcao para o menu utilizando switch para incializar a funcao correspondente */
  669. int menu(){
  670. system(CLEAR);
  671. char c;
  672. char w;
  673. c = '0';
  674. printf(RED ".##.....##.########.##....##.##.....##\n.###...###.##.......###...##.##.....##\n.####.####.##.......####..##.##.....##\n.##.###.##.######...##.##.##.##.....##\n.##.....##.##.......##..####.##.....##\n.##.....##.##.......##...###.##.....##\n.##.....##.########.##....##..#######.\n\n" RESET);
  675. printf("1 - Jogar\n2 - Instrucoes\n3 - Configuracoes\n4 - Ranking (Indisponivel)\n5 - Sair\n Digite o numero correspondente a opcao desejada: ");
  676. scanf("\n%c", &c);
  677. while ((c != '1') && (c != '2') && (c != '3') /*&& (c != '4')*/ && (c != '5')){
  678. printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
  679. scanf("\n%c", &c);
  680. }
  681. switch (c){
  682. case '1':
  683. system(CLEAR);
  684. printf("\nDigite 1 para jogar normalmente e 2 para entrar no Modo Replay: ");
  685. scanf("\n%c", &w);
  686. while ((w != '1') && (w != '2')){
  687. printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", w);
  688. scanf("\n%c", &w);
  689. }
  690. if (w == '2'){
  691. system(CLEAR);
  692. modoreplay();
  693. }
  694. system(CLEAR);
  695. if (jogar()){ /*se jogar retorna 0, menu retorna 0 e sai do jogo */
  696. return 1;
  697. }
  698. return 0;
  699. case '2':
  700. system(CLEAR);
  701. if (instrucoes()){ /* se instrucoes retorna 1, menu retorna 1 e a main faz voltar para o menu */
  702. return 1;
  703. }
  704. system(CLEAR);
  705. return 0;
  706. case '3':
  707. configuracoes();
  708. /*
  709. case '4':
  710. ranking(); */
  711. case '5':
  712. system(CLEAR);
  713. return 0;
  714. }
  715. return 0;
  716. }
  717.  
  718. /* funcao para o menu de configuracoes utilizando switch para incializar a funcao correspondente */
  719. int configuracoes(){
  720. system(CLEAR);
  721. char c;
  722. c = '0';
  723. printf("1 - Tabuleiro\n2 - Pecas\n3 - Ativar Modo Rankeado\n4 - Voltar\n Digite o numero correspondente a opcao desejada: ");
  724. scanf("\n%c", &c);
  725. while ((c != '1') && (c != '2') && (c != '3') && (c != '4')){
  726. printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
  727. scanf("\n%c", &c);
  728. }
  729. switch (c){
  730. case '1':
  731. configtabu();
  732. return 0;
  733. case '2':
  734. configpecas();
  735. return 0;
  736. case '3':
  737. printf("O modo ranqueado (e o ranking) nao foram implementados :( \nPressione qualquer tecla para voltar ao menu.");
  738. getch();
  739. return menu();
  740. case '4':
  741. return menu();
  742. }
  743. }
  744.  
  745. /* funcao para o menu de configuracoes de tabuleiro */
  746. int configtabu(){
  747. system(CLEAR);
  748. char c;
  749. c = '0';
  750. printf("Configuracoes de Tabuleiro\n\n1 - Altura (%d)\n2 - Largura (%d)\n3 - Velocidade (%d)\n4 - Voltar\n Digite o numero para alterar a opcao desejada: ", al, la, velocidade);
  751. scanf("\n%c", &c);
  752. while ((c != '1') && (c != '2') && (c != '3') && (c != '4')){
  753. printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
  754. scanf("\n%c", &c);
  755. }
  756. int o;
  757. FILE *arquivo;
  758. arquivo = fopen("config.txt", "w");
  759. switch (c){
  760. case '1':
  761. printf("\nDigite um novo valor para altura entre 5 e 80: ");
  762. scanf("%d", &o);
  763. while(o < 5 || o > 80){
  764. printf("\nValor fora dos limites! Digite novamente: ");
  765. scanf("%d", &o);
  766. }
  767. al = o;
  768. fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
  769. return configtabu();
  770. case '2':
  771. printf("\nDigite um novo valor para largura entre 5 e 80: ");
  772. scanf("%d", &o);
  773. while(o < 5 || o > 80){
  774. printf("\nValor fora dos limites! Digite novamente: ");
  775. scanf("%d", &o);
  776. }
  777. la = o;
  778. fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
  779. return configtabu();
  780. case '3':
  781. printf("\nDigite um novo valor para velocidade (em microssegundos) entre 100000 e 5000000: ");
  782. scanf("%d", &o);
  783. while(o < 100000 || o > 5000000){
  784. printf("\nValor fora dos limites! Digite novamente: ");
  785. scanf("%d", &o);
  786. }
  787. velocidade = o;
  788. fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
  789. return configtabu();
  790. case '4':
  791. return configuracoes();
  792. }
  793. }
  794.  
  795. /* funcao para o menu de configuracoes de pecas */
  796. int configpecas(){
  797. system(CLEAR);
  798. char c;
  799. c = '0';
  800. printf("Configuracoes de Pecas\n\n1 - Tipos de pecas diferentes (%d)\n2 - Quantidade de pecas ligadas para fazer ponto (%d)\n3 - Voltar\n Digite o numero para alterar a opcao desejada: ", qtdp, pontuacao);
  801. scanf("\n%c", &c);
  802. while ((c != '1') && (c != '2') && (c != '3')){
  803. printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
  804. scanf("\n%c", &c);
  805. }
  806. int o;
  807. FILE *arquivo;
  808. arquivo = fopen("config.txt", "w");
  809. switch (c){
  810. case '1':
  811. printf("\nDigite um novo valor para a variedade de pecas entre 2 e 10: ");
  812. scanf("%d", &o);
  813. while(o < 2 || o > 10){
  814. printf("\nValor fora dos limites! Digite novamente: ");
  815. scanf("%d", &o);
  816. }
  817. qtdp = o;
  818. fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
  819. return configpecas();
  820. case '2':
  821. printf("\nDigite um novo valor para o combo de pecas entre 3 e 10: ");
  822. scanf("%d", &o);
  823. while(o < 3 || o > 10){
  824. printf("\nValor fora dos limites! Digite novamente: ");
  825. scanf("%d", &o);
  826. }
  827. pontuacao = o;
  828. fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
  829. return configpecas();
  830. case '3':
  831. return configuracoes();
  832. }
  833. }
  834.  
  835. int main () {
  836. FILE *arquivo;
  837. arquivo = fopen("config.txt","r");
  838. if(arquivo == NULL){
  839. arquivo = fopen("config.txt", "w");
  840. fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
  841. }
  842. arquivo = fopen("config.txt","r");
  843. fscanf(arquivo, "%d %d %d %d %d", &al, &la, &qtdp, &pontuacao, &velocidade);
  844. system(CLEAR);
  845. if (inicial()){
  846. system(CLEAR);
  847. }
  848. srand(time(0));
  849. while(menu()){}
  850. fclose(arquivo);
  851. if (replay == 1 || gravar == 1){
  852. FILE *fd;
  853. fd = fopen(nomereplay,"r");
  854. fclose(fd);
  855. }
  856. return 0;
  857. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement