Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define TAM 4
- typedef struct
- {
- int linha, coluna;
- } Posicao;
- typedef struct
- {
- int mat[TAM][TAM];
- int maiorNum[1];
- int menorNum[1];
- } Tabuleiro;
- void removeEnter(char v[])
- {
- int i;
- for(i = 0; v[i] != '\0'; i++)
- if(v[i] == '\n')
- {
- v[i] = '\0'; // substitui o '\n' por '\0'
- break;
- }
- }
- //retorta 1 caso sejam iguais, 0 caso não
- int matrizesIguais(int mat1[][TAM], int mat2[][TAM])
- {
- int i, j;
- for(i = 0; i < TAM; i++)
- for(j = 0; j < TAM; j++)
- if(mat1[i][j] != mat2[i][j])
- return 0;
- return 1;
- }
- //retorta 1 caso sejam iguais, 0 caso não
- int stringsIguais(char v[], char s[])
- {
- int i;
- for(i = 0; v[i] != '\0' && s[i] != '\0'; i++)
- if (v[i] != s[i])
- return 0;
- return 1;
- }
- Tabuleiro atualizaTabuleiro(Tabuleiro tab, char opcao)
- {
- int i, j, k;
- switch(opcao)
- {
- case 'w':
- case 'W':
- for(j = 0; j < TAM ; j++) //lê as colunas da esquerda para a direita
- {
- for(i = 0; i < TAM ; i++) //lê as linhas de cima para baixo
- {
- if(!tab.mat[i][j]) //caso o elemento seja nulo
- {
- for(k = i + 1; k < TAM ; k++) //percorre aquela coluna ate achar um elemento nao-nulo
- if(tab.mat[k][j])
- {
- tab.mat[i][j] = tab.mat[k][j]; //coloca o elemento nao nulo no primeiro elemento nulo daquela coluna
- tab.mat[k][j] = 0; //o lugar do elemento nao nulo torna-se nulo
- break;
- }
- }
- }
- for(i = 0; i < TAM - 1; i++) // soma os numeros caso eles sejam iguais
- {
- if(tab.mat[i + 1][j] == tab.mat[i][j])
- {
- tab.mat[i][j] *= 2;
- for(k = i + 1; k < TAM - 1 && tab.mat[k + 1][j] != 0; k++)
- tab.mat[k][j] = tab.mat[k + 1][j];
- tab.mat[k][j] = 0;
- }
- }
- }
- break;
- case 'a':
- case 'A':
- for(i = 0; i < TAM ; i++)// lê as linhas de cima para baixo
- {
- for(j = 0; j < TAM ; j++) //lê as colunas da esquerda para a direita
- {
- if(!tab.mat[i][j]) //caso o elemento seja nulo
- {
- for(k = j + 1; k < TAM ; k++) //percorre aquela linha ate achar um elemento nao-nulo
- if(tab.mat[i][k])
- {
- tab.mat[i][j] = tab.mat[i][k]; //coloca o elemento nao nulo no primeiro elemento nulo daquela linha
- tab.mat[i][k] = 0; //o lugar do elemento nao nulo torna-se nulo
- break;
- }
- }
- }
- for(j = 0; j < TAM - 1; j++)
- {
- if(tab.mat[i][j + 1] == tab.mat[i][j])
- {
- tab.mat[i][j] *= 2;
- for(k = j + 1; k < TAM - 1 && tab.mat[i][k + 1] != 0; k++)
- tab.mat[i][k] = tab.mat[i][k + 1];
- tab.mat[i][k] = 0;
- }
- }
- }
- break;
- case 's':
- case 'S':
- for(j = 0; j < TAM ; j++) //lê as colunas da esquerda para a direita
- {
- for(i = TAM - 1; i >= 0 ; i--) //lê as linhas da baixo para cima
- {
- if(!tab.mat[i][j]) //caso o elemento seja nulo
- {
- for(k = i - 1 ; k >= 0 ; k--) //percorre aquela coluna de baixo para cima ate achar um elemento nao-nulo
- if(tab.mat[k][j])
- {
- tab.mat[i][j] = tab.mat[k][j]; //coloca o elemento nao nulo no ultimo elemento nulo daquela coluna
- tab.mat[k][j] = 0; //o lugar do elemento nao nulo torna-se nulo
- break;
- }
- }
- }
- for(i = TAM - 1; i > 0 ; i--)
- {
- if(tab.mat[i - 1][j] == tab.mat[i][j])
- {
- tab.mat[i][j] *= 2;
- for(k = i - 1; k > 0 && tab.mat[k - 1][j] != 0; k--)
- tab.mat[k][j] = tab.mat[k - 1][j];
- tab.mat[k][j] = 0;
- }
- }
- }
- break;
- case 'd':
- case 'D':
- for(i = 0; i < TAM ; i++) // lê as linhas de cima para baixo
- {
- for(j = TAM - 1; j >= 0 ; j--) //lê as colunas da direita para a esquerda
- {
- if(!tab.mat[i][j]) //caso o elemento seja nulo
- {
- for(k = j - 1; k >= 0; k--) //percorre aquela linha da direita para a esquerda ate achar um elemento nao-nulo
- if(tab.mat[i][k])
- {
- tab.mat[i][j] = tab.mat[i][k]; //coloca o elemento nao nulo no ultimo elemento nulo daquela linha
- tab.mat[i][k] = 0; //o lugar do elemento nao nulo torna-se nulo
- break;
- }
- }
- }
- for(j = TAM - 1; j > 0 ; j--)
- {
- if(tab.mat[i][j - 1] == tab.mat[i][j])
- {
- tab.mat[i][j] *= 2;
- for(k = j - 1; k > 0 && tab.mat[i][k - 1] != 0; k--)
- tab.mat[i][k] = tab.mat[i][k - 1];
- tab.mat[i][k] = 0;
- }
- }
- }
- break;
- }
- return tab;
- }
- void imprimeTabuleiro(int mat[][TAM])
- {
- printf("\n");
- printf("\t\t16\n");
- printf("\t ======\n\n");
- int i, j;
- for(i = 0; i < TAM; i++)
- {
- printf("\t ");
- for(j = 0; j < TAM; j++)
- if (mat[i][j] != 0)
- {
- printf("|%3d ", mat[i][j]);
- }
- else
- printf("| ");
- printf("|\n");
- }
- printf("\n\n");
- printf("\t\t^\n");
- printf("\t\tw\n");
- printf("\t <a s>\n");
- printf("\t\td\n");
- printf("\t\tv");
- }
- //percorre a matriz até achar o maior e o menor numero dela
- Tabuleiro checaMaiorMenor(Tabuleiro tab)
- {
- int i, j;
- tab.maiorNum[0] = 0;
- tab.menorNum[0] = 0;
- for(i = 0; i < TAM; i++)
- {
- for(j = 0; j < TAM; j++)
- {
- if (tab.maiorNum[0] < tab.mat[i][j])
- tab.maiorNum[0] = tab.mat[i][j];
- if (tab.menorNum[0] > tab.mat[i][j])
- tab.menorNum[0] = tab.mat[i][j];
- }
- }
- return tab;
- }
- Tabuleiro jogo()
- {
- Tabuleiro tab, controle, controle1; //variavel do tabuleiro e duas variaveis de controle para comparação
- char direcao; //direção de rotação do tabuleiro escolhida pelo jogador
- //variaveis auxiliares
- int novoNum;
- int i, j;
- Posicao posicao;
- srand(time(NULL));
- //preenche o tabuleiro com 0
- for(i = 0; i < TAM; i++)
- {
- for(j = 0; j < TAM; j++)
- {
- tab.mat[i][j] = 0;
- }
- }
- controle = tab;
- /*
- Creio que o problema está nesse próximo do-while
- quando o tabuleiro está cheio, ao invés de ele simplesmente sair do do-while
- e dar game over ele trava e fica parado aqui mesmo
- */
- do
- {
- tab = checaMaiorMenor(tab);
- //gera um 2 ou um 4 para colocar no tabuleiro
- novoNum = rand() % 2 + 1;
- novoNum *= 2;
- //procura uma posição nula aleatória para armazenar o novo numero gerado
- if (tab.menorNum[0] == 0)
- {
- do
- {
- posicao.linha = rand() % TAM;
- posicao.coluna = rand() % TAM;
- }
- while(tab.mat[posicao.linha][posicao.coluna]);
- }
- tab.mat[posicao.linha][posicao.coluna] = novoNum;
- printf("\n\n");
- system("cls");
- imprimeTabuleiro(tab.mat);
- controle = tab;
- do
- {
- scanf("%c%*c", &direcao);
- while(direcao != 'w' && direcao != 'a' && direcao != 's' && direcao != 'd' && direcao != 'W' && direcao != 'A' && direcao != 'S' && direcao != 'D')
- {
- scanf("%c%*c", &direcao);
- }
- tab = atualizaTabuleiro(tab, direcao);
- //rotaciona controle1 para verificar se existe algum movimento possivel
- controle1 = tab;
- controle1 = atualizaTabuleiro(controle1, 'w');
- controle1 = atualizaTabuleiro(controle1, 'a');
- controle1 = atualizaTabuleiro(controle1, 's');
- controle1 = atualizaTabuleiro(controle1, 'd');
- }
- //faz tudo isso enquanto há movimentos possíveis e a movimentação não mudar o tabuleiro
- //(ex.: a pessoa aperta w 500x mesmo não existindo mais nenhum movimento apertando a tecla w)
- while(matrizesIguais(tab.mat, controle.mat) && !(matrizesIguais(tab.mat, controle1.mat)));
- //imprime o novo tabuleiro rotacionado
- system("cls");
- imprimeTabuleiro(tab.mat);
- tab = checaMaiorMenor(tab);
- }
- //verifica se a pessoa terminou o jogo ou se não há mais movimentos possíveis
- while(tab.maiorNum[0] < 16 && tab.menorNum[0] == 0);
- system("cls");
- return tab;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement