Advertisement
Guest User

Untitled

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