Advertisement
Jvsierra

Number Slide Puzzle

Jun 7th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include "meuconio.h"
  5. #include <time.h>
  6.  
  7. #define TF 4
  8.  
  9. int linhaPos, colunaPos;
  10. int mat[TF][TF], valores[TF * TF];
  11.  
  12. int checaResultado();
  13. void geraBoard();
  14. void gameLoop();
  15. void imprimeMatriz();
  16.  
  17. int main()
  18. {
  19.     srand(time(NULL));
  20.    
  21.     gameLoop();
  22.    
  23.     getch();
  24. }
  25.  
  26. int checaResultado()
  27. {
  28.     int i, j, cont, resCerto, ret, stop;
  29.    
  30.     cont = 0;
  31.     resCerto = 1;
  32.    
  33.     if(linhaPos == TF - 1 && colunaPos == TF - 1)
  34.     {
  35.         stop = 0;
  36.        
  37.         for(i = 0; i < TF && stop == 0; i++)
  38.             for(j = 0; j < TF && stop == 0; j++)
  39.             if(i != linhaPos && j != colunaPos)
  40.                 if(mat[i][j] == resCerto)
  41.                     cont++;
  42.                 else
  43.                     stop = 1;
  44.                    
  45.         if(cont == TF * TF - 1)
  46.             ret = 1;
  47.         else
  48.             ret = 0;
  49.     }
  50.     else
  51.         ret = 0;
  52.  
  53.  
  54.         return ret;
  55. }
  56.  
  57. void geraBoard()
  58. {
  59.     int linha, coluna, i, j, pos, val, tlValores;
  60.    
  61.     linha = rand() % TF;
  62.     coluna = rand() % TF;
  63.    
  64.     linhaPos = linha;
  65.     colunaPos = coluna;
  66.    
  67.     mat[linha][coluna] = 0;
  68.    
  69.     tlValores = 0;
  70.    
  71.     for(i = 0; i < TF; i++)
  72.         for(j = 0; j < TF; j++)
  73.             if(i != linhaPos || j != colunaPos)
  74.             {
  75.                 do
  76.                 {
  77.                     val = rand() % (TF * TF - 1) + 1;
  78.                    
  79.                     pos = 0;
  80.                    
  81.                     while(pos < tlValores && valores[pos] != val)
  82.                         pos++;
  83.                 }while(pos < tlValores);
  84.                
  85.                 mat[i][j] = val;
  86.                
  87.                 valores[tlValores] = val;
  88.                 tlValores++;
  89.             }
  90. }
  91.  
  92. void gameLoop()
  93. {
  94.     char dir;
  95.     int resultado;
  96.    
  97.     geraBoard();
  98.    
  99.     imprimeMatriz();
  100.    
  101.     fflush(stdin);
  102.     scanf("%c", &dir);
  103.    
  104.     resultado = checaResultado();
  105.    
  106.     while(resultado == 0)
  107.     {
  108.         switch(dir)
  109.         {
  110.             case 'C': case 'c':
  111.                 if(linhaPos < TF - 1)
  112.                 {
  113.                     mat[linhaPos][colunaPos] = mat[linhaPos + 1][colunaPos];
  114.                     mat[linhaPos + 1][colunaPos] = 0;
  115.                    
  116.                     linhaPos++;
  117.                 }
  118.             break;
  119.             case 'B': case 'b':
  120.                 if(linhaPos > 0)
  121.                 {
  122.                     mat[linhaPos][colunaPos] = mat[linhaPos - 1][colunaPos];
  123.                     mat[linhaPos - 1][colunaPos] = 0;
  124.                    
  125.                     linhaPos--;
  126.                 }
  127.             break;
  128.             case 'E': case 'e':
  129.                 if(colunaPos < TF - 1)
  130.                 {
  131.                     mat[linhaPos][colunaPos] = mat[linhaPos][colunaPos + 1];
  132.                     mat[linhaPos][colunaPos + 1] = 0;
  133.                    
  134.                     colunaPos++;
  135.                 }
  136.             break;
  137.             case 'D': case 'd':
  138.                 if(colunaPos > 0)
  139.                 {
  140.                     mat[linhaPos][colunaPos] = mat[linhaPos][colunaPos - 1];
  141.                     mat[linhaPos][colunaPos - 1] = 0;
  142.                    
  143.                     colunaPos--;
  144.                 }
  145.             break;
  146.             default:
  147.                 printf("Opcao invalida\n");
  148.         }
  149.        
  150.         imprimeMatriz();
  151.        
  152.         resultado = checaResultado();
  153.        
  154.         printf("%d\n", resultado);
  155.        
  156.         if(resultado == 0)
  157.         {
  158.             fflush(stdin);
  159.             scanf("%c", &dir);
  160.         }
  161.     }
  162.    
  163. }
  164.  
  165. void imprimeMatriz()
  166. {
  167.     int i, j;
  168.    
  169.     clrscr();
  170.    
  171.     for(i = 0; i < TF; i++)
  172.     {
  173.         for(j = 0; j < TF; j++)
  174.                 printf("%6d ", mat[i][j]);
  175.        
  176.         printf("\n");
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement