示例#1
0
        public void IEraseTest()
        {
            TetrisPiece piece = new TetrisPiece(PieceType.I);

            piece.EraseRow(4);
            List <Square> squares = piece.OccupiedSquares;

            Assert.AreEqual(4, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 1 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 3 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 4 && s.X == 5));

            piece.EraseRow(4);
            squares = piece.OccupiedSquares;
            Assert.AreEqual(3, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 3 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 4 && s.X == 5));

            piece.EraseRow(4);
            squares = piece.OccupiedSquares;
            Assert.AreEqual(2, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 3 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 4 && s.X == 5));

            piece.EraseRow(4);
            squares = piece.OccupiedSquares;
            Assert.AreEqual(1, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 4 && s.X == 5));

            piece.EraseRow(4);
            squares = piece.OccupiedSquares;
            Assert.AreEqual(0, squares.Count);
        }
示例#2
0
    public bool IsObstructed(TetrisPiece piece, ObstructionDirection direction)
    {
        bool isObstructed = false;

        int yCheck = 0;
        int xCheck = 0;

        switch (direction)
        {
        case ObstructionDirection.DOWN:
        {
            yCheck = 1;
            xCheck = 0;
        }
        break;

        case ObstructionDirection.LEFT:
        {
            yCheck = 0;
            xCheck = -1;
        }
        break;

        case ObstructionDirection.RIGHT:
        {
            yCheck = 0;
            xCheck = 1;
        }
        break;
        }

        for (int itRow = piece.size.row - 1; itRow >= 0; itRow--)
        {
            for (int itCol = 0; itCol < piece.size.col; itCol++)
            {
                // check block is filled
                if (piece.Get(itRow, itCol) == 1)
                {
                    int rowPos = (piece.position.row + itRow) + yCheck;
                    int colPos = (piece.position.col + itCol) + xCheck;

                    if (rowPos >= 0 && rowPos < this.row &&
                        colPos >= 0 && colPos < this.col)
                    {
                        if (boardData[rowPos, colPos] == 1)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        // hit bottom
                        return(true);
                    }
                }
            }
        }

        return(isObstructed);
    }
        public async Task LoadAsync(string savedFileContents)
        {
            loader = savedFileContents.Split('\n');
            // reading table size
            string SizeData = loader[0];

            Size = Int32.Parse(SizeData);
            // reading current piece
            string[] currentPieceData = loader[1].Split(' ');
            CurrentPiece           = new TetrisPiece();
            CurrentPiece.Type      = (PieceType)(Int32.Parse(currentPieceData[0]));
            CurrentPiece.Direction = (PieceDirection)Int32.Parse(currentPieceData[1]);
            CurrentPiece.Coordinates.Clear();
            for (int coordinate = 0; coordinate < 4; ++coordinate)
            {
                CurrentPiece.Coordinates.Add((Int32.Parse(currentPieceData[2 * (coordinate + 1)]), Int32.Parse(currentPieceData[2 * (coordinate + 1) + 1])));
            }
            // reading table lines
            Table = new int[16, Size];
            for (int line = 0; line < 16; ++line)
            {
                string[] TableLineData = loader[line + 2].Split(' ');
                for (int row = 0; row < Size; ++row)
                {
                    Table[line, row] = Int32.Parse(TableLineData[row]);
                }
            }
        }
示例#4
0
    public bool IsSelectionCorrect(TetrisPiece selection)
    {
        switch (currentComparison)
        {
        case "area":
            if (selection == largerArea)
            {
                Debug.Log("correct!");
                return(true);
            }
            Debug.Log("incorrect!");
            return(false);

        case "perimeter":
            if (selection == largerPerimeter)
            {
                Debug.Log("correct!");
                return(true);
            }
            Debug.Log("incorrect!");
            return(false);
        }

        Debug.Log("warning : No matching case");
        return(false);
    }
示例#5
0
    public override bool Equals(System.Object other)
    {
        if (other == null)
        {
            return(false);
        }

        TetrisPiece p = other as TetrisPiece;

        if ((System.Object)p == null)
        {
            return(false);
        }

        if (type != p.type)
        {
            return(false);
        }

        for (int i = 0; i < boxes.Count; i++)
        {
            if (boxes[i].value != p.boxes[i].value)
            {
                return(false);
            }
        }
        return(true);
    }
示例#6
0
    public void UpdateGrid(TetrisPiece tetrisPiece)
    {
        for (int y = 0; y < gridHeight; ++y)
        {
            for (int x = 0; x < gridWidth; ++x)
            {
                if (grid[x, y] != null)
                {
                    if (grid[x, y].parent == tetrisPiece.transform)
                    {
                        grid[x, y] = null;
                    }
                }
            }
        }

        foreach (Transform piece in tetrisPiece.transform)
        {
            Vector2 pos = Round(piece.position);
            if (pos.y < gridHeight)
            {
                grid[(int)pos.x, (int)pos.y] = piece;
            }
        }
    }
示例#7
0
        public void TRotateTest()
        {
            TetrisPiece piece = new TetrisPiece(PieceType.T);

            piece.Rotate();
            List <Square> squares = piece.OccupiedSquares;

            Assert.AreEqual(4, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 1 && s.X == 6));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 6));
            Assert.IsTrue(squares.Any(s => s.Y == 3 && s.X == 6));

            piece.Rotate();
            squares = piece.OccupiedSquares;
            Assert.AreEqual(4, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 1 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 4));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 6));

            piece.Rotate();
            squares = piece.OccupiedSquares;
            Assert.AreEqual(4, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 0 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 1 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 1 && s.X == 6));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 5));
        }
示例#8
0
 void GetNextPiece()
 {
     if (game.splicer.pieces.Count > 0)
     {
         PieceIdx = Random.Range(0, game.splicer.pieces.Count);
         Piece    = game.splicer.pieces[PieceIdx];
     }
 }
示例#9
0
 /// <summary>
 /// Constructeur
 /// </summary>
 /// <param name="tetrisPiece"></param>
 /// <param name="x"></param>
 public TetrisPiece(TetrisPiece tetrisPiece, int x)
 {
     PieceID  = tetrisPiece.PieceID;
     Rotation = tetrisPiece.Rotation;
     X        = tetrisPiece.X + x;
     Y        = tetrisPiece.Y;
     Couleur  = tetrisPiece.Couleur;
 }
示例#10
0
    public void AttachPiece(TetrisPiece piece, int row, int col)
    {
        piece.SetPos(row, col);
        piece.ApplyChange();
        piece.RefreshView();

        tetrisBoard.AttachPiece(piece);
        tetrisBoard.CheckRowClear();
    }
示例#11
0
 /// <summary>
 /// Constructeur
 /// </summary>
 /// <param name="tetrisPiece"></param>
 public TetrisPiece(TetrisPiece tetrisPiece)
 {
     PieceID    = tetrisPiece.PieceID;
     Rotation   = tetrisPiece.Rotation;
     X          = tetrisPiece.X;
     Y          = tetrisPiece.Y;
     Couleur    = tetrisPiece.Couleur;
     TmpCouleur = tetrisPiece.Couleur;
 }
示例#12
0
    void OnMouseDown()
    {
        //get parent script Tetris Piece
        Transform parent = gameObject.transform.parent;

        TetrisPiece pieceScript = (TetrisPiece)parent.GetComponent(typeof(TetrisPiece));

        pieceScript.Selected();
    }
示例#13
0
        private void RotatePieceRandomly(TetrisPiece piece)
        {
            int rotationCount = _random.Next(4);

            for (int i = 0; i <= rotationCount; i++)
            {
                piece.Rotate90DegreesClockwise();
            }
        }
