-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomaton.cpp
175 lines (160 loc) · 3.14 KB
/
Automaton.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "Automaton.h"
Automaton::Automaton()
{
posx = 0;
posy = 0;
curState = 0;
orientation = 0;
initialOrientation = 0;
alive = true;
name = to_string((long)this);
color[0] = 1.0f;
color[1] = 1.0f;
color[2] = 1.0f;
silent = true;
}
Automaton::Automaton(int x, int y = 0, int s = 0, int o = 0)
{
posx = x;
posy = y;
curState = s;
orientation = o;
initialOrientation = o;
alive = true;
name = to_string((long)this);
color[0] = 1.0f;
color[1] = 1.0f;
color[2] = 1.0f;
silent = true;
}
Automaton::Automaton(const Automaton & a)
{
posx = a.posx;
posy = a.posy;
curState = a.curState;
orientation = a.initialOrientation;
alive = a.alive;
color[0] = a.color[0];
color[1] = a.color[1];
color[2] = a.color[2];
name = "undefined";
states = *(new vector<Statenode *>(a.states));
silent = a.silent;
}
Automaton::~Automaton()
{}
void Automaton::addState(int nsOff, int nsOn, int tOff, int tOn, bool wOff, bool wOn)
{
Statenode * sn = new Statenode(nsOff, nsOn, tOff, tOn, wOff, wOn);
states.push_back(sn);
}
int Automaton::shouldWrite(bool input)
{
return states[curState]->write[input];
}
bool Automaton::transition(bool input)
{
if(isDead())
{
return 1;
}
switch(states[curState]->turn[input])
{
case LEFT_TURN:
orientation = (orientation + 3) % 4;
break;
case RIGHT_TURN:
orientation = (orientation + 1) % 4;
break;
case U_TURN:
orientation = (orientation + 2) % 4;
break;
default:
orientation = (orientation ) % 4;
break;
}
switch(orientation)
{
case NORTH:
posy -= 1;
break;
case EAST:
posx += 1;
break;
case WEST:
posx -= 1;
break;
default:
posy += 1;
break;
}
if (!silent)
{
cout << posx << " " << posy << " " << curState << " ";
}
curState = states[curState]->next_state[input];
if (!silent)
{
cout << curState << " " << "NESW"[orientation] << endl;
}
return 1;
}
void Automaton::kill()
{
alive = false;
cout << name << " has died.\n";
}
void Automaton::raise()
{
alive = true;
cout << name << " is alive.\n";
}
bool Automaton::isDead()
{
return !alive;
}
void Automaton::setDirFromString(string orient)
{
if(orient == "N")
{
orientation = NORTH;
}
else if(orient == "S")
{
orientation = SOUTH;
}
else if(orient == "E")
{
orientation = EAST;
}
else if(orient == "W")
{
orientation = WEST;
}
}
int Automaton::getTurnFromString(string turn)
{
if(turn == "N")
{
return NO_TURN;
}
if(turn == "L")
{
return LEFT_TURN;
}
if(turn == "R")
{
return RIGHT_TURN;
}
if(turn == "U")
{
return U_TURN;
}
return -1;
}
void Automaton::setColor(float r, float g, float b)
{
color[0] = r;
color[1] = g;
color[2] = b;
}