private void AddTetrimino(TetrisState state, TetrisWell well) { foreach (Point segment in state.Set[state.Orientation]) { well.AddBlock(new Point(state.X + segment.X, state.Y + segment.Y), colors[state.Index]); } }
private int CountHoles(TetrisState state) { int holes = 0; for (int col = 0; col < state.Columns; col++) { bool foundBrick = false; for (int row = 0; row < state.Rows; row++) { TetrisBrick brick = state.FixedBricks[row][col]; if (brick != null) { foundBrick = true; } if (foundBrick && brick == null) { holes++; } } } return(holes); }
/// <summary> /// Executes random actions with the next pieces over the state of node /// </summary> /// <param name="node"></param> /// <returns></returns> protected virtual float Rollout(MCTSNode node) { TetrisState newState = node.state.CloneState(); int nPieces = 0; float totalScore = node.state.GetScore(); float weight = 1f; float totalWeight = weight; //node.height identifies the height of the node in the MCTreeSearch, but also identifies the index of the piece inside the history of all the pieces played //So, if that node.height plus the number of pieces played in the rollout are bigger than the number of known pieces, then the rollout must stop. //Also it stops if an action has caused a game over while ((node.height + nPieces) < pieces.Count && !newState.IsTerminal()) { weight *= rolloutScoreWeightReduction; totalWeight += weight; PieceModel piece; piece = new PieceModel(pieces[node.height + nPieces]); newState.DoAction(piece, newState.GetRandomAction(piece)); nPieces++; totalScore += newState.GetScore() * weight; } float score = totalScore / totalWeight; rollouts++; return(score); }
public TetrisState Restart() { Initialize(); TetrisState state = Start(); return(state); }
public int height; //Identifies in which height of the tree search is public MCTSNode(int id, MCTSNode parent, TetrisState state, PieceAction action, PieceModel currentPiece) { this.id = id; this.parent = parent; this.state = state; this.action = action; this.currentPiece = currentPiece; children = new List <MCTSNode>(); if (parent != null) { height = parent.height + 1; } else { height = 0; } MCTreeSearch.nNodes++; if (height > MCTreeSearch.currentHeight) { MCTreeSearch.currentHeight = height; } }
private TetrisState ComputeStateAfterOneMove(TetrisState initialState, TetrisMove move) { TetrisState finalState = null; switch (move) { case TetrisMove.MoveLeft: finalState = Engine.MovePieceLeft(initialState); break; case TetrisMove.MoveRight: finalState = Engine.MovePieceRight(initialState); break; case TetrisMove.MoveDown: finalState = Engine.MovePieceDown(initialState); break; case TetrisMove.MoveAllTheWayDown: finalState = Engine.MovePieceAllTheWayDown(initialState); break; case TetrisMove.Rotate: finalState = Engine.RotatePiece(initialState); break; default: throw new Exception($"Unknown move: {move}"); } return(finalState); }
internal TryMovePieceResult TryMovePiece(TetrisState state, int rowDelta, int columnDelta) { lock (PieceMoveLock) { SetState(state); TryMovePieceResult result = new TryMovePieceResult(); bool canMove = CanMovePiece(rowDelta, columnDelta); if (canMove) { MovePiece(CurrentPiece, rowDelta, columnDelta); result.Moved = true; } else { bool tryingToMoveDown = rowDelta > 0; if (tryingToMoveDown) { result.DeletedRows = StickPiece(); MakeNewPiece(); if (CurrentPiece == null) { result.IsGameEnd = true; } } } return(result); } }
private void RemoveTetrimino(TetrisState state, TetrisWell well) { foreach (Point segment in state.Set[state.Orientation]) { well.RemoveBlock(new Point(state.X + segment.X, state.Y + segment.Y)); } }
private void Engine_StateChanged(TetrisState newState) { TetrisState oldState = GameState; GameState = newState; Redraw(oldState, newState); }
// c'tor public TetrisModelImpl() { Debug.WriteLine("c'tor TetrisModel"); this.random = new Random(); this.testTetriCounter = 0; this.board = new TetrisBoard(this.NumRows, this.NumColumns); this.board.BoardChanged += this.TetrisBoard_Changed; this.board.LinesCompleted += this.TetrisBoard_LinesCompleted; this.state = TetrisState.Idle; this.gameState = GameState.GameIdle; this.viewCellList = new ViewCellList(); // initialize score management this.level = 1; this.lines = 0; this.score = 0; // setup timer this.timerMode = TimerMode.Normal; this.maxIntervalCounter = MaxIntervalCounter; this.currentIntervalCounter = 0; }
internal void Update(TetrisState oldState, TetrisState newState) { DisplayNextPiece(oldState?.NextPiece, newState.NextPiece); DisplayScore(newState.Score); DisplayLineCount(newState.Lines); DisplaySpeed(newState.Speed); }
/// <summary> /// Uses also the budget in order to make a fair calculation: if the budget is consumed checking the Tetris, then it stops /// </summary> /// <param name="nextPiece"></param> /// <param name="currentTetrisState"></param> /// <param name="possibleActions"></param> /// <param name="budget"></param> /// <returns></returns> protected IEnumerator CheckTetris(PieceModel nextPiece, TetrisState currentTetrisState, List <PieceAction> possibleActions, float budget) { int i = 0; while (t0 < budget && i < possibleActions.Count) { if (!TBController.pausedGame) { t0 += Time.deltaTime; PieceAction action = possibleActions[i]; if (action.rotationIndex == 0) //The I piece is horizontal, not vertical, so it can't be a Tetris { i++; continue; } TetrisState newState = currentTetrisState.CloneState(); newState.DoAction(nextPiece, action); nextPiece.ResetCoordinates(); if (newState.IsTetris()) { bestAction = action; } i++; } yield return(null); } }
private void ComputeCurrentPieceDifferences(TetrisState oldState, TetrisState newState, List <TetrisBrickWithPosition> diffList) { TetrisPieceWithPosition oldPieceWithPos = oldState?.CurrentPiece; TetrisPieceWithPosition newPieceWithPos = newState.CurrentPiece; AddNullsForOldPiece(diffList, oldPieceWithPos); if (newPieceWithPos == null) { return; } for (int row = 0; row < newPieceWithPos.Piece.MaxSize; row++) { for (int column = 0; column < newPieceWithPos.Piece.MaxSize; column++) { int rowRelativeToBoard = row + newPieceWithPos.Position.Row; int columnRelativeToBoard = column + newPieceWithPos.Position.Column; TetrisBrick newBrick = newPieceWithPos.Piece[row, column]; if (newBrick != null) { diffList.Add(new TetrisBrickWithPosition { Brick = (TetrisBrick)newBrick.Clone(), Position = new Coordinates(rowRelativeToBoard, columnRelativeToBoard) }); } } } }
public void FillCells(TetrisState state) { foreach (Point segment in state.Set[state.Orientation]) { FillCell(new Point(state.X, state.Y) + segment); } }
private void Redraw(TetrisState oldState, TetrisState newState) { SafeDraw(() => { BoardRenderer.DisplayBricks(oldState, newState); TextRenderer.Update(oldState, newState); }); }
private IEnumerable <TetrisMoves> MakePossibleSolutions(TetrisState state) { if (state.CurrentPiece == null) { yield break; } int crtPieceColumn = state.CurrentPiece.Position.Column; for (int rotations = 0; rotations < 4; rotations++) { TetrisMoves solution = MakeSolution(rotations, 0, 0); yield return(solution); for (int leftMoves = 1; leftMoves < crtPieceColumn + 2; leftMoves++) { TetrisMoves solutionsLeft = MakeSolution(rotations, leftMoves, 0); yield return(solutionsLeft); } for (int rightMoves = 1; rightMoves < state.Columns - crtPieceColumn; rightMoves++) { TetrisMoves solutionsRight = MakeSolution(rotations, 0, rightMoves); yield return(solutionsRight); } } }
private void ExecuteCommand(Func <TetrisState, TetrisState> command) { TetrisState oldState = GameState; GameState = command(GameState); Redraw(oldState, GameState); }
TetrisState GetState() { TetrisState state = BoardLogic.GetState(); AddEngineSpecificDataToState(state); return(state); }
public void Reset() { state = null; parent = null; children.Clear(); score = 0; simulations = 0; }
private void TranslateTetrimino(TetrisState state, Point translation) { foreach (Point segment in state.Set[state.Orientation]) { well.TranslateBlock(new Point(state.X + segment.X, state.Y + segment.Y), translation); } state.X += translation.X; state.Y += translation.Y; }
public Node Rent(TetrisState state) { if (pool.TryTake(out Node node)) { node.state = state; return(node); } return(new Node(state)); }
/// <summary> /// Giving a TetrisState and a PieceAction, the piece model reproduces this action /// </summary> /// <param name="action"></param> /// <param name="tetrisState"></param> public void DoAction(PieceAction action, TetrisState tetrisState) { for (int i = 0; i < action.rotationIndex; i++) { Rotate(); } Move(new Vector2Int(action.xCoord - tileCoordinates[0].x, 0), tetrisState); }
public List <TetrisBrickWithPosition> ComputeDifferences(TetrisState oldState, TetrisState newState) { List <TetrisBrickWithPosition> result = new List <TetrisBrickWithPosition>(); ComputeCurrentPieceDifferences(oldState, newState, result); ComputeFixedBricksDifferences(oldState, newState, result); result = result.Where(b => IsInsideBoard(b.Position)).ToList(); return(result); }
internal double ComputeScore(TetrisState state) { double brickScore = ComputeBrickScore(state); double holeScore = ComputeHoleScore(state); double heightScore = ComputeHeightScore(state); double score = brickScore + holeScore + heightScore; return(score); }
public TetrisBot() { boardWidth = TBController.boardWidth; boardHeight = TBController.boardHeight; currentTetrisState = new TetrisState(); emptyTetrisState = new TetrisState(); pieceActionDictionary = new Dictionary <PieceType, List <PieceAction> >(); }
private void DropTetrimino(TetrisState state) { RemoveTetrimino(state, well); while (grid.IsEmpty(state.Down())) { state.Y++; } AddTetrimino(state, well); AddTetriminoToGrid(state); }
public bool IsEmpty(TetrisState state) { foreach (Point segment in state.Set[state.Orientation]) { if (!IsEmpty(new Point(state.X, state.Y) + segment)) { return(false); } } return(true); }
public TetrisState GetState() { TetrisState state = new TetrisState(Rows, Columns); FixedBricksLogic.CopyFixedBricksInState(state); if (CurrentPiece != null) { state.CurrentPiece = (TetrisPieceWithPosition)CurrentPiece.Clone(); } state.NextPiece = (TetrisPiece)NextPiece.Clone(); return(state); }
private void SetState(TetrisState state) { FixedBricksLogic.CopyFixedBricksFromState(state); if (state.CurrentPiece != null) { CurrentPiece = (TetrisPieceWithPosition)state.CurrentPiece.Clone(); } else { CurrentPiece = null; } }
private TetrisState TryRotatePiece(TetrisState state) { if (State != GameState.Running) { return(state); } TryRotatePieceResult result = BoardLogic.TryRotatePiece(state); TetrisState newState = GetState(); return(newState); }