예제 #1
0
		/// <summary>
		/// Returns the direction of the valid move for the selected Square
		/// </summary>
		/// <param name="clickedSquare">Square to check for valid moves</param>
		/// <returns></returns>
		public static Direction determineValidMove(Square clickedSquare)
		{
			Direction validDirection;

			//Check for valid moves
			if (clickedSquare.SquareX == OpenX && clickedSquare.SquareY == OpenY + 1)
			{
				//Square can move Up
				validDirection = Direction.Up;
			}
			else if (clickedSquare.SquareX == OpenX - 1 && clickedSquare.SquareY == OpenY)
			{
				//Square can move Right
				validDirection = Direction.Right;
			}
			else if (clickedSquare.SquareX == OpenX && clickedSquare.SquareY == OpenY - 1)
			{
				//Square can move Down
				validDirection = Direction.Down;
			}
			else if (clickedSquare.SquareX == OpenX + 1 && clickedSquare.SquareY == OpenY)
			{
				//Square can move Left
				validDirection = Direction.Left;
			}
			else
			{
				//No valid move
				validDirection = Direction.None;
			}

			//Return the valid move direction
			return validDirection;
		}
예제 #2
0
		/// <summary>
		/// Loads an nPuzzle game save and sets up the board
		/// </summary>
		/// <param name="filePath">File path to the save file</param>
		public static void loadGame(string filePath)
		{
			//Create a new XML document and load the selected save file
			XmlDocument loadFile = new XmlDocument();
			loadFile.Load(filePath);
			XmlNodeList settings = loadFile.SelectNodes("/nPuzzleGame/*");

			//Iterate over each node in the XML file, and save it as the appropriate setting
			foreach (XmlNode item in settings)
			{
				switch (item.Name)
				{
					//Save the Board Size properties
					case "BoardSize":
						Square.BoardLengthX = int.Parse(item.Attributes["width"].Value);
						Square.BoardLengthY = int.Parse(item.Attributes["height"].Value);
						Square.SquaresArray = new Square[Square.BoardLengthX, Square.BoardLengthX];
						break;
					//Save the position of the Open Square
					case "OpenSquare":
						Square.OpenX = int.Parse(item.Attributes["openX"].Value);
						Square.OpenY = int.Parse(item.Attributes["openY"].Value);
						break;
					//Create the necessary squares for the board
					case "SquaresArray":
						foreach (XmlNode square in loadFile.SelectNodes("//SquaresArray/*"))
						{
							int x = int.Parse(square.Attributes["squareX"].Value);
							int y = int.Parse(square.Attributes["squareY"].Value);
							int value = int.Parse(square.Attributes["squareValue"].Value);
							Square currentSquare = new Square(x, y, value);
						}
						break;
					//Create the move counter
					case "MoveCounter":
						Square.MoveCounter = int.Parse(item.Attributes["counter"].Value);
						break;
					default:
						break;
				}
			}
		}
예제 #3
0
		/// <summary>
		/// Moves the clicked Square into the open space
		/// </summary>
		/// <param name="clickedSquare"></param>
		public static void moveSquare(Square clickedSquare)
		{
			//Temporarily store the clicked Square's coordinates
			int clickedX = clickedSquare.SquareX;
			int clickedY = clickedSquare.SquareY;

			//Update the array of Squares
			SquaresArray[clickedX, clickedY] = null;
			SquaresArray[OpenX, OpenY] = clickedSquare;

			//Move the clicked Square to the open space
			clickedSquare.SquareX = OpenX;
			clickedSquare.SquareY = OpenY;

			//Update the position of the open space
			OpenX = clickedX;
			OpenY = clickedY;
		}
예제 #4
0
		/// <summary>
		/// Generates a new board of Squares
		/// </summary>
		/// <param name="boardWidth">Width of the board</param>
		/// <param name="boardHeight">Height of the board</param>
		private void generateSquares(int boardWidth, int boardHeight)
		{
			//Initialize the array of Squares that form the Game Board
			Square.SquaresArray = new Square[boardWidth, boardHeight];

			//Calculate the number of squares in the puzzle
			int numberOfSquares = boardWidth * boardHeight - 1;

			//Keep track of each square's value
			int squareValueCounter = 0;

			for (int y = 0; y < boardHeight; y++)
			{
				for (int x = 0; x < boardWidth; x++)
				{					
					if (squareValueCounter++ == numberOfSquares)
					{
						//Store the location of the open square
						Square.OpenX = x;
						Square.OpenY = y;

						return;
					}

					//Create the new square and add it to the game board
					Square currentSquare = new Square(x, y, squareValueCounter);
				}
			}
		}