/// <summary> /// BoardNode Constructor - Instantiates a BoardNode object /// which contains a board state, /// the action which caused the current state, /// the parent of the node, and the nodes children. /// </summary> /// <param name="board">Current board state</param> /// <param name="x">X position of move</param> /// <param name="y">Y position of move</param> /// <param name="prev">Parent of node</param> public BoardNode(Board board, int x, int y, BoardNode prev, ArrayList clicked) { this.board = board; this.x = x; this.y = y; this.prev = prev; this.clicked = new ArrayList(clicked); this.clicked.Add(getAction()); children = new ArrayList(); }
public object[] BoardHandle(string board) { int[,] webBoard = JsonConvert.DeserializeObject<int[,]>(board); Board boardObj = new Board(webBoard); ArrayList clickedObj = new ArrayList(); BoardNode root = new BoardNode(boardObj, -1, -1, null, clickedObj); BoardNode solution; try { solution = Search.AStar(root); } catch(OutOfMemoryException ex) { solution = null; } ArrayList actionList = Search.buildActionList(solution); return actionList.ToArray(); }
/// <summary> /// checkVisited - Checks a visited list to see if a board state has already /// been added. /// </summary> /// <param name="curBoard">Board to check</param> /// <param name="visited">Visted list to check</param> /// <returns>True- Board is in visited. False- Board is not in visited.</returns> private static bool checkVisited(Board curBoard, ArrayList visited) { Board compareBoard; // Compares each board in the visited list to // the board sent as argument. for(int i = 0; i < visited.Count; i++) { compareBoard = (Board)visited[i]; if(compareBoard.compare(curBoard)) { return true; } } return false; }
/// <summary> /// Creates a copy of the this board. /// </summary> /// <returns>Copy Board Object</returns> public Board copyBoard() { int[,] newBoard = (int[,])board.Clone(); Board copy = new Board(newBoard, numLit); return copy; }
/// <summary> /// Compares two boards to see if they are equal. /// </summary> /// <param name="compareTo">Board to be comapred to</param> /// <returns></returns> public bool compare(Board compareTo) { int current; // Compare each board position, if a difference is found then boards are not the same. for (int curRow = 0; curRow < board.GetLength(0); curRow++) { for (int curCol = 0; curCol < board.GetLength(1); curCol++) { current = this.board[curRow, curCol]; int compareMe = compareTo.board[curRow, curCol]; if (current != compareMe) { return false; } } } return true; }