-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sensor.cs
158 lines (143 loc) · 4.59 KB
/
Sensor.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//Author: Anthony Caballero
//Milkowski
//CPSC 3200
//Due: 5/8/2020
//Implementation of the sensor class. Allows for multiple sensors to created
//or just one sensor to navigate the grid.
//Interface Invariants:
//Parameters are either protected or private and cannot be accessed. If there is
//an class that is using Sensor, it will be able to have access through inheritence.
using System;
using System.Threading;
using static p3.Robot;
namespace p3
{
public class Sensor
{
private const uint DEFAULT_CAPACITY = 1000;
private const int TWO_SEC_IN_MS = 2000;
private const uint THRESHOLD = 100;
private const uint row = 0;
private const uint column = 1;
private const uint VALID = 1;
private const uint MOVE = 1;
private bool state;
private uint battery;
private uint discharge;
protected SensorDirection sdir;
public enum SensorDirection
{
front,
back,
left,
right
}
//Constructor
public Sensor()
{
battery = DEFAULT_CAPACITY;
Random rnd = new Random();
discharge = 1;//(uint)rnd.Next(1,100);
state = true;
sdir = 0;
}
public Sensor(int dir)
{
battery = DEFAULT_CAPACITY;
Random rnd = new Random();
discharge = (uint)rnd.Next(1, 100);
state = true;
sdir = (SensorDirection)dir;
}
public Sensor(Sensor prevSensor)
{
battery = prevSensor.battery;
discharge = prevSensor.discharge;
state = prevSensor.state;
}
//Pre: input the current position, the working grid and current robot
//Post: updated validation if there is a valid place to move next.
public bool isValid(int[] pos, int[,]grid, Robot r)
{
chargeBattery();
int curr_row = pos[row];
int curr_column = pos[column];
if (curr_row - 1 < 0 || curr_column -1 < 0)
return false;
if (state && battery <= THRESHOLD)
{
state = false;
return state;
}
else if (state && battery > THRESHOLD)
{
if (sdir == (SensorDirection)0)
{
if (grid[curr_row - MOVE, curr_column] != VALID)
return false;
//sdir = (SensorDirection)2;
//if (direction == robotDirection.Forward)
else
{
battery -= discharge;
r.changeDirection(robotDirection.Forward);
return state;
}
}
if (sdir == (SensorDirection)2)
{
if (grid[curr_row, curr_column - MOVE] != VALID)
return false;
//sdir = (SensorDirection)3;
else
{
battery -= discharge;
r.changeDirection(robotDirection.Left);
return state;
}
}
if (sdir == (SensorDirection)3)
{
if (grid[curr_row, curr_column + MOVE] != VALID)
return false;
//sdir = (SensorDirection)4;
{
battery -= discharge;
r.changeDirection(robotDirection.Right);
return state;
}
}
else if (grid[curr_row + MOVE, curr_column] == VALID)
{
battery -= discharge;
r.changeDirection(robotDirection.Backward);
return state;
}
else
return !state;
}
else
{
state = false;
return state;
}
//return false;
}
//Pre: next valid direction to move in the grid
//Post: updated direction for the sensor to move with the robot.
public void changeDirection(robotDirection newDir)
{
sdir = (SensorDirection)newDir;
}
public uint chargeBattery()
{
//Thread.Sleep(TWO_SEC_IN_MS);
state = true;
return battery = DEFAULT_CAPACITY;
}
public bool getState()
{
return state;
}
}
}