示例#1
0
        public void CanRotate_BoundaryInTheWay_ReturnFalse(Rotation rotation, int x, int y)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(x, y));

            bool result = _tetrisBoard.CanRotate(tetrimino, rotation);

            Assert.False(result);
        }
示例#2
0
 public void HardDrop(Tetrimino piece, int moveVal)
 {
     if (CheckForMoveCollision(piece, moveVal))
     {
         piece.MoveUpDown(moveVal - 1);
     }
     else
     {
         HardDrop(piece, moveVal + 1);
     }
 }
示例#3
0
        public void CanRotate_WithSpace_ReturnTrue(Rotation rotation)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(2, 2));

            bool result = _tetrisBoard.CanRotate(tetrimino, rotation);

            Assert.True(result);
        }
示例#4
0
        public void CanMoveTetrimino_WithSpace_ReturnTrue(int byX, int byY)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(2, 2));

            bool result = _tetrisBoard.CanMoveTetrimino(tetrimino, new Point(byX, byY));

            Assert.True(result);
        }
示例#5
0
        public void Rotate_BoundaryInTheWay_ThrowException(Rotation rotation, int x, int y)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(x, y));

            Assert.Throws <Exceptions.BlockOutsideBoardException>(
                () => _tetrisBoard.Rotate(tetrimino, rotation)
                );
        }
示例#6
0
        public void AddTetriminoAt_TakenSpot_ThrowException()
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);
            Point     point     = new Point(2, 2);

            _tetrisBoard.AddBlockAt(new Block(), new Point(2, 2));

            Assert.Throws <Exceptions.NoOverwriteBlockException>(
                () => _tetrisBoard.AddTetriminoAt(tetrimino, point)
                );
        }
示例#7
0
 public void LegalMoveUpDown(Tetrimino piece, int moveVal)
 {
     // Check For Collisions
     if (!CheckForMoveCollision(piece, moveVal))
     {
         _activePiece.MoveUpDown(moveVal);
     }
     else
     {
         DropActivePiece();
     }
 }
示例#8
0
        public void Rotate_BlockInTheWay_ThrowException(Rotation rotation, int x, int y)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(x, y));
            _tetrisBoard.AddBlockAt(new Block(), new Point(2, 2));

            Assert.Throws <Exceptions.NoOverwriteBlockException>(
                () => _tetrisBoard.Rotate(tetrimino, rotation)
                );
        }
示例#9
0
        public void Rotate_WithSpace_ReplaceBlocks(Rotation rotation, int noseX, int noseY)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(2, 2));

            _tetrisBoard.Rotate(tetrimino, rotation);

            Assert.Equal(2, _tetrisBoard.AllBlocks().Length);
            Assert.Equal(blocks[0], _tetrisBoard.BlockAt(new Point(noseX, noseY)));
            Assert.Equal(blocks[1], _tetrisBoard.BlockAt(new Point(2, 2)));
        }
示例#10
0
        public void LegalMoveLeftRight(int moveVal)
        {
            // Copy Active Piece And Simulate Move
            Tetrimino copy = GeneratePiece(_activePiece.GetBlockValue());

            _activePiece.MakeCopy(copy);
            copy.MoveLeftRight(moveVal);

            // If In Bounds, Perform Actual Move
            if (copy.IsInBounds(_tower))
            {
                _activePiece.MoveLeftRight(moveVal);
            }
        }
示例#11
0
        public void MoveTetrimino_BoundaryInTheWay_ThrowException(int byX,
                                                                  int byY,
                                                                  int startX,
                                                                  int startY)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(startX, startY));

            Assert.Throws <Exceptions.BlockOutsideBoardException>(
                () => _tetrisBoard.MoveTetrimino(tetrimino, new Point(byX, byY))
                );
        }
示例#12
0
        public void AddTetriminoAt_WithSpace_AddTetriminoBlocks(Direction direction,
                                                                int noseX,
                                                                int noseY)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(direction, blocks);
            Point     point     = new Point(2, 2);

            _tetrisBoard.AddTetriminoAt(tetrimino, point);

            Assert.Equal(2, _tetrisBoard.AllBlocks().Length);
            Assert.Equal(blocks[0], _tetrisBoard.BlockAt(new Point(noseX, noseY)));
            Assert.Equal(blocks[1], _tetrisBoard.BlockAt(point));
        }
示例#13
0
        public void CanMoveTetrimino_BoundaryInTheWay_ReturnFalse(int byX,
                                                                  int byY,
                                                                  int startX,
                                                                  int startY)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(startX, startY));

            bool result = _tetrisBoard.CanMoveTetrimino(tetrimino, new Point(byX, byY));

            Assert.False(result);
        }
示例#14
0
 private void NewBlock()
 {
     if (!this.gameArray.checkForGameLoss())
     {
         CurrentBlock = NextBlock;
         NextBlock    = tetriminoFactory.getNewBlock();
         drawNext();
     }
     else
     {
         //Player has lost the game!
         endTheGame();
     }
 }
示例#15
0
        public void Reset()
        {
            gameOver              = false;
            tetriminos            = new List <Tetrimino>();
            level                 = 1;
            linesCleared          = 0;
            currentTetriminoIndex = -1;

            randomBag     = new RandomBag(7);
            nextTetrimino = new Tetrimino(tileTexture, (TetriminoType)randomBag.get());
            nextTetrimino.topLeftPosition = nextTextPos + new Vector2(0, 100);
            nextTetrimino.UpdateRectangles();
            NewTetrimino();
        }
示例#16
0
        public bool CheckPieceOverlap(Tetrimino piece)
        {
            for (int row = 0; row < Constants.TowerHeight; row++)
            {
                for (int col = 0; col < Constants.TowerWidth; col++)
                {
                    if (piece.IsBlockPosition(col, row) && TowerData[row, col] != BlockValue.Empty)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#17
0
        public void ReleaseTetrimino_TerminoPlaced_RemoveTetriminoButKeepBlocks()
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(2, 2));

            _tetrisBoard.ReleaseTetrimino(tetrimino);

            Assert.Empty(_tetrisBoard.AllTetriminos());
            Assert.Null(_tetrisBoard.TetriminoPoint(tetrimino));
            Assert.Equal(2, _tetrisBoard.AllBlocks().Length);
            Assert.Equal(blocks[1], _tetrisBoard.BlockAt(new Point(2, 2)));
            Assert.Equal(blocks[0], _tetrisBoard.BlockAt(new Point(2, 1)));
        }
示例#18
0
        public void RemoveTetrimino_TetriminoPlaced_RemoveTetriminoAndBlocks()
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(2, 2));

            _tetrisBoard.RemoveTetrimino(tetrimino);

            Assert.Empty(_tetrisBoard.AllBlocks());
            Assert.Empty(_tetrisBoard.AllTetriminos());
            Assert.Null(_tetrisBoard.TetriminoPoint(tetrimino));
            Assert.Null(_tetrisBoard.BlockAt(new Point(2, 2)));
            Assert.Null(_tetrisBoard.BlockAt(new Point(2, 3)));
        }
示例#19
0
        public void MoveTetrimino_BlockInTheWay_ThrowException(int byX,
                                                               int byY,
                                                               int blockX,
                                                               int blockY)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(2, 2));
            _tetrisBoard.AddBlockAt(new Block(), new Point(blockX, blockY));

            Assert.Throws <Exceptions.NoOverwriteBlockException>(
                () => _tetrisBoard.MoveTetrimino(tetrimino, new Point(byX, byY))
                );
        }
示例#20
0
        public void MoveTetrimino_WithSpace_ReplaceBlocks(int byX, int byY)
        {
            Block[]   blocks    = { new Block(), new Block() };
            Tetrimino tetrimino = new Tetrimino(Direction.UP, blocks);

            _tetrisBoard.AddTetriminoAt(tetrimino, new Point(2, 2));

            Point byPoint = new Point(byX, byY);

            _tetrisBoard.MoveTetrimino(tetrimino, byPoint);

            Point blockPoint1 = Point.AddPoints(new Point(2, 1), byPoint);
            Point blockPoint2 = Point.AddPoints(new Point(2, 2), byPoint);

            Assert.Equal(blocks[0], _tetrisBoard.BlockAt(blockPoint1));
            Assert.Equal(blocks[1], _tetrisBoard.BlockAt(blockPoint2));
        }
示例#21
0
        public void DrawPreviewImage(GameData data, Sprite pieceImg, Tetrimino piece)
        {
            if (piece != null)
            {
                int textureOffset = 0;

                switch (piece.GetBlockValue())
                {
                case BlockValue.IShape:
                    textureOffset = 0;
                    break;

                case BlockValue.LShape:
                    textureOffset = 1;
                    break;

                case BlockValue.JShape:
                    textureOffset = 2;
                    break;

                case BlockValue.SShape:
                    textureOffset = 3;
                    break;

                case BlockValue.ZShape:
                    textureOffset = 4;
                    break;

                case BlockValue.TShape:
                    textureOffset = 5;
                    break;

                case BlockValue.OShape:
                    textureOffset = 6;
                    break;
                }

                pieceImg.TextureRect = new IntRect(textureOffset * Constants.PreviewImageSize, 0, Constants.PreviewImageSize, Constants.PreviewImageSize);
                data.Window.Draw(pieceImg);
            }
        }
示例#22
0
        public GameArea(Stats.Stats stats, ContentManager content, SoundControl soundControl)
        {
            texture      = content.Load <Texture2D>("field");
            bgPosition   = new Vector2(X + OFFSET_X, Y + OFFSET_Y);
            slots        = new BlockSlot[WIDTH, HEIGHT];
            this.content = content;
            this.stats   = stats;

            for (int i = 0; i < WIDTH; i++)
            {
                for (int j = 0; j < HEIGHT; j++)
                {
                    slots[i, j] = new BlockSlot(i, j, X, Y);
                }
            }

            tetrimino = new Tetrimino(Tetrimino.GenerateType(null), content, soundControl);
            PrepareNextTetrimino();
            canHold           = true;
            this.soundControl = soundControl;
        }
示例#23
0
        public void newGame()
        {
            //Resets all necessary variables, then starts a new game.

            gameCanvas.Children.Clear();
            nextBlockCanvas.Children.Clear();

            this.logic = new GameLogic(scoreBox, levelBox, rowsBox);
            this.logic.initializeBoxes();
            gameArray        = new GameArray(gameCanvas, this.logic);
            tetriminoFactory = new TetriminoFactory(gameCanvas, gameArray);

            gameStarted = true;

            CurrentBlock = tetriminoFactory.getNewBlock();
            NextBlock    = tetriminoFactory.getNewBlock();
            drawNext();

            CurrentBlock.Move(CurrentBlock.XCoord, CurrentBlock.YCoord, CurrentBlock.Orientation);
            StartTimer();
        }
示例#24
0
        private void drawNext()
        {
            //These are necessary for drawing the next block in the side window.

            if (NextBlock.Type == "L")
            {
                Next = new LBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "J")
            {
                Next = new JBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "T")
            {
                Next = new TBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "S")
            {
                Next = new SBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "Z")
            {
                Next = new ZBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
            else if (NextBlock.Type == "Square")
            {
                Next = new SquareBlock(nextBlockCanvas, gameArray);
                Next.Move(30, 70, 0);
            }
            else if (NextBlock.Type == "Line")
            {
                Next = new LineBlock(nextBlockCanvas, gameArray);
                Next.Move(40, 80, 0);
            }
        }
示例#25
0
        public void SpawnNextPiece()
        {
            if (_randomPieceIndex > 6)
            {
                _randomPieceIndex = 0;
                ShuffleRandomPieceBag();
            }

            if (_nextPiece == null)
            {
                _nextPiece = GeneratePiece(_randomPieceBag[_randomPieceIndex]);
                _randomPieceIndex++;
            }

            _activePiece = _nextPiece;
            _activePiece.Spawn();
            _nextPiece = GeneratePiece(_randomPieceBag[_randomPieceIndex]);
            _randomPieceIndex++;

            // Create Piece For Outline
            _activeOutline = GeneratePiece(_activePiece.GetBlockValue());
        }
示例#26
0
        public bool CheckForMoveCollision(Tetrimino piece, int moveVal)
        {
            // Copy Active Piece And Simulate Move
            Tetrimino copy = GeneratePiece(piece.GetBlockValue());

            piece.MakeCopy(copy);
            copy.MoveUpDown(moveVal);

            // Check If Block Collides With Pile
            if (_tower.CheckPieceOverlap(copy))
            {
                return(true);
            }

            // Check If Block Collides With Bottom
            if (!copy.IsInBounds(_tower))
            {
                return(true);
            }

            return(false);
        }
示例#27
0
        public void Hold()
        {
            if (!held)
            {
                Tetrimino temp;
                if (holdTetrimino != null)
                {
                    temp = new Tetrimino(tileTexture, holdTetrimino.type);
                }
                else
                {
                    temp = null;
                }

                holdTetrimino                 = new Tetrimino(tileTexture, tetriminos[currentTetriminoIndex].type);
                holdTetrimino.active          = false;
                holdTetrimino.topLeftPosition = holdPos;
                holdTetrimino.UpdateRectangles();

                if (temp != null)
                {
                    tetriminos[currentTetriminoIndex] = new Tetrimino(tileTexture, temp.type);
                }
                else
                {
                    tetriminos[currentTetriminoIndex] = new Tetrimino(tileTexture, nextTetrimino.type);

                    nextTetrimino                 = new Tetrimino(tileTexture, (TetriminoType)randomBag.get());
                    nextTetrimino.active          = false;
                    nextTetrimino.topLeftPosition = nextPos;
                    nextTetrimino.UpdateRectangles();
                }
                CalculateShadowPos();
            }
            held = true;
        }
示例#28
0
        public bool CheckPotentialCollision(CollisionCheckType type, Tetrimino tetrimino, MoveDirection direction)
        {
            List <Rectangle> check;

            if (type == CollisionCheckType.Tetrimino)
            {
                check = tetrimino.rectangles;
            }
            else
            {
                check = tetrimino.shadowRectangles;
            }
            switch (direction)
            {
            case MoveDirection.Left:
                if (side == BoundarySide.Left)
                {
                    foreach (Rectangle rectangle in check)
                    {
                        Rectangle r = rectangle;     //copy
                        r.X -= Tetrimino.tileSize;
                        if (r.X <= box.X)
                        {
                            return(true);
                        }
                    }
                }
                break;

            case MoveDirection.Right:
                if (side == BoundarySide.Right)
                {
                    foreach (Rectangle rectangle in check)
                    {
                        Rectangle r = rectangle;     //copy
                        r.X += Tetrimino.tileSize;
                        if (r.X >= box.X)
                        {
                            return(true);
                        }
                    }
                }
                break;

            case MoveDirection.Down:
                if (side == BoundarySide.Bottom)
                {
                    foreach (Rectangle rectangle in check)
                    {
                        Rectangle r = rectangle;     //copy
                        r.Y += Tetrimino.tileSize;
                        if (r.Y >= box.Y)
                        {
                            return(true);
                        }
                    }
                }
                break;
            }
            return(false);
        }
示例#29
0
        public void Set(int type)
        {
            switch ((int)type)
            {
            case 0:
            {
                shape       = new byte[4, 1];
                size        = new Vector2(4, 1);
                shape[0, 0] = shape[1, 0] = shape[2, 0] = shape[3, 0] = 1;
                break;
            }

            case 1:
            {
                shape       = new byte[3, 2];
                size        = new Vector2(3, 2);
                shape[0, 0] = shape[0, 1] = shape[1, 1] = shape[2, 1] = 1;
                break;
            }

            case 2:
            {
                shape       = new byte[3, 2];
                size        = new Vector2(3, 2);
                shape[0, 1] = shape[1, 1] = shape[2, 1] = shape[2, 0] = 1;
                break;
            }

            case 3:
            {
                shape       = new byte[2, 2];
                size        = new Vector2(2, 2);
                shape[0, 0] = shape[1, 0] = shape[0, 1] = shape[1, 1] = 1;
                break;
            }

            case 4:
            {
                shape       = new byte[3, 2];
                size        = new Vector2(3, 2);
                shape[1, 0] = shape[2, 0] = shape[0, 1] = shape[1, 1] = 1;
                break;
            }

            case 5:
            {
                shape       = new byte[3, 2];
                size        = new Vector2(3, 2);
                shape[1, 0] = shape[0, 1] = shape[1, 1] = shape[2, 1] = 1;
                break;
            }

            case 6:
            {
                shape       = new byte[3, 2];
                size        = new Vector2(3, 2);
                shape[0, 0] = shape[1, 0] = shape[1, 1] = shape[2, 1] = 1;
                break;
            }
            }
            color = Tetrimino.GetColor(type);
        }
示例#30
0
        public void Update(GameControl gameControl, GameTime gameTime, KeyboardState keyState, KeyboardState prevKeyState)
        {
            tetrimino.Update(slots, stats, gameTime, keyState, prevKeyState);

            if (!tetrimino.Falling)
            {
                //Game over
                if (tetrimino.Position.Y < 0)
                {
                    gameControl.GameOver();
                }

                //Detach blocks
                foreach (Block block in tetrimino.Blocks)
                {
                    if (block != null)
                    {
                        block.Falling = false;
                    }
                }

                //Clear any complete lines
                int completeLines = 0;
                for (int i = 0; i < HEIGHT; i++)
                {
                    bool completeLine = true;
                    for (int j = 0; j < WIDTH; j++)
                    {
                        if (slots[j, i].Block == null)
                        {
                            completeLine = false;
                            break;
                        }
                    }
                    if (completeLine)
                    {
                        for (int j = 0; j < WIDTH; j++)
                        {
                            slots[j, i].Block = null;
                        }
                        for (int k = 0; k < WIDTH; k++)
                        {
                            for (int l = i - 1; l >= 0; l--)
                            {
                                if (slots[k, l].Block != null)
                                {
                                    if (!slots[k, l].Block.Falling)
                                    {
                                        slots[k, l + 1].Block = slots[k, l].Block;
                                        slots[k, l].Block     = null;
                                    }
                                }
                            }
                        }
                        stats.Goal--;
                        completeLines++;
                    }
                }
                switch (completeLines)
                {
                case 1: { stats.Score += 100; soundControl.PlayClear1(); break; }

                case 2: { stats.Score += 300; soundControl.PlayClear2(); break; }

                case 3: { stats.Score += 500; soundControl.PlayClear3(); break; }

                case 4: { stats.Score += 800; soundControl.PlayClear4(); break; }
                }

                //New tetrimino
                tetrimino = new Tetrimino(next, content, soundControl);
                PrepareNextTetrimino();
                canHold = true;
            }

            //Hold
            if (keyState.IsKeyDown(Keys.LeftShift) && prevKeyState.IsKeyUp(Keys.LeftShift))
            {
                int currentType = tetrimino.Type;
                if (canHold)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if ((int)tetrimino.Position.Y + j >= 0 && (int)tetrimino.Position.X + i >= 0 && (int)tetrimino.Position.X + i < WIDTH && (int)tetrimino.Position.Y + j < HEIGHT)
                            {
                                if (tetrimino.Blocks[i, j] != null)
                                {
                                    slots[(int)tetrimino.Position.X + i, (int)tetrimino.Position.Y + j].Block = null;
                                }
                            }
                        }
                    }
                    if (hold == null)
                    {
                        tetrimino = new Tetrimino(next, content, soundControl);
                        PrepareNextTetrimino();
                    }
                    else
                    {
                        tetrimino = new Tetrimino((int)hold, content, soundControl);
                    }
                    hold = currentType;
                    stats.Hold.Set((int)hold);
                    canHold = false;
                    soundControl.PlayDropSoft();
                }
            }
        }