private void AddCar(Orientation orientation, int length, Color color, int row, int col) { Car c = new Car(orientation, length, color, row, col); Mark(row, col, length, orientation, true); CarCanvas.Children.Add(c); c.SetValue(Canvas.LeftProperty, col * 60.0); c.SetValue(Canvas.TopProperty, row * 60.0); c.MouseLeftButtonDown += new MouseButtonEventHandler(OnCarLeftButtonDown); c.MouseMove += new MouseEventHandler(OnCarMove); c.MouseLeftButtonUp += new MouseButtonEventHandler(OnCarLeftButtonUp); }
public TrafficGrid(TrafficGrid g) { this.Cars = new List<Car>(); foreach (var car in g.Cars) { var c = new Car(); c.Orientation = car.Orientation; c.Length = car.Length; c.X = car.X; c.Y = car.Y; c.Color = car.Color; this.Cars.Add(c); } this.Dimension = g.Dimension; this.Occupancy = this.Occupancy = new bool[g.Dimension, g.Dimension]; this.genOccupancy(); }
public TrafficGrid(List<Car> cars, int dim) { this.Cars = new List<Car>(); foreach (var car in cars) { var c = new Car(); c.Orientation = car.Orientation; c.Length = car.Length; c.X = car.X; c.Y = car.Y; c.Color = car.Color; this.Cars.Add(c); } this.Dimension = dim; this.Occupancy = new bool[dim, dim]; this.genOccupancy(); }
//this method returns longest possible distance to travel //if car orientation doesnt permit moving in dir an exception is thrown public int CanMove(Car car, Direction dir) { int i = 0; switch (car.Orientation) { case Orientation.Vertical: switch (dir) { case Direction.Up: while (((car.Y - i - 1) >= 0) && !this.Occupancy[car.X, car.Y - i -1]) ++i; return i; case Direction.Down: while (((car.Y + i + car.Length) < this.Dimension) && !this.Occupancy[car.X, car.Y + i + car.Length ]) ++i; return i; default: string msg = String.Format("Orientation {0} doesn't allow moving {1}", car.Orientation.ToString(), dir.ToString()); throw new ArgumentException(msg); } case Orientation.Horizontal: switch (dir) { case Direction.Left: while (((car.X - i - 1) >= 0) && !this.Occupancy[car.X - i - 1, car.Y]) ++i; return i; case Direction.Right: while (((car.X + i + car.Length) < this.Dimension) && !this.Occupancy[car.X + i + car.Length , car.Y]) { ++i; } return i; default: string msg = String.Format("Orientation {0} doesn't allow moving {1}", car.Orientation.ToString(), dir.ToString()); throw new ArgumentException(msg); } default: throw new ArgumentException("Unrecognised car direction"); } }
//funkcia, ktora vytvori novu konfiguraci mriezky tym,ze pohneme nejakym autom public TrafficGrid Move(Car car, Direction direction, int distance) { TrafficGrid result = new TrafficGrid(this); Car actualCar = result.Cars.Find(c => c.Color == car.Color); //if (CanMove(actualCar,direction) < distance) throw new Exception("Cant move that far"); int newX = actualCar.X, newY = actualCar.Y; switch (actualCar.Orientation) { case Orientation.Vertical: switch (direction) { case Direction.Up: for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X, actualCar.Y + i] = false; } for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X, actualCar.Y - distance + i] = true; } newY -= distance; break; case Direction.Down: for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X, actualCar.Y + i] = false; } for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X, actualCar.Y + distance + i] = true; } newY += distance; break; default: break; } break; case Orientation.Horizontal: switch (direction) { case Direction.Left: for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X + i, actualCar.Y] = false; } for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X - distance + i, actualCar.Y] = true; } newX -= distance; break; case Direction.Right: for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X+i, actualCar.Y] = false; } for (int i = 0; i < actualCar.Length; i++) { result.Occupancy[actualCar.X + distance + i, actualCar.Y] = true; } newX +=distance; break; default: break; } break; default: break; } actualCar.X = newX; actualCar.Y = newY; return result; }
public void LoadGridFromFile(string filename) { try { string[] lines = File.ReadAllLines(filename); this.Dimension = int.Parse(lines[0]); this.Occupancy = new bool[this.Dimension, this.Dimension]; for (int i = 1; i < lines.Length; i++) { var car = new Car(); var c = lines[i].Split(' '); //tak just first letter of first string as color-identifier of th car car.Color = c[0][0]; car.X = int.Parse(c[1]); car.Y = int.Parse(c[2]); car.Orientation = c[3] == "h" ? Orientation.Horizontal : Orientation.Vertical; car.Length = int.Parse(c[4]); this.Cars.Add(car); } genOccupancy(); } catch (Exception) { throw; } }