/// <summary> /// Extracts the next piece visible on the screenshot. /// Throws an exception, if the next piece is not visible on the screenshot. /// </summary> /// <param name="screenshot">The screenshot to extract the piece from.</param> /// <returns>The next Tetrimino.</returns> public Tetrimino ExtractNextPiece(IScreenshot screenshot) { // relevant tiles on the screen: x : 14 - 17, y : 13 - 16 ushort mask = 0; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { byte mean = screenshot.GetTileMean(TetrisConstants.NextPieceTileOrigin.X + x, TetrisConstants.NextPieceTileOrigin.Y + y); if (IsBlock(mean)) { int index = 4 * (2 - y) + (x); mask |= (ushort)(1 << index); } } } var nextPiece = Piece.FromMask(mask); if (nextPiece?.Tetrimino == null) { throw new ApplicationException("Next piece not visible on the screenshot"); } return(nextPiece.Tetrimino); }
// x and y are in board coordinates private byte GetTileBlockMean(IScreenshot screenshot, int x, int y) { // TODO: make relative to board size? // ignore walls if (x < 2 || x > 11) { return(255); } if (y < 0 || y > 17) { return(255); } return(screenshot.GetTileMean(x, y)); }
// Tiles: x : 14 - 17, y : 13 - 16 public Tetrimino?ExtractNextPiece(IScreenshot screenshot) { ushort mask = 0; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { byte mean = screenshot.GetTileMean(TetrisConstants.NextPieceTileOrigin.X + x, TetrisConstants.NextPieceTileOrigin.Y + y); if (IsBlock(mean)) { int index = 4 * (2 - y) + (x); mask |= (ushort)(1 << index); } } } return(Piece.FromMask(mask)?.Tetrimino); }
// Tiles: x : 5 - 8, y : 0 - 2 public Board ExtractBoard(IScreenshot screenshot) { var board = new Board(); for (int x = 0; x < 10; x++) { // TODO: extract whole board but subtract current piece for (int y = 3; y < 18; y++) { byte mean = screenshot.GetTileMean(TetrisConstants.BoardTileOrigin.X + x, TetrisConstants.BoardTileOrigin.Y + y); if (IsBlock(mean)) { board.Occupy(x, 17 - y); } } } return(board); }
// x and y are in board coordinates private bool IsTileBlock(IScreenshot screenshot, int x, int y) { // TODO: make relative to board size? // ignore walls if (x < 2 || x > 11) { return(false); } if (y < 0 || y > 17) { return(false); } var mean = screenshot.GetTileMean(x, y); if (IsBlock(mean)) { return(true); } return(false); }