Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include "meuconio.h"
- #include <time.h>
- #define TF 4
- int linhaPos, colunaPos;
- int mat[TF][TF], valores[TF * TF];
- int checaResultado();
- void geraBoard();
- void gameLoop();
- void imprimeMatriz();
- int main()
- {
- srand(time(NULL));
- gameLoop();
- getch();
- }
- int checaResultado()
- {
- int i, j, cont, resCerto, ret, stop;
- cont = 0;
- resCerto = 1;
- if(linhaPos == TF - 1 && colunaPos == TF - 1)
- {
- stop = 0;
- for(i = 0; i < TF && stop == 0; i++)
- for(j = 0; j < TF && stop == 0; j++)
- if(i != linhaPos && j != colunaPos)
- if(mat[i][j] == resCerto)
- cont++;
- else
- stop = 1;
- if(cont == TF * TF - 1)
- ret = 1;
- else
- ret = 0;
- }
- else
- ret = 0;
- return ret;
- }
- void geraBoard()
- {
- int linha, coluna, i, j, pos, val, tlValores;
- linha = rand() % TF;
- coluna = rand() % TF;
- linhaPos = linha;
- colunaPos = coluna;
- mat[linha][coluna] = 0;
- tlValores = 0;
- for(i = 0; i < TF; i++)
- for(j = 0; j < TF; j++)
- if(i != linhaPos || j != colunaPos)
- {
- do
- {
- val = rand() % (TF * TF - 1) + 1;
- pos = 0;
- while(pos < tlValores && valores[pos] != val)
- pos++;
- }while(pos < tlValores);
- mat[i][j] = val;
- valores[tlValores] = val;
- tlValores++;
- }
- }
- void gameLoop()
- {
- char dir;
- int resultado;
- geraBoard();
- imprimeMatriz();
- fflush(stdin);
- scanf("%c", &dir);
- resultado = checaResultado();
- while(resultado == 0)
- {
- switch(dir)
- {
- case 'C': case 'c':
- if(linhaPos < TF - 1)
- {
- mat[linhaPos][colunaPos] = mat[linhaPos + 1][colunaPos];
- mat[linhaPos + 1][colunaPos] = 0;
- linhaPos++;
- }
- break;
- case 'B': case 'b':
- if(linhaPos > 0)
- {
- mat[linhaPos][colunaPos] = mat[linhaPos - 1][colunaPos];
- mat[linhaPos - 1][colunaPos] = 0;
- linhaPos--;
- }
- break;
- case 'E': case 'e':
- if(colunaPos < TF - 1)
- {
- mat[linhaPos][colunaPos] = mat[linhaPos][colunaPos + 1];
- mat[linhaPos][colunaPos + 1] = 0;
- colunaPos++;
- }
- break;
- case 'D': case 'd':
- if(colunaPos > 0)
- {
- mat[linhaPos][colunaPos] = mat[linhaPos][colunaPos - 1];
- mat[linhaPos][colunaPos - 1] = 0;
- colunaPos--;
- }
- break;
- default:
- printf("Opcao invalida\n");
- }
- imprimeMatriz();
- resultado = checaResultado();
- printf("%d\n", resultado);
- if(resultado == 0)
- {
- fflush(stdin);
- scanf("%c", &dir);
- }
- }
- }
- void imprimeMatriz()
- {
- int i, j;
- clrscr();
- for(i = 0; i < TF; i++)
- {
- for(j = 0; j < TF; j++)
- printf("%6d ", mat[i][j]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement