private void drawLights(Crossing[,] slots) { // addElement(pictureBox1.Location.X + 66 - 13, pictureBox1.Location.Y+66, "ped-red-light.png"); for(int i=0;i<4;i++) { for(int j=0;j<3;j++) { Crossing c = slots[i, j]; int x, y; x = pictureBox1.Location.X + (3*66*i); y = pictureBox1.Location.Y+ (3*66*j); //base values //x = pictureBox1.Location.X + 66 - 13; y = pictureBox1.Location.Y + 2 * 22 - 10; string str=""; if (c != null ) { //draw the following lights: //c.LightEtoNW, c.LightEtoS, c.LightWtoN, c.LightWtoSE if (c.LightEtoNW._color != Color.Gray)//if lights are NOT disabled { addElement(x + 2 * 66, y + 66 + 6, c.LightEtoNW._color.ToString()); //add LightEtoNW addElement(x + 2 * 66, y + 66 + 6 + 22, c.LightEtoS._color.ToString()); //add LightEtoS addElement(x + 66 - 6, y + 2 * 66 - 14 - 22, c.LightWtoN._color.ToString()); //add LightWtoN addElement(x + 66 - 6, y + 2 * 66 - 14, c.LightWtoSE._color.ToString()); //add LightWtoSE } } if (c != null && c.GetType() == typeof(Crossing_1)) { Crossing_1 c1 = (Crossing_1) c; if (c.LightEtoNW._color != Color.Gray)//if lights are NOT disabled { //draw the following lights: //LightStoEN, LightStoW, LightNtoE, LightNtoWS addElement(x + 2 * 66 - 14, y + 2 * 66, c1.LightStoEN._color.ToString());//LightStoEN addElement(x + 2 * 66 - 14 - 22, y + 2 * 66, c1.LightStoW._color.ToString());//LightStoW addElement(x + 66 + 22 - 14, y + 66 - 6, c1.LightNtoWS._color.ToString());//LightNtoWS addElement(x + 2 * 66 - 14 - 22, y + 66 - 6, c1.LightNtoE._color.ToString());//LightNtoE } } if (c != null && c.GetType() == typeof(Crossing_2)) { Crossing_2 c2 = (Crossing_2) c; if (c2.LightEtoS._color!=Color.Gray)//if lights are not disabled { str = "ped" + c2.LightPedestrian._color.ToString(); addElement(x + 66 - 14, y + 2 * 22, str); addElement(x + 2 * 66 + 4, y + 2 * 22, str); addElement(x + 66 - 14, y + 2 * 66 + 15, str ); addElement(x + 2 * 66 + 4, y + 2 * 66 + 15, str); addElement(x + 2 * 66 - 14, y + 2 * 66, c2.LightStoN._color.ToString());//LightStoN addElement(x + 66 + 22 - 14, y + 66 - 6, c2.LightNtoS._color.ToString());//LightNtoS } } } } }
/// <summary> /// Add a new cars the given crossing /// </summary> private void addCars(Crossing c) { ////////////add new cars to street E if (c.FlowE > c.EnteredE && //if there are still cars to enter a crossing and c.StreetE.LaneEnter1[0] == null && c.StreetE.LaneEnter2[0] == null) //there is an available slot at the entrance of the street { Car car = new Car(); car.Street = c.StreetE; car.Crossing = c; car.Direction = Direction.West; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredE++; _listOfCars.Add(car); //car.move(); } ////////////add new cars to street W if (c.FlowW > c.EnteredW && //if there are still cars to enter a crossing and c.StreetW.LaneEnter1[0] == null && c.StreetW.LaneEnter2[0] == null) //there is an available slot at the entrance of the street { Car car = new Car(); car.Street = c.StreetW; car.Crossing = c; car.Direction = Direction.East; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredW++; _listOfCars.Add(car); //car.move(); } ////////add new cars to street S if (c.FlowS > c.EnteredS && //if there are still cars to enter a crossing and c.StreetS.LaneEnter1[0] == null //there is an available slot at the entrance of lane enter 1 && (c.GetType() == typeof(Crossing_2) || c.StreetS.LaneEnter2[0] == null))//and, in case its a crossing 1, there is an available slot at the entrance of lane 2 { Car car = new Car(); car.Street = c.StreetS; car.Crossing = c; car.Direction = Direction.North; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredS++; _listOfCars.Add(car); //car.move(); } ////////add new cars to street N if (c.FlowN > c.EnteredN && //if there are still cars to enter a crossing c.StreetN.LaneEnter1[0] == null //and if there is an available slot at the entrance of lane enter 1 && (c.GetType() == typeof(Crossing_2) || c.StreetN.LaneEnter2[0] == null))//and, unless it's a crossing_2, if there is an available slot at the entrance of lane 2 { Car car = new Car(); car.Street = c.StreetN; car.Crossing = c; car.Direction = Direction.South; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredN++; _listOfCars.Add(car); //car.move(); } }
public Car(Crossing c) { _crossing = c; }
/// <summary> /// Add a new cars the given crossing /// </summary> private void addCars(Crossing c) { ////////////add new cars to street E if (c.FlowE > c.EnteredE)//if there are still cars to enter a crossing and { Car car = new Car(c); car.Street = c.StreetE; car.Direction = Direction.West; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredE++; _listOfCars.Add(car); } ////////////add new cars to street W if (c.FlowW > c.EnteredW) { Car car = new Car(c); car.Street = c.StreetW; car.Direction = Direction.East; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredW++; _listOfCars.Add(car); } ////////add new cars to street S if (c.FlowS > c.EnteredS) { Car car = new Car(c); car.Street = c.StreetS; car.Direction = Direction.North; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredS++; _listOfCars.Add(car); } ////////add new cars to street N if (c.FlowN > c.EnteredN) { Car car = new Car(c); car.Street = c.StreetN; car.Direction = Direction.South; car.StreetIndex[0] = -1; car.StreetIndex[1] = -1; car.HasEnteredGrid = false; car.HasExitedGrid = false; c.EnteredN++; _listOfCars.Add(car); } }
private void slot_click(object sender, EventArgs e) { if (CheckTextBoxes()) { string clickedElement = ((Control)sender).Tag.ToString(); PictureBox picbox = _gui_slots[((int)clickedElement[0]) - (int)'A', Convert.ToInt32(clickedElement[1].ToString())]; if (_copiedCrossing != null)//if there is an object to be pasted { if (Controller.slotIsAvailable(picbox.Tag.ToString())) { label1.Text = Controller.addCrossing(picbox.Tag.ToString(), _copiedCrossing.GetType()); if (label1.Text == "") { int[] values = new int[20]; if (_copiedCrossing.GetType() == typeof(Crossing_1)) { Crossing_1 c1 = (Crossing_1)_copiedCrossing; values[0] = c1.LightNtoWS._greenLightTime; values[1] = c1.LightStoW._greenLightTime; } if (_copiedCrossing.GetType() == typeof(Crossing_2)) { Crossing_2 c2 = (Crossing_2)_copiedCrossing; values[0] = c2.LightPedestrian._greenLightTime; values[1] = c2.LightStoN._greenLightTime; } values[2] = _copiedCrossing.LightWtoN._greenLightTime; values[3] = _copiedCrossing.LightWtoSE._greenLightTime; values[4] = _copiedCrossing.FlowN; values[5] = _copiedCrossing.FlowE; values[6] = _copiedCrossing.FlowS; values[7] = _copiedCrossing.FlowW; values[8] = (int)_copiedCrossing.ProbNtoE; values[9] = (int)_copiedCrossing.ProbNtoS; values[10] = (int)_copiedCrossing.ProbNtoW; values[11] = (int)_copiedCrossing.ProbEtoN; values[12] = (int)_copiedCrossing.ProbEtoS; values[13] = (int)_copiedCrossing.ProbEtoW; values[14] = (int)_copiedCrossing.ProbStoN; values[15] = (int)_copiedCrossing.ProbStoE; values[16] = (int)_copiedCrossing.ProbStoW; values[17] = (int)_copiedCrossing.ProbWtoN; values[18] = (int)_copiedCrossing.ProbWtoE; values[19] = (int)_copiedCrossing.ProbWtoS; Controller.setCrossingProperties(picbox.Tag.ToString(), values); _copiedCrossing = null; Controller.timerHasTriggered(null, null); } } else { label1.Text = "Please select an available slot."; } } else//if there is no object to be pasted { //if clicked on a (not null) crossing if (picbox.Image != null) { //if clicked on an unselected slot if (clickedElement != SelectedSlot) { //unselect pevious slot if (SelectedSlot != "") { _gui_slots[((int)SelectedSlot[0]) - (int)'A', Convert.ToInt32(SelectedSlot[1].ToString())].BorderStyle = BorderStyle.None; _gui_slots[((int)SelectedSlot[0]) - (int)'A', Convert.ToInt32(SelectedSlot[1].ToString())].SendToBack(); } //selects clicked slot _gui_slots[((int)picbox.Tag.ToString()[0]) - (int)'A', Convert.ToInt32(picbox.Tag.ToString()[1].ToString())].BorderStyle = BorderStyle.Fixed3D; _gui_slots[((int)picbox.Tag.ToString()[0]) - (int)'A', Convert.ToInt32(picbox.Tag.ToString()[1].ToString())].BringToFront(); foreach (PictureBox pb in _mergings) { if (pb.Tag.ToString() == picbox.Tag.ToString()) pb.BringToFront(); } SelectedSlot = clickedElement;//selects and displays crossing info } else //clicked on an already selected crossing { _gui_slots[((int)picbox.Tag.ToString()[0]) - (int)'A', Convert.ToInt32(picbox.Tag.ToString()[1].ToString())].BorderStyle = BorderStyle.None; _gui_slots[((int)SelectedSlot[0]) - (int)'A', Convert.ToInt32(SelectedSlot[1].ToString())].SendToBack(); SelectedSlot = ""; } } } label1.Text = ""; } }
/// <summary> /// Moves the car to the next position. /// </summary> /// <returns>returns true if the car moves</returns> public bool move() { Street[] streets = new Street[5]; Crossing cr = this.Crossing; streets[0] = this.Crossing.StreetN; streets[1] = this.Crossing.StreetE; streets[2] = this.Crossing.StreetS; streets[3] = this.Crossing.StreetW; streets[4] = this.Crossing.Intersection; nextSlot++;//increments next slot if (nextSlot > _path.Count-1)//if car is entering a crossin or leaving the grid { Crossing tempCrossing = this.Crossing; if (HasEnteredGrid)//gets new crossing { this._crossing = getNextCrossing(); } else//if car is entering the grid for the first time { calculateTurn(); calculateEntrance(streets); calculateIntersection(streets); calculateExit(streets); } if (this._crossing == null)//if car is leaving the grid { this.HasExitedGrid = true; this.Street.Lanes[this.StreetIndex[0]][this.StreetIndex[1]] = null; //clears the cars last position this.StreetIndex[0] = -1; this.StreetIndex[1] = -1; _path = null; nextSlot = 0; Street = null; return true; } else//if car is entering a new crossing { if (this.Street.Position == this.Turn && this.HasEnteredGrid)//adjust car street { streets[0] = this.Crossing.StreetN; streets[1] = this.Crossing.StreetE; streets[2] = this.Crossing.StreetS; streets[3] = this.Crossing.StreetW; streets[4] = this.Crossing.Intersection; Direction tempTurn = this.Turn; Direction tempDir = this.Direction; int[] lastPosition = this._path[_path.Count - 1]; Street tempStreet = this.Street;//holds old value of street this.Street = streets[((int)this.Turn + 2) % 4]; _path.Clear(); nextSlot = 0; calculateTurn(); calculateEntrance(streets); calculateIntersection(streets); calculateExit(streets); int str = _path[nextSlot][0], lane = _path[nextSlot][1], pos = _path[nextSlot][2]; if (streets[str].Lanes[lane][pos] != null)//if next position is NOT available, returns old values { this.Street = tempStreet; this._crossing = tempCrossing; List<int[]> oldPath = new List<int[]>(); oldPath.Add(lastPosition); this._path = oldPath; this.Turn = tempTurn; this.Direction = tempDir; return false; } else { tempStreet.Lanes[StreetIndex[0]][StreetIndex[1]] = null;//leaves previous position } } } }//end if(car is entering a new crosisng/leaving grid) int str2 = _path[nextSlot][0], lane2 = _path[nextSlot][1], pos2 = _path[nextSlot][2]; if (streets[str2].Lanes[lane2][pos2] == null && (nextSlot != 3 || checkLights()))//if next position is available and light is green { if (HasEnteredGrid == false) this.HasEnteredGrid = true; else Street.Lanes[StreetIndex[0]][StreetIndex[1]] = null;//leaves previous position streets[str2].Lanes[lane2][pos2] = this;//enters new position StreetIndex[0] = lane2; StreetIndex[1] = pos2; //updates the street if (nextSlot == 3) { this.Street = this.Crossing.Intersection; } if ((nextSlot == 4 && _path.Count == 7) || (nextSlot == 6 && _path.Count == 9))//if car left intersection zone { this.Street = streets[(int)this.Turn]; this.Direction = this.Turn; } } else//if car didnt move { nextSlot--; return false; } return false; }
private void drawCrossings(Crossing[,] slots) { SelectedSlot = "";//clears selected slot //clears all mergings foreach (PictureBox pb in _mergings ) { pb.Click -= slot_click; Controls.Remove(pb); } _mergings.Clear(); foreach (PictureBox pb in _gui_slots) { pb.Image = null; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { Crossing c = slots[i, j]; int x, y; x = pictureBoxSlotA0.Location.X + (3 * 66 * i); y = pictureBoxSlotA0.Location.Y + (3 * 66 * j); if (c != null) { if (c.GetType() == typeof(Crossing_1)) _gui_slots[i, j].Image = Properties.Resources.Traffic_Simulator_Crossing_1; else _gui_slots[i, j].Image = Properties.Resources.Traffic_Simulator_Crossing_2; _gui_slots[i, j].BorderStyle = BorderStyle.None; _gui_slots[i, j].SendToBack(); if ((i + 1) < 4 && slots[i + 1, j] != null)//check merging East { PictureBox pb = addElement(x + 2 * 66, y + 66, "mergingE"); pb.Tag = _gui_slots[i, j].Tag; pb.Click += slot_click; _mergings.Add(pb); } if ((i - 1) >= 0 && slots[i - 1, j] != null)//check merging West { PictureBox pb = addElement(x, y + 66, "mergingW"); pb.Tag = _gui_slots[i, j].Tag; pb.Click += slot_click; _mergings.Add(pb); } if ((j + 1) < 3 && slots[i, j + 1] != null && c.GetType() == typeof(Crossing_1))//check merging South { PictureBox pb = addElement(x + 66, y + 2 * 66, "mergingS"); pb.Tag = _gui_slots[i, j].Tag; pb.Click += slot_click; _mergings.Add(pb); } if ((j - 1) >= 0 && slots[i, j - 1] != null && c.GetType() == typeof(Crossing_1))//check merging North { PictureBox pb = addElement(x + 66, y, "mergingN"); pb.Tag = _gui_slots[i, j].Tag; pb.Click += slot_click; _mergings.Add(pb); } } } } foreach (PictureBox pb in _mergings) _elements.Remove(pb); }
private void cutToolStripMenuItem_Click(object sender, EventArgs e) { if (CheckTextBoxes()) { if (SelectedSlot == "") { label1.Text = "Please select a crossing first."; } else { _copiedCrossing = ObjectCopier.Clone<Crossing>(Grid.getCrossing(SelectedSlot)); Controller.removeCrossing(SelectedSlot); _gui_slots[((int)SelectedSlot[0]) - (int)'A', Convert.ToInt32(SelectedSlot[1].ToString())].BorderStyle = BorderStyle.FixedSingle; _selectedSlot = ""; Controller.timerHasTriggered(null, null); label1.Text = "Selected crossing has been cut.\nPlease select an available slot to paste."; } } }
private void copyToolStripMenuItem_Click(object sender, EventArgs e) { if (CheckTextBoxes()) { if (SelectedSlot == "") { label1.Text = "Please select a crossing first."; } else { _copiedCrossing = ObjectCopier.Clone<Crossing>(Grid.getCrossing(SelectedSlot)); label1.Text = "Selected crossing has been copied.\nPlease select an available slot to paste."; } } }
private void addCrossing2ToolStripMenuItem_Click(object sender, EventArgs e) { label1.Text = "Please select an available slot."; _copiedCrossing = new Crossing_2("XX"); }
/// <summary> /// Displays all the info of a crossing on the right pane /// </summary> /// <param name="c">crossing whose properties will be displayed</param> public void displayCrossingSettings(Crossing c) { if (c != null) { //if its stopped, enables textboxes, otherwise disables them foreach (TextBox tb in _crossingProperties) { tb.Enabled = _controller.State == State.Stopped; if(tb.Enabled) tb.BackColor = Color.White; else tb.BackColor = TextBoxBase.DefaultBackColor; } if (c.GetType() == typeof(Crossing_1)) { Crossing_1 cr1 = (Crossing_1)c; label5.Text = "N-->SW and S-->NE"; label6.Text = "N-->E and S-->W"; textBoxGLT1.Text = cr1.LightNtoWS._greenLightTime.ToString(); textBoxGLT2.Text = cr1.LightNtoE._greenLightTime.ToString(); textBoxGLT3.Text = cr1.LightWtoN._greenLightTime.ToString(); textBoxGLT4.Text = cr1.LightWtoSE._greenLightTime.ToString(); textBoxTF1.Text = cr1.FlowN.ToString(); textBoxTF2.Text = cr1.FlowE.ToString(); textBoxTF3.Text = cr1.FlowS.ToString(); textBoxTF4.Text = cr1.FlowW.ToString(); textBoxCTNE.Text = cr1.ProbNtoE.ToString(); textBoxCTNS.Text = cr1.ProbNtoS.ToString(); textBoxCTNW.Text = cr1.ProbNtoW.ToString(); textBoxCTEN.Text = cr1.ProbEtoN.ToString(); textBoxCTES.Text = cr1.ProbEtoS.ToString(); textBoxCTEW.Text = cr1.ProbEtoW.ToString(); textBoxCTSN.Text = cr1.ProbStoN.ToString(); textBoxCTSE.Text = cr1.ProbStoE.ToString(); textBoxCTSW.Text = cr1.ProbStoW.ToString(); textBoxCTWN.Text = cr1.ProbWtoN.ToString(); textBoxCTWE.Text = cr1.ProbWtoE.ToString(); textBoxCTWS.Text = cr1.ProbWtoS.ToString(); } if (c.GetType() == typeof(Crossing_2)) { Crossing_2 cr2 = (Crossing_2)c; label5.Text = "Pedestrians"; label6.Text = "N-->S and S-->N"; textBoxGLT1.Text = cr2.LightPedestrian._greenLightTime.ToString(); textBoxGLT2.Text = cr2.LightNtoS._greenLightTime.ToString(); textBoxGLT3.Text = cr2.LightWtoN._greenLightTime.ToString(); textBoxGLT4.Text = cr2.LightWtoSE._greenLightTime.ToString(); textBoxTF1.Text = cr2.FlowN.ToString(); textBoxTF2.Text = cr2.FlowE.ToString(); textBoxTF3.Text = cr2.FlowS.ToString(); textBoxTF4.Text = cr2.FlowW.ToString(); textBoxCTNE.Text = cr2.ProbNtoE.ToString(); textBoxCTNS.Text = cr2.ProbNtoS.ToString(); textBoxCTNW.Text = cr2.ProbNtoW.ToString(); textBoxCTEN.Text = cr2.ProbEtoN.ToString(); textBoxCTES.Text = cr2.ProbEtoS.ToString(); textBoxCTEW.Text = cr2.ProbEtoW.ToString(); textBoxCTSN.Text = cr2.ProbStoN.ToString(); textBoxCTSE.Text = cr2.ProbStoE.ToString(); textBoxCTSW.Text = cr2.ProbStoW.ToString(); textBoxCTWN.Text = cr2.ProbWtoN.ToString(); textBoxCTWE.Text = cr2.ProbWtoE.ToString(); textBoxCTWS.Text = cr2.ProbWtoS.ToString(); } } else//if no crossing is selected { foreach (TextBox tb in _crossingProperties) { if (tb.Focused) buttonShowHideGLT.Focus(); tb.Text = ""; tb.Enabled = false; tb.BackColor = TextBoxBase.DefaultBackColor; } } foreach (TextBox tb in _crossingProperties) { tb.Modified = false; } }