diff --git a/usermods/TetrisAI_v2/tetrisai.h b/usermods/TetrisAI_v2/tetrisai.h index ef7868a47..12afbb2fa 100644 --- a/usermods/TetrisAI_v2/tetrisai.h +++ b/usermods/TetrisAI_v2/tetrisai.h @@ -202,101 +202,6 @@ public: } } } - - bool findBestMoveNonBlocking(GridBW grid, std::vector::iterator start, std::vector::iterator end, Rating* bestRating) - { - //vector with pieces - //for every piece - //for every - switch (expression) - { - case INIT: - break; - - default: - break; - } - } - - bool findBestMoveNonBlocking(GridBW grid, std::vector::iterator start, std::vector::iterator end, Rating* bestRating) - { - //INIT - grid.cleanupFullLines(); - Rating curRating(grid.width); - Rating deeperRating(grid.width); - Piece piece = *start; - - // for every rotation of the piece - piece.rotation = 0; - - //HANDLE - while (piece.rotation < piece.pieceData->rotCount) - { - // put piece to top left corner - piece.x = 0; - piece.y = 0; - - //test for every column - piece.x = 0; - while (piece.x <= grid.width - piece.getRotation().width) - { - - //todo optimise by the use of the previous grids height - piece.landingY = 0; - //will set landingY to final position - grid.findLandingPosition(&piece); - - // draw piece - grid.placePiece(&piece, piece.x, piece.landingY); - - if(start == end - 1) - { - //at the deepest level - updateRating(grid, &curRating); - } - else - { - //go deeper to take another piece into account - findBestMove(grid, start + 1, end, &deeperRating); - curRating = deeperRating; - } - - // eraese piece - grid.erasePiece(&piece, piece.x, piece.landingY); - - if(findWorstMove) - { - //init rating for worst - if(bestRating->score == -DBL_MAX) - { - bestRating->score = DBL_MAX; - } - - // update if we found a worse one - if (bestRating->score > curRating.score) - { - *bestRating = curRating; - (*start) = piece; - } - } - else - { - // update if we found a better one - if (bestRating->score < curRating.score) - { - *bestRating = curRating; - (*start) = piece; - } - } - piece.x++; - } - piece.rotation++; - } - - //EXIT - - return true; - } }; #endif /* __AI_H__ */ \ No newline at end of file diff --git a/usermods/TetrisAI_v2/usermod_v2_tetrisai.h b/usermods/TetrisAI_v2/usermod_v2_tetrisai.h index 1c077d048..a312508b5 100644 --- a/usermods/TetrisAI_v2/usermod_v2_tetrisai.h +++ b/usermods/TetrisAI_v2/usermod_v2_tetrisai.h @@ -9,8 +9,6 @@ typedef struct TetrisAI_data { - unsigned long lastTime = 0; - TetrisAIGame tetris; uint8_t intelligence; uint8_t rotate; bool showNext; @@ -96,6 +94,8 @@ void drawGrid(TetrisAIGame* tetris, TetrisAI_data* tetrisai_data) //////////////////////////// uint16_t mode_2DTetrisAI() { + static unsigned long lastTime = 0; + if (!strip.isMatrix || !SEGENV.allocateData(sizeof(tetrisai_data))) { // not a 2D set-up @@ -116,14 +116,16 @@ uint16_t mode_2DTetrisAI() //range 0 - 16 tetrisai_data->colorInc = SEGMENT.custom2 >> 4; - if (!tetrisai_data->tetris || (tetrisai_data->tetris.nLookAhead != nLookAhead + static TetrisAIGame tetris(cols < 32 ? cols : 32, rows, 1, piecesData, numPieces); + + if (tetris.nLookAhead != nLookAhead || tetrisai_data->showNext != SEGMENT.check1 || tetrisai_data->showBorder != SEGMENT.check2 - ) ) { tetrisai_data->showNext = SEGMENT.check1; tetrisai_data->showBorder = SEGMENT.check2; + tetris.nLookAhead = nLookAhead; //not more than 32 as this is the limit of this implementation uint8_t gridWidth = cols < 32 ? cols : 32; @@ -142,7 +144,7 @@ uint16_t mode_2DTetrisAI() } } - tetrisai_data->tetris = TetrisAIGame(gridWidth, gridHeight, nLookAhead, piecesData, numPieces); + tetris = TetrisAIGame(gridWidth, gridHeight, nLookAhead, piecesData, numPieces); SEGMENT.fill(SEGCOLOR(1)); } @@ -151,48 +153,48 @@ uint16_t mode_2DTetrisAI() tetrisai_data->intelligence = SEGMENT.custom1; double dui = 0.2 - (0.2 * (tetrisai_data->intelligence / 255.0)); - tetrisai_data->tetris.ai.aHeight = -0.510066 + dui; - tetrisai_data->tetris.ai.fullLines = 0.760666 - dui; - tetrisai_data->tetris.ai.holes = -0.35663 + dui; - tetrisai_data->tetris.ai.bumpiness = -0.184483 + dui; + tetris.ai.aHeight = -0.510066 + dui; + tetris.ai.fullLines = 0.760666 - dui; + tetris.ai.holes = -0.35663 + dui; + tetris.ai.bumpiness = -0.184483 + dui; } - if (tetrisai_data->tetris.state == TetrisAIGame::ANIMATE_MOVE) + if (tetris.state == TetrisAIGame::ANIMATE_MOVE) { - if (millis() - tetrisai_data->lastTime > msDelayMove) + if (millis() - lastTime > msDelayMove) { - drawGrid(&tetrisai_data->tetris, tetrisai_data); - tetrisai_data->lastTime = millis(); - tetrisai_data->tetris.poll(); + drawGrid(&tetris, tetrisai_data); + lastTime = millis(); + tetris.poll(); } } - else if (tetrisai_data->tetris.state == TetrisAIGame::ANIMATE_GAME_OVER) + else if (tetris.state == TetrisAIGame::ANIMATE_GAME_OVER) { - if (millis() - tetrisai_data->lastTime > msDelayGameOver) + if (millis() - lastTime > msDelayGameOver) { - drawGrid(&tetrisai_data->tetris, tetrisai_data); - tetrisai_data->lastTime = millis(); - tetrisai_data->tetris.poll(); + drawGrid(&tetris, tetrisai_data); + lastTime = millis(); + tetris.poll(); } } - else if (tetrisai_data->tetris.state == TetrisAIGame::FIND_BEST_MOVE) + else if (tetris.state == TetrisAIGame::FIND_BEST_MOVE) { if (SEGMENT.check3) { if(tetrisai_data->mistaceCountdown == 0) { - tetrisai_data->tetris.ai.findWorstMove = true; - tetrisai_data->tetris.poll(); - tetrisai_data->tetris.ai.findWorstMove = false; + tetris.ai.findWorstMove = true; + tetris.poll(); + tetris.ai.findWorstMove = false; tetrisai_data->mistaceCountdown = SEGMENT.custom3; } tetrisai_data->mistaceCountdown--; } - tetrisai_data->tetris.poll(); + tetris.poll(); } else { - tetrisai_data->tetris.poll(); + tetris.poll(); } return FRAMETIME;