Guest User

Untitled

a guest
Feb 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. state move (int direction, state s, int length) {
  2.    //returns a state that has moved in the direction specified if that move is valid,
  3.    //or a state identical to that passed if not. returns NULL on a board with no blank.
  4.    board b = newBoard(s, length);
  5.    int x;
  6.    int y;
  7.    state ret = NULL;
  8.    for (x = 0; x < b->sideLength; x++) {
  9.       for (y = 0; y < b->sideLength; y++) {
  10.          if (b->matrix[x][y] == BLANK_SQUARE) {
  11.             if (direction == UP) {
  12.                if (y > 0) { //not at top of board
  13.                   b->matrix[x][y] = b->matrix[x][y-1];
  14.                   b->matrix[x][y-1] = BLANK_SQUARE;
  15.                }
  16.             } else if (direction == RIGHT) {
  17.                if (x < b->sideLength - 1) {
  18.                   b->matrix[x][y] = b->matrix[x+1][y];
  19.                   b->matrix[x+1][y] = BLANK_SQUARE;
  20.                }
  21.             } else if (direction == DOWN) {
  22.                if (y < b->sideLength - 1) { //not at bottom of board
  23.                   b->matrix[x][y] = b->matrix[x][y+1];
  24.                   b->matrix[x][y+1] = BLANK_SQUARE;
  25.                }
  26.             } else if (direction == LEFT) {
  27.                if (x > 0) {
  28.                   b->matrix[x][y] = b->matrix[x-1][y];
  29.                   b->matrix[x-1][y] = BLANK_SQUARE;
  30.                }
  31.             }
  32.             ret = boardToState(b);
  33.             fuckUpBoard(b);
  34.             return ret; //so much easier than scanning the rest of the board
  35.          }
  36.       }
  37.    }
  38.    return ret;
  39. }
Add Comment
Please, Sign In to add comment