Advertisement
Guest User

fifteen

a guest
Jan 17th, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 (verouderd), but it offers more granularity (meer detail) 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> //enables GetString
  21. #include <stdio.h> //enables printf, fprintf
  22. #include <stdlib.h> //
  23. #include <unistd.h> //enables usleep
  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.         // Prints the board and values in log.text
  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]); //prints the the ciphers on the board
  89.                 if (j < d - 1)
  90.                 {
  91.                     fprintf(file, "|"); //Prints a stripe throughout the board
  92.                 }
  93.             }
  94.             fprintf(file, "\n"); // prints an enter after each row on teh board
  95.         }
  96.         fflush(file); //prints the preceding all at once 'flush'.
  97.  
  98.         // check for win
  99.         if (won())
  100.         {
  101.             printf("ftw!\n");
  102.             break;
  103.         }
  104.  
  105.         // prompt for move
  106.         printf("Tile to move: ");
  107.         int tile = GetInt();
  108.        
  109.         move(tile);
  110.        
  111.         // log move (for testing)
  112.         fprintf(file, "%i\n", tile);
  113.         fflush(file);
  114.  
  115.         // move if possible, else report illegality
  116.         if (!move(tile))
  117.         {
  118.             printf("\nIllegal move.\n");
  119.             usleep(500000);
  120.         }
  121.  
  122.         // sleep thread for animation's sake
  123.         usleep(500000);
  124.     }
  125.    
  126.     // close log
  127.     fclose(file);
  128.  
  129.     // success
  130.     return 0;
  131. }
  132.  
  133. /**
  134.  * Clears screen using ANSI escape sequences.
  135.  */
  136. void clear(void)
  137. {
  138.     printf("\033[2J"); //Clear the screen, move to (0, 0)
  139.     printf("\033[%d;%dH", 0, 0);
  140. }
  141.  
  142. /**
  143.  * Greets player.
  144.  */
  145. void greet(void)
  146. {
  147.     clear();
  148.     printf("WELCOME TO GAME OF FIFTEEN\n");
  149.     usleep(2000000);
  150. }
  151.  
  152. /**
  153.  * Initializes the game's board with tiles numbered 1 through d*d - 1
  154.  * (i.e., fills 2D array with values but does not actually print them).  
  155.  */
  156. void init(void)
  157. {
  158. int StartValue = d*d - 1;
  159.     // TODO
  160.     for (int i = 0; i < d; i++)
  161.     {
  162.         for (int j = 0; j < d; j++)
  163.         {
  164.             board[i][j] = StartValue;
  165.             StartValue = StartValue -1;
  166.         }
  167.     }
  168.     if ((d * d -1) % 2 != 0) // if odd number of tiles change number 1 and 2
  169.     {
  170.     int temp; //create temporary storage
  171.     temp = board[d-1][d-3]; //puts 2 in temporary storage
  172.     board[d-1][d-3] = board[d-1][d-2]; //puts 1 in place of two
  173.     board[d-1][d-2] = temp; //puts to in place of one
  174.     }
  175. }
  176.  
  177.  
  178. /**
  179.  * Prints the board in its current state.
  180.  */
  181. void draw(void)
  182. {
  183.     // TODO
  184.     for (int i = 0; i < d; i++)
  185.     {
  186.         for (int j = 0; j < d; j++)
  187.         {
  188.             if (board[i][j] > 9) //prints one space in combi with 2 digits
  189.             {
  190.                 printf(" ");
  191.             }
  192.             if (board[i][j] < 10) //prints two spaces in combi with one digit
  193.             {
  194.                 printf("  ");
  195.             }
  196.             if (board[i][j] == 0) //prints 3 spaces and no zero if digit is zero
  197.             {
  198.             printf("   ");
  199.             }
  200.             else
  201.             {
  202.             printf("%d",board[i][j]); //prints every digit except the zero
  203.             }
  204.         }
  205.     printf("\n");
  206.     }
  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. {
  216.     // TODO
  217.     //if (board[m][n] == 0)  //checks the array for the number zero
  218.     int temp;
  219.    
  220.     if (tile == 0)
  221.     {
  222.         return false;
  223.     }
  224.     for (int row = 0; row < d; row++)
  225.     {
  226.         for (int column = 0; column < d; column++)
  227.         {
  228.             if (tile == board[row][column]) //checks array for tile
  229.             {
  230.                
  231.                 if (column > 0) //makes sure C does not acces any space to the left of 2D-array
  232.                 {
  233.                     if (board[row-0][column-1] == 0) // checks if blank space is left to tile
  234.                     {
  235.                         temp = tile;     //switch the tiles
  236.                         board[row][column] = board[row-0][column-1];
  237.                         board[row-0][column-1] = temp;
  238.                         return true;
  239.                     }
  240.                 }
  241.                 if (row > 0) //makes sure C does not access any space above 2D-array
  242.                 {
  243.                     if (board[row-1][column-0] == 0) // checks if blank space is above tile
  244.                     {
  245.                         temp = tile;     //switch the tiles
  246.                         board[row][column] = board[row-1][column-0];
  247.                         board[row-1][column-0] = temp;
  248.                         return true;
  249.                     }
  250.                 }
  251.                 if (column < d-1) //makes sure C does not access any space to the right of 2D-array
  252.                 {
  253.                     if (board[row+0][column+1] == 0) // checks if blank space is right to tile
  254.                     {
  255.                         temp = tile;     //switch the tiles
  256.                         board[row][column] = board[row+0][column+1];
  257.                         board[row+0][column+1] = temp;
  258.                         return true;
  259.                     }
  260.                 }
  261.                 if (row < d-1) //makes sure C does not acces any space underneath 2D-array
  262.                 {
  263.                     if (board[row+1][column+0] == 0) // checks if blank space is underneath tile
  264.                     {
  265.                         temp = tile;     //switch the tiles
  266.                         board[row][column] = board[row+1][column+0];
  267.                         board[row+1][column+0] = temp;
  268.                         return true;
  269.                     }
  270.                 }
  271.             }
  272.         }
  273.     }
  274.     return false;
  275. }
  276.  
  277. http://pastebin.com/cjRptJn3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement