Exemple #1
0
        public TetraminoPice Rotation(TetraminoPice pice)
        {
            TetraminoPice rotationPice = new TetraminoPice();

            if (CheckForObstacle(pice, "Rotation") != true)
            {
                rotationPice.axis.X   = pice.axis.X;
                rotationPice.axis.Y   = pice.axis.Y;
                rotationPice.first.X  = -pice.first.Y;
                rotationPice.first.Y  = pice.first.X;
                rotationPice.second.X = -pice.second.Y;
                rotationPice.second.Y = pice.second.X;
                rotationPice.third.X  = -pice.third.Y;
                rotationPice.third.Y  = pice.third.X;
                rotationPice.brush    = pice.brush;
                rotationPice          = CheckIfOnBoard(rotationPice);
                if (CheckForObstacle(rotationPice) == true)
                {
                    return(pice);
                }
            }
            else
            {
                rotationPice = changePiceToBoard(pice);
            }

            return(rotationPice);
        }
Exemple #2
0
        public TetraminoPice Moving(TetraminoPice pice, int X, int Y)
        {
            TetraminoPice movingPice = new TetraminoPice();

            movingPice.axis.X   = pice.axis.X + X;
            movingPice.axis.Y   = pice.axis.Y + Y;
            movingPice.first.X  = pice.first.X;
            movingPice.first.Y  = pice.first.Y;
            movingPice.second.X = pice.second.X;
            movingPice.second.Y = pice.second.Y;
            movingPice.third.X  = pice.third.X;
            movingPice.third.Y  = pice.third.Y;
            movingPice.brush    = pice.brush;
            movingPice          = CheckIfOnBoard(movingPice);
            bool checkerForObstacle = CheckForObstacle(movingPice);

            if (checkerForObstacle == true && Y == 1 && X == 0)
            {
                movingPice = changePiceToBoard(movingPice, 0, -Y);
            }
            if (checkerForObstacle == true && Y == 0)
            {
                return(pice);
            }
            return(movingPice);
        }
Exemple #3
0
 public List <Vector> GamePice(TetraminoPice pice)
 {
     piceCoordinates.Clear();
     piceCoordinates.Add(new Vector(pice.axis.X, pice.axis.Y));
     piceCoordinates.Add(new Vector((pice.axis.X + pice.first.X), (pice.axis.Y + pice.first.Y)));
     piceCoordinates.Add(new Vector((pice.axis.X + pice.second.X), (pice.axis.Y + pice.second.Y)));
     piceCoordinates.Add(new Vector((pice.axis.X + pice.third.X), (pice.axis.Y + pice.third.Y)));
     return(piceCoordinates);
 }
Exemple #4
0
 public void PiceGenerator()
 {
     nextPice        = TetrominoPices.tetraminos[randomizer.Next(0, 7)];
     nextPice.axis.X = 5;
     if (nextPice.first.Y >= 0)
     {
         nextPice.axis.Y = 0;
     }
     else
     {
         nextPice.axis.Y = 0 - nextPice.first.Y;
     }
 }
Exemple #5
0
        public TetraminoPice changePiceToBoard(TetraminoPice changeToBoardPice, int X = 0, int Y = 0)
        {
            Vector[] changeToBoardArray = new Vector[4];
            changeToBoardArray[0] = (new Vector(changeToBoardPice.axis.X, changeToBoardPice.axis.Y));
            changeToBoardArray[1] = (new Vector((changeToBoardPice.first.X + changeToBoardPice.axis.X), (changeToBoardPice.first.Y + changeToBoardPice.axis.Y)));
            changeToBoardArray[2] = (new Vector((changeToBoardPice.second.X + changeToBoardPice.axis.X), (changeToBoardPice.second.Y + changeToBoardPice.axis.Y)));
            changeToBoardArray[3] = (new Vector((changeToBoardPice.third.X + changeToBoardPice.axis.X), (changeToBoardPice.third.Y + changeToBoardPice.axis.Y)));

            for (int i = 0; i < 4; i++)
            {
                gameBoard[(int)changeToBoardArray[i].X + X, ((int)changeToBoardArray[i].Y + Y)] = 1;
            }
            changeToBoardPice = nextPice;
            NextPice();
            return(changeToBoardPice);
        }
Exemple #6
0
        static public TetraminoPice CheckIfOnBoard(TetraminoPice onBoardPice)
        {
            double verticalCorection   = 0;
            double horizontalCorection = 0;

            Vector[] checkOnBoardArray = new Vector[4];
            checkOnBoardArray[0] = (new Vector(onBoardPice.axis.X, onBoardPice.axis.Y));
            checkOnBoardArray[1] = (new Vector(onBoardPice.first.X, onBoardPice.first.Y));
            checkOnBoardArray[2] = (new Vector(onBoardPice.second.X, onBoardPice.second.Y));
            checkOnBoardArray[3] = (new Vector(onBoardPice.third.X, onBoardPice.third.Y));
            for (int i = 1; i < 4; i++)
            {
                double tempVertical;
                if (checkOnBoardArray[i].X + checkOnBoardArray[0].X > 9)
                {
                    tempVertical = checkOnBoardArray[i].X + checkOnBoardArray[0].X - 9;
                    if (tempVertical > verticalCorection)
                    {
                        verticalCorection = tempVertical;
                    }
                }
                if (checkOnBoardArray[i].X + checkOnBoardArray[0].X < 0)
                {
                    tempVertical = checkOnBoardArray[i].X + checkOnBoardArray[0].X;
                    if (tempVertical < verticalCorection)
                    {
                        verticalCorection = tempVertical;
                    }
                }
                double tempHorizontal;
                if (checkOnBoardArray[i].Y + checkOnBoardArray[0].Y < 0)
                {
                    tempHorizontal = checkOnBoardArray[i].Y + checkOnBoardArray[0].Y;
                    if (tempHorizontal < horizontalCorection)
                    {
                        horizontalCorection = tempHorizontal;
                    }
                }
            }
            onBoardPice.axis.X = onBoardPice.axis.X - verticalCorection;
            onBoardPice.axis.Y = onBoardPice.axis.Y - horizontalCorection;
            return(onBoardPice);
        }
Exemple #7
0
        void PiceShow(Tetris tetris)
        {
            List <int> lastPiceAddedLine = new List <int>();

            boardPice = tetris.pice;
            if (gamePice != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    TetrisGrid.Children.RemoveAt(TetrisGrid.Children.Count - 1);
                }
            }
            if (boardWipe == true)
            {
                TetrisGrid.Children.Clear();
                boardWipe = false;
                for (int ii = 0; ii < 10; ii++)
                {
                    for (int jj = 0; jj < 20; jj++)
                    {
                        if (tetris.gameBoard[ii, jj] == 2)
                        {
                            tetris.gameBoard[ii, jj] = 1;
                        }
                    }
                }
            }
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    if (tetris.gameBoard[i, j] == 1)
                    {
                        tetris.gameBoard[i, j] = 2;
                        Rectangle rec = new Rectangle();
                        rec.Fill = Brushes.Gray;
                        Grid.SetColumn(rec, i);
                        Grid.SetRow(rec, j);
                        TetrisGrid.Children.Add(rec);
                        boardChildren[i, j] = rec;
                        if (lastPiceAddedLine.Contains(j) == false)
                        {
                            lastPiceAddedLine.Add(j);
                        }
                    }
                }
            }
            gamePice = null;
            gamePice = tetris.GamePice(tetris.pice);
            for (int i = 0; i < gamePice.Count; i++)
            {
                Rectangle rec = new Rectangle();
                rec.Fill = boardPice.brush;
                Grid.SetColumn(rec, (int)gamePice[i].X);
                Grid.SetRow(rec, (int)gamePice[i].Y);
                TetrisGrid.Children.Add(rec);
            }
            if (lastPiceAddedLine.Count > 0)
            {
                List <int> removedLines           = new List <int>();
                int        lastPiceAddedLineCount = lastPiceAddedLine.Count;
                for (int i = 0; i < lastPiceAddedLineCount; i++)
                {
                    int j = lastPiceAddedLine[0];
                    lastPiceAddedLine.RemoveAt(0);
                    if (Tetris.LineChecker(j, tetris.gameBoard) == true)
                    {
                        removedLines.Add(j);
                        for (int ii = 0; ii < 10; ii++)
                        {
                            TetrisGrid.Children.Remove(boardChildren[ii, j]);
                        }
                    }
                }
                if (removedLines.Count > 0)
                {
                    removedLineCounter = removedLineCounter + removedLines.Count;
                    switch (removedLines.Count)
                    {
                    case 1:
                        gameScore = gameScore + 100;
                        break;

                    case 2:
                        gameScore = gameScore + 300;
                        break;

                    case 3:
                        gameScore = gameScore + 600;
                        break;

                    case 4:
                        gameScore = gameScore + 1000;
                        break;
                    }
                    Tetris.RemovedLines(tetrisGame.gameBoard, removedLines);
                    boardWipe = true;
                }
                //TJ
                lock (semafore)
                {
                    if (!isEndGame /* endTJ*/ && tetris.CheckForObstacle(tetris.pice, "EndGame") == true)
                    {
                        string endGame = "Your Score: " + gameScore;
                        //TJ
                        isEndGame = true;
                        var scoreName = new ScoreWindow();
                        scoreName.ShowDialog();
                        string name = scoreName.Result;
                        //endTJ
                        HighScore.TetrisHighScore((int)gameScore, name);
                        timer.Stop();
                        new HighscoreWindow();
                        this.Close();
                    }
                }
            }
            gameNextPice = tetris.GamePice(tetris.nextPice);
            PreviewGrid.Children.Clear();
            for (int i = 0; i < 4; i++)
            {
                Rectangle rec = new Rectangle();
                rec.Fill = tetris.nextPice.brush;
                Grid.SetColumn(rec, (int)gameNextPice[i].X - 3);
                Grid.SetRow(rec, (int)gameNextPice[i].Y);
                PreviewGrid.Children.Add(rec);
            }
        }
Exemple #8
0
 public void NextPice()
 {
     pice = nextPice;
     PiceGenerator();
 }
Exemple #9
0
 public void TetrisStart()
 {
     PiceGenerator();
     pice = nextPice;
     PiceGenerator();
 }
Exemple #10
0
        public bool CheckForObstacle(TetraminoPice obstaclePice, String switcher = "default")
        {
            int[] Y = new int[] { 0, 0, 0, 0 };
            int[] X = new int[] { 0, 0, 0, 0 };
            double[,] obstaclePiceArray = new double[4, 2];
            obstaclePiceArray[0, 0]     = obstaclePice.axis.X;
            obstaclePiceArray[0, 1]     = obstaclePice.axis.Y;
            obstaclePiceArray[1, 0]     = obstaclePice.first.X;
            obstaclePiceArray[1, 1]     = obstaclePice.first.Y;
            obstaclePiceArray[2, 0]     = obstaclePice.second.X;
            obstaclePiceArray[2, 1]     = obstaclePice.second.Y;
            obstaclePiceArray[3, 0]     = obstaclePice.third.X;
            obstaclePiceArray[3, 1]     = obstaclePice.third.Y;

            Vector[] checkForObstacleArray = new Vector[4];
            switch (switcher)
            {
            case "Rotation":
                for (int i = 1; i < 4; i++)
                {
                    string rotationChecker = "Default";
                    if ((X[i] > 0) && (Y[i] >= 0))
                    {
                        rotationChecker = "Down";
                    }
                    if ((X[i] >= 0) && (Y[i] < 0))
                    {
                        rotationChecker = "Left";
                    }
                    if ((X[i] < 0) && (Y[i] <= 0))
                    {
                        rotationChecker = "Up";
                    }
                    if ((X[i] <= 0) && (Y[i] > 0))
                    {
                        rotationChecker = "Right";
                    }
                    switch (rotationChecker)
                    {
                    case "Down":
                        for (int ii = 1; ii <= X[i]; ii++)
                        {
                            if ((Y[i] + Y[0] + ii <= 19) && (gameBoard[X[i] + X[0], Y[i] + Y[0] + ii] > 0))
                            {
                                return(true);
                            }
                        }
                        break;

                    case "Up":
                        for (int ii = -1; ii >= X[i]; ii--)
                        {
                            if ((Y[i] + Y[0] + ii > 0) && (gameBoard[X[i] + X[0], Y[i] + Y[0] + ii] > 0))
                            {
                                return(true);
                            }
                        }
                        break;

                    case "Right":
                        for (int ii = 1; ii <= Y[i]; ii++)
                        {
                            if ((X[i] + X[0] + ii < 9) && (gameBoard[X[i] + X[0] + ii, Y[i] + Y[0]] > 0))
                            {
                                return(true);
                            }
                        }
                        break;

                    case "Left":
                        for (int ii = -1; ii >= Y[i]; ii--)
                        {
                            if ((X[i] + X[0] + ii > 0) && (gameBoard[X[i] + X[0] + ii, Y[i] + Y[0]] > 0))
                            {
                                return(true);
                            }
                        }
                        break;

                    case "Default":
                        break;
                    }
                }
                break;

            case "EndGame":
                break;

            case "Down":
                Y[0] = Y[0] + 1;
                break;

            case "Up":
                Y[0] = Y[0] - 1;
                break;

            case "Left":
                X[0] = X[0] - 1;
                break;

            case "Right":
                X[0] = X[0] + 1;
                break;

            default:
                break;
            }
            checkForObstacleArray[0] = (new Vector(obstaclePiceArray[0, 0] + X[0], obstaclePiceArray[0, 1] + Y[0]));
            for (int i = 1; i < 4; i++)
            {
                checkForObstacleArray[i] = (new Vector((obstaclePiceArray[i, 0] + obstaclePiceArray[0, 0] + X[i]), (obstaclePiceArray[i, 1] + obstaclePiceArray[0, 1] + Y[i])));
            }

            for (int i = 0; i < checkForObstacleArray.Count(); i++)
            {
                if (checkForObstacleArray[i].Y >= 0)
                {
                    if (checkForObstacleArray[i].Y > 19)
                    {
                        return(true);
                    }
                    if (gameBoard[(int)checkForObstacleArray[i].X, ((int)checkForObstacleArray[i].Y)] > 0)
                    {
                        return(true);                                                                                   //== 1) || (gameBoard[(int)checkForObstacleArray[i].X, ((int)checkForObstacleArray[i].Y)] == 2)) return true;
                    }
                }
            }
            return(false);
        }