static Boolean isValidAndUnvisited(CartPoint pos) { if ((pos.x >= Laby.GetLength(0)) || (pos.x < 0) || (pos.y >= Laby.GetLength(1)) || (pos.y < 0)) { return(false); } LabPoint l = Laby[pos.x, pos.y]; if (l == null) { l = new LabPoint(); } if (l.barrier) { return(false); } if (l.visited) { return(false); } else { l.visited = true; Laby[pos.x, pos.y] = l; return(true); } }
static void collectTreasure(CartPoint pos) { LabPoint l = Laby[pos.x, pos.y]; if (!l.treasure.Equals(' ')) { Console.WriteLine(" Found treasure : " + l.treasure); treasuresFound += l.treasure; l.treasure = ' '; // remove treasure :D } Laby[pos.x, pos.y] = l; }
static void MoveTo(CartPoint pos) { increm++; printWithIncrem("-> (" + pos.x + "," + pos.y + ")"); if (!isValidAndUnvisited(pos)) { printWithIncrem("-> X"); increm--; return; } collectTreasure(pos); //already marked as visited .. PrintMaze(); MoveTo(new CartPoint(pos.x + 1, pos.y)); MoveTo(new CartPoint(pos.x, pos.y + 1)); MoveTo(new CartPoint(pos.x - 1, pos.y)); MoveTo(new CartPoint(pos.x, pos.y - 1)); }