-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffector.cs
More file actions
130 lines (105 loc) · 5.37 KB
/
Effector.cs
File metadata and controls
130 lines (105 loc) · 5.37 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Collections;
using System.Threading;
class Effector
{
public void DoAction(ArrayList path, ArrayList cellsToCollect, ArrayList cellsToSuck, Environment ev, int numberOfActions, int millisecondsToWait){
foreach(int [] cellInPath in path){
//Console.WriteLine("cell in path : " + cellInPath[0] + cellInPath[1]);
if(numberOfActions!=0){
int [] robotCell = new int [2] {ev.GetRobotXPosition(),ev.GetRobotYPosition()};
//Move the robot
if(isTheCellAToTheLeftOfCellB(robotCell,cellInPath)){
Console.WriteLine("Effector : Robot, go RIGHT!");
ev.GetMap().Right();
ev.SetNumberOfActions(ev.GetNumberOfActions()+1);
numberOfActions--;
if(numberOfActions==0){
return;
}
}
else if(isTheCellAToTheRightOfCellB(robotCell,cellInPath)){
Console.WriteLine("Effector : Robot, go LEFT!");
ev.GetMap().Left();
ev.SetNumberOfActions(ev.GetNumberOfActions()+1);
numberOfActions--;
if(numberOfActions==0){
return;
}
}
else if(isTheCellAAboveCellB(robotCell,cellInPath)){
Console.WriteLine("Effector : Robot, go DOWN!");
ev.GetMap().Down();
ev.SetNumberOfActions(ev.GetNumberOfActions()+1);
numberOfActions--;
if(numberOfActions==0){
return;
}
}
else if(isTheCellABelowCellB(robotCell,cellInPath)){
Console.WriteLine("Effector : Robot, go UP!");
ev.GetMap().Up();
ev.SetNumberOfActions(ev.GetNumberOfActions()+1);
numberOfActions--;
if(numberOfActions==0){
return;
}
}
//ev.GetMap().DisplayMap();
//verification with cells to COLLECT
foreach(int [] cellToCollect in cellsToCollect){
if(cellToCollect[0] == cellInPath[0] && cellToCollect[1] == cellInPath[1]){
Console.WriteLine("Effector : Robot, COLLECT cell (" + cellInPath[0] + "," + cellInPath[1] + ") !");
int result = ev.GetMap().Collect(cellInPath[0],cellInPath[1]);
ev.SetNumberOfActions(ev.GetNumberOfActions()+1);
if(result<0){
ev.SetPenalty(ev.GetPenalty()+1); //fail => increase penalty
}
else{
ev.SetPenalty(ev.GetPenalty()-2); //suceed => decrease penalty
}
//ev.GetMap().DisplayMap();
cellToCollect[0]=-1;
cellToCollect[1]=-1;
numberOfActions--;
if(numberOfActions==0){
return;
}
}
}
//verification with cells to SUCK
foreach(int [] cellToSuck in cellsToSuck){
if(cellToSuck[0] == cellInPath[0] && cellToSuck[1] == cellInPath[1]){
Console.WriteLine("Effector : Robot, SUCK cell (" + cellInPath[0] + "," + cellInPath[1] + ") !");
int result = ev.GetMap().Suck(cellInPath[0],cellInPath[1]);
ev.SetNumberOfActions(ev.GetNumberOfActions()+1);
if(result<0){
ev.SetPenalty(ev.GetPenalty()+ Math.Abs(result)); //fail => increase penalty
}
else{
ev.SetPenalty(ev.GetPenalty()-2); //suceed => decrease penalty
}
ev.GetMap().DisplayMap();
cellToSuck[0]=-1;
cellToSuck[1]=-1;
if(numberOfActions==0){
return;
}
}
}
Thread.Sleep(millisecondsToWait);
}
}
}
public bool isTheCellAToTheLeftOfCellB(int [] cellA, int [] cellB){
return cellA[1] == (cellB[1]-1);
}
public bool isTheCellAToTheRightOfCellB(int [] cellA, int [] cellB){
return cellA[1] == (cellB[1]+1);
}
public bool isTheCellAAboveCellB(int [] cellA, int [] cellB){
return cellA[0] == (cellB[0]-1);
}
public bool isTheCellABelowCellB(int [] cellA, int [] cellB){
return cellA[0] == (cellB[0]+1);
}
}