Esempio n. 1
0
int main(void){
    //initialize stuff
    init_zobrist();
    srand(time(NULL));
    //create a new board
    Board_t* goal = newBoard();
    //create a new visited cache
    HashTable_t* visited = newHashTable(32);
    //create a cloned state of the board and scrable it
    Board_t* scrabled = cloneBoard(goal);
    scramble_times(scrabled, TIMES_TO_SCRAMBLE);

    printf("Starting search...\n");
    SearchNode_t* root = newSearchNode(scrabled, 0, NULL);
    SearchNode_t* result = idf_search(root, goal, visited, 30);

    //print out the search
    if(result!=NULL){
        printf("Search Path:\n");
        printSearchNode(result);
    }
    else
        printf("Search failed.\n");

    //clean up
    deleteSearchNode(root, visited);
    assert(TOTAL_SEARCH_NODES==0);
    assert(TOTAL_HASH_NODES==0);
    deleteHashTable(visited);
    deleteBoard(goal);
    return 0;
}
Esempio n. 2
0
TetrisResult TetrisEmulator::emulate(int step_limit)
{
    step = 0;
    lastMaxHeight = 0;
    init_board(board);
    while (!fail())
    {
        TetrominoType type = randType();
        // For each rotation of the certain type.
        int bestRotation = -1;
        int bestX = T_WIDTH / 2;
        double bestValue = NEG_INF;
        for (int rotation = 0; rotation < TetrominoRotationCount[type]; ++rotation)
        {
            // for each possible drop position of the certain type and rotation.
            for (int x = TetrominoLeftBound[type][rotation];
                 x < T_WIDTH - TetrominoRightBound[type][rotation]; ++x)    // right bound is positive offset.
            {
                cloneBoard(board_temp, board); // TODO : potential efficiency enhancement point.
                if ((lastClearLines = drop(board_temp, type, rotation, x)) < 0)
                {
                    continue;
                }
                TetrisValue value = getValue(board_temp);
                if (value > bestValue)
                {
                    bestValue = value;
                    bestRotation = rotation;
                    bestX = x;
                }
            }
        }
        if (bestRotation == -1 || step >= step_limit)
        {
            break;
        }
		//if (_DEBUG) // debug-time only.
		//{
		//	printBoard(board);
		//	std::cout << getHoleDepth(board);
		//}
        drop(board, type, bestRotation, bestX);
        getValue(board);
        step++;
//		printBoard(board);
    }
    return step;
}