Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void init(void)
- {
- int val = d*d - 1;
- int c;
- for(int i = 0; i < d; i++)
- {
- for(int j = 0; j < d; j++)
- {
- board[i][j] = val;
- val--;
- }
- }
- if(d % 2 == 0)
- {
- c = board[d - 1][d - 2];
- board[d - 1][d - 2] = board[d - 1][d - 3];
- board[d - 1][d - 3] = c;
- }
- }
- /**
- * Prints the board in its current state.
- */
- void draw(void)
- {
- for(int i = 0; i < d; i++)
- {
- for(int j = 0; j < d; j++)
- {
- if(board[i][j] == 0)
- {
- printf(" _ ");
- }
- else
- {
- printf("%2i ", board[i][j]);
- }
- }
- printf("\n");
- }
- }
- /**
- * If tile borders empty space, moves tile and returns true, else
- * returns false.
- */
- bool move(int tile)
- {
- int a,b,c;
- for(int i = 0; i < d; i++)
- {
- for(int j = 0; j < d; j++)
- {
- if(board[i][j] == 0)
- {
- a = i;
- b = j;
- }
- }
- }
- for(int i = 0; i < d; i++)
- {
- for(int j = 0; j < d; j++)
- {
- if(board[i][j] == tile)
- {
- if(abs(i - a) + abs(j - b) == 1)
- {
- c = board[i][j];
- board[i][j] = board[a][b];
- board[a][b] = c;
- return true;
- }
- }
- }
- }
- return false;
- }
- /**
- * Returns true if game is won (i.e., board is in winning configuration),
- * else false.
- */
- bool won(void)
- {
- for (int i = 0; i < d; i++)
- {
- for(int j = 0; j < d; j++)
- {
- if(!board[i][j] != (j + 1) + (i * d) || board[i][j] != 0 )
- {
- return false;
- }
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment