From 499b4a6644fd2f73871cf493d00adc62e9dda6a6 Mon Sep 17 00:00:00 2001 From: mgorvat Date: Wed, 13 Apr 2016 02:01:32 +0400 Subject: [PATCH 1/2] Basic labyrinth classes --- Labyrinth/Labyrinth/Cell.cs | 22 ++++ Labyrinth/Labyrinth/Coordinate.cs | 54 ++++++++ Labyrinth/Labyrinth/Labyrinth.cs | 209 ++++++++++++++++++++++++++++++ 3 files changed, 285 insertions(+) create mode 100644 Labyrinth/Labyrinth/Cell.cs create mode 100644 Labyrinth/Labyrinth/Coordinate.cs create mode 100644 Labyrinth/Labyrinth/Labyrinth.cs diff --git a/Labyrinth/Labyrinth/Cell.cs b/Labyrinth/Labyrinth/Cell.cs new file mode 100644 index 0000000..4ef26c3 --- /dev/null +++ b/Labyrinth/Labyrinth/Cell.cs @@ -0,0 +1,22 @@ +using System; + + +namespace Labyrinth.Labyrinth +{ + public class Cell + { + public enum CellContent { NONE, PLAYER, GOAL } + + public bool down = true, + right = true; + + public CellContent content = CellContent.NONE; + + public Cell() { } + public Cell(bool down, bool right) + { + this.down = down; + this.right = right; + } + } +} diff --git a/Labyrinth/Labyrinth/Coordinate.cs b/Labyrinth/Labyrinth/Coordinate.cs new file mode 100644 index 0000000..1baa20c --- /dev/null +++ b/Labyrinth/Labyrinth/Coordinate.cs @@ -0,0 +1,54 @@ +using System; + +namespace Labyrinth.Labyrinth +{ + class Coordinate + { + public enum Direction { UP, RIGHT, DOWN, LEFT } + + private int x, y; + public int X + { + get { return x; } + set { x = value; } + } + + public int Y + { + get { return y; } + set { y = value; } + } + + public Coordinate(int x, int y) + { + this.x = x; + this.y = y; + } + + public Coordinate(Coordinate from, Direction dir) + { + switch (dir) + { + case Direction.UP: + this.x = from.x; + this.y = from.y - 1; + break; + + case Direction.RIGHT: + this.x = from.x + 1; + this.y = from.y; + break; + + case Direction.DOWN: + this.x = from.x; + this.y = from.y + 1; + break; + + case Direction.LEFT: + this.x = from.x - 1; + this.y = from.y; + break; + } + } + } +} diff --git a/Labyrinth/Labyrinth/Labyrinth.cs b/Labyrinth/Labyrinth/Labyrinth.cs new file mode 100644 index 0000000..c752663 --- /dev/null +++ b/Labyrinth/Labyrinth/Labyrinth.cs @@ -0,0 +1,209 @@ +using System; +using Labyrinth.Labyrinth; +using System.Collections; + +namespace Labyrinth.MazeGenerator +{ + + class Labyrinth + { + public delegate int GameEndedDelegate(); + + private GameEndedDelegate callBack; + public GameEndedDelegate CallBack + { + set { callBack = value; } + } + + private int size; + public int Size + { + get { return size; } + } + private Cell[][] labyrinth; + private Coordinate playerCoordinate; + private bool gameInProgress = false; + + public Coordinate getPlayerCoordinate() + { + return playerCoordinate; + } + + + public Labyrinth(int size) + { + this.size = size; + labyrinth = new Cell[size][]; + for (int i = 0; i < size; i++) + { + labyrinth[i] = new Cell[size]; + for (int j = 0; j < size; j++) + { + labyrinth[i][j] = new Cell(); + } + } + } + /* + public void initializeGame() + { + gameInProgress = true; + ArrayList pts = findDiametr(); + getCell(pts.get(0).x, pts.get(0).y).content = CellContent.PLAYER; + playerCoordinate = pts.get(0); + + getCell(pts.get(1).x, pts.get(1).y).content = CellContent.GOAL; + } + */ + public void movePlayer(Coordinate.Direction dir) + { + if (gameInProgress) + { + //check whether we can move this direction + if (!checkWall(playerCoordinate, dir)) + { + getCell(playerCoordinate).content = Cell.CellContent.NONE; + + playerCoordinate = new Coordinate(playerCoordinate, dir); + if (getCell(playerCoordinate).content == Cell.CellContent.GOAL) + { + callBack(); + gameInProgress = false; + } + getCell(playerCoordinate).content = Cell.CellContent.PLAYER; + } + } + } + + public Cell getCell(int x, int y) + { + return labyrinth[x][y]; + } + + public Cell getCell(Coordinate coord) + { + return labyrinth[coord.X][coord.Y]; + } + + public void breakWall(int x, int y, Coordinate.Direction dir) + { + switch (dir) + { + case Coordinate.Direction.UP: + labyrinth[x][y - 1].down = false; + break; + + case Coordinate.Direction.RIGHT: + labyrinth[x][y].right = false; + break; + + case Coordinate.Direction.DOWN: + labyrinth[x][y].down = false; + break; + + case Coordinate.Direction.LEFT: + labyrinth[x - 1][y].right = false; + break; + } + } + + public void breakWall(Coordinate coord, Coordinate.Direction dir) + { + breakWall(coord.X, coord.Y, dir); + } + + public bool checkWall(int x, int y, Coordinate.Direction dir) + { + switch (dir) + { + case Coordinate.Direction.UP: + if (y > 0 && labyrinth[x][y - 1].down == false) return false; + break; + + case Coordinate.Direction.RIGHT: + if (labyrinth[x][y].right == false) return false; + break; + + case Coordinate.Direction.DOWN: + if (labyrinth[x][y].down == false) return false; + break; + + case Coordinate.Direction.LEFT: + if (x > 0 && labyrinth[x - 1][y].right == false) return false; + break; + } + return true; + } + + public bool checkWall(Coordinate coord, Coordinate.Direction dir) + { + return checkWall(coord.X, coord.Y, dir); + } + + public Coordinate farestCell(Coordinate begin){ + int [,] length = new int[this.size, this.size]; + int inf = this.size * this.size + 1; + for(int i = 0; i < this.size; i++){ + for(int j = 0; j < this.size; j++){ + length[i, j] = inf; + } + } + length[begin.X, begin.Y] = 0; + + Queue cells = new Queue(); + int maxPath = 0; + Coordinate farestPoint = begin; + + cells.add(begin); + while(!cells.isEmpty()){ + Coordinate curCoord = cells.poll(); + int x = curCoord.x; + int y = curCoord.y; + int curLength = length[x][y]; + if(curLength > maxPath){ + farestPoint = curCoord; + maxPath = curLength; + } + if(!checkWall(curCoord, Coordinate.Direction.UP)){ + if(length[x][y - 1] > curLength + 1){ + cells.add(new Coordinate(curCoord, Coordinate.Direction.UP)); + length[x][y - 1] = curLength + 1; + } + } + + if(!checkWall(curCoord, Coordinate.Direction.RIGHT)){ + if(length[x + 1][y] > curLength + 1){ + cells.add(new Coordinate(curCoord, Coordinate.Direction.RIGHT)); + length[x + 1][y] = curLength + 1; + } + } + + if(!checkWall(curCoord, Coordinate.Direction.DOWN)){ + if(length[x][y + 1] > curLength + 1){ + cells.add(new Coordinate(curCoord, Coordinate.Direction.DOWN)); + length[x][y + 1] = curLength + 1; + } + } + + if(!checkWall(curCoord, Coordinate.Direction.LEFT)){ + if(length[x - 1][y] > curLength + 1){ + cells.add(new Coordinate(curCoord, Coordinate.Direction.LEFT)); + length[x - 1][y] = curLength + 1; + } + } + } + + return farestPoint; + } + + public ArrayList findDiametr(){ + ArrayList res = new ArrayList<>(); + Coordinate diamCell1 = farestCell(new Coordinate(0, 0)); + Coordinate diamCell2 = farestCell(diamCell1); + res.add(diamCell1); + res.add(diamCell2); + return res; + } + + + } +} From b74263ce66e860b40ece2f838ac5ee95a2c883d0 Mon Sep 17 00:00:00 2001 From: mgorvat Date: Sat, 16 Apr 2016 05:04:54 +0400 Subject: [PATCH 2/2] Maze generation --- Labyrinth/Labyrinth.csproj | 9 +- Labyrinth/Labyrinth/Labyrinth.cs | 142 ++++++++++-------- Labyrinth/MazeGenerator/BackTrackGenerator.cs | 36 +++++ .../MazeGenerator/DivideEtImperaGenerator.cs | 72 +++++++++ .../MazeGenerator/HuntAndKillGenerator.cs | 12 ++ Labyrinth/MazeGenerator/IMazeGenerator.cs | 12 ++ Labyrinth/MazeGenerator/MazeGeneratorUtils.cs | 29 ++++ 7 files changed, 245 insertions(+), 67 deletions(-) create mode 100644 Labyrinth/MazeGenerator/BackTrackGenerator.cs create mode 100644 Labyrinth/MazeGenerator/DivideEtImperaGenerator.cs create mode 100644 Labyrinth/MazeGenerator/HuntAndKillGenerator.cs create mode 100644 Labyrinth/MazeGenerator/IMazeGenerator.cs create mode 100644 Labyrinth/MazeGenerator/MazeGeneratorUtils.cs diff --git a/Labyrinth/Labyrinth.csproj b/Labyrinth/Labyrinth.csproj index 2d2f016..19b4648 100644 --- a/Labyrinth/Labyrinth.csproj +++ b/Labyrinth/Labyrinth.csproj @@ -9,11 +9,12 @@ Properties Labyrinth Labyrinth - v4.5.2 + v4.5 512 {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 4 true + AnyCPU @@ -63,12 +64,15 @@ App.xaml Code + + MainWindow.xaml Code + Code @@ -95,6 +99,9 @@ + + +