Advertisement
Guest User

Game of fifteen cs50

a guest
Sep 20th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1. /**
  2.  * fifteen.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 3
  6.  *
  7.  * Implements the 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 [MIN,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. // board's minimal dimension
  26. #define MIN 3
  27.  
  28. // board's maximal dimension
  29. #define MAX 9
  30.  
  31. // board, whereby board[i][j] represents row i and column j
  32. int board[MAX][MAX];
  33.  
  34. // board's dimension
  35. int d;
  36.  
  37. // prototypes
  38. void clear(void);
  39. void greet(void);
  40. void init(void);
  41. void draw(void);
  42. bool move(int tile);
  43. bool won(void);
  44. void save(void);
  45.  
  46. //variables that will help us track tiles (blank and moving one)
  47. int btr;
  48. int btc;
  49. int tmr;
  50. int tmc;
  51.  
  52. int main(int argc, string argv[])
  53. {
  54.     // greet player
  55.     greet();
  56.  
  57.     // ensure proper usage
  58.     if (argc != 2)
  59.     {
  60.         printf("Usage: ./fifteen d\n");
  61.         return 1;
  62.     }
  63.  
  64.     // ensure valid dimensions
  65.     d = atoi(argv[1]);
  66.     if (d < MIN || d > MAX)
  67.     {
  68.         printf("Board must be between %i x %i and %i x %i, inclusive.\n",
  69.             MIN, MIN, MAX, MAX);
  70.         return 2;
  71.     }
  72.  
  73.     // initialize the board
  74.     init();
  75.  
  76.     // accept moves until game is won
  77.     while (true)
  78.     {
  79.         // clear the screen
  80.         clear();
  81.  
  82.         // draw the current state of the board
  83.         draw();
  84.  
  85.         // saves the current state of the board (for testing)
  86.         save();
  87.  
  88.         // check for win
  89.         if (won())
  90.         {
  91.             printf("ftw!\n");
  92.             break;
  93.         }
  94.  
  95.         // prompt for move
  96.         printf("Tile to move: ");
  97.         int tile = GetInt();
  98.  
  99.         // move if possible, else report illegality
  100.         if (!move(tile))
  101.         {
  102.             printf("\nIllegal move.\n");
  103.             usleep(500000);
  104.         }
  105.  
  106.         // sleep for animation's sake
  107.         usleep(500000);
  108.     }
  109.  
  110.     // that's all folks
  111.     return 0;
  112. }
  113.  
  114. /**
  115.  * Clears screen using ANSI escape sequences.
  116.  */
  117. void clear(void)
  118. {
  119.     printf("\033[2J");
  120.     printf("\033[%d;%dH", 0, 0);
  121. }
  122.  
  123. /**
  124.  * Greets player.
  125.  */
  126. void greet(void)
  127. {
  128.     clear();
  129.     printf("GAME OF FIFTEEN\n");
  130.     usleep(2000000);
  131. }
  132.  
  133. /**
  134.  * Initializes the game's board with tiles numbered 1 through d*d - 1,
  135.  * (i.e., fills board with values but does not actually print them),
  136.  * whereby board[i][j] represents row i and column j.
  137.  */
  138. void init(void)
  139. {
  140.     // TODO-done
  141.     //setting up count variable
  142.     int count=(d*d-1);
  143.     for(int i=0; i<d;i++)
  144.     {
  145.         for(int j=0; j<d; j++)
  146.         {
  147.             board[i][j]= count;
  148.             count= count-1;
  149.         }
  150.     }
  151.     if((d*d-1)%2!=0)
  152.     {
  153.         board[d-1][d-2]=2;
  154.         board[d-1][d-3]=1;
  155.     }
  156.     btr=d-1;
  157.     btc=d-1;
  158.    
  159. }
  160.  
  161. /**
  162.  * Prints the board in its current state.
  163.  */
  164. void draw(void)
  165. {
  166.     // TODO-done
  167.     for (int i=0; i<d; i++)
  168.     {
  169.         for(int j=0; j<d; j++)
  170.         {  
  171.             if (board[i][j]==0)
  172.             {
  173.               printf("_   ");  
  174.             }
  175.             else
  176.             {
  177.             printf("%i",board[i][j]);
  178.                 if(board[i][j]>9)
  179.                 {
  180.                     printf("  ");
  181.                 }
  182.                 else
  183.                 {
  184.                     printf("   ");
  185.                 }
  186.             }
  187.         }
  188.         printf("\n");
  189.     }
  190.    
  191.    
  192. }
  193.  
  194. /**
  195.  * If tile borders empty space, moves tile and returns true, else
  196.  * returns false.
  197.  */
  198. bool move(int tile)
  199. {
  200.     // TODO
  201.     //bt stands for blank tile, tm for time moving, r for right, and l for left
  202.     for (int i=0; i<d; i++)
  203.     {
  204.         for (int j=0;j<d;j++)
  205.         {
  206.             if(board[i][j]==tile)
  207.             {
  208.                 tmr=i;
  209.                 tmc=j;
  210.             }
  211.         }
  212.     }
  213.     if (btr== tmr+1 || btr== tmr-1)
  214.     {
  215.         if (btc==tmc)
  216.         {
  217.             //swipping the variables we're using to take changes into account
  218.             int swap=btr;
  219.             btr=tmr;
  220.             tmr=swap;
  221.             //swiping the board values btw the board with the blank tile and the chosen tile
  222.             board[tmr][tmc]=board[btr][btc];
  223.             board[btr][btc]=0;
  224.             return true;
  225.         }
  226.     }
  227.     else if(btc==tmc+1 || btc==tmc-1)
  228.     {
  229.         if(btr==tmr)
  230.         {
  231.             //swipping the variables we're using to take changes into account
  232.             int swap=btc;
  233.             btc=tmc;
  234.             tmc=swap;
  235.             //swiping the board values btw the board with the blank tile and the chosen tile
  236.             board[tmr][tmc]=board[btr][btc];
  237.             board[btr][btc]=0;
  238.             return true;
  239.         }
  240.     }
  241.     return false;
  242. }
  243.  
  244.  
  245. /**
  246.  * Returns true if game is won (i.e., board is in winning configuration),
  247.  * else false.
  248.  */
  249. bool won(void)
  250. {
  251.     // TODO-done
  252.     //keep track of the count
  253.     int count=1;
  254.     for(int i=0; i<d;i++)
  255.     {  
  256.         for(int j=0;j<d;j++)
  257.         {
  258.             if(board[i][j]==count)
  259.             {
  260.                 count=count+1;
  261.             }
  262.             else if(board[i][j]==0)
  263.             {
  264.                 if(board[d-1][d-1]==0)
  265.                 {
  266.                     break;
  267.                 }
  268.             }
  269.             else
  270.             {
  271.                 return false;
  272.             }
  273.         }  
  274.     }
  275. return true;
  276. }
  277.  
  278. /**
  279.  * Saves the current state of the board to disk (for testing).
  280.  */
  281. void save(void)
  282. {
  283.     // log
  284.     const string log = "log.txt";
  285.  
  286.     // delete existing log, if any, before first save
  287.     static bool saved = false;
  288.     if (!saved)
  289.     {
  290.         unlink(log);
  291.         saved = true;
  292.     }
  293.  
  294.     // open log
  295.     FILE* p = fopen(log, "a");
  296.     if (p == NULL)
  297.     {
  298.         return;
  299.     }
  300.  
  301.     // log board
  302.     fprintf(p, "{");
  303.     for (int i = 0; i < d; i++)
  304.     {
  305.         fprintf(p, "{");
  306.         for (int j = 0; j < d; j++)
  307.         {
  308.             fprintf(p, "%i", board[i][j]);
  309.             if (j < d - 1)
  310.             {
  311.                 fprintf(p, ",");
  312.             }
  313.         }
  314.         fprintf(p, "}");
  315.         if (i < d - 1)
  316.         {
  317.             fprintf(p, ",");
  318.         }
  319.     }
  320.     fprintf(p, "}\n");
  321.  
  322.     // close log
  323.     fclose(p);
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement