Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * fifteen.c
- *
- * Computer Science 50
- * Problem Set 3
- *
- * Implements 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 [DIM_MIN,DIM_MAX]
- *
- * Note that usleep is obsolete (verouderd), but it offers more granularity (meer detail) than
- * sleep and is simpler to use than nanosleep; `man usleep` for more.
- */
- #define _XOPEN_SOURCE 500
- #include <cs50.h> //enables GetString
- #include <stdio.h> //enables printf, fprintf
- #include <stdlib.h> //
- #include <unistd.h> //enables usleep
- // constants
- #define DIM_MIN 3
- #define DIM_MAX 9
- // board
- int board[DIM_MAX][DIM_MAX];
- // dimensions
- int d;
- // prototypes
- void clear(void);
- void greet(void);
- void init(void);
- void draw(void);
- bool move(int tile);
- bool won(void);
- int main(int argc, string argv[])
- {
- // ensure proper usage
- if (argc != 2)
- {
- printf("Usage: fifteen d\n");
- return 1;
- }
- // ensure valid dimensions
- d = atoi(argv[1]);
- if (d < DIM_MIN || d > DIM_MAX)
- {
- printf("Board must be between %i x %i and %i x %i, inclusive.\n",
- DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
- return 2;
- }
- // open log
- FILE* file = fopen("log.txt", "w");
- if (file == NULL)
- {
- return 3;
- }
- // greet user with instructions
- greet();
- // initialize the board
- init();
- // accept moves until game is won
- while (true)
- {
- // clear the screen
- clear();
- // draw the current state of the board
- draw();
- // Prints the board and values in log.text
- for (int i = 0; i < d; i++)
- {
- for (int j = 0; j < d; j++)
- {
- fprintf(file, "%i", board[i][j]); //prints the the ciphers on the board
- if (j < d - 1)
- {
- fprintf(file, "|"); //Prints a stripe throughout the board
- }
- }
- fprintf(file, "\n"); // prints an enter after each row on teh board
- }
- fflush(file); //prints the preceding all at once 'flush'.
- // check for win
- if (won())
- {
- printf("ftw!\n");
- break;
- }
- // prompt for move
- printf("Tile to move: ");
- int tile = GetInt();
- move(tile);
- // log move (for testing)
- fprintf(file, "%i\n", tile);
- fflush(file);
- // move if possible, else report illegality
- if (!move(tile))
- {
- printf("\nIllegal move.\n");
- usleep(500000);
- }
- // sleep thread for animation's sake
- usleep(500000);
- }
- // close log
- fclose(file);
- // success
- return 0;
- }
- /**
- * Clears screen using ANSI escape sequences.
- */
- void clear(void)
- {
- printf("\033[2J"); //Clear the screen, move to (0, 0)
- printf("\033[%d;%dH", 0, 0);
- }
- /**
- * Greets player.
- */
- void greet(void)
- {
- clear();
- printf("WELCOME TO GAME OF FIFTEEN\n");
- usleep(2000000);
- }
- /**
- * Initializes the game's board with tiles numbered 1 through d*d - 1
- * (i.e., fills 2D array with values but does not actually print them).
- */
- void init(void)
- {
- int StartValue = d*d - 1;
- // TODO
- for (int i = 0; i < d; i++)
- {
- for (int j = 0; j < d; j++)
- {
- board[i][j] = StartValue;
- StartValue = StartValue -1;
- }
- }
- if ((d * d -1) % 2 != 0) // if odd number of tiles change number 1 and 2
- {
- int temp; //create temporary storage
- temp = board[d-1][d-3]; //puts 2 in temporary storage
- board[d-1][d-3] = board[d-1][d-2]; //puts 1 in place of two
- board[d-1][d-2] = temp; //puts to in place of one
- }
- }
- /**
- * Prints the board in its current state.
- */
- void draw(void)
- {
- // TODO
- for (int i = 0; i < d; i++)
- {
- for (int j = 0; j < d; j++)
- {
- if (board[i][j] > 9) //prints one space in combi with 2 digits
- {
- printf(" ");
- }
- if (board[i][j] < 10) //prints two spaces in combi with one digit
- {
- printf(" ");
- }
- if (board[i][j] == 0) //prints 3 spaces and no zero if digit is zero
- {
- printf(" ");
- }
- else
- {
- printf("%d",board[i][j]); //prints every digit except the zero
- }
- }
- printf("\n");
- }
- }
- /**
- * If tile borders empty space, moves tile and returns true, else
- * returns false.
- */
- bool move(int tile)
- {
- // TODO
- //if (board[m][n] == 0) //checks the array for the number zero
- int temp;
- if (tile == 0)
- {
- return false;
- }
- for (int row = 0; row < d; row++)
- {
- for (int column = 0; column < d; column++)
- {
- if (tile == board[row][column]) //checks array for tile
- {
- if (column > 0) //makes sure C does not acces any space to the left of 2D-array
- {
- if (board[row-0][column-1] == 0) // checks if blank space is left to tile
- {
- temp = tile; //switch the tiles
- board[row][column] = board[row-0][column-1];
- board[row-0][column-1] = temp;
- return true;
- }
- }
- if (row > 0) //makes sure C does not access any space above 2D-array
- {
- if (board[row-1][column-0] == 0) // checks if blank space is above tile
- {
- temp = tile; //switch the tiles
- board[row][column] = board[row-1][column-0];
- board[row-1][column-0] = temp;
- return true;
- }
- }
- if (column < d-1) //makes sure C does not access any space to the right of 2D-array
- {
- if (board[row+0][column+1] == 0) // checks if blank space is right to tile
- {
- temp = tile; //switch the tiles
- board[row][column] = board[row+0][column+1];
- board[row+0][column+1] = temp;
- return true;
- }
- }
- if (row < d-1) //makes sure C does not acces any space underneath 2D-array
- {
- if (board[row+1][column+0] == 0) // checks if blank space is underneath tile
- {
- temp = tile; //switch the tiles
- board[row][column] = board[row+1][column+0];
- board[row+1][column+0] = temp;
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- http://pastebin.com/cjRptJn3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement