forked from dotnet/dotnet-console-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
150 lines (144 loc) · 3.16 KB
/
Program.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
using System;
using System.Collections.Generic;
Exception? exception = null;
int speedInput;
string prompt = $"Select speed [1], [2] (default), or [3]: ";
string? input;
Console.Write(prompt);
while (!int.TryParse(input = Console.ReadLine(), out speedInput) || speedInput < 1 || 3 < speedInput)
{
if (string.IsNullOrWhiteSpace(input))
{
speedInput = 2;
break;
}
else
{
Console.WriteLine("Invalid Input. Try Again...");
Console.Write(prompt);
}
}
int[] velocities = [100, 70, 50];
int velocity = velocities[speedInput - 1];
char[] DirectionChars = ['^', 'v', '<', '>',];
TimeSpan sleep = TimeSpan.FromMilliseconds(velocity);
int width = Console.WindowWidth;
int height = Console.WindowHeight;
Tile[,] map = new Tile[width, height];
Direction? direction = null;
Queue<(int X, int Y)> snake = new();
(int X, int Y) = (width / 2, height / 2);
bool closeRequested = false;
try
{
Console.CursorVisible = false;
Console.Clear();
snake.Enqueue((X, Y));
map[X, Y] = Tile.Snake;
PositionFood();
Console.SetCursorPosition(X, Y);
Console.Write('@');
while (!direction.HasValue && !closeRequested)
{
GetDirection();
}
while (!closeRequested)
{
if (Console.WindowWidth != width || Console.WindowHeight != height)
{
Console.Clear();
Console.Write("Console was resized. Snake game has ended.");
return;
}
switch (direction)
{
case Direction.Up: Y--; break;
case Direction.Down: Y++; break;
case Direction.Left: X--; break;
case Direction.Right: X++; break;
}
if (X < 0 || X >= width ||
Y < 0 || Y >= height ||
map[X, Y] is Tile.Snake)
{
Console.Clear();
Console.Write("Game Over. Score: " + (snake.Count - 1) + ".");
return;
}
Console.SetCursorPosition(X, Y);
Console.Write(DirectionChars[(int)direction!]);
snake.Enqueue((X, Y));
if (map[X, Y] is Tile.Food)
{
PositionFood();
}
else
{
(int x, int y) = snake.Dequeue();
map[x, y] = Tile.Open;
Console.SetCursorPosition(x, y);
Console.Write(' ');
}
map[X, Y] = Tile.Snake;
if (Console.KeyAvailable)
{
GetDirection();
}
System.Threading.Thread.Sleep(sleep);
}
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
Console.CursorVisible = true;
Console.Clear();
Console.WriteLine(exception?.ToString() ?? "Snake was closed.");
}
void GetDirection()
// takes direction from arrow keys
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow: direction = Direction.Up; break;
case ConsoleKey.DownArrow: direction = Direction.Down; break;
case ConsoleKey.LeftArrow: direction = Direction.Left; break;
case ConsoleKey.RightArrow: direction = Direction.Right; break;
case ConsoleKey.Escape: closeRequested = true; break;
}
}
void PositionFood()
{
List<(int X, int Y)> possibleCoordinates = new();
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (map[i, j] is Tile.Open)
{
possibleCoordinates.Add((i, j));
}
}
}
int index = Random.Shared.Next(possibleCoordinates.Count);
(int X, int Y) = possibleCoordinates[index];
map[X, Y] = Tile.Food;
Console.SetCursorPosition(X, Y);
Console.Write('+');
}
enum Direction
{
Up = 0,
Down = 1,
Left = 2,
Right = 3,
}
enum Tile
{
Open = 0,
Snake,
Food,
}