コード例 #1
0
 /// <summary>
 /// Initialize new prime puzzle game board with specified name, length, and width.
 /// </summary>
 /// <param name="name">Name of the game board.</param>
 /// <param name="length">Length of the game board.</param>
 /// <param name="width">Width of the game board.</param>
 public PrimeGameBoard(string name, int length, int width)
 {
     Name   = name;
     Length = length;
     Width  = width;
     InitializeBoard(length, width);
     StartSpace = GetSpaceAt(0, 0);
     EndSpace   = GetSpaceAt(length - 1, width - 1);
 }
コード例 #2
0
 /// <summary>
 /// Initialize new prime puzzle game board with only 2 spaces.
 /// </summary>
 public PrimeGameBoard()
 {
     Name   = "";
     Length = 2;
     Width  = 1;
     InitializeBoard(2, 1);
     StartSpace = GetSpaceAt(0, 0);
     EndSpace   = GetSpaceAt(1, 0);
 }
コード例 #3
0
        protected List <GameBoardSpace> GetSurroundingSpaces(GameBoardSpace space)
        {
            List <GameBoardSpace> spaces = new List <GameBoardSpace>();

            GameBoardSpace neighbor;

            neighbor = GetSpaceUpper(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            neighbor = GetSpaceLower(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            neighbor = GetSpaceLeft(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            neighbor = GetSpaceRight(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            neighbor = GetSpaceUpperLeft(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            neighbor = GetSpaceUpperRight(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            neighbor = GetSpaceLowerLeft(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            neighbor = GetSpaceLowerRight(space);
            if (neighbor != null)
            {
                spaces.Add(neighbor);
            }

            return(spaces);
        }
コード例 #4
0
        public void UpdateSpace(int x, int y, int value)
        {
            if (x < 0 || y < 0)
            {
                Console.WriteLine("GameBoard: UpdateSpace(): x and y cannot be less than 0.");
                return;
            }
            else if (x > Length - 1 || y > Width - 1)
            {
                Console.WriteLine("GameBoard: UpdateSpace(): x and y cannot be outside the board dimensions.");
                return;
            }

            GameBoardSpace space = GetSpaceAt(x, y);

            space.Value = value;
        }
コード例 #5
0
        /// <summary>
        /// Initialize new prime puzzle game board with specified name, length, width, and the coordinates of the start and end spaces.
        /// </summary>
        /// <param name="name">Name of the game board.</param>
        /// <param name="length">Length of the game board.</param>
        /// <param name="width">Width of the game board.</param>
        /// <param name="startX">X coordinate of start space.</param>
        /// <param name="startY">Y coordinate of start space.</param>
        /// <param name="endX">X coordinate of end space.</param>
        /// <param name="endY">Y coordinate of end space.</param>
        public PrimeGameBoard(string name, int length, int width, int startX, int startY, int endX, int endY)
        {
            Name   = name;
            Length = length;
            Width  = width;
            InitializeBoard(length, width);

            if (IsInBounds(startX, startY) && IsInBounds(endX, endY) && (startX != endX && startY != endY))
            {
                StartSpace = GetSpaceAt(startX, startY);
                EndSpace   = GetSpaceAt(endX, endY);
            }
            else
            {
                StartSpace = GetSpaceAt(0, 0);
                EndSpace   = GetSpaceAt(length - 1, width - 1);
            }
        }
コード例 #6
0
        public void Solve(GameBoardSpace space, int movesRemaining)
        {
            path.Add(space);

            if (space == EndSpace)
            {
                Console.WriteLine("Solved!");
                foreach (GameBoardSpace node in path)
                {
                    Console.WriteLine(node);
                }
                solved = true;
            }
            else
            {
                Random rng = new Random();

                List <GameBoardSpace> spaces   = GetSurroundingSpaces(space);
                List <GameBoardSpace> shuffled = spaces.OrderBy(a => rng.Next()).ToList();

                foreach (GameBoardSpace nextSpace in shuffled)
                {
                    if (solved)
                    {
                        break;
                    }
                    else if (!path.Contains(nextSpace) && movesRemaining > 0)
                    {
                        Solve(nextSpace, movesRemaining - 1);
                    }
                }

                if (!solved)
                {
                    path.Remove(space);
                }
            }
        }
コード例 #7
0
        protected void InitializeBoard(int length, int width)
        {
            if (length <= 0 || width <= 0)
            {
                Spaces = new List <GameBoardSpace>();
                Console.WriteLine("InitializeBoard(): Length or width cannot be 0.");
                return;
            }

            List <GameBoardSpace> newSpaces = new List <GameBoardSpace>();

            for (int l = 0; l < length; l++)
            {
                for (int w = 0; w < width; w++)
                {
                    GameBoardSpace space = new GameBoardSpace(l, w);
                    newSpaces.Add(space);
                }
            }

            Spaces = newSpaces;
            Console.WriteLine("InitializeBoard(): Created board with " + length * width + " spaces.");
        }
コード例 #8
0
 protected GameBoardSpace GetSpaceLowerRight(GameBoardSpace space)
 {
     return(GetSpaceAt(space.X + 1, space.Y - 1));
 }
コード例 #9
0
 protected GameBoardSpace GetSpaceLowerLeft(GameBoardSpace space)
 {
     return(GetSpaceAt(space.X - 1, space.Y - 1));
 }
コード例 #10
0
 protected GameBoardSpace GetSpaceUpperLeft(GameBoardSpace space)
 {
     return(GetSpaceAt(space.X - 1, space.Y + 1));
 }
コード例 #11
0
 public bool Equals(GameBoardSpace space)
 {
     return(this.X == space.X && this.Y == space.Y);
 }