示例#14
0
    public void CreateBlock(int x, float rotation)
    {
        V2Int[]     positions = tetrominoes[currentPiece].GetBlockPositions(x, TetrisSettings.SpawnY, rotation);
        TetrisPiece newBlock  = Instantiate(tetrominoes[currentPiece]).GetComponent <TetrisPiece>();

        newBlock.transform.SetParent(transform);
        newBlock.gameObject.SetActive(true);
        newBlock.Init(this, grid, agent);
        newBlock.SetBlockPositions(positions);
    }
示例#15
0
 /// <summary>
 /// Constructeur
 /// </summary>
 /// <param name="tetrisPiece"></param>
 /// <param name="y"></param>
 /// <param name="testScore"></param>
 public TetrisPiece(TetrisPiece tetrisPiece, int y, bool testScore)
 {
     PieceID    = tetrisPiece.PieceID;
     Rotation   = tetrisPiece.Rotation;
     X          = tetrisPiece.X;
     Y          = tetrisPiece.Y + y;
     Couleur    = tetrisPiece.Couleur;
     TmpCouleur = tetrisPiece.Couleur;
     TestScore  = testScore;
 }
示例#16
0
    public void StartTetrisMode(GameObject piece)
    {
        isRunning   = true;
        tetrisBlock = SpawnBlock(piece.transform);

        position2D  = ConvertV3toV2Int(spawn.localPosition);
        tetrisPiece = tetrisBlock.GetComponent <TetrisPiece>();
        GameInputManager.Instance.SetPlayerInputMode(playerId, 1);
        //Set the local position to be 0, but be sure to set the pivot correctly in the 2D Object
        tetrisBlock.transform.localPosition = new Vector3(0, 0, 0);
    }
示例#17
0
 public void NewGame(int size)
 {
     Size         = size;
     Table        = new int[16, Size];
     GameActive   = true;
     CurrentPiece = new TetrisPiece();
     Table[CurrentPiece.Coordinates[0].Item1, CurrentPiece.Coordinates[0].Item2] = (int)CurrentPiece.Type + 1;
     Table[CurrentPiece.Coordinates[1].Item1, CurrentPiece.Coordinates[1].Item2] = (int)CurrentPiece.Type + 1;
     Table[CurrentPiece.Coordinates[2].Item1, CurrentPiece.Coordinates[2].Item2] = (int)CurrentPiece.Type + 1;
     Table[CurrentPiece.Coordinates[3].Item1, CurrentPiece.Coordinates[3].Item2] = (int)CurrentPiece.Type + 1;
 }
示例#18
0
        private TetrisPiece MakeRandomPiece()
        {
            int         pieceIndex     = _random.Next(_prototypePieces.Length);
            TetrisPiece prototypePiece = _prototypePieces[pieceIndex];
            TetrisPiece piece          = (TetrisPiece)prototypePiece.Clone();

            piece.Color = _colorFactory.MakeRandomColor();
            RotatePieceRandomly(piece);

            return(piece);
        }
示例#19
0
 public int anchorPiece(Point pos, List <TetrisPiece> pieces)
 {
     for (int i = 0; i < pieces.Count; i++)
     {
         TetrisPiece other = pieces[i];
         if (other.Fits(pos) && this == other)
         {
             return(i);
         }
     }
     return(-1);
 }
示例#20
0
 public TetrisCore(TemplatePiece[] pTemplatePieces, int tw = 10, int th = 20)
 {
     TetrisWidth    = tw;
     TetrisHeight   = th;
     TetrisBoard    = new bool [TetrisWidth, TetrisHeight];
     ColourBoard    = new Color[TetrisWidth, TetrisHeight];
     templatePieces = pTemplatePieces;
     CurrPiece      = new TetrisPiece();
     NextPiece      = new TetrisPiece();
     ClearBoard();
     EventSystem <TetrisControlEvent> .Subscribe(TetrisControlEvent.Restart, ClearBoard);
 }
示例#21
0
    private void MoveNextPieceToTop()
    {
        _currentPiece = _nextPiece;
        _currentPiece.transform.position = new Vector3(BOARD_WIDTH / 2 * PIECE_SIZE - PIECE_SIZE, BOARD_HEIGHT * PIECE_SIZE - (3 * PIECE_SIZE));
        _currentPiece.name = "Current Piece";

        _controller.CurrentPiece = _currentPiece;

        var descendingPiece = _currentPiece.gameObject.AddComponent <DescendingPiece>();

        descendingPiece.SecondsBetweenDrops = _secondsBetweenVerticalDrops;

        SpawnNextUpPiece();
    }
示例#22
0
    public TetrisPiece CreatePiece(TetrisPiece.PieceType type)
    {
        TetrisPiece tetrisPiece = this.tetrisPieceFactory.Create();

        tetrisPiece.Init(type);
        //TetrisPiece tetrisPiece = this.tetrisPieceFactory.Generate(TetrisPiece.PieceType.I);
        tetrisPiece.position.row = 0;
        tetrisPiece.position.col = 3;

        RenderPiece(tetrisPiece);

        currentFallingPiece = tetrisPiece;

        return(tetrisPiece);
    }
示例#23
0
    public bool CheckIsAboveGrid(TetrisPiece piece)
    {
        for (int x = 0; x < gridWidth; ++x)
        {
            foreach (Transform mino in piece.transform)
            {
                Vector2 pos = Round(mino.position);
                if (pos.y > gridHeight - 1)
                {
                    return(true);
                }
            }
        }

        return(false);
    }
示例#24
0
        private void DisplayInPlaceOfPiece(TetrisPiece piece, Coordinates coord, string displayChar, string blankChar)
        {
            System.Console.ForegroundColor = (piece.Color as ConsoleColor2).Value;

            for (int pieceRow = 0; pieceRow < piece.MaxSize; pieceRow++)
            {
                for (int pieceColumn = 0; pieceColumn < piece.MaxSize; pieceColumn++)
                {
                    int  windowRow    = coord.Row + pieceRow;
                    int  windowColumn = coord.Column + pieceColumn;
                    bool isBrick      = piece[pieceRow, pieceColumn] != null;

                    System.Console.SetCursorPosition(windowColumn, windowRow);
                    System.Console.Write(isBrick ? displayChar.ToString() : blankChar);
                }
            }
        }
示例#25
0
 public void AttachPiece(TetrisPiece piece)
 {
     if (IsInside(piece))
     {
         for (int itRow = 0; itRow < piece.size.row; itRow++)
         {
             for (int itCol = 0; itCol < piece.size.col; itCol++)
             {
                 if (piece.Get(itRow, itCol) == 1)
                 {
                     boardData[piece.position.row + itRow, piece.position.col + itCol]   = piece.Get(itRow, itCol);
                     tetrisCells[piece.position.row + itRow, piece.position.col + itCol] = piece.GetCell(itRow, itCol);
                 }
             }
         }
     }
 }
示例#26
0
    public void RotateIPieceCCW()
    {
        CommonInstall();
        // Use the Assert class to test conditions.

        TetrisPiece piece = factory.Create();

        piece.Init(TetrisPiece.PieceType.I);

        piece.Rotate(TetrisPiece.Rotation.CCW);
        piece.ApplyChange();
        Assert.AreEqual(
            "0100\n" +
            "0100\n" +
            "0100\n" +
            "0100",
            piece.GetString(), "Failed CCW rotate once");

        piece.Rotate(TetrisPiece.Rotation.CCW);
        piece.ApplyChange();
        Assert.AreEqual(
            "0000\n" +
            "0000\n" +
            "1111\n" +
            "0000",
            piece.GetString(), "Failed CCW rotate twice");

        piece.Rotate(TetrisPiece.Rotation.CCW);
        piece.ApplyChange();
        Assert.AreEqual(
            "0010\n" +
            "0010\n" +
            "0010\n" +
            "0010",
            piece.GetString(), "Failed CCW rotate three times");

        piece.Rotate(TetrisPiece.Rotation.CCW);
        piece.ApplyChange();
        Assert.AreEqual(
            "0000\n" +
            "1111\n" +
            "0000\n" +
            "0000",
            piece.GetString(), "Failed CCW rotate four times");
    }
示例#27
0
        public void AddPiece(TetrisPieceWithPosition pieceWithPos)
        {
            TetrisPiece piece = pieceWithPos.Piece;

            for (int row = 0; row < piece.MaxSize; row++)
            {
                for (int column = 0; column < piece.MaxSize; column++)
                {
                    TetrisBrick brick = piece[row, column];

                    if (brick != null)
                    {
                        int rowRelativeToBoard    = row + pieceWithPos.Position.Row;
                        int columnRelativeToBoard = column + pieceWithPos.Position.Column;
                        Bricks[rowRelativeToBoard][columnRelativeToBoard] = (TetrisBrick)brick.Clone();
                    }
                }
            }
        }
示例#28
0
    public void UpdateFallingPiece()
    {
        if (currentFallingPiece != null)
        {
            if (!currentFallingPiece.IsObstructed(TetrisBoard.ObstructionDirection.DOWN))
            {
                // move object
                currentFallingPiece.Move(1, 0);
                currentFallingPiece.RefreshView();
            }
            else if (currentFallingPiece.IsObstructed(TetrisBoard.ObstructionDirection.DOWN))
            {
                tetrisBoard.AttachPiece(currentFallingPiece);
                tetrisBoard.CheckRowClear();

                currentFallingPiece = null;
            }
        }
    }
示例#29
0
        private TetrisPieceWithPosition MakePieceWithPosition()
        {
            TetrisPieceWithPosition result = new TetrisPieceWithPosition();

            if (NextPiece != null)
            {
                result.Piece = NextPiece;
            }
            else
            {
                result.Piece = PieceFactory.MakePiece();
            }

            NextPiece        = PieceFactory.MakePiece();
            result.NextPiece = NextPiece;
            result.Position  = new Coordinates(0, Columns / 2);

            return(result);
        }
示例#30
0
        public void IRotateEraseTest()
        {
            TetrisPiece piece = new TetrisPiece(PieceType.I);

            piece.Rotate();

            piece.EraseRow(2);
            List <Square> squares = piece.OccupiedSquares;

            Assert.AreEqual(4, squares.Count);
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 4));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 5));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 6));
            Assert.IsTrue(squares.Any(s => s.Y == 2 && s.X == 7));

            piece.EraseRow(2);
            squares = piece.OccupiedSquares;
            Assert.AreEqual(0, squares.Count);
        }
示例#31
0
 public static List<IntVector3> NewPiece(TetrisPiece type)
 {
     switch (type) {
       case TetrisPiece.I:
     return I;
       case TetrisPiece.J:
     return J;
       case TetrisPiece.L:
     return L;
       case TetrisPiece.O:
     return O;
       case TetrisPiece.S:
     return S;
       case TetrisPiece.T:
     return T;
       case TetrisPiece.Z:
     return Z;
       default:
     throw new ArgumentException("Unknown piece type");
     }
 }
示例#32
0
    private void MoveNextPieceToTop()
    {
        _currentPiece = _nextPiece;
        _currentPiece.transform.position = new Vector3(BOARD_WIDTH/2 * PIECE_SIZE - PIECE_SIZE, BOARD_HEIGHT * PIECE_SIZE - (3 * PIECE_SIZE));
        _currentPiece.name = "Current Piece";

        _controller.CurrentPiece = _currentPiece;

        var descendingPiece = _currentPiece.gameObject.AddComponent<DescendingPiece>();
        descendingPiece.SecondsBetweenDrops = _secondsBetweenVerticalDrops;

        SpawnNextUpPiece();
    }