Exemplo n.º 1
0
        /// <summary>
        /// Update all necessary logic, 60 times per second.
        /// </summary>
        private void tick(object sender, EventArgs e)
        {
            Input.tick();

            if (Input.wasKeyPressed(Input.UP))
            {
                bool addTile = false;
                for (int x = 0; x < 4; x++)
                {
                    for (int y = 1; y < 4; y++)
                    {
                        //tile at x,y
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;
                            for (int d = y; d > 0; d--)
                            {
                                if (!board.isTileAt(x, d - 1))
                                {
                                    board.moveTile(x, d, x, d - 1);
                                    addTile = true;
                                }
                                else if (board.tileNumberAt(x, d) == board.tileNumberAt(x, d - 1) && !hasCombined)
                                {
                                    board.setTileAt(x, d - 1, board.tileNumberAt(x, d) * 2);
                                    board.deleteTileAt(x, d);
                                    hasCombined = true;
                                    addTile     = true;
                                }
                            }
                        }
                    }
                }
                if (addTile)
                {
                    addRandomTile();
                }
            }
            if (Input.wasKeyPressed(Input.DOWN))
            {
                bool addtile = false;
                for (int x = 0; x < 4; x++)
                {
                    for (int y = 2; y > -1; y--)
                    {
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;
                            for (int d = y; d < 3; d++)
                            {
                                if (!board.isTileAt(x, d + 1))
                                {
                                    board.moveTile(x, d, x, d + 1);
                                    addtile = true;
                                }
                                else if (board.tileNumberAt(x, d) == board.tileNumberAt(x, d + 1) && !hasCombined)
                                {
                                    board.setTileAt(x, d + 1, board.tileNumberAt(x, d) * 2);
                                    board.deleteTileAt(x, d);
                                    hasCombined = true;
                                    addtile     = true;
                                }
                            }
                        }
                    }
                }
                if (addtile)
                {
                    addRandomTile();
                }
            }
            if (Input.wasKeyPressed(Input.LEFT))
            {
                bool addtile = false;
                for (int x = 1; x < 4; x++)
                {
                    for (int y = 0; y < 4; y++)
                    {
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;

                            for (int d = x; d > 0; d--)
                            {
                                if (!board.isTileAt(d - 1, y))
                                {
                                    board.moveTile(d, y, d - 1, y);
                                    addtile = true;
                                }
                                else if (board.tileNumberAt(d, y) == board.tileNumberAt(d - 1, y) && !hasCombined)
                                {
                                    board.setTileAt(d - 1, y, board.tileNumberAt(d, y) * 2);
                                    board.deleteTileAt(d, y);
                                    hasCombined = true;
                                    addtile     = true;
                                }
                            }
                        }
                    }
                }
                if (addtile)
                {
                    addRandomTile();
                }
            }
            if (Input.wasKeyPressed(Input.RIGHT))
            {
                bool addtile = false;
                for (int x = 2; x > -1; x--)
                {
                    for (int y = 0; y < 4; y++)
                    {
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;
                            for (int d = x; d < 3; d++)
                            {
                                if (!board.isTileAt(d + 1, y))
                                {
                                    board.moveTile(d, y, d + 1, y);
                                    addtile = true;
                                }
                                else if (board.tileNumberAt(d, y) == board.tileNumberAt(d + 1, y) && !hasCombined)
                                {
                                    board.setTileAt(d + 1, y, board.tileNumberAt(d, y) * 2);
                                    board.deleteTileAt(d, y);
                                    hasCombined = true;
                                    addtile     = true;
                                }
                            }
                        }
                    }
                }
                if (addtile)
                {
                    addRandomTile();
                }
            }

            /*if(Keyboard.IsKeyToggled(Key.Down) || Keyboard.IsKeyToggled(Key.Up) || Keyboard.IsKeyToggled(Key.Left) || Keyboard.IsKeyToggled(Key.Right))
             * {
             *  Random random = new Random();
             *  int TileRandomNumber = random.Next(0, 10);
             *  int TileNumber;
             *  if (TileRandomNumber <= 9)
             *  {
             *      TileNumber = 2;
             *  }
             *  else
             *  {
             *      TileNumber = 4;
             *  }
             *  int Tile1PositionX = random.Next(4);
             *  int Tile1PositionY = random.Next(4);
             *  board.setTileAt(Tile1PositionX, Tile1PositionY, TileNumber);
             * }*/
            board.tick();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update all necessary logic, 60 times per second.
        /// </summary>
        private void tick(object sender, EventArgs e)
        {
            //Written by Oliver:
            if (lostGame)
            {
                //Update the board even if the game is lost so tiles
                //finish their animations.
                board.tick();
                return;
            }

            //Update all key presses.
            Input.tick();

            //Written by Morghan:

            //The logic for each key press is similar but not similar enough to
            //seperate into a method.
            //Basically, for each key press:
            //1. Visualize the board roatated such that the top is the direction the
            //tiles should move in.
            //2. Loop from the top left to the bottom right.
            //3. If a tile is found, loop from its current position and move it as far
            //as possible back up the board.
            //4. Combine if possible and if the tile hasn't already combined.

            if (Input.wasKeyPressed(Input.UP))
            {
                bool addTile = false;
                for (int x = 0; x < 4; x++)
                {
                    for (int y = 1; y < 4; y++)
                    {
                        //tile at x,y
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;
                            for (int d = y; d > 0; d--)
                            {
                                if (!board.isTileAt(x, d - 1))
                                {
                                    board.moveTile(x, d, x, d - 1);
                                    addTile = true;
                                }
                                else if (board.tileNumberAt(x, d) == board.tileNumberAt(x, d - 1) && !hasCombined)
                                {
                                    board.setTileAt(x, d - 1, board.tileNumberAt(x, d) * 2);
                                    addToScore(board.tileNumberAt(x, d) * 2);
                                    board.deleteTileAt(x, d);
                                    hasCombined = true;
                                    addTile     = true;
                                }
                            }
                        }
                    }
                }
                if (addTile)
                {
                    addRandomTile();
                }

                lostGame = checkIfLost();
            }
            if (Input.wasKeyPressed(Input.DOWN))
            {
                bool addtile = false;
                for (int x = 0; x < 4; x++)
                {
                    for (int y = 2; y > -1; y--)
                    {
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;
                            for (int d = y; d < 3; d++)
                            {
                                if (!board.isTileAt(x, d + 1))
                                {
                                    board.moveTile(x, d, x, d + 1);
                                    addtile = true;
                                }
                                else if (board.tileNumberAt(x, d) == board.tileNumberAt(x, d + 1) && !hasCombined)
                                {
                                    board.setTileAt(x, d + 1, board.tileNumberAt(x, d) * 2);
                                    addToScore(board.tileNumberAt(x, d) * 2);
                                    board.deleteTileAt(x, d);
                                    hasCombined = true;
                                    addtile     = true;
                                }
                            }
                        }
                    }
                }
                if (addtile)
                {
                    addRandomTile();
                }

                lostGame = checkIfLost();
            }
            if (Input.wasKeyPressed(Input.LEFT))
            {
                bool addtile = false;
                for (int x = 1; x < 4; x++)
                {
                    for (int y = 0; y < 4; y++)
                    {
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;

                            for (int d = x; d > 0; d--)
                            {
                                if (!board.isTileAt(d - 1, y))
                                {
                                    board.moveTile(d, y, d - 1, y);
                                    addtile = true;
                                }
                                else if (board.tileNumberAt(d, y) == board.tileNumberAt(d - 1, y) && !hasCombined)
                                {
                                    board.setTileAt(d - 1, y, board.tileNumberAt(d, y) * 2);
                                    addToScore(board.tileNumberAt(d, y) * 2);
                                    board.deleteTileAt(d, y);
                                    hasCombined = true;
                                    addtile     = true;
                                }
                            }
                        }
                    }
                }
                if (addtile)
                {
                    addRandomTile();
                }

                lostGame = checkIfLost();
            }
            if (Input.wasKeyPressed(Input.RIGHT))
            {
                bool addtile = false;
                for (int x = 2; x > -1; x--)
                {
                    for (int y = 0; y < 4; y++)
                    {
                        if (board.isTileAt(x, y))
                        {
                            bool hasCombined = false;
                            for (int d = x; d < 3; d++)
                            {
                                if (!board.isTileAt(d + 1, y))
                                {
                                    board.moveTile(d, y, d + 1, y);
                                    addtile = true;
                                }
                                else if (board.tileNumberAt(d, y) == board.tileNumberAt(d + 1, y) && !hasCombined)
                                {
                                    board.setTileAt(d + 1, y, board.tileNumberAt(d, y) * 2);
                                    addToScore(board.tileNumberAt(d, y) * 2);
                                    board.deleteTileAt(d, y);
                                    hasCombined = true;
                                    addtile     = true;
                                }
                            }
                        }
                    }
                }
                if (addtile)
                {
                    addRandomTile();
                }

                lostGame = checkIfLost();
            }

            //Update all tile animations:
            board.tick();
        }