Created
March 23, 2017 23:02
-
-
Save EbbeVang/7e540575e3e5adbdb6c21a63096cca25 to your computer and use it in GitHub Desktop.
A* algorithm execise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.CodeDom; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml.Schema; | |
namespace aStarAlgorithm | |
{ | |
class GameExample | |
{ | |
private int length = 12; | |
private int height = 8; | |
// remember that | |
private Point startPoint = new Point(2,2); | |
private Point endPoint = new Point(6,8); | |
private List<Point> walls = new List<Point>() {new Point(4,6), new Point(3,6), new Point(4,5), new Point(4,4), new Point(2,6)}; | |
public enum Node { Wall, Path, Start, End } | |
private Node[,] gameBoard; | |
public void aStarPath() | |
{ | |
// find the shortest path from node.Start to node.End | |
} | |
private int calculateDistanceFromEndPoint() | |
{ | |
// complete this method - it will help you write the a* algorithm | |
return -1; | |
} | |
private int calculateDistanceFromStartPoint() | |
{ | |
// complete this method - it will help you write the a* algorithm | |
return -1; | |
} | |
public GameExample() | |
{ | |
// fill board with empty spaces | |
gameBoard = new Node[height, length]; | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < length; j++) | |
{ | |
gameBoard[i,j] = Node.Path; | |
} | |
} | |
// add walls | |
foreach (var wall in walls) | |
{ | |
gameBoard[wall.x, wall.y] = Node.Wall; | |
} | |
// add start and end points | |
gameBoard[startPoint.x, startPoint.y] = Node.Start; | |
gameBoard[endPoint.x, endPoint.y] = Node.End; | |
} | |
public override string ToString() | |
{ | |
StringBuilder stringBuilder = new StringBuilder(); | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < length; j++) | |
{ | |
if (gameBoard[i,j] == Node.Path) stringBuilder.Append((char)183); | |
if (gameBoard[i,j] == Node.Wall) stringBuilder.Append((char)219); | |
if (gameBoard[i, j] == Node.Start) stringBuilder.Append("S"); | |
if (gameBoard[i, j] == Node.End) stringBuilder.Append("E"); | |
} | |
stringBuilder.Append(Environment.NewLine); | |
} | |
return stringBuilder.ToString(); | |
} | |
private class Point | |
{ | |
public Point(int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
public int x { get; set; } | |
public int y { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment