Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.71 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.     int down=1;
  160.     for(int i=0;i<d;i++)
  161.     {
  162.         for(int j=0;j<d;j++)
  163.         {
  164.             board[i][j]=(d*d)-down;
  165.             down++;
  166.             printf("Zahl:%i, i=%i, j=%i\n", board[i][j], i, j);
  167.         }
  168.     }
  169. }
  170.  
  171. /**
  172.  * Prints the board in its current state.
  173.  */
  174. void draw(void)
  175. {
  176.     // TODO
  177. }
  178.  
  179. /**
  180.  * If tile borders empty space, moves tile and returns true, else
  181.  * returns false.
  182.  */
  183. bool move(int tile)
  184. {
  185.     // TODO
  186.     return false;
  187. }
  188.  
  189. /**
  190.  * Returns true if game is won (i.e., board is in winning configuration),
  191.  * else false.
  192.  */
  193. bool won(void)
  194. {
  195.     // TODO
  196.     return false;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement