cs-lazaro

fifteen.c

Aug 28th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.21 KB | None | 0 0
  1. /**
  2.  * fifteen.c
  3.  *
  4.  * Implements Game of Fifteen (generalized to d x d).
  5.  *
  6.  * Usage: fifteen d
  7.  *
  8.  * whereby the board's dimensions are to be d x d,
  9.  * where d must be in [DIM_MIN,DIM_MAX]
  10.  *
  11.  * Note that usleep is obsolete, but it offers more granularity than
  12.  * sleep and is simpler to use than nanosleep; `man usleep` for more.
  13.  */
  14.  
  15. #define _XOPEN_SOURCE 500
  16.  
  17. #include <cs50.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21.  
  22. // constants
  23. #define DIM_MIN 3
  24. #define DIM_MAX 9
  25.  
  26. // board
  27. int board[DIM_MAX][DIM_MAX];
  28.  
  29. // dimensions
  30. int d;
  31.  
  32. // prototypes
  33. void clear(void);
  34. void greet(void);
  35. void init(void);
  36. void draw(void);
  37. bool move(int tile);
  38. bool won(void);
  39.  
  40. int main(int argc, string argv[])
  41. {
  42.     // ensure proper usage
  43.     if (argc != 2)
  44.     {
  45.         printf("Usage: fifteen d\n");
  46.         return 1;
  47.     }
  48.  
  49.     // ensure valid dimensions
  50.     d = atoi(argv[1]);
  51.     if (d < DIM_MIN || d > DIM_MAX)
  52.     {
  53.         printf("Board must be between %i x %i and %i x %i, inclusive.\n",
  54.             DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
  55.         return 2;
  56.     }
  57.    
  58.     // open log
  59.     FILE *file = fopen("log.txt", "w");
  60.     if (file == NULL)
  61.     {
  62.         return 3;
  63.     }
  64.  
  65.     // greet user with instructions
  66.     greet();
  67.  
  68.     // initialize the board
  69.     init();
  70.  
  71.     // accept moves until game is won
  72.     while (true)
  73.     {
  74.         // clear the screen
  75.         clear();
  76.  
  77.         // draw the current state of the board
  78.         draw();
  79.  
  80.         // log the current state of the board (for testing)
  81.         for (int i = 0; i < d; i++)
  82.         {
  83.             for (int j = 0; j < d; j++)
  84.             {
  85.                 fprintf(file, "%i", board[i][j]);
  86.                 if (j < d - 1)
  87.                 {
  88.                     fprintf(file, "|");
  89.                 }
  90.             }
  91.             fprintf(file, "\n");
  92.         }
  93.         fflush(file);
  94.  
  95.         // check for win
  96.         if (won())
  97.         {
  98.             printf("ftw!\n");
  99.             break;
  100.         }
  101.        
  102.  
  103.         // prompt for move
  104.         printf("Tile to move: ");
  105.         int tile = get_int();
  106.        
  107.         // quit if user inputs 0 (for testing)
  108.         if (tile == 0)
  109.         {
  110.             break;
  111.         }
  112.  
  113.         // log move (for testing)
  114.         fprintf(file, "%i\n", tile);
  115.         fflush(file);
  116.  
  117.         // move if possible, else report illegality
  118.         if (!move(tile))
  119.         {
  120.             printf("\nIllegal move.\n");
  121.             usleep(500000);
  122.         }
  123.  
  124.         // sleep thread for animation's sake
  125.         usleep(500000);
  126.     }
  127.    
  128.     // close log
  129.     fclose(file);
  130.  
  131.     // success
  132.     return 0;
  133. }
  134.  
  135. /**
  136.  * Clears screen using ANSI escape sequences.
  137.  */
  138. void clear(void)
  139. {
  140.     printf("\033[2J");
  141.     printf("\033[%d;%dH", 0, 0);
  142.  //   printf("\033[%d;%dm", 35, 01);
  143. }
  144.  
  145. /**
  146.  * Greets player.
  147.  */
  148. void greet(void)
  149. {
  150.     clear();
  151.     printf("WELCOME TO GAME OF FIFTEEN\n");
  152.     usleep(2000000);
  153. }
  154.  
  155. /**
  156.  * Initializes the game's board with tiles numbered 1 through d*d - 1
  157.  * (i.e., fills 2D array with values but does not actually print them).  
  158.  */
  159. void init(void)
  160. {
  161.    /* switch(d)  
  162.     {
  163.         case 3:
  164.             int tiles[3][3] = {8,7,6,5,4,3,2,1,0};
  165.        
  166.         case 4:
  167.             int tiles[4][4] = {15,14,13,12,11,10,9,8,7,6,5,4,3,1,2,0};
  168.        
  169.         case 5:
  170.             int tiles[5][5] = {24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
  171.            
  172.         case 6:
  173.             int tiles[6][6] = {35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,1,2,0};
  174.            
  175.         case 7:
  176.             int tiles[7][7] = {48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,
  177.                             15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
  178.                            
  179.         case 8:
  180.             int tiles[8][8] = {63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,
  181.                             30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,1,2,0};
  182.        
  183.         case 9:
  184.             int tiles[9][9] = {80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,
  185.                             47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,
  186.                             13,12,11,10,9,8,7,65,4,3,2,1,0};
  187.     }*/
  188.      if ((d*d-1)%2 != 0)
  189.         {
  190.             for (int i=0; i<d; i++)
  191.             {
  192.                 for (int j=0; j<d; j++)
  193.                 {
  194.                     board[i][j] = (d*d - 1)-(d*i + j);
  195.                 }
  196.             }
  197.             int temporary;
  198.             temporary = board[d-1] [d-2];
  199.             board[d-1][d-2] = board[d-1] [d-3];
  200.             board[d-1][d-3] = temporary;
  201.         }
  202.     else
  203.     {
  204.         for (int i=0; i<d; i++)
  205.         {
  206.             for (int j=0; j<d; j++)
  207.             {
  208.                 board[i][j] = (d*d - 1)-(d*i+j);
  209.             }
  210.         }
  211.     }
  212. }
  213.  
  214. /**
  215.  * Prints the board in its current state.
  216.  */
  217. void draw(void)
  218. {
  219.     for (int i=0; i<d; i++)
  220.     {
  221.         for (int j=0; j<d; j++)
  222.         {
  223.            if (board[i][j] < 10 && board[i][j] > 0)
  224.             {
  225.                 printf("%2i", board[i][j]);
  226.             }
  227.             else if (board[i][j] == 0)
  228.             {
  229.                 printf(" _");
  230.             }
  231.             else
  232.             {
  233.                 printf("%i", board[i][j]);
  234.             }
  235.            
  236.             if (j < d - 1)
  237.             {
  238.                 printf("|");
  239.             }
  240.         }
  241.         printf("\n");
  242.     }
  243. }
  244.  
  245. /**
  246.  * If tile borders empty space, moves tile and returns true, else
  247.  * returns false.
  248.  */
  249. bool move(int tile)
  250. {
  251.     for (int i=0; i<d; i++)
  252.     {
  253.         for (int j=0; j<d; j++)
  254.         {
  255.             if (tile == board[i][j])
  256.             {
  257.               if (board[i-1][j] == 0)
  258.               {
  259.                   int temporary = board[i][j];
  260.                   board[i][j] = board[i-1][j];
  261.                   board[i-1][j] = temporary;
  262.                   return true;
  263.               }
  264.              
  265.               else if (board[i][j-1] == 0)
  266.               {
  267.                   int temporary = board[i][j];
  268.                   board[i][j] = board[i][j-1];
  269.                   board[i][j-1] = temporary;
  270.                   return true;
  271.               }
  272.              
  273.               else if (board[i+1][j] == 0)
  274.               {
  275.                   int temporary = board[i][j];
  276.                   board[i][j] = board[i+1][j];
  277.                   board[i+1][j] = temporary;
  278.                   return true;
  279.               }
  280.              
  281.               else if (board[i][j+1] == 0)
  282.               {
  283.                   int temporary = board[i][j];
  284.                   board[i][j] = board[i][j+1];
  285.                   board[i][j+1] = temporary;
  286.                   return true;
  287.               }
  288.               return false;
  289.             }
  290.            
  291.         }
  292.     }
  293.     return false;
  294. }
  295.  
  296. /**
  297.  * Returns true if game is won (i.e., board is in winning configuration),
  298.  * else false.
  299.  */
  300. bool won(void)
  301. {
  302.     // TODO
  303.     return false;
  304. }
Add Comment
Please, Sign In to add comment