/// <summary> /// mark the grid /// </summary> /// <param name="list_pos">array list of positions</param> /// <param name="maze2d">maze2d maze</param> /// <param name="curr_layer">current layer</param> private void MarkGrid(ArrayList list_pos, Maze2d maze2d, int curr_layer) { int x; int y; ArrayList new_a = new ArrayList(); new_a = addPoints(list_pos); foreach (Position p in new_a) { x = (p.X); y = (p.Y); if (curr_layer == p.Z) { if (p.X == 0) { x = 0; } if (p.Y == 0) { y = 0; } maze2d.MAZE2d[x, y] = 2; } } }
/// <summary> /// check if finished the maze /// </summary> /// <param name="row">row in maze</param> /// <param name="column">column in maze</param> private void checkIfFinished(int row, int column) { Maze2d maze2d = (Maze2d)(m_maze.MAZE3dArray[cur_layer]); if (maze2d.getGoalPosition().X == row && maze2d.getGoalPosition().Y == column && cur_layer == goal_layer) { MessageBox.Show(" ### Congratulaions! You finished the Maze! ###"); } }
/// <summary> /// check if it is a wall /// </summary> /// <param name="row">row in maze</param> /// <param name="column">column in maze</param> /// <returns>true if there is a wall or false.</returns> private bool CheckIfWall(int row, int column) { Maze2d maze2d = (Maze2d)m_maze.MAZE3dArray[cur_layer]; if (maze2d.MAZE2d[row, column] == 1) { return(true); } return(false); }
/// <summary> /// check if there is a wall in the cell /// </summary> /// <param name="p">get the position to check about</param> /// <returns>true if there is a wall</returns> public bool checkIfThereIsAWall(Position p) { Maze3d m3d = (Maze3d)(m_maze); Maze2d m2d = (Maze2d)(m3d.MAZE3dArray[p.Z]); if (m2d.MAZE2d[p.X, p.Y] == 1) { return(true); } return(false); }
/// <summary> /// get all possible states that can be reached from given state /// </summary> /// <remarks>in a given state s look to all directions - up, down, left, right, floor up, floor down and insert to a list /// all possible directions</remarks> /// <param name="s">Get state for which we need to seek for legible neighbors</param> /// <returns>returns a list with all the directions that we can go through</returns> public List <Astate> getAllPossibleStates(Astate s) { List <MazeState> list = new List <MazeState>(); List <Astate> list2 = new List <Astate>(); Position p = (s as MazeState).currentp; Maze2d maze2 = maze.maze3d[p.z]; try{ if (maze2.maze2d[p.x, p.y + 1] == 0 || maze2.maze2d[p.x, p.y + 1] == 4 || maze2.maze2d[p.x, p.y + 1] == 3) { list.Add(new MazeState(s, new Position(p.x, p.y + 1, p.z))); } } catch (IndexOutOfRangeException e) { }; try{ if (maze2.maze2d[p.x - 1, p.y] == 0 || maze2.maze2d[p.x - 1, p.y] == 3 || maze2.maze2d[p.x - 1, p.y] == 4) { list.Add(new MazeState(s, new Position(p.x - 1, p.y, p.z))); } } catch (Exception e) { }; try{ if (maze2.maze2d[p.x, p.y - 1] == 0 || maze2.maze2d[p.x, p.y - 1] == 3 || maze2.maze2d[p.x, p.y - 1] == 4) { list.Add(new MazeState(s, new Position(p.x, p.y - 1, p.z))); } } catch (Exception e) { }; try{ if (maze2.maze2d[p.x + 1, p.y] == 0 || maze2.maze2d[p.x + 1, p.y] == 3 || maze2.maze2d[p.x + 1, p.y] == 4) { list.Add(new MazeState(s, new Position(p.x + 1, p.y, p.z))); } } catch (Exception e) { }; if (p.z + 1 < (maze as Maze3d).maze3d.Length && ((maze as Maze3d).maze3d[p.z + 1].maze2d[p.x, p.y] == 0 || (maze as Maze3d).maze3d[p.z + 1].maze2d[p.x, p.y] == 4 || (maze as Maze3d).maze3d[p.z + 1].maze2d[p.x, p.y] == 3)) { list.Add(new MazeState(s, new Position(p.x, p.y, p.z + 1))); } if (p.z - 1 >= 0 && p.z - 1 < (maze as Maze3d).maze3d.Length && (((maze as Maze3d).maze3d[p.z - 1].maze2d[p.x, p.y] == 0) || ((maze as Maze3d).maze3d[p.z - 1].maze2d[p.x, p.y] == 3) || ((maze as Maze3d).maze3d[p.z - 1].maze2d[p.x, p.y] == 4))) { list.Add(new MazeState(s, new Position(p.x, p.y, p.z - 1))); } foreach (MazeState m in list) { list2.Add((m as Astate)); } return(list2); }
/// <summary> /// color the maze /// </summary> /// <param name="mazewall2d">maze 2d</param> /// <param name="maze">maze 3d m</param> /// <param name="curr_layer">current layer</param> /// <param name="final_layer">final layer</param> /// <param name="s">name of maze</param> private void colorMazeWall(Grid mazewall2d, Maze maze, int curr_layer, int final_layer) { Maze2d maze2d = maze as Maze2d; for (int row = 0; row < maze2d.MX * 2 - 1; row++) { for (int column = 0; column < maze2d.MY * 2 - 1; column++) { if (curr_layer == 0 && maze2d.getStartPosition().X == row && maze2d.getStartPosition().Y == column) { StartControl StartCell = new StartControl(); Grid.SetColumn(StartCell, column); Grid.SetRow(StartCell, row); mazewall2d.Children.Add(StartCell); } else { if (curr_layer == final_layer - 1 && maze2d.getGoalPosition().X == row && maze2d.getGoalPosition().Y == column) { EndControl EndCell = new EndControl(); Grid.SetColumn(EndCell, column); Grid.SetRow(EndCell, row); mazewall2d.Children.Add(EndCell); } else { if (maze2d.MAZE2d[row, column] == 1) { MazeCell1 CellWall = new MazeCell1(); Grid.SetColumn(CellWall, column); Grid.SetRow(CellWall, row); mazewall2d.Children.Add(CellWall); } else { GrassCell grassCell = new GrassCell(); Grid.SetColumn(grassCell, column); Grid.SetRow(grassCell, row); mazewall2d.Children.Add(grassCell); } } } } } m_layerOfGrid[curr_layer] = mazewall2d; }
/// <summary> /// when the key is up /// </summary> /// <param name="sender">object sender</param> /// <param name="e">eventargs e</param> public void Grid_KeyUp(object sender, KeyEventArgs e) { e.Handled = true; ScaleY = Canvas.GetTop(m_player); ScaleX = Canvas.GetLeft(m_player); if (Double.IsNaN(ScaleY)) { ScaleY = 0; } if (Double.IsNaN(ScaleX)) { ScaleX = 0; } switch (e.Key) { case Key.Up: { if (CheckIfInLimits(m_row - 1, m_column) == true && CheckIfWall(m_row - 1, m_column) == false) { ScaleY -= cellHeight; m_row--; checkIfFinished(m_row, m_column); } break; } case Key.Down: { if (CheckIfInLimits(m_row + 1, m_column) == true && CheckIfWall(m_row + 1, m_column) == false) { ScaleY += cellHeight; m_row++; checkIfFinished(m_row, m_column); } break; } case Key.Left: { if (CheckIfInLimits(m_row, m_column - 1) == true && CheckIfWall(m_row, m_column - 1) == false) { ScaleX -= cellWidth; m_column--; checkIfFinished(m_row, m_column); } break; } case Key.Right: { if (CheckIfInLimits(m_row, m_column + 1) == true && CheckIfWall(m_row, m_column + 1) == false) { ScaleX += cellWidth; m_column++; checkIfFinished(m_row, m_column); } break; } case Key.PageUp: { Maze2d maze2d = ((Maze2d)m_maze.MAZE3dArray[cur_layer]); if (maze2d.getGoalPosition().X == m_row && maze2d.getGoalPosition().Y == m_column) { if (cur_layer == goal_layer) { MessageBox.Show("You are in the final level!"); } else { PanelUp.Children.Clear(); cur_layer++; CreateTheMaze(); CreatePlayer(); checkIfFinished(m_row, m_column); } } break; } case Key.PageDown: { Maze2d maze2d = ((Maze2d)m_maze.MAZE3dArray[cur_layer]); if (maze2d.getGoalPosition().X == m_row && maze2d.getGoalPosition().Y == m_column) { if (cur_layer == 0) { MessageBox.Show("You are in the lowest level!"); } else { PanelUp.Children.Clear(); cur_layer--; CreateTheMaze(); CreatePlayer(); checkIfFinished(m_row, m_column); } } break; } } Canvas.SetTop(m_player, ScaleY); Canvas.SetLeft(m_player, ScaleX); }
/// <summary> /// create the maze /// </summary> private void CreateTheMaze() { double PositionX = 0; double PositionY = 0; Maze2d maze2d = m_maze.MAZE3dArray[cur_layer] as Maze2d; for (int row = 0; row < maze2d.MX * 2 - 1; row++) { PositionX = 0; for (int column = 0; column < maze2d.MY * 2 - 1; column++) { if (cur_layer == 0 && maze2d.getStartPosition().X == row && maze2d.getStartPosition().Y == column) { StartControl s = new StartControl(); s.Width = cellWidth; s.Height = cellHeight; Canvas.SetLeft(s, PositionX); Canvas.SetTop(s, PositionY); PanelDown.Children.Add(s); } else { if (cur_layer == goal_layer && maze2d.getGoalPosition().X == row && maze2d.getGoalPosition().Y == column) { EndControl EndCell = new EndControl(); EndCell.Width = cellWidth; EndCell.Height = cellHeight; Canvas.SetLeft(EndCell, PositionX); Canvas.SetTop(EndCell, PositionY); PanelDown.Children.Add(EndCell); } else { if (maze2d.getGoalPosition().X == row && maze2d.getGoalPosition().Y == column) { ArrowControl arrow = new ArrowControl(); arrow.Width = cellWidth; arrow.Height = cellHeight; Canvas.SetLeft(arrow, PositionX); Canvas.SetTop(arrow, PositionY); PanelDown.Children.Add(arrow); } else { if (maze2d.MAZE2d[row, column] == 1) { MazeCell1 wall = new MazeCell1(); wall.Width = cellWidth; wall.Height = cellHeight; Canvas.SetLeft(wall, PositionX); Canvas.SetTop(wall, PositionY); PanelDown.Children.Add(wall); } else { if (maze2d.MAZE2d[row, column] == 0) { GrassCell grass = new GrassCell(); grass.Width = cellWidth; grass.Height = cellHeight; Canvas.SetLeft(grass, PositionX); Canvas.SetTop(grass, PositionY); PanelDown.Children.Add(grass); } } } } } PositionX += cellWidth; } PositionY += cellHeight; } }