Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.49 KB | None | 0 0
  1. // Code for MainWindow.xaml.cs
  2. //
  3. // #- ant1, ant2, ant3 etc. im loop erstellen
  4. // #-- fields + properties: name, start-position (100, 100), foodEaten (0)
  5. // #-- methods: move(), eatFood()
  6. // #-- xaml-file data
  7. // -- show antxaml in the MainWindow
  8. // -- bind xaml-file position with ant-position
  9. //
  10. // #- food1, food2, food3 etc. im loop erstellen
  11. // #-- fields + properties: name(not necessary), position (random)
  12. // -- xaml-file data
  13. // -- bind xaml-file position with food-position
  14. //
  15. // #- while loop ( t += dt )
  16. // #-- calculate new position of all ants
  17. // #-- move ants
  18. // #-- check distance between all ants and all foods
  19. // #--- if distance < 3 --> ant eats food; food disapear
  20. //
  21. // #- struct Position
  22. // ##############################################################################
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Text;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using System.Windows;
  31. using System.Windows.Controls;
  32. using System.Windows.Data;
  33. using System.Windows.Documents;
  34. using System.Windows.Input;
  35. using System.Windows.Media;
  36. using System.Windows.Media.Imaging;
  37. using System.Windows.Navigation;
  38. using System.Windows.Shapes;
  39.  
  40. namespace WpfApplication1
  41. {
  42.     /// <summary>
  43.     /// Interaktionslogik für MainWindow.xaml
  44.     /// </summary>
  45.     public partial class MainWindow : Window
  46.     {
  47.         public MainWindow()
  48.         {
  49.             InitializeComponent();
  50.  
  51.             var random = new Random();
  52.  
  53.             var iterations = 100;
  54.  
  55.             var numberOfAnts = 5;
  56.             var ants = CreateAntCollection(numberOfAnts);
  57.             var antsxaml = CreateAntXamlCollection(numberOfAnts);
  58.  
  59.             var numberOfFoods = 5;
  60.             var foods = CreateFoodCollection(numberOfFoods);
  61.  
  62.             // time-loop
  63.             for (var iteration = 0; iteration < iterations; iteration++)
  64.             {
  65.                 Thread.Sleep(3);
  66.  
  67.  
  68.                 Task.Run(() =>
  69.                 {
  70.  
  71.                     // HERE I TRY TO CALL MY OBJECT FROM THE XAML FILE
  72.                     this.Dispatcher.Invoke((Action)(() =>
  73.                     {
  74.  
  75.                         // move ants
  76.                         foreach (var ant in ants)
  77.                         {
  78.                             var x = (random.Next(3) - 1) + ant.Position.X;
  79.                             var y = (random.Next(3) - 1) + ant.Position.Y;
  80.                             ant.Move(x, y);
  81.                         }
  82.                     }));
  83.                 });
  84.  
  85.                 // distance between ants and food
  86.                 for (var i = ants.Count - 1; i >= 0; i--)
  87.                 {
  88.                     var ant = ants[i];
  89.  
  90.                     for (var y = foods.Count - 1; y >= 0; y--)
  91.                     {
  92.                         var food = foods[y];
  93.  
  94.                         if (ant.CanReach(food))
  95.                         {
  96.                             Console.WriteLine(
  97.                                 "Iteration: {0} | Ant: {1} | Ant-pos: {2} | found food at: {3}",
  98.                                 iteration,
  99.                                 ant,
  100.                                 ant.Position,
  101.                                 food.Position);
  102.  
  103.                             ant.Eat(food);
  104.  
  105.                             foods.RemoveAt(y);
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.  
  112.         private static List<Ant> CreateAntCollection(int count)
  113.         {
  114.             // MainWindow.xaml.cs-file objects
  115.             var ants = new List<Ant>(count);
  116.  
  117.             for (var i = 0; i < count; i++)
  118.             {
  119.                 var name = string.Format("ant-{0}", i);
  120.  
  121.                 var ant = new Ant(name);
  122.  
  123.                 ants.Add(ant);
  124.             }
  125.  
  126.             return ants;
  127.         }
  128.  
  129.         private static List<Ellipse> CreateAntXamlCollection(int count)
  130.         {
  131.             var antsxaml = new List<Ellipse>(count);
  132.  
  133.             for (var i = 0; i < count; i++)
  134.             {
  135.                 var name = string.Format("ant{0}", i);
  136.                 var antxaml = new Ellipse();
  137.  
  138.                 antxaml.Name = name;
  139.  
  140.                 // create object properties
  141.                 antxaml.Fill = new SolidColorBrush(Color.FromArgb(0x44, 0x88, 0x88, 0x44));
  142.                 antxaml.Width = 44;
  143.                 antxaml.Height = 44;
  144.                 Canvas.SetLeft(antxaml, 100);
  145.                 Canvas.SetTop(antxaml, 100);
  146.                 Console.WriteLine("created xaml-ant");
  147.                 antsxaml.Add(antxaml);
  148.             }
  149.  
  150.             return antsxaml;
  151.         }
  152.  
  153.         private static List<Food> CreateFoodCollection(int count)
  154.         {
  155.             var foods = new List<Food>(count);
  156.  
  157.             for (var i = 0; i < count; i++)
  158.             {
  159.                 foods.Add(new Food());
  160.             }
  161.  
  162.             return foods;
  163.         }
  164.     }
  165.  
  166.     class Ant
  167.     {
  168.         public Ant(string name)
  169.         {
  170.             Name = name;
  171.             Position = new Position(100, 100);
  172.         }
  173.  
  174.         public string Name { get; private set; }
  175.  
  176.         public int FoodEaten { get; private set; }
  177.  
  178.         public Position Position { get; private set; }
  179.  
  180.         public void Eat(Food food)
  181.         {
  182.             FoodEaten += 1;
  183.         }
  184.  
  185.         public bool CanReach(Food food)
  186.         {
  187.             var distance = Position.DistanceTo(food.Position);
  188.  
  189.             return distance < 3.0;
  190.         }
  191.  
  192.         public void Move(int x, int y)
  193.         {
  194.             Position = new Position(x, y);
  195.         }
  196.  
  197.         public override string ToString()
  198.         {
  199.             return Name;
  200.         }
  201.     }
  202.  
  203.     class Food
  204.     {
  205.         public Food()
  206.         {
  207.             Position = new Position(0, 0);
  208.         }
  209.  
  210.         public Position Position { get; private set; }
  211.  
  212.         public void Move(int x, int y)
  213.         {
  214.             Position = new Position(x, y);
  215.         }
  216.     }
  217.  
  218.     struct Position
  219.     {
  220.         public readonly int X;
  221.  
  222.         public readonly int Y;
  223.  
  224.         public Position(int x, int y)
  225.         {
  226.             X = x;
  227.             Y = y;
  228.         }
  229.  
  230.         public double DistanceTo(Position other)
  231.         {
  232.             var x = X - other.X;
  233.             var y = Y - other.Y;
  234.  
  235.             var distance = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
  236.  
  237.             return distance;
  238.         }
  239.  
  240.         public override string ToString()
  241.         {
  242.             return string.Format("{0},{1}", X, Y);
  243.         }
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement