-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cs
More file actions
505 lines (425 loc) · 14.9 KB
/
Copy pathSnake.cs
File metadata and controls
505 lines (425 loc) · 14.9 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SnakeGame
{
class Program
{
static void Main(string[] args)
{
var app = new SnakeGameApp();
app.Run();
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine("\nPress <Enter> to continue...");
Console.ReadLine();
}
}
}
public class SnakeGameApp
{
private readonly int delayBetweenGames = 1500;
public void Run()
{
var finished = false;
while (!finished)
{
var game = new Game();
game.Play();
finished = game.Quit;
if (game.Lost)
{
Console.WriteLine("\nYou lost.");
Thread.Sleep(delayBetweenGames);
Console.Clear();
}
}
}
}
public class Game
{
private readonly eHeading initialHeading = eHeading.Up;
private DirectionMap directionMap = Board.DirectionMap;
private Board board;
public bool Lost { get; private set; } = false;
public bool Quit { get; private set; } = false;
public Game(int snakeLength = 5)
{
board = new Board();
var head = board.Center;
board.Add(new Snake(head, initBody(head, snakeLength - 1), directionMap.Get(initialHeading)));
board.AddFood(); //to avoid the snake overwriting food, add food after snake
board.Draw();
}
public void Play()
{
while (!Lost && !Quit)
{
ConsoleKeyInfo input;
if (Console.KeyAvailable)
{
input = Console.ReadKey(true); //intercept = true (don't print char on console)
//If the directionMap doesn't find a direction for the character
//the user pressed it throws an exception.
//Wrapping this in a try/catch allows us to ignore all non-direction keys
//except Escape, which quits the game.
try
{
var direction = directionMap.Get(input.KeyChar);
board.DoTurn(direction);
Lost = board.HasCollided;
}
catch
{
Quit = input.Key == ConsoleKey.Escape;
}
}
else
{
board.DoTurn();
Lost = board.HasCollided;
}
}
}
private LinkedList<Cell> initBody(Cell head, int bodyLength)
{
var body = new LinkedList<Cell>();
var current = board.BottomNeighbor(head);
for (var i = 0; i < bodyLength; i++)
{
body.AddLast(current);
current.SetBody(bodyLength - i);
current = board.BottomNeighbor(current);
}
return body;
}
}
public class Board
{
public static DirectionMap DirectionMap { get; } = new DirectionMap();
private readonly string instructions = "How to Play: Avoid hitting walls or yourself. Grow by eating food (%). Highest length wins.";
private readonly string commandBase = "Commands: {0}, Esc: Quit\n";
private readonly string lengthBase = "Length: {0}\n";
private Cell[,] grid;
private Random random = new Random();
private int leftEdgeX => 0;
private int rightEdgeX => Width - 1;
private int topEdgeY => 0;
private int bottomEdgeY => Height - 1;
public Snake Snake { get; private set; }
public int Height { get; private set; }
public int Width { get; private set; }
public Cell Current => Snake.Head;
public Cell Center => get(Width / 2, Height / 2);
public Cell Food { get; private set; }
public bool HasCollided { get; private set; }
public Board(int width = 90, int height = 25)
{
Width = width;
Height = height;
grid = new Cell[Width, Height];
initGrid();
}
//continue in the same direction
public void DoTurn()
{
doTurn(Snake.Direction, getDestination(Snake.Direction));
}
public void DoTurn(Direction direction)
{
doTurn(direction, getDestination(direction));
}
public void Draw()
{
Console.SetCursorPosition(0, 0);
Console.WriteLine(ToString());
}
public void Add(Snake snake) => Snake = snake;
public void AddFood() => randomCell().SetFood();
public Cell TopNeighbor(Cell cell) => grid[cell.X, cell.Y - 1];
public Cell RightNeighbor(Cell cell) => grid[cell.X + 1, cell.Y];
public Cell BottomNeighbor(Cell cell) => grid[cell.X, cell.Y + 1];
public Cell LeftNeighbor(Cell cell) => grid[cell.X - 1, cell.Y];
public override string ToString()
{
var sb = new StringBuilder();
//y is in outer loop, so we draw by rows
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
sb.Append(grid[x, y].Value);
}
sb.Append("\n"); //terminate row
}
sb.AppendFormat(lengthBase, Snake.Length);
sb.AppendLine(instructions);
sb.AppendFormat(commandBase, DirectionMap.ToString());
return sb.ToString();
}
private Cell get(int x, int y) => grid[x, y];
private void add(Cell cell) => grid[cell.X, cell.Y] = cell;
private bool isBorder(Cell cell) => cell.X == leftEdgeX || cell.X >= rightEdgeX
|| cell.Y == topEdgeY || cell.Y >= bottomEdgeY;
private void doTurn(Direction direction, Cell target)
{
if (isLegalMove(direction, target))
{
Snake.Move(direction, target);
if (Snake.HasEaten)
{
Snake.Grow(getNewTail());
AddFood();
}
Draw();
}
}
private bool isLegalMove(Direction direction, Cell target)
{
if (direction.IsOpposite(Snake.Direction))
{
return false;
}
HasCollided = target.IsForbidden;
return !HasCollided;
}
private Cell getDestination(Direction direction) => getDirectionalNeighbor(Snake.Head, direction);
private Cell getNewTail() => getDirectionalNeighbor(Snake.Tail, Snake.Direction.Opposite);
private Cell getDirectionalNeighbor(Cell cell, Direction direction)
{
var neighbor = new Cell(-1, -1); //initialize to dummy cell
if (direction.IsUp)
{
neighbor = TopNeighbor(cell);
}
else if (direction.IsRight)
{
neighbor = RightNeighbor(cell);
}
else if (direction.IsDown)
{
neighbor = BottomNeighbor(cell);
}
else if (direction.IsLeft)
{
neighbor = LeftNeighbor(cell);
}
return neighbor;
}
private Cell randomCell()
{
bool isEmpty;
var cell = new Cell(-1, -1); //initialize to dummy cell
do
{
cell = grid[random.Next(Width), random.Next(Height)];
isEmpty = cell.IsEmpty;
} while (!isEmpty);
return cell;
}
private void initGrid()
{
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
var cell = new Cell(x, y);
add(cell);
if (isBorder(cell))
{
cell.SetBorder();
}
else
{
cell.SetEmpty();
}
}
}
}
}
//int values are degrees
public enum eHeading
{
Up = 0,
Right = 90,
Down = 180,
Left = 270
}
public class Direction
{
public eHeading Heading { get; private set; }
public char KeyPress { get; private set; }
public char HeadToken { get; private set; }
public int Degrees => (int)Heading;
public Direction Opposite => Board.DirectionMap.Get((eHeading)(Degrees >= 180 ? Degrees - 180 : Degrees + 180));
public bool IsUp => Heading == eHeading.Up;
public bool IsRight => Heading == eHeading.Right;
public bool IsDown => Heading == eHeading.Down;
public bool IsLeft => Heading == eHeading.Left;
public Direction(eHeading vector, char keyPress, char headToken)
{
Heading = vector;
KeyPress = keyPress;
HeadToken = headToken;
}
public bool IsOpposite(Direction dir) => Math.Abs(Degrees - dir.Degrees) == 180;
public bool IsSame(Direction dir) => Heading == dir.Heading;
public string ToCommand() => $"{KeyPress}: {Heading}";
}
public class DirectionMap
{
private Dictionary<char, Direction> _directionKeys;
private Dictionary<char, Direction> directionKeys
{
get
{
_directionKeys = _directionKeys ?? directionKeyMap();
return _directionKeys;
}
}
private Dictionary<eHeading, Direction> _directionVectors;
private Dictionary<eHeading, Direction> directionVectors
{
get
{
_directionVectors = _directionVectors ?? directionVectorMap();
return _directionVectors;
}
}
public Direction Get(char c)
{
if (directionKeys.TryGetValue(c, out Direction direction))
{
return direction;
}
else
{
throw new Exception($"{c} not found in direction map.");
}
}
public Direction Get(eHeading vector)
{
if (directionVectors.TryGetValue(vector, out Direction direction))
{
return direction;
}
else
{
throw new Exception($"Vector {vector.ToString()} not found in direction map.");
}
}
public override string ToString() => string.Join(", ", directionVectors.Select(v => v.Value.ToCommand()));
private Dictionary<eHeading, Direction> directionVectorMap()
{
return new Dictionary<eHeading, Direction>
{
{eHeading.Left, new Direction(eHeading.Left , 'a', '■')},
{eHeading.Right, new Direction(eHeading.Right,'d', '■') },
{eHeading.Down, new Direction(eHeading.Down, 's', '■') },
{eHeading.Up, new Direction(eHeading.Up, 'w', '■') }
};
}
private Dictionary<char, Direction> directionKeyMap() => directionVectors.ToDictionary(d => d.Value.KeyPress, d => d.Value);
}
public class Cell
{
private readonly char unitializedToken = char.MinValue;
private readonly char emptyToken = ' ';
private readonly char borderToken = '*';
private readonly char bodyToken = '■';
private readonly char foodToken = '⧇';
private int remaining;
public int X { get; private set; }
public int Y { get; private set; }
public char Value { get; private set; }
public bool IsBorder => Value == borderToken;
public bool IsBody => Value == bodyToken;
public bool IsFood => Value == foodToken;
public bool IsEmpty => Value == emptyToken || Value == unitializedToken;
public bool IsForbidden => IsBorder || IsBody;
public Cell(int x, int y)
{
X = x;
Y = y;
}
public void SetEmpty() => Update(emptyToken);
public void SetHead(char headToken)
{
Update(headToken);
}
public void SetBody(int length)
{
Update(bodyToken);
remaining = length;
}
public void SetBorder() => Update(borderToken);
public void SetFood() => Update(foodToken);
public void Update(char newVal) => Value = newVal;
public void Decay()
{
if (--remaining == 0)
{
SetEmpty();
}
}
public override string ToString() => $"{X}, {Y}";
}
public class Snake
{
private readonly int maxSpeed = 5;
private readonly int delayMultiplier = 500;
public Cell Head { get; private set; }
public LinkedList<Cell> Body { get; private set; }
public Cell Tail => Body.Last();
public Direction Direction { get; private set; }
public int Length => BodyLength + 1;
public int BodyLength => Body.Count; // full length includes head
public int Speed { get; private set; }
public bool HasEaten { get; private set; }
public Snake(Cell head, LinkedList<Cell> body, Direction initialHeading, int speed = 1)
{
Head = head;
Body = body;
Speed = Math.Min(speed, maxSpeed);
Direction = initialHeading;
Head.SetHead(Direction.HeadToken);
}
public void Move(Direction direction, Cell nextHead)
{
var originalHead = Head;
Direction = direction;
HasEaten = false; //reset to false on each turn
//be sure to eat before resetting the head cell to an arrow
if (nextHead.IsFood)
{
Eat();
}
Head = nextHead;
Head.SetHead(direction.HeadToken);
moveBody(originalHead);
pause(); //controls speed of play
}
public void Eat()
{
HasEaten = true;
}
public void Grow(Cell newTail)
{
newTail.SetBody(1);
Body.AddLast(newTail);
}
private void moveBody(Cell originalHead)
{
foreach (var cell in Body)
{
cell.Decay(); //handles clearing
}
Body.AddFirst(originalHead);
originalHead.SetBody(BodyLength - 1);
Body.RemoveLast();
}
private void pause() => Thread.Sleep(maxSpeed - Speed + 1 * delayMultiplier);
}
}