From 952b95294fbc16e9542a75d653b5d1c6468d5696 Mon Sep 17 00:00:00 2001 From: Alex JL Date: Thu, 5 Oct 2023 12:38:14 +0200 Subject: [PATCH 1/6] added level selector sleep TimeSpan via velocity integer changeable depending of the level selector --- Projects/Snake/Program.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Projects/Snake/Program.cs b/Projects/Snake/Program.cs index 80b37ae8..561d9f5b 100644 --- a/Projects/Snake/Program.cs +++ b/Projects/Snake/Program.cs @@ -2,9 +2,17 @@ using System.Collections.Generic; Exception? exception = null; +int level=default; +do{ + // level selector + Console.Write("Select Level (1,2,3): "); + level=Convert.ToInt32(Console.ReadLine()); +}while(level!=1 && level!=2 && level!=3 ); +int[] velocities = { 100, 70, 50 }; +int velocity=velocities[level-1]; char[] DirectionChars = { '^', 'v', '<', '>', }; -TimeSpan sleep = TimeSpan.FromMilliseconds(70); +TimeSpan sleep = TimeSpan.FromMilliseconds(velocity); //snake's velocity int width = Console.WindowWidth; int height = Console.WindowHeight; Random random = new(); @@ -16,7 +24,7 @@ try { - Console.CursorVisible = false; + Console.CursorVisible = false; //clean cursor direction Console.Clear(); snake.Enqueue((X, Y)); map[X, Y] = Tile.Snake; @@ -69,7 +77,7 @@ { GetDirection(); } - System.Threading.Thread.Sleep(sleep); + System.Threading.Thread.Sleep(velocity); } } catch (Exception e) @@ -85,6 +93,7 @@ } void GetDirection() +// takes direction from arrow keys { switch (Console.ReadKey(true).Key) { From a0b4bcc197b9a1e2a16e19d4c9851a49d6917a2c Mon Sep 17 00:00:00 2001 From: Alex JL Date: Thu, 5 Oct 2023 17:26:19 +0200 Subject: [PATCH 2/6] check level entry from console conditional to prevent non valid number and catch to prevent other imputs --- Projects/Snake/Program.cs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/Projects/Snake/Program.cs b/Projects/Snake/Program.cs index 561d9f5b..058a63d9 100644 --- a/Projects/Snake/Program.cs +++ b/Projects/Snake/Program.cs @@ -2,15 +2,31 @@ using System.Collections.Generic; Exception? exception = null; -int level=default; -do{ +int level = default; +do +{ // level selector Console.Write("Select Level (1,2,3): "); - level=Convert.ToInt32(Console.ReadLine()); -}while(level!=1 && level!=2 && level!=3 ); + try + { + level = Convert.ToInt32(Console.ReadLine()); + if (level != 1 && level != 2 && level != 3) + { + Console.WriteLine("Número no válido"); + } + } + catch (FormatException) + { + Console.WriteLine("Error: La entrada no es un número válido."); + } + catch (Exception ex) + { + Console.WriteLine("Ocurrió un error inesperado: " + ex.Message); + } +} while (level != 1 && level != 2 && level != 3); int[] velocities = { 100, 70, 50 }; -int velocity=velocities[level-1]; +int velocity = velocities[level - 1]; char[] DirectionChars = { '^', 'v', '<', '>', }; TimeSpan sleep = TimeSpan.FromMilliseconds(velocity); //snake's velocity int width = Console.WindowWidth; @@ -97,11 +113,11 @@ void GetDirection() { 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.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; + case ConsoleKey.Escape: closeRequested = true; break; } } From 6608f2a3e542279aa3f2dab033868f6dac890454 Mon Sep 17 00:00:00 2001 From: Alex JL Date: Tue, 17 Oct 2023 10:46:31 +0200 Subject: [PATCH 3/6] translated texts --- Projects/Snake/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Projects/Snake/Program.cs b/Projects/Snake/Program.cs index 058a63d9..ad10537d 100644 --- a/Projects/Snake/Program.cs +++ b/Projects/Snake/Program.cs @@ -12,16 +12,16 @@ level = Convert.ToInt32(Console.ReadLine()); if (level != 1 && level != 2 && level != 3) { - Console.WriteLine("Número no válido"); + Console.WriteLine("Not valid number"); } } catch (FormatException) { - Console.WriteLine("Error: La entrada no es un número válido."); + Console.WriteLine("Error: imput is not a valid number."); } catch (Exception ex) { - Console.WriteLine("Ocurrió un error inesperado: " + ex.Message); + Console.WriteLine("An unexpected error occurred: " + ex.Message); } } while (level != 1 && level != 2 && level != 3); From 912ab5dc457544b922503fa0574c7dd5ebd4cfa8 Mon Sep 17 00:00:00 2001 From: Alex JL Date: Tue, 17 Oct 2023 10:53:08 +0200 Subject: [PATCH 4/6] int.TryParse instead of Convert.ToInt32 --- Projects/Snake/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Projects/Snake/Program.cs b/Projects/Snake/Program.cs index ad10537d..cf9ece44 100644 --- a/Projects/Snake/Program.cs +++ b/Projects/Snake/Program.cs @@ -9,7 +9,7 @@ Console.Write("Select Level (1,2,3): "); try { - level = Convert.ToInt32(Console.ReadLine()); + int.TryParse(Console.ReadLine(), out level); if (level != 1 && level != 2 && level != 3) { Console.WriteLine("Not valid number"); From 30330f2a0d71639ee9badda09a2917496622c6fb Mon Sep 17 00:00:00 2001 From: Alex JL Date: Tue, 17 Oct 2023 11:05:29 +0200 Subject: [PATCH 5/6] New_Snake on website --- Projects/Website/Games/Snake/Snake.cs | 37 ++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/Projects/Website/Games/Snake/Snake.cs b/Projects/Website/Games/Snake/Snake.cs index 2b8403db..d1aea484 100644 --- a/Projects/Website/Games/Snake/Snake.cs +++ b/Projects/Website/Games/Snake/Snake.cs @@ -11,9 +11,33 @@ public class Snake public async Task Run() { Exception? exception = null; + int level = default; + do + { + // level selector + await Console.Write("Select Level (1,2,3): "); + try + { + int.TryParse(await Console.ReadLine(), out level); + if (level != 1 && level != 2 && level != 3) + { + await Console.WriteLine("Not valid number"); + } + } + catch (FormatException) + { + await Console.WriteLine("Error: imput is not a valid number."); + } + catch (Exception ex) + { + await Console.WriteLine("An unexpected error occurred: " + ex.Message); + } + } while (level != 1 && level != 2 && level != 3); + int[] velocities = { 100, 70, 50 }; + int velocity = velocities[level - 1]; char[] DirectionChars = { '^', 'v', '<', '>', }; - TimeSpan sleep = TimeSpan.FromMilliseconds(35); + TimeSpan sleep = TimeSpan.FromMilliseconds(velocity); //snake's velocity int width = Console.WindowWidth; int height = Console.WindowHeight; Random random = new(); @@ -25,7 +49,7 @@ public async Task Run() try { - Console.CursorVisible = false; + Console.CursorVisible = false; //clean cursor direction await Console.Clear(); snake.Enqueue((X, Y)); map[X, Y] = Tile.Snake; @@ -100,11 +124,11 @@ async Task GetDirection() { switch ((await 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.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; + case ConsoleKey.Escape: closeRequested = true; break; } } @@ -143,4 +167,5 @@ enum Tile Snake, Food, } + } From 80e0ce808b241ff8cfb37c0a6f93d5b0b5f572c9 Mon Sep 17 00:00:00 2001 From: ZacharyPatten Date: Tue, 17 Oct 2023 10:38:02 -0500 Subject: [PATCH 6/6] clean up --- Projects/Snake/Program.cs | 40 ++++++++++++--------------- Projects/Website/Games/Snake/Snake.cs | 40 ++++++++++++--------------- 2 files changed, 34 insertions(+), 46 deletions(-) diff --git a/Projects/Snake/Program.cs b/Projects/Snake/Program.cs index cf9ece44..63fecca1 100644 --- a/Projects/Snake/Program.cs +++ b/Projects/Snake/Program.cs @@ -2,33 +2,27 @@ using System.Collections.Generic; Exception? exception = null; -int level = default; -do +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) { - // level selector - Console.Write("Select Level (1,2,3): "); - try + if (string.IsNullOrWhiteSpace(input)) { - int.TryParse(Console.ReadLine(), out level); - if (level != 1 && level != 2 && level != 3) - { - Console.WriteLine("Not valid number"); - } - } - catch (FormatException) - { - Console.WriteLine("Error: imput is not a valid number."); + speedInput = 2; + break; } - catch (Exception ex) + else { - Console.WriteLine("An unexpected error occurred: " + ex.Message); + Console.WriteLine("Invalid Input. Try Again..."); + Console.Write(prompt); } -} while (level != 1 && level != 2 && level != 3); - +} int[] velocities = { 100, 70, 50 }; -int velocity = velocities[level - 1]; +int velocity = velocities[speedInput - 1]; char[] DirectionChars = { '^', 'v', '<', '>', }; -TimeSpan sleep = TimeSpan.FromMilliseconds(velocity); //snake's velocity +TimeSpan sleep = TimeSpan.FromMilliseconds(velocity); int width = Console.WindowWidth; int height = Console.WindowHeight; Random random = new(); @@ -40,7 +34,7 @@ try { - Console.CursorVisible = false; //clean cursor direction + Console.CursorVisible = false; Console.Clear(); snake.Enqueue((X, Y)); map[X, Y] = Tile.Snake; @@ -77,7 +71,7 @@ Console.SetCursorPosition(X, Y); Console.Write(DirectionChars[(int)direction!]); snake.Enqueue((X, Y)); - if (map[X, Y] == Tile.Food) + if (map[X, Y] is Tile.Food) { PositionFood(); } @@ -93,7 +87,7 @@ { GetDirection(); } - System.Threading.Thread.Sleep(velocity); + System.Threading.Thread.Sleep(sleep); } } catch (Exception e) diff --git a/Projects/Website/Games/Snake/Snake.cs b/Projects/Website/Games/Snake/Snake.cs index d1aea484..6276e6da 100644 --- a/Projects/Website/Games/Snake/Snake.cs +++ b/Projects/Website/Games/Snake/Snake.cs @@ -11,33 +11,27 @@ public class Snake public async Task Run() { Exception? exception = null; - int level = default; - do + int speedInput; + string prompt = $"Select speed [1], [2] (default), or [3]: "; + string? input; + await Console.Write(prompt); + while (!int.TryParse(input = await Console.ReadLine(), out speedInput) || speedInput < 1 || 3 < speedInput) { - // level selector - await Console.Write("Select Level (1,2,3): "); - try + if (string.IsNullOrWhiteSpace(input)) { - int.TryParse(await Console.ReadLine(), out level); - if (level != 1 && level != 2 && level != 3) - { - await Console.WriteLine("Not valid number"); - } - } - catch (FormatException) - { - await Console.WriteLine("Error: imput is not a valid number."); + speedInput = 2; + break; } - catch (Exception ex) + else { - await Console.WriteLine("An unexpected error occurred: " + ex.Message); + await Console.WriteLine("Invalid Input. Try Again..."); + await Console.Write(prompt); } - } while (level != 1 && level != 2 && level != 3); - - int[] velocities = { 100, 70, 50 }; - int velocity = velocities[level - 1]; + } + int[] velocities = { 50, 35, 20 }; + int velocity = velocities[speedInput - 1]; char[] DirectionChars = { '^', 'v', '<', '>', }; - TimeSpan sleep = TimeSpan.FromMilliseconds(velocity); //snake's velocity + TimeSpan sleep = TimeSpan.FromMilliseconds(velocity); int width = Console.WindowWidth; int height = Console.WindowHeight; Random random = new(); @@ -49,7 +43,7 @@ public async Task Run() try { - Console.CursorVisible = false; //clean cursor direction + Console.CursorVisible = false; await Console.Clear(); snake.Enqueue((X, Y)); map[X, Y] = Tile.Snake; @@ -88,7 +82,7 @@ public async Task Run() await Console.SetCursorPosition(X, Y); await Console.Write(DirectionChars[(int)direction!]); snake.Enqueue((X, Y)); - if (map[X, Y] == Tile.Food) + if (map[X, Y] is Tile.Food) { await PositionFood(); }