-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.cs
271 lines (239 loc) · 9.35 KB
/
Bot.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
namespace BOT
{
class AI
{
public Robot Robot;
public Maze Maze;
public Robot2 Robot2;
public Robot3 Robot3;
public bool MakeStep()
{
//достигли выхода?
if (Maze[Robot.Location] == Cell.Exit) return false;
//получаем значение ячейки слева
var left2 = Maze[Robot.Location + Robot.Direction.Rotate(1)];
//если слева нет стены - поворачиваем налево
if (left2 != Cell.Wall)
Robot.Direction = Robot.Direction.Rotate(1);
else
//пока впереди есть стена - поворачиваем направо
while (Maze[Robot.Location + Robot.Direction] == Cell.Wall)
Robot.Direction = Robot.Direction.Rotate(-1);
//идем вперед
Robot.GoForward();
return true;
}
public bool MakeStep3()
{
//достигли выхода?
if (Maze[Robot3.Location] == Cell.Exit) return false;
//получаем значение ячейки слева
var righr3 = Maze[Robot3.Location + Robot3.Direction.Rotate(-1)];
//если слева нет стены - поворачиваем налево
if (righr3 != Cell.Wall)
Robot3.Direction = Robot3.Direction.Rotate(-1);
else
//пока впереди есть стена - поворачиваем направо
while (Maze[Robot3.Location + Robot3.Direction] == Cell.Wall)
Robot3.Direction = Robot3.Direction.Rotate(1);
//идем вперед
Robot3.GoForward();
return true;
}
public bool MakeStep2()
{
//достигли выхода?
if (Maze[Robot2.Location] == Cell.Exit) return false;
//получаем значение ячейки справа
var left = Maze[Robot2.Location + Robot2.Direction.Rotate(1)];
//получаем значение ячейки слева
var right = Maze[Robot2.Location + Robot2.Direction.Rotate(-1)];
var center = Maze[Robot2.Location + Robot2.Direction];
// //если справа нет стены - поворачиваем направо
// if (left != Cell.Wall)
// Robot2.Direction = Robot2.Direction.Rotate(1);
// else if (right != Cell.Wall)
// Robot2.Direction = Robot2.Direction.Rotate(-1);
// else
// //пока впереди есть стена - поворачиваем налево
//
//tdgnsdil;tkgn
Random rnd = new Random();
int value = rnd.Next(1, 3);
Console.WriteLine(value);
if (right == Cell.Wall && left == Cell.Wall && center == Cell.Wall)
{
Robot2.Direction = Robot2.Direction.Rotate(-1);
Robot2.Direction = Robot2.Direction.Rotate(-1);
}
else if (right != Cell.Wall && left != Cell.Wall)
{
if (value == 1)
Robot2.Direction = Robot2.Direction.Rotate(-1);
else
Robot2.Direction = Robot2.Direction.Rotate(1);
}
//dbjknjkgnjn;dgdl
else if (right != Cell.Wall && center != Cell.Wall)
{
if (value == 1)
Robot2.Direction = Robot2.Direction.Rotate(-1);
}
//jl;t;tnjktgnj;stotdnxjlbk
else if (center != Cell.Wall && left != Cell.Wall)
{
if (value == 1)
Robot2.Direction = Robot2.Direction.Rotate(1);
}
else if (right != Cell.Wall)
Robot2.Direction = Robot2.Direction.Rotate(-1);
else if (left != Cell.Wall)
Robot2.Direction = Robot2.Direction.Rotate(1);
// if (Maze[Robot2.Location + Robot2.Direction] != Cell.Wall)
// Robot2.GoForward();
// else if (right == Cell.Wall)
// Robot2.Direction = Robot2.Direction.Rotate(1);
// else if (left == Cell.Wall)
// Robot2.Direction = Robot2.Direction.Rotate(-1);
// else if (center == Cell.Wall)
// Robot2.Direction = Robot2.Direction.Rotate(-1);
Robot2.GoForward();
return true;
}
public bool MakeStep3()
{
Point current = maze[0, 0];
current.Visited = true;
Stack<Point> stack = new Stack<Point>();
do
{
int x = current.X;
int y = current.Y;
List<Point> unvisitedNeighbours = new List<Point>();
if (x > 0 && !maze[x - 1, y].Visited)
unvisitedNeighbours.Add(maze[x - 1, y]);
if (x < Width - 2 && !maze[x + 1, y].Visited)
unvisitedNeighbours.Add(maze[x + 1, y]);
if (y > 0 && !maze[x, y - 1].Visited)
unvisitedNeighbours.Add(maze[x, y - 1]);
if (y < Height - 2 && !maze[x, y + 1].Visited)
unvisitedNeighbours.Add(maze[x, y + 1]);
}
public void StartBot(AI ai)
{
var a = true;
var b = true;
var c = true;
do
{
DrawMaze(ai.Maze, ai.Robot, ai.Robot2, ai.Robot3);
Thread.Sleep(100);
a = ai.MakeStep();
b = ai.MakeStep2();
c = ai.MakeStep3();
} while (a || b || c);
}
public static void DrawMaze(Maze maze, Robot robot, Robot2 robot2, Robot3 robot3)
{
Console.Clear();
for (int row = 0; row < maze.Height; row++)
{
for (int col = 0; col < maze.Width; col++)
switch (maze[col, row])
{
case Cell.Wall: Console.Write('#'); break;
case Cell.Exit: Console.Write('E'); break;
default: Console.Write(' '); break;
}
Console.WriteLine();
}
Console.SetCursorPosition(robot.Location.X, robot.Location.Y);
Console.Write("1");
Console.SetCursorPosition(robot2.Location.X, robot2.Location.Y);
Console.Write("2");
Console.SetCursorPosition(robot3.Location.X, robot3.Location.Y);
Console.Write("3");
}
}
public class Point
{
public int X;
public int Y;
public bool Visited = false;
public Point Rotate(int angle)
{
if (angle > 0)
return new Point() { X = Y, Y = -X };
else
return new Point() { X = -Y, Y = X };
}
public static Point operator +(Point p1, Point p2)
{
return new Point {X = p1.X + p2.X, Y = p1.Y + p2.Y};
}
}
class Robot
{
public Point Location = new Point();
public Point Direction = new Point() { X = 1, Y = 0};
public void GoForward()
{
Location.X += Direction.X;
Location.Y += Direction.Y;
}
}
class Robot2
{
public Point Location = new Point();
public Point Direction = new Point() { X = 1, Y = 0};
public void GoForward()
{
Location.X += Direction.X;
Location.Y += Direction.Y;
}
}
class Robot3
{
public Point Location = new Point();
public Point Direction = new Point() { X = 1, Y = 0};
public void GoForward()
{
Location.X += Direction.X;
Location.Y += Direction.Y;
}
}
class Maze
{
private Cell[,] Items;
public static Maze Load(string filePath)
{
var lines = File.ReadAllLines(filePath);
var res = new Maze();
res.Items = new Cell[lines[0].Length, lines.Length];
for(int row =0;row<lines.Length;row++)
for(int col =0;col<lines[row].Length;col++)
res.Items[col, row] = (Cell)byte.Parse(lines[row][col].ToString());
return res;
}
public Cell this[int col, int row]
{
get { return Items[col, row]; }
}
public Cell this[Point p]
{
get { return Items[p.X, p.Y]; }
}
public int Height{ get { return Items.GetLength(1); } }
public int Width { get { return Items.GetLength(0); } }
}
enum Cell : byte
{
Empty = 0,
Wall = 1,
Exit = 2
}
}