/// <summary> /// Changes the color of a block on the GUI. /// </summary> /// <param name="s">The state of the block to change color based on</param> /// <param name="l">The block's location in the grid</param> public void SetBlockColor(SquareState s, Location l) { Color light = Color.Red; Color dark = Color.RosyBrown; Color block = Color.Gray; Color player = Color.Crimson; //Find the picturebox to be changed based on it's name. i.e.: Location 1/20 would show up as "box0120" PictureBox pb = (PictureBox)gameboard.Controls.Find( "box" + (l.x < 10 ? l.x.ToString() : "0" + l.x) + (l.y < 10 ? l.y.ToString() : "0" + l.y), false)[0]; switch (s) { case SquareState.Dark: pb.BackColor = dark; break; case SquareState.Light: pb.BackColor = light; break; case SquareState.Block: pb.BackColor = block; break; case SquareState.Player: pb.BackColor = player; break; } }
/// <summary> /// Checks if the player can make any valid moves from their location, if not, then the game is over. /// </summary> /// <param name="l">Location to check</param> private void CheckIfStuck(Location l) { //Look at all the possible moves a player could make, and see if any of them would be valid moves //If there is a valid move, then we return to break out of the function entirely. foreach (Location direction in Location.Directions) { if (LightBlockEngine.grid.LocationValid(l + direction) && LightBlockEngine.grid.GetSquare(l + direction).currentState == SquareState.Dark) return; } //If we've gotten here, then there are no valid moves. Let's see if the player won or lost. if (CheckCompletion()) LightBlockEngine.EndGame(true); else LightBlockEngine.EndGame(false); }
public Square(int x, int y, int size) { this.currentState = SquareState.Dark; this.location = new Location(x, y); }
/// <summary> /// Attempts to move the player to a location given, making sure that location would be valid /// </summary> /// <param name="l">Location to move to</param> public void Move(Location l) { if (LightBlockEngine.grid.LocationValid(l) && LightBlockEngine.grid.Board[l.x, l.y].currentState == SquareState.Dark) { //Since we're using a different color for the player block, //we need to change it back to the normal color after the player moves LightBlockEngine.grid.ChangeState(location, SquareState.Light); this.location = l; LightBlockEngine.grid.ChangeState(l, SquareState.Player); CheckIfStuck(l); } }
/// <summary> /// Places the player at a specific point on the grid passed in. /// </summary> /// <param name="l">The location to move the player to</param> public void PlacePlayer(Location l) { this.location = l; LightBlockEngine.grid.ChangeState(location, SquareState.Player); }
/// <summary> /// Creates a new game with the passed in elements /// </summary> /// <param name="x">Width of board</param> /// <param name="y">Height of board</param> /// <param name="size">Size in pixels of each block</param> public static void NewGame(int x, int y, int size) { //Make sure that the GUI has been set before getting here, or there'll be problems. if (gui == null) { Debug.WriteLine("ERROR! GUI not found!"); return; } maxX = x; maxY = y; blockSize = size; grid = new Grid(); player = new Player(); origPlayerLoc = player.location; }
/// <summary> /// Creates a new player and places it randomly on the grid. /// </summary> public Player() { this.location = PlacePlayer(); LightBlockEngine.grid.ChangeState(location, SquareState.Player); }
/// <summary> /// Finds a location on the grid where a block could exist without making the puzzle impossible to beat /// </summary> /// <param name="l">Location on grid for block to be placed</param> /// <returns>True if location is valid, false if invalid</returns> private bool FindValidBlockLocation(Location l) { //If the square state isn't dark, then it plain isn't valid. Move on. if (GetSquare(l).currentState != SquareState.Dark) return false; //Set up a full cardinal set of directions. //In addition to up, down, left, right, we also add Up-right, up-left, down-right, down-left. List<Location> fullDirections = new List<Location>(); fullDirections.AddRange(Location.Directions); fullDirections.AddRange( new Location[] { new Location(1, 1), new Location(1, -1), new Location(-1, 1), new Location(-1, -1) }); //Goes through each direction and sees what's there. If it's off the board or has a block there, then we add 1 to an int. //Once it does, it checks the "score" of that location, if it's over 3, then we don't allow a block to be placed there. //This keeps from situations where you have a block formation that you can enter but not exit, one of these are okay //but two would make for an impossible puzzle. int blocks = 0; foreach (Location direction in fullDirections) { if (!LocationValid(l + direction) || GetSquare(l + direction).currentState == SquareState.Block) blocks++; } if (blocks >= 3) return false; //If we managed to get through all that without triggering a return, then the location must be valid. return true; }
/// <summary> /// Checks if a Location given is a valid location on the grid. /// </summary> /// <param name="l">Location given</param> /// <returns>True if valid, false if invalid</returns> public bool LocationValid(Location l) { return l.x >= 0 & l.y >= 0 & l.x < LightBlockEngine.maxX & l.y < LightBlockEngine.maxY; }
/// <summary> /// Gets a random square on the board /// </summary> /// <returns>Square object reference</returns> public Square GetSquare() { Random r = new Random(); Location randomLoc = new Location(r.Next(0, LightBlockEngine.maxX), r.Next(0, LightBlockEngine.maxY)); return GetSquare(randomLoc); }
/// <summary> /// Takes a location and returns a Square object at that location on the board. /// </summary> /// <param name="l">Location of Square</param> /// <returns>Square object reference</returns> public Square GetSquare(Location l) { return board[l.x, l.y]; }
/// <summary> /// Changes the state of a block, including a call to update GUI. /// </summary> /// <param name="l">Location of block to be changed</param> /// <param name="s">State to change to</param> public void ChangeState(Location l, SquareState s) { board[l.x, l.y].currentState = s; LightBlockEngine.gui.UpdateBlockColor(board[l.x, l.y]); }