geneviedube

Untitled

Sep 16th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.59 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.         // prompt for move
  103.         printf("Tile to move: ");
  104.         int tile = get_int();
  105.        
  106.         // quit if user inputs 0 (for testing)
  107.         if (tile == 0)
  108.         {
  109.             break;
  110.         }
  111.  
  112.         // log move (for testing)
  113.         fprintf(file, "%i\n", tile);
  114.         fflush(file);
  115.  
  116.         // move if possible, else report illegality
  117.         if (!move(tile))
  118.         {
  119.             printf("\nIllegal move.\n");
  120.             usleep(500000);
  121.         }
  122.  
  123.         // sleep thread for animation's sake
  124.         usleep(500000);
  125.     }
  126.    
  127.     // close log
  128.     fclose(file);
  129.  
  130.     // success
  131.     return 0;
  132. }
  133.  
  134. /**
  135.  * Clears screen using ANSI escape sequences.
  136.  */
  137. void clear(void)
  138. {
  139.     printf("\033[2J");
  140.     printf("\033[%d;%dH", 0, 0);
  141. }
  142.  
  143. /**
  144.  * Greets player.
  145.  */
  146. void greet(void)
  147. {
  148.     clear();
  149.     printf("WELCOME TO GAME OF FIFTEEN\n");
  150.     usleep(2000000);
  151. }
  152.  
  153. /**
  154.  * Initializes the game's board with tiles numbered 1 through d*d - 1
  155.  * (i.e., fills 2D array with values but does not actually print them).  
  156.  */
  157. void init(void)
  158. {
  159.      //represent the total numbers of tiles which correspond the variable d entered
  160.     int total_tiles = (d * d) - 1;
  161.  
  162.      //set a value for each tile  
  163.      
  164.      //iterate for each row and column  
  165.     for (int i= 0; i < d; i++)
  166.     {
  167.         for (int j = 0; j < d; j++)
  168.         {
  169.              // set value in descending way, left to right, top to bottom
  170.             board[i][j] = total_tiles;
  171.             total_tiles --;
  172.         }
  173.     }
  174.    
  175.      //reverse when d is even, reverse numbers 2 and 1  
  176.     if (d % 2 == 0)
  177.         {
  178.             board[d - 1][d - 3] = 1;
  179.             board[d - 1][d - 2] = 2;
  180.         }
  181.  
  182. }
  183. /**
  184.  * Prints the board in its current state.
  185.  */
  186. void draw(void)
  187. {
  188.      // iterate for each row and column
  189.     for (int i = 0; i < d; i++)
  190.     {
  191.         for (int j = 0; j < d; j++)
  192.         {
  193.        
  194.             //print the value of each tile with a space before each numbers
  195.             if (board[i][j] > 0)
  196.             {
  197.                 printf("%2d", board[i][j]);
  198.             }
  199.            
  200.              // print blank space with a symbol  
  201.             if (board[i][j] == 0)
  202.             {
  203.                 printf(" -");
  204.             }
  205.         }
  206.     printf("\n");    
  207.     }
  208. }
  209.  
  210. /**
  211.  * If tile borders empty space, moves tile and returns true, else
  212.  * returns false.
  213.  */
  214. bool move(int tile)
  215.  //int tile is number choosen to move by the player
  216. {
  217.   int i = 0;  // row  
  218.   int j = 0;  // column
  219.  
  220.  
  221.      // find the location of the number choosen by player
  222.     for (i = 0; i < d; i++)
  223.     {
  224.         for (j = 0; j < d; j++)
  225.         {
  226.             if (board[i][j] == tile)
  227.             {
  228.          
  229.                  //check if tile's location is a legal move from the blank's tile
  230.                 if (i - 1 >= 0 && board[i - 1][j] == 0) // if blank is above tile
  231.                 {
  232.                      // swap the blank's tile and the tile
  233.                     int tmp = board[i][j];
  234.                     board[i][j] = board[i - 1][j];
  235.                     board[i - 1][j] = tmp;
  236.                     return true;
  237.                 }
  238.                
  239.                 else if (i + 1 < d && board[i + 1][j] ==  0) // if blank is top tile
  240.                 {
  241.                     int tmp = board[i][j];
  242.                     board[i][j] = board[i + 1][j];
  243.                     board[i + 1][j] = tmp;
  244.                     return true;
  245.                 }
  246.            
  247.                 else if (j + 1 < d && board[i][j + 1] == 0) // if blank is above left to
  248.                 {
  249.                     int tmp = board[i][j];
  250.                     board[i][j] = board[i][j + 1];
  251.                     board[i][j + 1] = tmp;
  252.                     return true;
  253.                 }
  254.                
  255.                 else if (j - 1 >= 0 && board[i][j - 1] == 0) // if blank is right to tile
  256.                 {
  257.                     int tmp = board[i][j];
  258.                     board[i][j] = board[i][j - 1];
  259.                     board[i][j - 1] = tmp;
  260.                     return true;
  261.                 }
  262.                 return false;
  263.             }
  264.         }
  265.     }
  266.     return 0;
  267. }
  268.  
  269.  
  270. /**
  271.  * Returns true if game is won (i.e., board is in winning configuration),
  272.  * else false.{
  273.  */
  274. bool won(void)
  275. {
  276.  
  277. //first number is 1
  278. int count = 1;
  279.  
  280.     for (int i = 0; i < d; i++)
  281.     {
  282.        for (int j = 0; j < d; j++)
  283.        {
  284.            if(board[i][j] != count++ && !( (i==d-1) && (j==d-1) ))
  285.            {
  286.                return false;
  287.            }
  288.        }
  289.     }
  290.     return true;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment