Example #1
0
 public Game(int width, int height, ImmutableArray<Piece> pieces, bool pieceFixed = false)
 {
     this.width = width;
     this.pieces = CentralizedPieces(pieces, width);
     movingPiece = this.pieces.GetCurrentPiece();
     this.pieces = new Pieces(this.pieces.PiecesArray,this.pieces.CurrentPiece + 1);
     gameField = CreateGameField(height);
     score = 0;
     this.pieceFixed = pieceFixed;
 }
Example #2
0
 public Game(ImmutableArray<ImmutableSortedSet<Cell>> gameField, Pieces pieces,
     Piece movingPiece, int score, int width, bool pieceFixed = false)
 {
     this.gameField = gameField;
     this.pieces = pieces;
     this.score = score;
     this.movingPiece = movingPiece;
     this.width = width;
     this.pieceFixed = pieceFixed;
 }
Example #3
0
        public static Panel Draw(Piece piece)
        {
            var panel = new Panel();

            for (int x = 0; x < piece.W; x++)
            {
                for (int y = 0; y < piece.H; y++)
                {
                    if (piece.Squares[x, y] == 1)
                    {
                        var a = 10+x + Square.size * x;
                        var b = 10 + y + Square.size * y;
                        panel.Controls.Add(new Square(new Point(a, b)));
                    }
                }
            }
            return panel;
        }
Example #4
0
 //Returns false if the whole piece is off the board, returns true otherwise
 public bool checkOnBoard(Piece piece)
 {
     foreach(Block block in piece.Blocks)
     {
         if (checkOnBoard(block.Position))
             return true;
     }
     return false;
 }
Example #5
0
 //used when the currentPiece lands and locks out and the currentPiece is switched to the next one
 public void changeCurrentPiece()
 {
     currentPiece = upcomingPieces.Dequeue();
     canHold = true;
 }
Example #6
0
 //makes the hold piece the current piece if there is no hold piece, otherwise switches the hold piece and the current piece
 public void changeHoldPiece()
 {
     if (canHold)
     {
         if (holdPiece == null)
         {
             holdPiece = currentPiece;
             removeCurrentPiece();
             changeCurrentPiece();
         }
         else
         {
             Piece pieceValue = holdPiece;
             holdPiece = currentPiece;
             removeCurrentPiece();
             currentPiece = new Piece(pieceValue.PieceType);
         }
         canHold = false;
     }
 }
Example #7
0
        private bool kick(Piece piece)
        {
            bool value = piece.canMoveUp();
            if (value)
                piece.moveUp();
            if (isValid(piece))
            {
                blocks = piece.blocks;
                return true;
            }
            else
            {
                bool value1 = piece.canMoveUp();
                if (value1)
                    piece.moveUp();
                if (isValid(piece))
                {
                    blocks = piece.blocks;
                    return true;
                }
                else
                {
                    if (value1)
                        piece.fall();
                }
                if (value)
                    piece.fall();
            }

            value = piece.canMoveLeft();
            if (value)
                piece.moveLeft();
            if (isValid(piece))
            {
                blocks = piece.blocks;
                return true;
            }
            else
            {
                bool value1 = piece.canMoveLeft();
                if (value1)
                    piece.moveLeft();
                if (isValid(piece))
                {
                    blocks = piece.blocks;
                    return true;
                }
                else
                {
                    if (value1)
                        piece.moveRight();
                }
                if (value)
                    piece.moveRight();
            }

            value = piece.canMoveRight();
            if (value)
                piece.moveRight();
            if (isValid(piece))
            {
                blocks = piece.blocks;
                return true;
            }
            else
            {
                bool value1 = piece.canMoveRight();
                if (value1)
                    piece.moveRight();
                if (isValid(piece))
                {
                    blocks = piece.blocks;
                    return true;
                }
                else
                {
                    if (value1)
                        piece.moveLeft();
                }
                if (value)
                    piece.moveLeft();
            }
            return false;
        }
Example #8
0
 private void rotateZ()
 {
     Piece piece = new Piece(pieceType);
     Block[] blocks = piece.blocks;
     switch (rotationState)
     {
         case RotationState.One:
             blocks[0].Position = boundingSquare[0, 0];
             blocks[1].Position = boundingSquare[1, 0];
             blocks[2].Position = boundingSquare[1, 1];
             blocks[3].Position = boundingSquare[2, 1];
             checkRotateValid(piece);
             break;
         case RotationState.Two:
             blocks[0].Position = boundingSquare[2, 0];
             blocks[1].Position = boundingSquare[2, 1];
             blocks[2].Position = boundingSquare[1, 1];
             blocks[3].Position = boundingSquare[1, 2];
             checkRotateValid(piece);
             break;
         case RotationState.Three:
             blocks[0].Position = boundingSquare[2, 2];
             blocks[1].Position = boundingSquare[1, 2];
             blocks[2].Position = boundingSquare[1, 1];
             blocks[3].Position = boundingSquare[0, 1];
             checkRotateValid(piece);
             break;
         case RotationState.Four:
             blocks[0].Position = boundingSquare[0, 2];
             blocks[1].Position = boundingSquare[0, 1];
             blocks[2].Position = boundingSquare[1, 1];
             blocks[3].Position = boundingSquare[1, 0];
             checkRotateValid(piece);
             break;
     }
 }
Example #9
0
 private void checkRotateValid(Piece piece)
 {
     if (isValid(piece))
     {
         this.blocks = piece.blocks;
     }
     else
     {
         bool kicked = kick(piece);
         if (!kicked)
         {
             StackTrace stackTrace = new StackTrace();
             if (stackTrace.GetFrame(2).GetMethod().Name == "rotateRight")
                 rotationState = (RotationState)((mod((int)rotationState - 1, 4)));
             else
                 rotationState = (RotationState)((mod((int)rotationState + 1, 4)));
         }
     }
 }
Example #10
0
 private bool isValid(Piece piece)
 {
     foreach (Block block in piece.Blocks)
     {
         if (!TetrisGame.PlayerBoard.checkOnBoard(block.Position) || TetrisGame.PlayerBoard.BoardState[block.Position.X, block.Position.Y] != null)
             return false;
     }
     return true;
 }
Example #11
0
 bool GetPointState(int x, int y, Piece piece)
 {
     return piece.Now[x, y];
 }
Example #12
0
 public void AddNewPiece()
 {
     CurrentPiece = new Piece();
 }
Example #13
0
 public void AddNewPiece(Block[] blocks)
 {
     CurrentPiece = new Piece(blocks);
 }
Example #14
0
 //Deletes the currentpiece
 private void removeCurrentPiece()
 {
     for (int counter = 0; counter < currentPiece.Blocks.Length; counter++ )
     {
         boardState[currentPiece.Blocks[counter].Position.X, currentPiece.Blocks[counter].Position.Y] = null;
         currentPiece.Blocks[counter] = null;
     }
     currentPiece = null;
 }
Example #15
0
 private ImmutableArray<ImmutableSortedSet<Cell>> CreateGameField(int height, Piece movingPiece = null)
 {
     if (movingPiece == null)
         movingPiece = this.movingPiece != null ? this.movingPiece : pieces.GetCurrentPiece();
     ImmutableSortedSet<Cell>[] mutableGameField = new ImmutableSortedSet<Cell>[height];
     for (int i = 0; i < mutableGameField.Length;i++)
         mutableGameField[i] = ImmutableSortedSet<Cell>.Empty;
     foreach (var point in movingPiece.Cells)
     {
         mutableGameField[point.Y] = mutableGameField[point.Y].Add(point);
     }
     return ImmutableArray.Create(mutableGameField);
 }
