// Function creates the Board with specified size static board_p boardCreate(long seed, int width, int height) { board_p board = NULL; ASSERT(MINIMAL_SIZE <= width, "boardCreate"); ASSERT(MAXIMAL_SIZE >= width, "boardCreate"); ASSERT(MINIMAL_SIZE <= height, "boardCreate"); ASSERT(MAXIMAL_SIZE >= height, "boardCreate"); board = (board_p)objectCreate( sizeof(board_t), (destructor_f)boardDestroy); board->width = width; board->height = height; board->seed = seed; board->progress = 0x7; // empty, 2 and 4 board->goal = width * height * 3 / 4; if (board->goal > 11) board->goal = 11; boardGenerate(board); boardGenerate(board); board->state = State_Game; board->round = 1; board->score = 0; board->prev = NULL; return board; };
// Function rotates Board, slides a Board to Left, // creates new Cell and rotates Board back static long boardStep(board_p board, int r) { long score = 0; long progress = 0; long goal = 0; char victory = 0; char full = 0; board_p prev = NULL; int i; ASSERT(NULL != board, "boardStep"); ASSERT(0 <= r, "boardStep"); ASSERT(r <= 3, "boardStep"); score = board->score; progress = board->progress; goal = ((long)1) << board->goal; prev = boardClone(board); for (i = 0; i < r; ++i) boardRotate(board); if (boardSlide(board)) if (boardGenerate(board)) ++board->round; victory = !(progress & goal) && (board->progress & goal); full = boardFull(board); switch (board->state) { case State_Game: if (full && victory) board->state = State_Victory; else if (full) board->state = State_Defeat; else if (victory) board->state = State_Goal; break; case State_Victory: break; case State_Defeat: break; case State_Goal: board->state = State_Free; break; case State_Free: if (boardFull(board)) board->state = State_Victory; break; } if (board->score < 0) board->score = 0; for (i = 4 - r; i > 0; --i) boardRotate(board); if (boardEquals(board, prev)) { // don't autorelease prev beause it's // already autoreleased } else { boardAutorelease(board->prev); board->prev = boardRetain(prev); } return board->score - score; };
void boardLoad() { int i; if (!boardBackgroundIMG) { boardBackgroundIMG = loadImage("data/gfx/background.bmp"); } tilesetLoad(&stonesTileset, "data/gfx/stones.bmp", STONE_W, STONE_H, 9, STONE_COUNT); stones = malloc(sizeof(stone*) * BOARD_W); if (!stones) { return; } for (i = 0; i < BOARD_W; ++i) { stones[i] = malloc(sizeof(stone) * BOARD_H); if (!stones[i]) { return; } memset(stones[i], 0, sizeof(stone) * BOARD_H); } boardSetAlpha(255); gameOver = 0; if (continueGame) { stonesLeft = 0; getBoard(0); if (!boardCheckAvailableMoves()) { gameOver = 1; if (!stonesLeft) { gameOver = 0; boardGenerate(); cursorX = 1; cursorY = 1; if (practice) { boardCheckAvailableMoves(); } } } } else { boardGenerate(); cursorX = 1; cursorY = 1; if (practice) { boardCheckAvailableMoves(); } } crossing = 0; }