Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code for MainWindow.xaml.cs
- //
- // #- ant1, ant2, ant3 etc. im loop erstellen
- // #-- fields + properties: name, start-position (100, 100), foodEaten (0)
- // #-- methods: move(), eatFood()
- // #-- xaml-file data
- // -- show antxaml in the MainWindow
- // -- bind xaml-file position with ant-position
- //
- // #- food1, food2, food3 etc. im loop erstellen
- // #-- fields + properties: name(not necessary), position (random)
- // -- xaml-file data
- // -- bind xaml-file position with food-position
- //
- // #- while loop ( t += dt )
- // #-- calculate new position of all ants
- // #-- move ants
- // #-- check distance between all ants and all foods
- // #--- if distance < 3 --> ant eats food; food disapear
- //
- // #- struct Position
- // ##############################################################################
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WpfApplication1
- {
- /// <summary>
- /// Interaktionslogik für MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- var random = new Random();
- var iterations = 100;
- var numberOfAnts = 5;
- var ants = CreateAntCollection(numberOfAnts);
- var antsxaml = CreateAntXamlCollection(numberOfAnts);
- var numberOfFoods = 5;
- var foods = CreateFoodCollection(numberOfFoods);
- // time-loop
- for (var iteration = 0; iteration < iterations; iteration++)
- {
- Thread.Sleep(3);
- Task.Run(() =>
- {
- // HERE I TRY TO CALL MY OBJECT FROM THE XAML FILE
- this.Dispatcher.Invoke((Action)(() =>
- {
- // move ants
- foreach (var ant in ants)
- {
- var x = (random.Next(3) - 1) + ant.Position.X;
- var y = (random.Next(3) - 1) + ant.Position.Y;
- ant.Move(x, y);
- }
- }));
- });
- // distance between ants and food
- for (var i = ants.Count - 1; i >= 0; i--)
- {
- var ant = ants[i];
- for (var y = foods.Count - 1; y >= 0; y--)
- {
- var food = foods[y];
- if (ant.CanReach(food))
- {
- Console.WriteLine(
- "Iteration: {0} | Ant: {1} | Ant-pos: {2} | found food at: {3}",
- iteration,
- ant,
- ant.Position,
- food.Position);
- ant.Eat(food);
- foods.RemoveAt(y);
- }
- }
- }
- }
- }
- private static List<Ant> CreateAntCollection(int count)
- {
- // MainWindow.xaml.cs-file objects
- var ants = new List<Ant>(count);
- for (var i = 0; i < count; i++)
- {
- var name = string.Format("ant-{0}", i);
- var ant = new Ant(name);
- ants.Add(ant);
- }
- return ants;
- }
- private static List<Ellipse> CreateAntXamlCollection(int count)
- {
- var antsxaml = new List<Ellipse>(count);
- for (var i = 0; i < count; i++)
- {
- var name = string.Format("ant{0}", i);
- var antxaml = new Ellipse();
- antxaml.Name = name;
- // create object properties
- antxaml.Fill = new SolidColorBrush(Color.FromArgb(0x44, 0x88, 0x88, 0x44));
- antxaml.Width = 44;
- antxaml.Height = 44;
- Canvas.SetLeft(antxaml, 100);
- Canvas.SetTop(antxaml, 100);
- Console.WriteLine("created xaml-ant");
- antsxaml.Add(antxaml);
- }
- return antsxaml;
- }
- private static List<Food> CreateFoodCollection(int count)
- {
- var foods = new List<Food>(count);
- for (var i = 0; i < count; i++)
- {
- foods.Add(new Food());
- }
- return foods;
- }
- }
- class Ant
- {
- public Ant(string name)
- {
- Name = name;
- Position = new Position(100, 100);
- }
- public string Name { get; private set; }
- public int FoodEaten { get; private set; }
- public Position Position { get; private set; }
- public void Eat(Food food)
- {
- FoodEaten += 1;
- }
- public bool CanReach(Food food)
- {
- var distance = Position.DistanceTo(food.Position);
- return distance < 3.0;
- }
- public void Move(int x, int y)
- {
- Position = new Position(x, y);
- }
- public override string ToString()
- {
- return Name;
- }
- }
- class Food
- {
- public Food()
- {
- Position = new Position(0, 0);
- }
- public Position Position { get; private set; }
- public void Move(int x, int y)
- {
- Position = new Position(x, y);
- }
- }
- struct Position
- {
- public readonly int X;
- public readonly int Y;
- public Position(int x, int y)
- {
- X = x;
- Y = y;
- }
- public double DistanceTo(Position other)
- {
- var x = X - other.X;
- var y = Y - other.Y;
- var distance = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
- return distance;
- }
- public override string ToString()
- {
- return string.Format("{0},{1}", X, Y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement