Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.92 KB | None | 0 0
  1. /* Universidade de Brasilia
  2. Instituto de Ciencias Exatas
  3. Departamento de Ciencia da Computacao
  4. Algoritmos e Programação de Computadores – 2/2018
  5. Aluno(a): Murilo Ferreira Pires
  6. Matricula: 180128361
  7. Turma: A
  8. Versao do compilador: <versao utilizada>
  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. #define GREEN "\x1b[32m"
  20. #define YELLOW "\x1b[33m"
  21. #define BLUE "\x1b[34m"
  22. #define MAGENTA "\x1b[35m"
  23. #define CYAN "\x1b[36m"
  24. #define RESET "\x1b[0m"
  25. #define RED "\x1b[31m"
  26.  
  27. /* 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 */
  28.  
  29. #ifndef _WIN32
  30. #define cls “CLEAR”
  31. int kbhit(){
  32. struct termios oldt, newt;
  33. int ch, oldf;
  34. tcgetattr(STDIN_FILENO,&oldt);
  35. newt = oldt;
  36. newt.c_lflag &= ~(ICANON | ECHO);
  37. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  38. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  39. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  40. ch = getchar();
  41. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  42. fcntl(STDIN_FILENO, F_SETFL, oldf);
  43. if(ch != EOF){
  44. ungetc(ch,stdin);
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. int getch(void) {
  50. int ch;
  51. struct termios oldt;
  52. struct termios newt;
  53. tcgetattr(STDIN_FILENO,&oldt);
  54. newt = oldt;
  55. newt.c_lflag &= ~(ICANON | ECHO);
  56. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  57. ch = getchar();
  58. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  59. return ch;
  60. }
  61. #else
  62. #include <conio.h>
  63. #endif
  64.  
  65. /* Variaveis globais */
  66. int al = 13;
  67. int la = 8;
  68. int x1 = 1, y1 = 0, x2 = 2, y2 = 0;
  69. char tabu[8][13];
  70. int state = 1;
  71. int pg = 1;
  72. char p1, p2;
  73. int velocidade = 720000;
  74.  
  75.  
  76. /* funcao para a tela inicial */
  77. int inicial(){
  78. char c;
  79. c = '0';
  80. printf (".########..##.....##.##....##..#######.....########..##.....##.##....##..#######.\n.##.....##.##.....##..##..##..##.....##....##.....##.##.....##..##..##..##.....##\n.##.....##.##.....##...####...##.....##....##.....##.##.....##...####...##.....##\n.########..##.....##....##....##.....##....########..##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##.........#######.....##.....#######.....##.........#######.....##.....#######.\n\n\n\n\n Pressione <enter> para jogar!");
  81. while (c != 13){
  82. c = getch();
  83. }
  84. return 1;
  85. }
  86.  
  87. /*funcao para criar as pecas compostas */
  88. void criachar(){
  89. p1 = 'A' + rand() % 5;
  90. p2 = 'A' + rand() % 5;
  91. }
  92.  
  93. /* zerar o tabuleiro com # e ' ' */
  94. void zeratabu(void){
  95. int x;
  96. int y;
  97. for (y=0; y < al; y++){
  98. for(x=0; x < la; x++){
  99. if ((y == 12) || (x == 7) || (x == 0)){
  100. tabu[x][y] = '#';
  101. }
  102. else{
  103. tabu[x][y] = ' ';
  104. }
  105. }
  106.  
  107. }
  108. }
  109.  
  110. /* atualiza a tela */
  111. void attframe(void){
  112. system("cls");
  113. int x, y;
  114. for (y = 0; y < al; y++){
  115. for (x=0; x < la; x++){
  116. printf("%c", tabu[x][y]);
  117. }
  118. printf("\n");
  119. }
  120. }
  121.  
  122.  
  123. /* salva as pecas no tabuleiro */
  124. void saveframe(int x){
  125. if (x == 1 && p1 != '0'){
  126. tabu[x1][y1] = p1;
  127. }
  128. if (x == 2 && p2 != '0'){
  129. tabu[x2][y2] = p2;
  130. }
  131. if (x == 3 && p1 != '0' && p2 != '0'){
  132. tabu[x1][y1] = p1;
  133. tabu[x2][y2] = p2;
  134. }
  135. }
  136.  
  137. /* funcao que gira as pecas */
  138. void girar(){
  139. if (pg == 0){
  140. return;
  141. }
  142. if (state == 1 && tabu[x2][y2+1] == ' '){
  143. tabu[x1][y1] = ' ';
  144. tabu[x2][y2] = p1;
  145. x1 = x2;
  146. y2 = y1 + 1;
  147. saveframe(3);
  148. attframe();
  149. state++;
  150. return;
  151. }
  152. if (state == 2 && tabu[x1-1][y1] == ' '){
  153. tabu[x2][y2] = ' ';
  154. x2--;
  155. y2--;
  156. saveframe(2);
  157. attframe();
  158. state++;
  159. return;
  160. }
  161. if (state == 3 && tabu[x1][y1+1] == ' '){
  162. tabu[x2][y2] = ' ';
  163. x2++;
  164. y1++;
  165. saveframe(3);
  166. attframe();
  167. state++;
  168. return;
  169. }
  170. if(state == 4 && tabu[x2-1][y2] == ' '){
  171. tabu[x1][y1] = ' ';
  172. x1--;
  173. y1--;
  174. saveframe(1);
  175. attframe();
  176. state = 1;
  177. return;
  178. }
  179. return;
  180. }
  181.  
  182.  
  183.  
  184. /*movimento para baixo automatico */
  185. void smove(void){
  186. if (p1 == '0'){
  187. tabu[x2][y2] = ' ';
  188. y2++;
  189. saveframe(2);
  190. attframe();
  191. return;
  192. }
  193. if (p2 == '0'){
  194. tabu[x1][y1] = ' ';
  195. y1++;
  196. saveframe(1);
  197. attframe();
  198. return;
  199. }
  200. tabu[x1][y1] = ' ';
  201. tabu[x2][y2] = ' ';
  202. y2++;
  203. y1++;
  204. saveframe(3);
  205. attframe();
  206. }
  207.  
  208.  
  209. /*
  210. int check(){
  211. int x, y;
  212. for (x = 0; x < la; x++){
  213. for (y=0; y < al; y++){
  214. if(tabu[x][y]);
  215. }
  216. }
  217. }
  218. */
  219. /* ve se alguma das pecas colidiu com alguma coisa */
  220. int colisao(){
  221. if (p2 != '0' && p1 != '0'){
  222. if (state == 1 || state == 3){
  223. if (tabu[x1][y1+1] != ' ' && tabu[x2][y2+1] != ' '){
  224. if(y1 == al){
  225. return 4;
  226. }
  227. return 3;
  228. }
  229. }
  230. if (state == 2){
  231. if (tabu[x2][y2+1] != ' '){
  232. if (y1 == al){
  233. return 4;
  234. }
  235. return 3;
  236. }
  237. return 0;
  238. }
  239. if (state == 4){
  240. if (tabu[x1][y1+1] != ' '){
  241. if (y2 == al){
  242. return 4;
  243. }
  244. return 3;
  245. }
  246. return 0;
  247. }
  248. }
  249. if (tabu[x2][y2+1] != ' ' && p2 != '0'){
  250. if (y2 == al){
  251. return 4;
  252. }
  253. return 2;
  254. }
  255. if (tabu[x1][y1+1] != ' ' && p1 != '0'){
  256. if (y1 == al){
  257. return 4;
  258. }
  259. return 1;
  260. }
  261. return 0;
  262. }
  263. /* move a(s) peca(s) para a esquerda */
  264. void mleft(void){
  265. if (x2 < x1 && p2 != '0'){
  266. if(tabu[x2-1][y2] == ' '){
  267. tabu[x2][y2] = ' ';
  268. x2--;
  269. saveframe(2);
  270. if (p1 != '0' && tabu[x1+1][y1] == ' '){
  271. tabu[x1][y1] = ' ';
  272. x1--;
  273. saveframe(1);
  274. }
  275. }
  276. }
  277. if (x1 < x2 && p1 != '0'){
  278. if(tabu[x1-1][y1] == ' '){
  279. tabu[x1][y1] = ' ';
  280. x1--;
  281. saveframe(1);
  282. if (p2 != '0' && tabu[x2-1][y2] == ' '){
  283. tabu[x2][y2] = ' ';
  284. x2--;
  285. saveframe(2);
  286. }
  287. }
  288. }
  289. if (x2 == x1){
  290. if(tabu[x2-1][y2] == ' ' && p2 != '0'){
  291. tabu[x2][y2] = ' ';
  292. x2--;
  293. saveframe(2);
  294. }
  295. if (p1 != '0' && tabu[x1-1][y1] == ' '){
  296. tabu[x1][y1] = ' ';
  297. x1--;
  298. saveframe(1);
  299. }
  300. }
  301. }
  302. /* move a(s) peca(s) para a direita */
  303. void mright(void){
  304. if (x2 > x1 && p2 != '0'){
  305. if(tabu[x2+1][y2] == ' '){
  306. tabu[x2][y2] = ' ';
  307. x2++;
  308. saveframe(2);
  309. if (p1 != '0' && tabu[x1+1][y1] == ' '){
  310. tabu[x1][y1] = ' ';
  311. x1++;
  312. saveframe(1);
  313. }
  314. }
  315. }
  316. if (x1 > x2 && p1 != '0'){
  317. if(tabu[x1+1][y1] == ' '){
  318. tabu[x1][y1] = ' ';
  319. x1 = x1 + 1;
  320. saveframe(1);
  321. if (p2 != '0' && tabu[x2+1][y2] == ' '){
  322. tabu[x2][y2] = ' ';
  323. x2 = x2 + 1;
  324. saveframe(2);
  325. }
  326. }
  327. }
  328. if (x2 == x1){
  329. if(tabu[x2+1][y2] == ' ' && p2 != '0'){
  330. tabu[x2][y2] = ' ';
  331. x2 = x2 + 1;
  332. saveframe(2);
  333. }
  334. if (p1 != '0' && tabu[x1+1][y1] == ' '){
  335. tabu[x1][y1] = ' ';
  336. x1 = x1 + 1;
  337. saveframe(1);
  338. }
  339. }
  340. }
  341.  
  342. /*controla as inputs do jogador */
  343. void acao(){
  344. char c;
  345. c = '0';
  346. while ((c != ' ') && (c != 'a') && (c != 's') && (c != 'd')){
  347. c = getch();
  348. }
  349. if(c == ' '){
  350. if (pg == 1){
  351. girar();
  352. attframe();
  353. }
  354. }
  355. if (c == 'a'){
  356. mleft();
  357. attframe();
  358. }
  359. if (c == 'd'){
  360. mright();
  361. attframe();
  362. }
  363. if (c == 's'){
  364. if (colisao() == 0){
  365. smove();
  366. attframe;
  367. }
  368. }
  369. }
  370.  
  371. /*essa funcao ve se alguma peca chegou ao topo da grid */
  372. int gameon(){
  373. int x;
  374. for (x = 1; x < la - 1; x++){
  375. if (tabu[x][0] != ' ' && tabu[x][1] != ' '){
  376. return 0;
  377. }
  378. }
  379. return 1;
  380. }
  381.  
  382.  
  383. int game(){
  384. attframe();
  385. criachar();
  386. x1 = 1; y1 = 0; x2 = x1 + 1; y2 = y1;
  387. pg = 1;
  388. state = 1;
  389. saveframe(3);
  390. attframe();
  391. while (gameon()){
  392. while(colisao() == 0){
  393. if(kbhit()){
  394. acao();
  395. saveframe(3);
  396. attframe();
  397. break;
  398. }
  399. usleep(velocidade);
  400. smove();
  401. }
  402. if (colisao() == 3){ /* as 2 pecas colidiram */
  403. saveframe(3);
  404. p1 = '0';
  405. p2 = '0';
  406. }
  407. if (colisao() == 2){ /* p2 colidiu */
  408. pg = 0;
  409. saveframe(2);
  410. p2 = '0';
  411. }
  412. if (colisao() == 1){ /* p1 colidiu */
  413. pg = 0;
  414. saveframe(1);
  415. p1 = '0';
  416. }
  417. saveframe(3);
  418. attframe();
  419. /*
  420. while (check()){
  421. attframe();
  422. score();
  423. saveframe(3);
  424. attframe();
  425. }
  426. */
  427. if(p1 == '0' && p2 == '0' && gameon != 0){
  428. return 1;
  429. }
  430. }
  431. return 0;
  432. }
  433.  
  434. /* A funcao do jogo em si */
  435.  
  436. int jogar(){
  437. int p1, p2;
  438. int x1 = 1, y1 = 1, x2 = x1 + 1, y2 = y1;
  439. zeratabu();
  440. while(game()){}
  441. return 0; /*retornar 0 sai do jogo, retornar 1 volta pro menu */
  442. }
  443.  
  444. /* funcao para instrucoes com a opcao de voltar para o menu ou sair do jogo */
  445.  
  446. int instrucoes(){
  447. char c;
  448. c = '0';
  449. system("cls");
  450. printf(" +-+-+-+-+-+-+-+-+-+-+\n |I|n|s|t|r|u|c|o|e|s|\n +-+-+-+-+-+-+-+-+-+-+\n\n\n");
  451. 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\n\nDigite 1 para voltar para o menu e 2 para sair do jogo.");
  452. while ((c != 49) && (c != 50)){
  453. c = getch();
  454. }
  455. if (c == 49){
  456. return 1;
  457. }
  458. return 0;
  459. }
  460.  
  461. /* funcao para o menu utilizando switch para incializar a funcao correspondente */
  462.  
  463. int menu(){
  464. system("cls");
  465. char c;
  466. c = '0';
  467. printf(".##.....##.########.##....##.##.....##\n.###...###.##.......###...##.##.....##\n.####.####.##.......####..##.##.....##\n.##.###.##.######...##.##.##.##.....##\n.##.....##.##.......##..####.##.....##\n.##.....##.##.......##...###.##.....##\n.##.....##.########.##....##..#######.\n\n");
  468. printf("1 - Jogar\n2 - Instrucoes\n3 - Configuracoes (Indisponivel)\n4 - Ranking (Indisponivel)\n5 - Sair\n Digite o numero correspondente a opcao desejada: ");
  469. scanf("\n%c", &c);
  470. while ((c != '1') && (c != '2') /*&& (c != '3') && (c != '4')*/ && (c != '5')){
  471. printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
  472. scanf("\n%c", &c);
  473. }
  474. switch (c){
  475. case '1':
  476. system("cls");
  477. if (jogar()){ /*se jogar retorna 0, menu retorna 0 e sai do jogo */
  478. return 1;
  479. }
  480. return 0;
  481. case '2':
  482. system("cls");
  483. if (instrucoes()){ /* se instrucoes retorna 1, menu retorna 1 e a main faz voltar para o menu */
  484. return 1;
  485. }
  486. system("cls");
  487. return 0;
  488. /*
  489. case '3':
  490. configuracoes();
  491. case '4':
  492. ranking(); */
  493. case '5':
  494. system("cls");
  495. return 0;
  496. }
  497.  
  498. }
  499.  
  500. int main () {
  501. if (inicial()){
  502. system("cls");
  503. }
  504. srand(time(0));
  505. while(menu()){}
  506. return 0;
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement