public static bool add(EnvironmentObject E, int X, int Y) { //Console.WriteLine("Added " + E.name + " at X : " + X + " Y : " + Y); int[] position = new int[2]; position[0] = X; position[1] = Y; if(!queueToBeAdded.ContainsKey(E)) queueToBeAdded.Add(E, position); return true; }
public Agent(BayesNet Bnet, EnvironmentObject home) { this.bnet = Bnet; this.name = "Agent"; this.category = "Animal"; this.Speed = 1; this.iCanMove = true; this.cannotBeWith.Add("water", true); this.chanceToDuplicate = 0.8; this.priority = 15; this.resouces.Add("meat", 25); this.maxSquare = 500; this.goal = "hunt"; if (home.name.Equals("Cave")) { Cave cave = (Cave)home; if (cave.moveIn(this)) this.Home = cave; else this.goal = "findNewHouse"; } else if (home.name.Equals("House")) { House house = (House)home; if (house.moveIn(this)) this.Home = house; else this.goal = "findNewHouse"; } hunts.Add("rabbit", true); // can gather fruits from trees hunts.Add("tree", true); hunts.Add("garden", true); gathers.Add("tree", true); gathers.Add("woodmill", true); gathers.Add("mountain", true); this.Family.Add(this); myMap = new ArrayList[EnvironmentMap.sizeX, EnvironmentMap.sizeY]; // Initialize all ArrayLists to default size (empty) for (int row = 0; row < EnvironmentMap.sizeX; ++row) { for (int column = 0; column < EnvironmentMap.sizeY; ++column) { myMap[row, column] = new ArrayList(); } } }
public static bool move(EnvironmentObject E, int X, int Y) { if (envMap[E.X,E.Y].Contains(E)) { remove(E, E.X, E.Y); add(E, X, Y); E.X = X; E.Y = Y; return true; } else { return false; } }
private void think() { //Console.WriteLine("My current goal is: " + this.goal); --hungry; this.remember(); this.takeCare(); if (this.goal.Equals("findNewHouse")) { HashSet<EnvironmentObject> O = EnvironmentMap.getAll(X, Y); // is there a house here? foreach (EnvironmentObject E in O) if (E.name.Equals("Cave")) { // we found a cave... try to move in Cave cave = (Cave)E; if (cave.moveIn(this)) { //Console.WriteLine("Found a new home!"); this.Home = cave; this.goal = "hunt"; } } else if (E.name.Equals("House")) { // we found a house... try to move in House house = (House)E; if (house.moveIn(this)) { //Console.WriteLine("Found a new home!"); this.Home = house; this.goal = "hunt"; } } return; } if (this.goal.Equals("buildHouse")) { this.buildHouse(); return; } if (this.goal.Equals("collectWood")) this.collectWood(); if (this.goal.Equals("collectStone")) this.collectStone(); if (this.goal.Equals("hunt")) this.huntFood(); if(this.goal.Equals("stayHome")) { if (Home.name.Equals("Cave")) { Cave home = (Cave)Home; home.relax(this); } else if (Home.name.Equals("House")) { House house = (House)Home; house.relax(this); } } if (!this.goal.Equals("goHome") && Home != null) { if ((MaxStorage - AmountInventory) < 10) { //Console.WriteLine("My inventory is at " + AmountInventory + ". I am going home."); path = astar.findPath(this, Home.X, Home.Y); this.goal = "goHome"; // You are running out of space... go back home } else if (hungry <= 15) { //Console.WriteLine("My hunger is at " + hungry + ". I am going home."); path = astar.findPath(this, Home.X, Home.Y); this.goal = "goHome"; // you are hungry and need to be home to eat } } else { this.buildDam(); // If we have stone we can build a dam to protect the house and the people this.huntFood(); // while heading home... if we see somthing to kill lets kill it } }
private bool buildHouse() { bool goodPlace = false; HashSet<EnvironmentObject> O = EnvironmentMap.getAll(X, Y); foreach (EnvironmentObject E in O) if (E.name.Equals("tree")) goodPlace = true; if (this.hungry <= 10) // i am to hungry to care goodPlace = true; if(goodPlace) { House house = new House(this.bnet); EnvironmentMap.add(house, X, Y); house.moveIn(this); this.Home = house; this.goal = "hunt"; } else return false; return true; }
/* Ok lets explain how this works. First we will start by the Output. * * OutPut : This function call returns a stack with the top elements being the next location you wish * to go to and the bottom element is the destination. The stack is composed of int[2] which * at location int[0] is the value of X and at location int[1] is the value of Y. * */ public Stack<int[]> findPath(EnvironmentObject O, int X, int Y) { Stack<int[]> Path = new Stack<int[]>(); int CurrentX = O.X; int CurrentY = O.Y; /* NOTE : * * The list Open and Closed are not optimal. If we run into problems with the search being to slow * we will need to change them from a list to a tree for faster look up. * */ List<Node> Open = new List<Node>(); List<Node> Closed = new List<Node>(); // Default Huristic int MD = Math.Abs(CurrentX - X) + Math.Abs(CurrentY - Y); Node BestOption; Open.Add(new Node(CurrentX, CurrentY, MD, 0, null)); do { if (Open.Count == 0) // its empty meaning we can't find a path :( return Path; // We then return an empty path since there is no way to get to the destination BestOption = FindBestOption(Open); Closed.Add(BestOption); Open.Remove(BestOption); // lets check all the neighbors. // Can we move to them? // We need to check all 4 neighbors.... :( if (checkNode(BestOption.X + 1, BestOption.Y, Open, Closed, BestOption)) if (O.canMove(BestOption.X + 1, BestOption.Y)) { MD = Math.Abs(BestOption.X + 1 - X) + Math.Abs(BestOption.Y - Y); Open.Add(new Node(BestOption.X + 1, BestOption.Y, MD, BestOption.G + 1, BestOption)); } if (checkNode(BestOption.X - 1, BestOption.Y, Open, Closed, BestOption)) if (O.canMove(BestOption.X - 1, BestOption.Y)) { MD = Math.Abs(BestOption.X - 1 - X) + Math.Abs(BestOption.Y - Y); Open.Add(new Node(BestOption.X - 1, BestOption.Y, MD, BestOption.G + 1, BestOption)); } if (checkNode(BestOption.X, BestOption.Y + 1, Open, Closed, BestOption)) if (O.canMove(BestOption.X, BestOption.Y + 1)) { MD = Math.Abs(BestOption.X - X) + Math.Abs(BestOption.Y + 1 - Y); Open.Add(new Node(BestOption.X, BestOption.Y + 1, MD, BestOption.G + 1, BestOption)); } if (checkNode(BestOption.X, BestOption.Y - 1, Open, Closed, BestOption)) if (O.canMove(BestOption.X, BestOption.Y - 1)) { MD = Math.Abs(BestOption.X - X) + Math.Abs(BestOption.Y - 1 - Y); Open.Add(new Node(BestOption.X, BestOption.Y - 1, MD, BestOption.G + 1, BestOption)); } // set the current X and Y to the Best Option previously defined in this iterastion of the loop CurrentX = BestOption.X; CurrentY = BestOption.Y; } while (CurrentX != X || CurrentY != Y); // Now we need to generate the path Path = GeneratePath(BestOption); return Path; }