/// <summary> /// Increments the boxes pushed. /// </summary> public void IncrementBoxesPushed() { boxesPushed += 1; PlayForm playForm = (PlayForm)SokobanProgram.getFormFromState(GameState.PLAY); playForm.numberOfPushesLabel.Text = "Number of Pushes: " + boxesPushed; }
/// <summary> /// Increments the moves. /// </summary> public void IncrementMoves() { moves += 1; PlayForm playForm = (PlayForm)SokobanProgram.getFormFromState(GameState.PLAY); playForm.numberOfMovesLabel.Text = "Number of Moves: " + moves; }
private void CheckGameWinning() { int destinations = 0; int boxes = 0; for (int row = 0; row < GetRowLength(); row++) { for (int col = 0; col < GetColumnLength(); col++) { foreach (Entity entity in Map[row, col].GetEntitys()) { if (entity is Box) { boxes++; } if (entity is Destination) { destinations++; if (!Map[row, col].HasEntityType(EntityType.BOX)) { return; } } } } } if (destinations == boxes) { MessageBox.Show("Congratulations, you have completed this map. Total moves: " + SokobanProgram.GetGameManager().GetHero().GetMoves() + ". Total Boxes Pushed: " + SokobanProgram.GetGameManager().GetHero().GetBoxesPushed()); SokobanProgram.GetGameManager().Unload(); } }
/// <summary> /// Writes out a game map file. /// </summary> /// <param name="path">The path of the file.</param> public static void WriteGameFile(String path) { GameMap map = SokobanProgram.GetGameManager().GetGameMap(); MapLocation maploc; using (writer = new StreamWriter(path)) { //Write the dimensions of the map array writer.Write(map.GetRowLength() + ","); writer.Write(map.GetColumnLength()); writer.WriteLine(); for (int row = 0; row < map.GetRowLength(); row++) { for (int col = 0; col < map.GetColumnLength(); col++) { maploc = map.GetMap()[row, col]; writer.Write(row + "," + col + ","); writer.Write(maploc.GetEntitys().Count + (maploc.GetEntitys().Count > 0 ? "," : "")); Entity entity; for (int i = 0; i < maploc.GetEntitys().Count; i++) { entity = maploc.GetEntitys()[i]; writer.Write((int)entity.GetEntityType() + (i + 1 != maploc.GetEntitys().Count ? "," : "")); } writer.WriteLine(); } } } }
/// <summary> /// Reads a game map file from a path. /// </summary> /// <param name="path">The path to read.</param> public static void ReadGameFile(String path) { try { EntityType[] types = (EntityType[])Enum.GetValues(typeof(EntityType)); using (reader = new StreamReader(path)) { String line; String[] tokens; Entity entity; int count = 0; while ((line = reader.ReadLine()) != null) { switch (count) { case 0: tokens = line.Split(','); int rows = Int32.Parse(tokens[0]); int columns = Int32.Parse(tokens[1]); SokobanProgram.GetGameManager().CreateMap(rows, columns); break; default: tokens = line.Split(','); int row = Int32.Parse(tokens[0]); int col = Int32.Parse(tokens[1]); int size = Int32.Parse(tokens[2]); int entityIndex; int entityId; if (size > 0) { for (int i = 0; i < size; i++) { entityIndex = 3 + i; entityId = Int32.Parse(tokens[entityIndex]); entity = Entity.GetEntityByType(types[entityId]); if (entity == null) { Console.WriteLine("Error reading entity opcode for " + tokens[i] + ", entityIndex=" + entityIndex); continue; } if (SokobanProgram.GetGameManager().GetGameState() == GameState.PLAY && types[entityId] == EntityType.HERO) { SokobanProgram.GetGameManager().SetHero((Hero)entity); Console.WriteLine("Set Hero Instance in Game Manager"); } Console.WriteLine("Adding entity to " + row + ", " + col + ", " + entity); SokobanProgram.GetGameManager().GetGameMap().GetMap()[row, col].AddEntity(entity); } } break; } count++; } } } catch (IOException io) { Console.WriteLine(io.Message); } }
/// <summary> /// Unloads the Game Manager. /// </summary> public void Unload() { gameMap = null; hero = null; if (gameState == GameState.PLAY) { PlayForm playForm = (PlayForm)SokobanProgram.getFormFromState(GameState.PLAY); playForm.numberOfPushesLabel.Text = "Number of Pushes: 0"; playForm.numberOfMovesLabel.Text = "Number of Moves: 0"; } }
/// <summary> /// Handles the click method specifically in design mode. /// </summary> /// <param name="mapLocation">The map location clicked.</param> private void HandleDesignClick(MapLocation mapLocation) { List <Entity> entitys = mapLocation.GetEntitys(); EntityType entityType = ((DesignForm)SokobanProgram.getFormFromState(GameState.DESIGN)).GetToolType(); if (entityType == EntityType.HERO && HasEntity(EntityType.HERO)) { MessageBox.Show("There can only be one Hero on the game board."); return; } if (entitys.Count > 0) { mapLocation.ClearEntities(); } Entity entity = Entity.GetEntityByType(entityType); if (entity == null) { return; } mapLocation.AddEntity(entity); }
/// <summary> /// Handle the Click event on the map. /// </summary> /// <param name="mouseEvent">The mouse event.</param> public void Click(MouseEventArgs mouseEvent) { int x = mouseEvent.X; int y = mouseEvent.Y; MapLocation mapLocation = GetMapLocation(x, y); if (mapLocation == null) { Console.WriteLine("Map Location was not found for: x=" + x + ", y=" + y); return; } if (SokobanProgram.GetGameManager().GetGameState() == GameState.DESIGN) { HandleDesignClick(mapLocation); } else if (SokobanProgram.GetGameManager().GetGameState() == GameState.PLAY) { HandlePlayClick(mapLocation); } else { Console.WriteLine("Error: Invalid Game State set!"); } }
public void HandleMoveClick(Direction direction) { Hero hero = SokobanProgram.GetGameManager().GetHero(); if (hero == null) { return; } MapLocation location = hero.GetLocation(); hero.SetDirection(direction); MapLocation moveSpot = GetNextMoveSpot(location, direction); if (moveSpot == null) { return; } bool checkWinning = false; if (moveSpot.HasEntitys()) { Entity entity; for (int i = 0; i < moveSpot.GetEntitys().Count; i++) { entity = moveSpot.GetEntitys()[i]; if (entity.GetEntityType() == EntityType.BOX) { MapLocation boxMoveSpot = GetNextMoveSpot(entity.GetLocation(), direction); if (boxMoveSpot == null) { return; } //Box attempting to move to a spot with entites. if (boxMoveSpot.HasEntitys()) { Entity e; for (int k = 0; k < boxMoveSpot.GetEntitys().Count; k++) { e = boxMoveSpot.GetEntitys()[k]; if (e.GetEntityType() != EntityType.DESTINATION) { return; } if (e.GetEntityType() == EntityType.DESTINATION) { ((Box)entity).SetColor(BoxColor.BLUE); checkWinning = true; break; } } } else { //Box moving off destination if (entity.GetEntityType() == EntityType.BOX) { ((Box)entity).SetColor(BoxColor.RED); } } entity.GetLocation().RemoveEntity(entity); boxMoveSpot.AddEntity(entity); hero.IncrementBoxesPushed(); break; } if (entity.GetEntityType() != EntityType.DESTINATION) { return; } } } location.RemoveEntity(hero); moveSpot.AddEntity(hero); hero.IncrementMoves(); if (checkWinning) { CheckGameWinning(); } }