Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- state move (int direction, state s, int length) {
- //returns a state that has moved in the direction specified if that move is valid,
- //or a state identical to that passed if not. returns NULL on a board with no blank.
- board b = newBoard(s, length);
- int x;
- int y;
- state ret = NULL;
- for (x = 0; x < b->sideLength; x++) {
- for (y = 0; y < b->sideLength; y++) {
- if (b->matrix[x][y] == BLANK_SQUARE) {
- if (direction == UP) {
- if (y > 0) { //not at top of board
- b->matrix[x][y] = b->matrix[x][y-1];
- b->matrix[x][y-1] = BLANK_SQUARE;
- }
- } else if (direction == RIGHT) {
- if (x < b->sideLength - 1) {
- b->matrix[x][y] = b->matrix[x+1][y];
- b->matrix[x+1][y] = BLANK_SQUARE;
- }
- } else if (direction == DOWN) {
- if (y < b->sideLength - 1) { //not at bottom of board
- b->matrix[x][y] = b->matrix[x][y+1];
- b->matrix[x][y+1] = BLANK_SQUARE;
- }
- } else if (direction == LEFT) {
- if (x > 0) {
- b->matrix[x][y] = b->matrix[x-1][y];
- b->matrix[x-1][y] = BLANK_SQUARE;
- }
- }
- ret = boardToState(b);
- fuckUpBoard(b);
- return ret; //so much easier than scanning the rest of the board
- }
- }
- }
- return ret;
- }
Add Comment
Please, Sign In to add comment