Example #16
0
 private Pieces CentralizedPieces(ImmutableArray<Piece> piecesArray,int width)
 {
     Piece[] centralizedPieces = new Piece[piecesArray.Length];
     for (int i = 0; i < centralizedPieces.Length; i++)
     {
         int xShift = (width - (piecesArray[i].MaxX - piecesArray[i].MinX + 1)) / 2 - piecesArray[i].MinX;
         int yShift = piecesArray[i].MinY;
         Cell[] centralizedCells = new Cell[piecesArray[i].Cells.Length];
         for (int j = 0; j < centralizedCells.Length; j++)
         {
             centralizedCells[j] = piecesArray[i].Cells[j].MoveTowards(xShift).MoveDown(-yShift);
         }
         centralizedPieces[i] = new Piece(centralizedCells,piecesArray[i].FindCentralCellIndex());
     }
     return new Pieces(ImmutableArray.Create(centralizedPieces));
 }
Example #17
0
 public Game MovePiece(Piece movedPiece)
 {
     if (movedPiece == null)
         return PlaceNextPiece();
     int minY = movedPiece.MinY > movingPiece.MinY ? movingPiece.MinY : movedPiece.MinY;
     int maxY = movedPiece.MaxY > movingPiece.MaxY ? movedPiece.MaxY : movingPiece.MaxY;
     ImmutableSortedSet<Cell>[] changedArrays = GameFieldMutablePart(minY, maxY); //movedPiece,
     //int[] mutableLinesLength = linesLength.Select(z => z).ToArray();
     int movingYDiff = movingPiece.MinY - minY;
     foreach (var cell in movingPiece.Cells)
     {//?DSLKFJDSNLFSKDJLSEFDKL///
         int y = cell.Y - movingPiece.MinY + movingYDiff;
         //mutableLinesLength[cell.Y]--;
         /*if (mutableLinesLength[cell.Y] == 0)
             changedArrays[y] = null;
         else*/
         changedArrays[y] = changedArrays[y].Remove(cell);// = null;
     }
     bool wrongPlacement = false;
     int movedYDiff = movedPiece.MinY - minY;
     foreach (var cell in movedPiece.Cells)
     {
         int y = cell.Y - movedPiece.MinY + movedYDiff;
         //if (mutableLinesLength[cell.Y] == 0)
         //    changedArrays[y] = new Cell[width];
         if (changedArrays[y].Contains(cell))// != null)
         {
             wrongPlacement = true;
             break;
         }
         changedArrays[y] = changedArrays[y].Add(cell);//[cell.X] = cell;
         //mutableLinesLength[cell.Y]++;
     }
     if (wrongPlacement)
     {
         return PlaceNextPiece();
     }
     return new Game(ReworkedGameField(changedArrays,minY,maxY),pieces,
         movedPiece,score,width);
 }
Example #18
0
 static void Main()
 {
     int level = 0;
     int linesCount = 0;
     int score = 0;
     int playfieldWidth = 16;
     int playfieldHeigth = 30;
     field = new byte[playfieldHeigth, playfieldWidth, 2];
     RemoveScrollBars(playfieldWidth * 2, playfieldHeigth);        //Remove the scroll bars
     Console.Title = "Tetris";
     Piece currentPiece = new Piece(playfieldWidth / 2 - 2);       //Create the first piece
     while (true)
     {
         Console.Clear();                //Clear the field
         DrawField();                    //Redraw the field;
         currentPiece.Draw();            //Draw the piece
         while (Console.KeyAvailable)    //Move if possible the piece left/right or rotate if a proper key is pressed
         {
             ConsoleKeyInfo pressedKey = Console.ReadKey(true);
             currentPiece.Clean();
             currentPiece.Move(pressedKey);
             currentPiece.Draw();
         };
         currentPiece.Move();            //Check if possible and move down or stop moving
         if (currentPiece.isFinal)       //If stop moving check lines and score
         {
             score += 4;
             for ( int i = 0; i < field.GetLength(0); i++)
             {
                 int line = 1;
                 for (int j = 1; j < field.GetLength(1) - 1; j++)
                 {
                     line *= field[i, j, 0];
                 }
                 if (line != 0)
                 {
                     linesCount++;
                     level = level < 5 ? linesCount / 20 : 5;
                     score += 40;
                     for (int c = 0; c < field.GetLength(1); c++)
                     {
                         for (int r = i; r > 0; r--)
                         {
                             field[r, c, 0] = field[r - 1, c, 0];
                             field[r, c, 1] = field[r - 1, c, 1];
                         }
                         field[0, c, 0] = 0;
                         field[0, c, 1] = 0;
                     }
                 }
             }
             currentPiece = new Piece(playfieldWidth / 2 - 2);
         }
         DrawInfo(level, score, linesCount); //Draw info
         if (GameOver())                 //Check if game over
         {
             DrawStringOnPosition(field.GetLength(1) - 6, field.GetLength(0) / 2, "!!!GAME OVER!!!", ConsoleColor.Red);
             Console.WriteLine();
             break;
         }
         Console.Beep();                               //Play sound
         Thread.Sleep(600 - 100 * level);              //Slow the program
     }
 }
Example #19
0
 private static Texture2D fullTextureForPiece(Piece piece)
 {
     Texture2D texture = null;
     if (piece == null)
         return TetrisGame.emptyTexture;
     else
     {
         switch (piece.PieceType)
         {
             case BlockType.I:
                 texture = TetrisGame.IblockTexture;
                 break;
             case BlockType.J:
                 texture = TetrisGame.JblockTexture;
                 break;
             case BlockType.L:
                 texture = TetrisGame.LblockTexture;
                 break;
             case BlockType.O:
                 texture = TetrisGame.OblockTexture;
                 break;
             case BlockType.S:
                 texture = TetrisGame.SblockTexture;
                 break;
             case BlockType.T:
                 texture = TetrisGame.TblockTexture;
                 break;
             case BlockType.Z:
                 texture = TetrisGame.ZblockTexture;
                 break;
         }
         return texture;
     }
 }
Example #20
0
        public object Clone()
        {
            Piece p;
            p = new Piece(this.m_data, this.m_type, new TransFormation(this.m_trans), this.m_color);
            return p;

            switch (Type)
            {
                case PieceType.P1:
                    p = Piece.P1;
                    break;

                default:
                    p = Piece.P1;
                    break;

            }
            p.SetPosition(X, Y);

            switch (m_trans.Rotation)
            {
                case 90:
                    p.Rotate(Direction.Right);
                    break;

                case 180:
                    p.Rotate(Direction.Right);
                    p.Rotate(Direction.Right);
                    break;

                case 270:
                    p.Rotate(Direction.Left);
                    break;

            }

            return p;
        }
Example #21
0
        private static void InitStocked()
        {
            m_index = 0;

            //shuffle
            Piece[] pieces = new Piece[] { Piece.P1, Piece.P2, Piece.P1, Piece.P2, Piece.P1, Piece.P2, Piece.P1 };
            new Random().Shuffle(pieces);

            m_Stocked.Clear();
            m_Stocked.AddRange(pieces);
        }
Example #22
0
 void timer_Tick(object sender, EventArgs e)
 {
     if(!falling)
     {
         falling = true;
         currentPiece = pieces[new Random().Next(pieces.Count)];
         currentPosition = new Point(painter.SceneSize.Width / 2 - currentPiece.Size.Width / 2, 0);
         if(!CheckCollision())
         {
             painter.DrawPiece(currentPiece, currentPosition, true);
         }
         else
         {
             GameOver();
         }
     }
     else
     {
         if(!CheckCollision())
         {
             Move();
         }
         else
         {
             if(goWhere == Direction.Down)
             {
                 falling = false;
                 SetOccupied();
                 Eliminate();
             }
         }
     }
 }
Example #23
0
 public void DrawPiece(Piece piece, Point position, bool state)
 {
     for(int i = 0; i < piece.Size.Width; i++)
     {
         for(int j = 0; j < piece.Size.Height; j++)
         {
             if(piece.Now[i, j]) DrawGrid(i + position.X, j + position.Y, state);
         }
     }
 }