forked from ROALOCH/cucei-filosofos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhilosopher.cs
113 lines (99 loc) · 3.83 KB
/
Philosopher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Filosofos
{
class Philosopher
{
int id = 0;
int meals = 0;
char state = 'W'; // W = Waiting, E = Eating
int forkOne, forkTwo;
Semaphore[] forks;
// UI //
Label box = null;
ListBox listBox = null;
Image forkLeftImg = null, forkRightImg = null;
PictureBox forkLeft = null, forkRight = null;
Color waitingColor = Color.FromName("SkyBlue");
Color eatingColor = Color.FromName("LimeGreen");
public Philosopher(int id, int forkOne, int forkTwo, PictureBox forkLeft, PictureBox forkRight, Label box, Semaphore[] forks, ListBox listBox)
{
this.id = id;
this.forkOne = forkOne;
this.forkTwo = forkTwo;
this.forkLeft = forkLeft;
this.forkRight = forkRight;
this.box = box;
this.forks = forks;
this.listBox = listBox;
changeForksStateUI(false);
changePhilosopherBoxStateUI(waitingColor);
}
public int getID() => id;
public int getMeals() => meals;
public void addMeal()
{
meals = meals + 1;
}
public char getState() => state;
public void setState(char state)
{
this.state = state;
}
// UI //
public void changeState(char state)
{
// W = Waiting, E = Eating
switch (state)
{
case 'W':
changeForksStateUI(false);
changePhilosopherBoxStateUI(waitingColor);
break;
case 'E':
changeForksStateUI(true);
changePhilosopherBoxStateUI(eatingColor);
break;
default:
break;
}
}
private void changeForksStateUI(bool active)
{
switch (id)
{
case 0:
forkLeftImg = active ? Filosofos.Properties.Resources.Fork4A : Filosofos.Properties.Resources.Fork4;
forkRightImg = active ? Filosofos.Properties.Resources.Fork0A : Filosofos.Properties.Resources.Fork0;
break;
case 1:
forkLeftImg = active ? Filosofos.Properties.Resources.Fork0A : Filosofos.Properties.Resources.Fork0;
forkRightImg = active ? Filosofos.Properties.Resources.Fork1A : Filosofos.Properties.Resources.Fork1;
break;
case 2:
forkLeftImg = active ? Filosofos.Properties.Resources.Fork1A : Filosofos.Properties.Resources.Fork1;
forkRightImg = active ? Filosofos.Properties.Resources.Fork2A : Filosofos.Properties.Resources.Fork2;
break;
case 3:
forkLeftImg = active ? Filosofos.Properties.Resources.Fork2A : Filosofos.Properties.Resources.Fork2;
forkRightImg = active ? Filosofos.Properties.Resources.Fork3A : Filosofos.Properties.Resources.Fork3;
break;
case 4:
forkLeftImg = active ? Filosofos.Properties.Resources.Fork3A : Filosofos.Properties.Resources.Fork3;
forkRightImg = active ? Filosofos.Properties.Resources.Fork4A : Filosofos.Properties.Resources.Fork4;
break;
}
this.forkLeft.Image = forkLeftImg;
this.forkRight.Image = forkRightImg;
}
private void changePhilosopherBoxStateUI(Color color)
{
box.ForeColor = color;
}
public void updateMeals()
{
listBox.Items[id] = ($"Filósofo {id + 1} Comidas: {meals}");
}
}
}