/* * Returns true if a move is legal for the given side; false otherwise. */ bool Board::checkMove(Move *m, Side side) { // Passing is only legal if you have no moves. if (m == NULL) return !hasMoves(side); int X = m->getX(); int Y = m->getY(); // Make sure the square hasn't already been taken. if (occupied(X, Y)) return false; Side other = (side == BLACK) ? WHITE : BLACK; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dy == 0 && dx == 0) continue; // Is there a capture in that direction? int x = X + dx; int y = Y + dy; if (onBoard(x, y) && get(other, x, y)) { do { x += dx; y += dy; } while (onBoard(x, y) && get(other, x, y)); if (onBoard(x, y) && get(side, x, y)) return true; } } } return false; }
Result game(int seed) { Result result = {LOSE, 0}; srand(seed); Block blocks[size*size]; int blocksLeft = 0; resetGame(blocks, &blocksLeft, &result); int index = 0; int spawn = FALSE; int quit = FALSE; while (!quit) { if (KEY_DOWN_NOW(BUTTON_UP)) { if (moveBlocks(blocks, UP, &result.score, &blocksLeft, &result.outcome) && blocksLeft) { spawn = TRUE; } while (KEY_DOWN_NOW(BUTTON_UP)); } if (KEY_DOWN_NOW(BUTTON_DOWN)) { if (moveBlocks(blocks, DOWN, &result.score, &blocksLeft, &result.outcome) && blocksLeft) { spawn = TRUE; } while (KEY_DOWN_NOW(BUTTON_DOWN)); } if (KEY_DOWN_NOW(BUTTON_LEFT)) { if (moveBlocks(blocks, LEFT, &result.score, &blocksLeft, &result.outcome) && blocksLeft) { spawn = TRUE; } while (KEY_DOWN_NOW(BUTTON_LEFT)); } if (KEY_DOWN_NOW(BUTTON_RIGHT)) { if (moveBlocks(blocks, RIGHT, &result.score, &blocksLeft, &result.outcome) && blocksLeft) { spawn = TRUE; } while (KEY_DOWN_NOW(BUTTON_RIGHT)); } if (spawn) { index = spawnBlock(blocks, blocksLeft--); result.score += 1<<blocks[index].num; waitForVblank(); drawBlock(blocks, index); updateScoreDisplay(result.score); quit = !hasMoves(blocks); spawn = FALSE; } if (KEY_DOWN_NOW(BUTTON_B)) { quit = TRUE; while (KEY_DOWN_NOW(BUTTON_B)); } if (KEY_DOWN_NOW(BUTTON_SELECT)) { resetGame(blocks, &blocksLeft, &result); while (KEY_DOWN_NOW(BUTTON_SELECT)); } } //put a while loop here to check if game ends properly return result; }
/*Evaluation Function for Board*/ int scoreBoard(Game *game, Color uCol, int depth) { int i, j; int count = 0; for (i = 0; i < BOARD_SIZE; i++) { for (j = 0; j < BOARD_SIZE; j++) { if (!isEmpty(game->board[i][j])) { count += scoreChar(game->board[i][j], uCol); } } } if (depth % 2 == 0 && !hasMoves(game, uCol)) { if (canKillKing(game, oppositeCol(uCol))) return -MATE_SCORE; else return -TIE_SCORE; } if (depth % 2 == 1 && !hasMoves(game, oppositeCol(uCol))) { if (canKillKing(game, uCol)) return MATE_SCORE; else return TIE_SCORE; } return count; }
/* * Returns true if the game is finished; false otherwise. The game is finished * if neither side has a legal move. */ bool Board::isDone() { return !(hasMoves(BLACK) || hasMoves(WHITE)); }
/*Determing whether Game is ongoing, Mate or Tie*/ int gameOver(Game *game, Color col){ if (hasMoves(game, col)) return GAME_CONTINUE; if (canKillKing(game, oppositeCol(col))) return GAME_MATE; else return GAME_TIE; }