/// <summary> /// Initializes the game: creates the board, initializes the score variables, calls to shuffle the bags, emits the first piece and starts the bot, if it's the case /// If the training is activated, plays it too /// </summary> void StartGame() { board = new TileBehaviour[boardWidth, boardHeight]; level = 1; linesCleared = score = pieces = 0; UpdateUIGameStats(); TRG.ShuffleBags(); currentPiece = PieceEmitter.EmitPiece(); startedGame = true; if (playMode != PlayMode.Player) { if (playMode != PlayMode.Training) { if (playMode == PlayMode.Testing) { nextActionsTime = initialCalculationTime; } PlayBot(); } else { geneticAlgorithm.PlayNextGame(); } } }
/// <summary> /// Checks if there are lines to clear and if there are, calls to clear that lines /// </summary> public void CheckLinesToClear() { //This list will stored the same bool values as rows the board have, and the rows that have to be cleared will be true in this list List <bool> linesToClear = new List <bool>(); int firstClearedLine = -1; //First line that has to be cleared starting from bottom int clearedLinesCount = 0; //Amount of lines that have to be cleared for (int i = 0; i < boardHeight; i++) { bool toClear = true; //If there is any position without a tile, then the row isn't completed for (int j = 0; j < boardWidth; j++) { toClear &= board[j, i]; if (!toClear) { break; } } linesToClear.Add(toClear); if (toClear) { clearedLinesCount++; if (firstClearedLine == -1) { firstClearedLine = i; } } } if (clearedLinesCount > 0) { UpdateScoreLines(clearedLinesCount); ClearLines(linesToClear, firstClearedLine); } //Next piece is spawned currentPiece = PieceEmitter.EmitPiece(); pieces++; UpdateUIGameStats(); if (playMode == PlayMode.Training && pieceLimitTraining != 0 && pieces > pieceLimitTraining) //In case training is activated and there is a limit of pieces per game, it checks that this limit has been reached { GameOver(); //In that case, the game is over return; } //Since this is a method called when a piece is locked in the board, here a new next piece is sent to the bot (if that bot is based in MCTS) if (botVersion == BotVersion.MCTSBot) { ((MCTSTetrisBot)bot).AddNewPiece(TRG.GetLastNextPiece()); } }