📄 elevator.cs
字号:
this.Controls.Add(this.upButton1);
this.Controls.Add(this.floor1);
this.Controls.Add(this.elevator2);
this.Controls.Add(this.elevator1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "Elevator";
this.Text = "Elevator";
this.elevator1.ResumeLayout(false);
this.elevator2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Elevator());
}
//Click of buttons in elevators
private void elevatorButton_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.Button tmp = (System.Windows.Forms.Button)sender;
if(passengerE1 > 0)
{
if(sender == elevatorButton11 && elevator1.Top != floor1.Top)
destination1[1]++;
if(sender == elevatorButton12 && elevator1.Top != floor2.Top)
destination1[2]++;
if(sender == elevatorButton13 && elevator1.Top != floor3.Top)
destination1[3]++;
if(sender == elevatorButton14 && elevator1.Top != floor4.Top)
destination1[4]++;
}
else if(passengerE2 > 0)
{
if(sender == elevatorButton21 && elevator2.Top != floor1.Top)
destination2[1]++;
if(sender == elevatorButton22 && elevator2.Top != floor2.Top)
destination2[2]++;
if(sender == elevatorButton23 && elevator2.Top != floor3.Top)
destination2[3]++;
if(sender == elevatorButton24 && elevator2.Top != floor4.Top)
destination2[4]++;
}
tmp.Enabled = false;
tmp.BackColor = System.Drawing.Color.Yellow;
}
//Click of inbuttons and outbuttons of elevator1
private void pressButton1_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.Button tmp = (System.Windows.Forms.Button)sender;
if (sender == inButton21 || sender == inButton11 || sender == inButton31 || sender == inButton41)
if(passengerE1 < 10)
++passengerE1;
if (sender == outButton22 || sender == outButton12 || sender == outButton32 || sender == outButton42)
if(passengerE1 > 0)
--passengerE1;
tmp.BackColor = System.Drawing.Color.Yellow;
elevatorTextBox1.Text = passengerE1.ToString ();
}
//Click of inbuttons and outbuttons of elevator2
private void pressButton2_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.Button tmp = (System.Windows.Forms.Button)sender;
if (sender == inButton23 || sender == inButton13 || sender == inButton33 || sender == inButton43)
if(passengerE2 < 10)
++passengerE2;
if (sender == outButton24 || sender == outButton14 || sender == outButton34 || sender == outButton44)
if(passengerE2 > 0)
--passengerE2;
tmp.BackColor = System.Drawing.Color.Yellow;
elevatorTextBox2.Text = passengerE2.ToString();
}
//Click of upbuttons and downbuttons
private void pressButton3_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.Button tmp = (System.Windows.Forms.Button)sender;
tmp.BackColor = System.Drawing.Color.Yellow;
if(sender == upButton1 && elevator1.Top != floor1.Top && elevator2.Top != floor1.Top)
unhandled[0,1] = true;
if(sender == upButton2 && elevator1.Top != floor2.Top && elevator2.Top != floor2.Top)
unhandled[0,2] = true;
if(sender == upButton3 && elevator1.Top != floor3.Top && elevator2.Top != floor3.Top)
unhandled[0,3] = true;
if(sender == downButton2 && elevator1.Top != floor2.Top && elevator2.Top != floor2.Top)
unhandled[1,2] = true;
if(sender == downButton3 && elevator1.Top != floor3.Top && elevator2.Top != floor3.Top)
unhandled[1,3] = true;
if(sender == downButton4 && elevator1.Top != floor4.Top && elevator2.Top != floor4.Top)
unhandled[1,4] = true;
}
private void timerControl_Tick(object sender, System.EventArgs e)
{
//Assign the current missions to one of the elevators
assignMission();
//Elevators approach one of its destinations
touchDownE1();
touchDownE2();
//Set the direction and adjust their properties
setDirection();
adjustProperty();
//Suspend when reaching any floor
suspendE1();
suspendE2();
moveUpDown();
}
private void assignMission ()
{
if (unhandled[1,4]) //DownButton4
{
if ((floor4.Top > elevator1.Top && directE1 == (int)Direct.UP) || (floor4.Top > elevator2.Top && directE2 == (int)Direct.UP))
{
if (directE1 == (int)Direct.HALT)
destination2[4] = 1;
else if (directE2 == (int)Direct.HALT)
destination1[4] = 1;
else if (floor3.Top-elevator1.Top < floor3.Top-elevator2.Top)
destination1[4] = 1;
else
destination2[4] = 1;
unhandled[1,4] = false;
}
else if (directE1 == (int)Direct.HALT || directE2 == (int)Direct.HALT)
{
if (directE1 != (int)Direct.HALT)
destination2[4] = 1;
else if (directE2 != (int)Direct.HALT)
destination1[4] = 1;
else if (Math.Abs(floor4.Top-elevator1.Top) < Math.Abs(floor4.Top-elevator2.Top))
destination1[4] = 1;
else
destination2[4] = 1;
unhandled[1,4] = false;
}
}
if (unhandled[0,3]) //UpButton3
{
if ((floor3.Top > elevator1.Top && directE1 == (int)Direct.UP) || (floor3.Top > elevator2.Top && directE2 == (int)Direct.UP))
{
if (directE2 == (int)Direct.HALT)
destination2[3] = 1;
else if (directE2 == (int)Direct.HALT)
destination1[3] = 1;
else if (floor3.Top-elevator1.Top < floor3.Top-elevator2.Top)
destination1[3] = 1;
else
destination2[3] = 1;
unhandled[0,3] = false;
}
else if (directE1 == (int)Direct.HALT || directE2 == (int)Direct.HALT)
{
if (directE1 != (int)Direct.HALT)
destination2[3] = 1;
else if (directE2 != (int)Direct.HALT)
destination1[3] = 1;
else if (Math.Abs(floor3.Top-elevator1.Top) < Math.Abs(floor3.Top-elevator2.Top))
destination1[3] = 1;
else
destination2[3] = 1;
unhandled[0,3] = false;
}
}
if (unhandled[1,3]) //DownButton3
{
if ((floor3.Top < elevator1.Top && directE1 == (int)Direct.DOWN) || (floor3.Top < elevator2.Top && directE2 == (int)Direct.DOWN))
{
if (directE2 == (int)Direct.HALT)
destination2[3] = 1;
else if (directE2 == (int)Direct.HALT)
destination1[3] = 1;
else if (floor3.Top-elevator1.Top < floor3.Top-elevator2.Top)
destination2[3] = 1;
else
destination1[3] = 1;
unhandled[1,3] = false;
}
else if (directE1 == (int)Direct.HALT || directE2 == (int)Direct.HALT)
{
if (directE1 != (int)Direct.HALT)
destination2[3] = 1;
else if (directE2 != (int)Direct.HALT)
destination1[3] = 1;
else if (Math.Abs(floor3.Top-elevator1.Top) < Math.Abs(floor3.Top-elevator2.Top))
destination1[3] = 1;
else
destination2[3] = 1;
unhandled[1,3] = false;
}
}
if (unhandled[0,2]) //UpButton2
{
if ((floor2.Top > elevator1.Top && directE1 == (int)Direct.UP) || (floor2.Top > elevator2.Top && directE2 == (int)Direct.UP))
{
if (directE2 == (int)Direct.HALT)
destination2[2] = 1;
else if (directE2 == (int)Direct.HALT)
destination1[2] = 1;
else if (floor2.Top-elevator1.Top < floor2.Top-elevator2.Top)
destination1[2] = 1;
else
destination2[2] = 1;
unhandled[0,2] = false;
}
else if (directE1 == (int)Direct.HALT || directE2 == (int)Direct.HALT)
{
if (directE1 != (int)Direct.HALT)
destination2[2] = 1;
else if (directE2 != (int)Direct.HALT)
destination1[2] = 1;
else if (Math.Abs(floor2.Top-elevator1.Top) < Math.Abs(floor2.Top-elevator2.Top))
destination1[2] = 1;
else
destination2[2] = 1;
unhandled[0,2] = false;
}
}
if (unhandled[1,2]) //DownButton2
{
if ((floor2.Top < elevator1.Top && directE1 == (int)Direct.DOWN) || (floor2.Top < elevator2.Top && directE2 == (int)Direct.DOWN))
{
if (directE2 == (int)Direct.HALT)
destination2[2] = 1;
else if (directE2 == (int)Direct.HALT)
destination1[2] = 1;
else if (floor2.Top-elevator1.Top < floor2.Top-elevator2.Top)
destination2[2] = 1;
else
destination1[2] = 1;
unhandled[1,2] = false;
}
else if (directE1 == (int)Direct.HALT || directE2 == (int)Direct.HALT)
{
if (directE1 != (int)Direct.HALT)
destination2[2] = 1;
else if (directE2 != (int)Direct.HALT)
destination1[2] = 1;
else if (Math.Abs(floor2.Top-elevator1.Top) < Math.Abs(floor2.Top-elevator2.Top))
destination1[2] = 1;
else
destination2[2] = 1;
unhandled[1,2] = false;
}
}
if (unhandled[0,1]) //UpButton1
{
if ((floor1.Top < elevator1.Top && directE1 == (int)Direct.DOWN) || (floor1.Top < elevator2.Top && directE2 == (int)Direct.DOWN))
{
if (directE2 == (int)Direct.HALT)
destination2[1] = 1;
else if (directE2 == (int)Direct.HALT)
destination1[1] = 1;
else if (floor1.Top-elevator1.Top < floor1.Top-elevator2.Top)
destination2[1] = 1;
else
destination1[1] = 1;
unhandled[0,1] = false;
}
else if (directE1 == (int)Direct.HALT || directE2 == (int)Direct.HALT)
{
if (directE1 != (int)Direct.HALT)
destination2[1] = 1;
else if (directE2 != (int)Direct.HALT)
destination1[1] = 1;
else if (Math.Abs(floor2.Top-elevator1.Top) < Math.Abs(floor2.Top-elevator2.Top))
destination1[1] = 1;
else
destination2[1] = 1;
unhandled[0,1] = false;
}
}
}
private void touchDownE1 ()
{
//Elevator1 approaches one of the floors
if (directE1 != (int)Direct.HALT && suspendTime1 == 0)
{
if (elevator1.Top == floor1.Top && destination1[1] > 0)
{
inButton11.Enabled = true;
outButton12.Enabled = true;
elevatorButton12.Enabled = true;
elevatorButton13.Enabled = true;
elevatorButton14.Enabled = true;
upButton1.BackColor = System.Drawing.SystemColors.Control;
elevatorButton11.Enabled = false;
elevatorButton11.BackColor = System.Drawing.SystemColors.Control;
destination1[1]--;
suspendTime1 = 300;
directE1 = (int)Direct.HALT;
}
else if (elevator1.Top == floor2.Top && destination1[2] > 0)
{
if (downButton2.BackColor == System.Drawing.Color.Yellow || upButton2.BackColor==System.Drawing.Color.Yellow)
{
downButton2.BackColor = System.Drawing.SystemColors.Control;
upButton2.BackColor = System.Drawing.SystemColors.Control;
}
else
{
if (directE1 == (int)Direct.DOWN)
downButton2.BackColor = System.Drawing.SystemColors.Control;
else
upButton2.BackColor = System.Drawing.SystemColors.Control;
}
inButton21.Enabled = true;
outButton22.Enabled = true;
elevatorButton11.Enabled = true;
elevatorButton13.Enabled = true;
elevatorButton14.Enabled = true;
elevatorButton12.Enabled = false;
elevatorButton12.BackColor = System.Drawing.SystemColors.Control;
destination1[2]--;
suspendTime1 = 300;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -