コード例 #1
0
ファイル: Tile.cs プロジェクト: KarlZero0/15-Puzzle
        public void MovingPos(Tile toMove, Tile movingTo, Board board)
        {
            if (toMove.X < movingTo.X)
            {
                X += (int)toMove.moveSpeed;
            }
            else if (toMove.X > movingTo.X)
            {
                X -= (int)toMove.moveSpeed;
            }
            else if (toMove.Y < movingTo.Y)
            {
                Y += (int)toMove.moveSpeed;
            }
            else if (toMove.Y > movingTo.Y)
            {
                Y -= (int)toMove.moveSpeed;
            }

            if (Math.Abs(toMove.X - movingTo.X) <= 5 && Math.Abs(toMove.Y - movingTo.Y) <= 5)
            {
                X = movingTo.X;
                Y = movingTo.Y;
                board.TileMoving = false;
            }

            board.ToMove.X = X;
            board.ToMove.Y = Y;
        }
コード例 #2
0
ファイル: Shuffle.cs プロジェクト: KarlZero0/15-Puzzle
        //Randomises tile placement
        public void ShuffleArray(Tile[] array)
        {
            //Temporary variable for storing tiles
            Tile tmp;

            //Checks there are more than 1 tiles left to iterate through
            if (array.Length > 1)
            {
                //Loops backwards through the array of tiles
                for (int i = array.Length - 1; i >= 0; i--)
                {
                    //Stores tile at current index in the temporary variable
                    tmp = array[i];

                    //Selects a random index out of the tiles that have yet to be moved
                    int randomIndex = random.Next(i + 1);

                    //Replaces tile at current index with the randomly selected tile
                    array[i] = array[randomIndex];

                    //Places previously stored tile in new position
                    array[randomIndex] = tmp;
                }
            }
        }
コード例 #3
0
        //Click event for starting the game
        public void Click(GameLoop game, Tile[] array, Board board)
        {
            //Instatiates class and randomises the tile placement
            Shuffle shuffle = new Shuffle();
            shuffle.ShuffleArray(array);

            //Creates variables
            int rowCounter  = 0;
            int colCounter  = 0;
            Tile index;

            //Loops through array of tiles
            for (int i = 0; i < array.Length; i++)
            {
                //Checks if the current tile is empty and it isn't placed in the bottom right
                if (array[i] == null && i != array.Length - 1)
                {
                    //Stores tile currently in bottom right
                    index = array[array.Length - 1];

                    //Swaps it with empty tile
                    array[array.Length - 1] = null;
                    array[i]                = index;

                }
            }

            //Loops through array of tiles
            for (int i = 0; i < array.Length; i++)
            {
                //Checks counter hasn't reached the empty tile
                if (i < array.Length - 1)
                {
                    //Assigns tile's column and row
                    array[i].Col = colCounter;
                    array[i].Row = rowCounter;

                    //Increment column counter
                    colCounter++;

                    //If the end of the row has been reached, reset column to zero and move to next row
                    if (colCounter >= 4)
                    {
                        colCounter = 0;
                        rowCounter++;
                    }

                    //Sets current tile's x and y position based on row and column
                    array[i].SetPos(board);
                }
            }

            //Tracks that the game is now in progress
            game.GameStarted = true;
        }
コード例 #4
0
ファイル: Board.cs プロジェクト: KarlZero0/15-Puzzle
        //private CheckButton check;

        //Constructor taking in current screen size
        public Board(ContentManager Content, SpriteBatch sprBatch, int screenWidth, int screenHeight)
        {
            //Get's GameLoop's ContentManager
            content = Content;

            spriteBatch = sprBatch;

            //Creates a variable to pass into parameters
            self = this;

            //Creates buttons
            start = new ingame_StartButton("gameStart", content);
            //check = new CheckButton("check", content);

            //Sets background image and centers the board based on the screen
            background = new Sprite("graphics/background", content);
            int bgx = screenWidth / 2 - background.W / 2;
            int bgY = screenHeight / 2 - background.H / 2 - start.H / 2;
            background.SetPos(bgx, bgY);

            //Locally stores properties of the background image
            width = background.W;
            height = background.H;
            x = background.X;
            y = background.Y;

            //Creates an empty array for the tiles
            array = new Tile[numTiles];
            //Creates another empty array to compare against
            winPattern = new Tile[numTiles];

            toMove = new Tile("1", content);
            movingTo = new Tile("1", content);

            //Creates the board
            CreateBoard();
        }
コード例 #5
0
ファイル: Board.cs プロジェクト: KarlZero0/15-Puzzle
        //Swaps the position of two tiles
        public void MoveTile(int indexPos, int movingToIndex, int row, int col)
        {
            //Creates a temporary array for tiles
            Tile[] tempArray = new Tile[numTiles];
            //Copies current array of tiles
            Array.Copy(array, tempArray, numTiles);

            toMove.Col = array[indexPos].Col;
            toMove.Row = array[indexPos].Row;
            toMove.X = array[indexPos].X;
            toMove.Y = array[indexPos].Y;
            toMove.Num = movingToIndex;

            //Swaps positions of passed in tiles
            tempArray[movingToIndex] = array[indexPos];
            //Sets the tile's new row and column
            tempArray[movingToIndex].Row = row;
            tempArray[movingToIndex].Col = col;
            tempArray[indexPos] = null;

            movingTo.Col = col;
            movingTo.Row = row;

            tempArray[movingToIndex].SetPos(self);
            
            movingTo.X = tempArray[movingToIndex].X;
            movingTo.Y = tempArray[movingToIndex].Y;

            int distance = Math.Abs(toMove.X - movingTo.X);
            if (distance == 0)
            {
                distance = Math.Abs(toMove.Y - movingTo.Y);
            }

            toMove.MoveSpeed = distance / 10;

            tempArray[movingToIndex].X = toMove.X;
            tempArray[movingToIndex].Y = toMove.Y;
             

            //Copies new positions back to original array
            array = tempArray;
            tileMoving = true;
            //Resets the positions of the tiles relative to the board
            //array[movingToIndex].SetPos(self);
        }
コード例 #6
0
ファイル: Board.cs プロジェクト: KarlZero0/15-Puzzle
        //Creates the game board
        public void CreateBoard()
        {
            //Counters for current position
            int rowCounter = 0;
            int colCounter = 0;

            //Loops through array of tiles
            for (int i = 0; i < array.Length; i++)
            {
                //If the counter hasn't reached the last index
                if (i < array.Length - 1)
                {
                    //Stores number of tile (1-based) for image name
                    string tileNum = (i + 1).ToString();
                    //Creates a new tile using the string
                    array[i] = new Tile(tileNum, content);

                    //Sets array's properties for position
                    array[i].Num = i;
                    array[i].Col = colCounter;
                    array[i].Row = rowCounter;

                    //Increments the column counter
                    colCounter++;

                    //Checks if the end of the row has been reached
                    if (colCounter >= 4)
                    {
                        //Resets column back to 0 and moves onto the next row
                        colCounter = 0;
                        rowCounter++;
                    }

                    //Sets the tile's X and Y position based relative to the board
                    array[i].SetPos(self);
                }
                //Once the last indes has been reached
                else
                {
                    //Sets bottom right tile as empty
                    array[i] = null;
                }
            }

            //Copies the array to be used to compare against for the win condition
            Array.Copy(array, winPattern, numTiles);

            //Set's the positions of the buttons relative to the board
            start.SetPos(this, "middle");
            //check.SetPos(this, "right");
        }