Advertisement
Guest User

Untitled

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