Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * fifteen.c
- *
- * Computer Science 50
- * Problem Set 3
- *
- * Implements the Game of Fifteen (generalized to d x d).
- *
- * Usage: ./fifteen d
- *
- * whereby the board's dimensions are to be d x d,
- * where d must be in [MIN,MAX]
- *
- * Note that usleep is obsolete, but it offers more granularity than
- * sleep and is simpler to use than nanosleep; `man usleep` for more.
- */
- #define _XOPEN_SOURCE 500
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- // board's minimal dimension
- #define MIN 3
- // board's maximal dimension
- #define MAX 9
- #define BLANK 2422
- // board, whereby board[i][j] represents row i and column j
- int board[MAX][MAX];
- // board's dimension
- int d;
- int space = 0 ;
- int g = 0 ;
- int h = 0 ;
- // prototypes
- void clear(void);
- void greet(void);
- void init(void);
- void draw(void);
- bool move(int tile);
- bool won(void);
- void save(void);
- int main(int argc, string argv[])
- {
- // greet player
- greet();
- // ensure proper usage
- if (argc != 2)
- {
- printf("Usage: ./fifteen d\n");
- return 1;
- }
- // ensure valid dimensions
- d = atoi(argv[1]);
- if (d < MIN || d > MAX)
- {
- printf("Board must be between %i x %i and %i x %i, inclusive.\n",
- MIN, MIN, MAX, MAX);
- return 2;
- }
- // initialize the board
- init();
- // accept moves until game is won
- while (true)
- {
- // clear the screen
- clear();
- // draw the current state of the board
- draw();
- // saves the current state of the board (for testing)
- save();
- // check for win
- if (won())
- {
- printf("ftw!\n");
- break;
- }
- // prompt for move
- printf("Tile to move: ");
- int tile = GetInt();
- // move if possible, else report illegality
- if (!move(tile))
- {
- printf("\nIllegal move.\n");
- usleep(500000);
- }
- // sleep for animation's sake
- usleep(500000);
- }
- // that's all folks
- return 0;
- }
- /**
- * Clears screen using ANSI escape sequences.
- */
- void clear(void)
- {
- printf("\033[2J");
- printf("\033[%d;%dH", 0, 0);
- }
- /**
- * Greets player.
- */
- void greet(void)
- {
- clear();
- printf("GAME OF FIFTEEN\n");
- usleep(2000000);
- }
- /**
- * Initializes the game's board with tiles numbered 1 through d*d - 1,
- * (i.e., fills board with values but does not actually print them),
- * whereby board[i][j] represents row i and column j.
- */
- void init(void)
- {
- bool isOdd = false;
- // TODO
- int k = d*d - 1 ;
- for(g = 0 ; g < d ; g++)
- {
- for(h = 0 ; h < d ; h++)
- {
- board[g][h] = k ;
- k-- ;
- }
- }
- if(k % 2 == 1 )
- isOdd = true ;
- if(isOdd == true )
- {
- board[d-1][d] = board[d-2][d];
- board[d-2][d] = board[d-1][d];
- }
- }
- /**
- * Prints the board in its current state.
- */
- void draw(void)
- {
- // TODO
- for ( g = 0 ; g < d ; g++ )
- {
- for ( h = 0 ; h < d ; h++ )
- {
- printf (" ");
- if(board[g][h] != 0 )
- printf("%2d",board[g][h]);
- else{ printf(" ");}
- }
- printf("\n");
- }
- }
- /**
- * If tile borders empty space, moves tile and returns true, else
- * returns false.
- */
- int empty = 32 ;
- bool move (int tile)
- {
- for (int i = 0 ; i < d ; i++ )
- {
- for (int j = 0 ; j < d ; j++ )
- {
- if(board[i][j] == tile )
- {
- if (board[i][j-1] == empty)
- {
- empty = board[i][j];
- board[i][j] = board[i][j-1];
- board[i][j-1] = empty;
- return true;
- }
- else if (board[i][j+1] == empty)
- {
- empty = board[i][j];
- board[i][j] = board[i][j+1];
- board[i][j+1] = empty;
- return true;
- }
- else if (board[i-1][j] == empty)
- {
- empty = board[i][j];
- board[i][j] = board[i-1][j];
- board[i-1][j] = empty;
- return true;
- }
- else if (board[i+1][j] == empty)
- {
- empty = board[i][j];
- board[i][j] = board[i+1][j];
- board[i+1][j] = empty;
- return true;
- }
- else
- return false;
- }
- }
- }
- }
- /**
- * Returns true if game is won (i.e., board is in winning configuration),
- * else false.
- */
- bool won(void)
- {
- // TODO
- return false;
- }
- /**
- * Saves the current state of the board to disk (for testing).
- */
- void save(void)
- {
- // log
- const string log = "log.txt";
- // delete existing log, if any, before first save
- static bool saved = false;
- if (!saved)
- {
- unlink(log);
- saved = true;
- }
- // open log
- FILE* p = fopen(log, "a");
- if (p == NULL)
- {
- return;
- }
- // log board
- fprintf(p, "{");
- for (int i = 0; i < d; i++)
- {
- fprintf(p, "{");
- for (int j = 0; j < d; j++)
- {
- fprintf(p, "%i", board[i][j]);
- if (j < d - 1)
- {
- fprintf(p, ",");
- }
- }
- fprintf(p, "}");
- if (i < d - 1)
- {
- fprintf(p, ",");
- }
- }
- fprintf(p, "}\n");
- // close log
- fclose(p);
- }
Advertisement
Add Comment
Please, Sign In to add comment