Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Universidade de Brasilia
- Instituto de Ciencias Exatas
- Departamento de Ciencia da Computacao
- Algoritmos e Programação de Computadores – 2/2018
- Aluno(a): Murilo Ferreira Pires
- Matricula: 180128361
- Turma: A
- Versao do compilador: <versao utilizada>
- Descricao: Um programa que tem como objetivo implementar um jogo em um grid 2D representado por uma matriz similar ao jogo Puyo Puyo,
- porem em versao DIY (Do it Yourself, ou seja, Faca voce mesmo). O jogo possui diversos “tijolos” aleatorios, que cairao de posicoes
- randomicas a partir do topo do tabuleiro. A partir do momento em que o tijolo esta caindo, o jogador pode controla-lo horizontalmente,
- inverter a ordem das pecas, vira-los em posicao vertical e faze-los descer rapido. O jogador tem como objetivo conseguir o maior numero
- de pontos antes do fim do jogo, que acaba no momento em que os tijolos alcancam o topo do grid.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define GREEN "\x1b[32m"
- #define YELLOW "\x1b[33m"
- #define BLUE "\x1b[34m"
- #define MAGENTA "\x1b[35m"
- #define CYAN "\x1b[36m"
- #define RESET "\x1b[0m"
- #define RED "\x1b[31m"
- /* 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 */
- #ifndef _WIN32
- #define cls “CLEAR”
- int kbhit(){
- struct termios oldt, newt;
- int ch, oldf;
- tcgetattr(STDIN_FILENO,&oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
- fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- fcntl(STDIN_FILENO, F_SETFL, oldf);
- if(ch != EOF){
- ungetc(ch,stdin);
- return 1;
- }
- return 0;
- }
- int getch(void) {
- int ch;
- struct termios oldt;
- struct termios newt;
- tcgetattr(STDIN_FILENO,&oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- return ch;
- }
- #else
- #include <conio.h>
- #endif
- /* Variaveis globais */
- int al = 13;
- int la = 8;
- int x1 = 1, y1 = 0, x2 = 2, y2 = 0;
- char tabu[8][13];
- int state = 1;
- int pg = 1;
- char p1, p2;
- int velocidade = 720000;
- /* funcao para a tela inicial */
- int inicial(){
- char c;
- c = '0';
- printf (".########..##.....##.##....##..#######.....########..##.....##.##....##..#######.\n.##.....##.##.....##..##..##..##.....##....##.....##.##.....##..##..##..##.....##\n.##.....##.##.....##...####...##.....##....##.....##.##.....##...####...##.....##\n.########..##.....##....##....##.....##....########..##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##.........#######.....##.....#######.....##.........#######.....##.....#######.\n\n\n\n\n Pressione <enter> para jogar!");
- while (c != 13){
- c = getch();
- }
- return 1;
- }
- /*funcao para criar as pecas compostas */
- void criachar(){
- p1 = 'A' + rand() % 5;
- p2 = 'A' + rand() % 5;
- }
- /* zerar o tabuleiro com # e ' ' */
- void zeratabu(void){
- int x;
- int y;
- for (y=0; y < al; y++){
- for(x=0; x < la; x++){
- if ((y == 12) || (x == 7) || (x == 0)){
- tabu[x][y] = '#';
- }
- else{
- tabu[x][y] = ' ';
- }
- }
- }
- }
- /* atualiza a tela */
- void attframe(void){
- system("cls");
- int x, y;
- for (y = 0; y < al; y++){
- for (x=0; x < la; x++){
- printf("%c", tabu[x][y]);
- }
- printf("\n");
- }
- }
- /* salva as pecas no tabuleiro */
- void saveframe(int x){
- if (x == 1 && p1 != '0'){
- tabu[x1][y1] = p1;
- }
- if (x == 2 && p2 != '0'){
- tabu[x2][y2] = p2;
- }
- if (x == 3 && p1 != '0' && p2 != '0'){
- tabu[x1][y1] = p1;
- tabu[x2][y2] = p2;
- }
- }
- /* funcao que gira as pecas */
- void girar(){
- if (pg == 0){
- return;
- }
- if (state == 1 && tabu[x2][y2+1] == ' '){
- tabu[x1][y1] = ' ';
- tabu[x2][y2] = p1;
- x1 = x2;
- y2 = y1 + 1;
- saveframe(3);
- attframe();
- state++;
- return;
- }
- if (state == 2 && tabu[x1-1][y1] == ' '){
- tabu[x2][y2] = ' ';
- x2--;
- y2--;
- saveframe(2);
- attframe();
- state++;
- return;
- }
- if (state == 3 && tabu[x1][y1+1] == ' '){
- tabu[x2][y2] = ' ';
- x2++;
- y1++;
- saveframe(3);
- attframe();
- state++;
- return;
- }
- if(state == 4 && tabu[x2-1][y2] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- y1--;
- saveframe(1);
- attframe();
- state = 1;
- return;
- }
- return;
- }
- /*movimento para baixo automatico */
- void smove(void){
- if (p1 == '0'){
- tabu[x2][y2] = ' ';
- y2++;
- saveframe(2);
- attframe();
- return;
- }
- if (p2 == '0'){
- tabu[x1][y1] = ' ';
- y1++;
- saveframe(1);
- attframe();
- return;
- }
- tabu[x1][y1] = ' ';
- tabu[x2][y2] = ' ';
- y2++;
- y1++;
- saveframe(3);
- attframe();
- }
- /*
- int check(){
- int x, y;
- for (x = 0; x < la; x++){
- for (y=0; y < al; y++){
- if(tabu[x][y]);
- }
- }
- }
- */
- /* ve se alguma das pecas colidiu com alguma coisa */
- int colisao(){
- if (p2 != '0' && p1 != '0'){
- if (state == 1 || state == 3){
- if (tabu[x1][y1+1] != ' ' && tabu[x2][y2+1] != ' '){
- if(y1 == al){
- return 4;
- }
- return 3;
- }
- }
- if (state == 2){
- if (tabu[x2][y2+1] != ' '){
- if (y1 == al){
- return 4;
- }
- return 3;
- }
- return 0;
- }
- if (state == 4){
- if (tabu[x1][y1+1] != ' '){
- if (y2 == al){
- return 4;
- }
- return 3;
- }
- return 0;
- }
- }
- if (tabu[x2][y2+1] != ' ' && p2 != '0'){
- if (y2 == al){
- return 4;
- }
- return 2;
- }
- if (tabu[x1][y1+1] != ' ' && p1 != '0'){
- if (y1 == al){
- return 4;
- }
- return 1;
- }
- return 0;
- }
- /* move a(s) peca(s) para a esquerda */
- void mleft(void){
- if (x2 < x1 && p2 != '0'){
- if(tabu[x2-1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2--;
- saveframe(2);
- if (p1 != '0' && tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- saveframe(1);
- }
- }
- }
- if (x1 < x2 && p1 != '0'){
- if(tabu[x1-1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- saveframe(1);
- if (p2 != '0' && tabu[x2-1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2--;
- saveframe(2);
- }
- }
- }
- if (x2 == x1){
- if(tabu[x2-1][y2] == ' ' && p2 != '0'){
- tabu[x2][y2] = ' ';
- x2--;
- saveframe(2);
- }
- if (p1 != '0' && tabu[x1-1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- saveframe(1);
- }
- }
- }
- /* move a(s) peca(s) para a direita */
- void mright(void){
- if (x2 > x1 && p2 != '0'){
- if(tabu[x2+1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2++;
- saveframe(2);
- if (p1 != '0' && tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1++;
- saveframe(1);
- }
- }
- }
- if (x1 > x2 && p1 != '0'){
- if(tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1 = x1 + 1;
- saveframe(1);
- if (p2 != '0' && tabu[x2+1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2 = x2 + 1;
- saveframe(2);
- }
- }
- }
- if (x2 == x1){
- if(tabu[x2+1][y2] == ' ' && p2 != '0'){
- tabu[x2][y2] = ' ';
- x2 = x2 + 1;
- saveframe(2);
- }
- if (p1 != '0' && tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1 = x1 + 1;
- saveframe(1);
- }
- }
- }
- /*controla as inputs do jogador */
- void acao(){
- char c;
- c = '0';
- while ((c != ' ') && (c != 'a') && (c != 's') && (c != 'd')){
- c = getch();
- }
- if(c == ' '){
- if (pg == 1){
- girar();
- attframe();
- }
- }
- if (c == 'a'){
- mleft();
- attframe();
- }
- if (c == 'd'){
- mright();
- attframe();
- }
- if (c == 's'){
- if (colisao() == 0){
- smove();
- attframe;
- }
- }
- }
- /*essa funcao ve se alguma peca chegou ao topo da grid */
- int gameon(){
- int x;
- for (x = 1; x < la - 1; x++){
- if (tabu[x][0] != ' ' && tabu[x][1] != ' '){
- return 0;
- }
- }
- return 1;
- }
- int game(){
- attframe();
- criachar();
- x1 = 1; y1 = 0; x2 = x1 + 1; y2 = y1;
- pg = 1;
- state = 1;
- saveframe(3);
- attframe();
- while (gameon()){
- while(colisao() == 0){
- if(kbhit()){
- acao();
- saveframe(3);
- attframe();
- break;
- }
- usleep(velocidade);
- smove();
- }
- if (colisao() == 3){ /* as 2 pecas colidiram */
- saveframe(3);
- p1 = '0';
- p2 = '0';
- }
- if (colisao() == 2){ /* p2 colidiu */
- pg = 0;
- saveframe(2);
- p2 = '0';
- }
- if (colisao() == 1){ /* p1 colidiu */
- pg = 0;
- saveframe(1);
- p1 = '0';
- }
- saveframe(3);
- attframe();
- /*
- while (check()){
- attframe();
- score();
- saveframe(3);
- attframe();
- }
- */
- if(p1 == '0' && p2 == '0' && gameon != 0){
- return 1;
- }
- }
- return 0;
- }
- /* A funcao do jogo em si */
- int jogar(){
- int p1, p2;
- int x1 = 1, y1 = 1, x2 = x1 + 1, y2 = y1;
- zeratabu();
- while(game()){}
- return 0; /*retornar 0 sai do jogo, retornar 1 volta pro menu */
- }
- /* funcao para instrucoes com a opcao de voltar para o menu ou sair do jogo */
- int instrucoes(){
- char c;
- c = '0';
- system("cls");
- printf(" +-+-+-+-+-+-+-+-+-+-+\n |I|n|s|t|r|u|c|o|e|s|\n +-+-+-+-+-+-+-+-+-+-+\n\n\n");
- 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.");
- while ((c != 49) && (c != 50)){
- c = getch();
- }
- if (c == 49){
- return 1;
- }
- return 0;
- }
- /* funcao para o menu utilizando switch para incializar a funcao correspondente */
- int menu(){
- system("cls");
- char c;
- c = '0';
- printf(".##.....##.########.##....##.##.....##\n.###...###.##.......###...##.##.....##\n.####.####.##.......####..##.##.....##\n.##.###.##.######...##.##.##.##.....##\n.##.....##.##.......##..####.##.....##\n.##.....##.##.......##...###.##.....##\n.##.....##.########.##....##..#######.\n\n");
- printf("1 - Jogar\n2 - Instrucoes\n3 - Configuracoes (Indisponivel)\n4 - Ranking (Indisponivel)\n5 - Sair\n Digite o numero correspondente a opcao desejada: ");
- scanf("\n%c", &c);
- while ((c != '1') && (c != '2') /*&& (c != '3') && (c != '4')*/ && (c != '5')){
- printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
- scanf("\n%c", &c);
- }
- switch (c){
- case '1':
- system("cls");
- if (jogar()){ /*se jogar retorna 0, menu retorna 0 e sai do jogo */
- return 1;
- }
- return 0;
- case '2':
- system("cls");
- if (instrucoes()){ /* se instrucoes retorna 1, menu retorna 1 e a main faz voltar para o menu */
- return 1;
- }
- system("cls");
- return 0;
- /*
- case '3':
- configuracoes();
- case '4':
- ranking(); */
- case '5':
- system("cls");
- return 0;
- }
- }
- int main () {
- if (inicial()){
- system("cls");
- }
- srand(time(0));
- while(menu()){}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement