static void Main(string[] args) { DataBaseManager.SetupDataBase(); CreateRandomNamesAndColorsList(); Console.Write("How many monkeys do you want in each forest? : "); int amount = int.Parse(Console.ReadLine()); Run(amount); }
public async Task MakeMonkeysJump() { List <Task> tasks = new List <Task>(); foreach (Monkey monkey in MonkeysInTheForest) { tasks.Add(Task.Run(() => CalculateEscapeRoute(monkey))); } Task.WaitAll(tasks.ToArray()); await DataBaseManager.WriteLogsToDataBase(this); }
public static void Run(int amount) { List <Forest> forests = new List <Forest>(); forests.Add(new Forest(0, 0, 1000, 0, 1000)); forests.Add(new Forest(1, 0, 1000, 0, 1000)); forests.Add(new Forest(2, 0, 1000, 0, 1000)); forests.Add(new Forest(3, 0, 1000, 0, 1000)); foreach (Forest forest in forests) { forest.GenerateForest(); } List <Task> tasks = new List <Task>(); foreach (Forest forest in forests) { for (int i = 0; i < amount; i++) { forest.GenerateMonkey(namen[i], colors[i]); } tasks.Add(DataBaseManager.WriteForestToDataBase(forest)); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); foreach (Forest forest in forests) { tasks.Add(forest.MakeMonkeysJump()); } Task.WhenAll(tasks); foreach (Forest forest in forests) { BitmapGenerator bmGen = new BitmapGenerator(forest.xmax - forest.xmin, forest.ymax - forest.ymin, forest); Task.Run(() => bmGen.createBitmap()); } //foreach (Forest f in forests) //{ // f.GenerateForest(); // Task.Run(() => DataBaseManager.WriteForestToDataBase(f)); // for (int i = 0; i < 8; i++) // { // f.GenerateMonkey(namen[i], colors[i]); // } // Task.Run(() => f.MakeMonkeysJump()); // BitmapGenerator bmGen = new BitmapGenerator(f.xmax - f.xmin, f.ymax - f.ymin, f); // Task.Run(() => bmGen.createBitmap()); //} }
public async Task CalculateClosestTree(Forest forest) { double distanceToBorder = (new List <double>() { forest.ymax - CurrentTree.PositionY, forest.xmax - CurrentTree.PositionX, CurrentTree.PositionY - forest.ymin, CurrentTree.PositionX - forest.xmin }).Min(); double distanceToClosestTree = 100; foreach (Tree tree in forest.Trees) { if (!VisitedTrees.Contains(tree) && tree.ID != CurrentTree.ID && !tree.IsOccupied) { if (ClosestTree == null && tree != CurrentTree) { ClosestTree = tree; } else { int x1 = CurrentTree.PositionX; int y1 = CurrentTree.PositionY; int x2 = tree.PositionX; int y2 = tree.PositionY; double distanceToTree = Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2)); distanceToClosestTree = Math.Sqrt(Math.Pow(x1 - ClosestTree.PositionX, 2) + Math.Pow(y1 - ClosestTree.PositionY, 2)); if (distanceToTree < distanceToClosestTree) { ClosestTree = tree; } } } } if (distanceToBorder < distanceToClosestTree) { Escaped = true; await DataBaseManager.WriteMonkeyRecordsToDataBase(this, forest.ID); } else { await DataBaseManager.WriteToTextFile(forest); await Jump(forest); } }