diff --git a/README.md b/README.md index edb2386..8bb459c 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ - [x] Yanlış tahminde limit - [x] İnsan figürü - [x] [Animasyon gecikmeleri](work/hangman/animasyon-gecikmeleri.md) +- [x] Oyun sonu seçenekleri - [ ] Ana Menü - [x] [Başlangıç Menüsü](work/hangman/ana-menu.md) - [ ] [Ayarlar Menüsü](work/hangman/ayarlar-menusu.md) diff --git a/src/learn-console/Learn.Hangman.Test/EndMenuTest.cs b/src/learn-console/Learn.Hangman.Test/EndMenuTest.cs new file mode 100644 index 0000000..e773dd0 --- /dev/null +++ b/src/learn-console/Learn.Hangman.Test/EndMenuTest.cs @@ -0,0 +1,33 @@ +using Moq; +using System; +using System.Collections.Generic; +using Xunit; + +namespace Learn.Hangman.Test +{ + public class EndMenuTest : TestBase + { + [Fact] + public void Menuler_parantez_icinde_menu_basliginin_bas_harfi_ile_menu_basligi_listelenir() + { + var endMenu = AnEndMenu( + Play(), + Exit()); + var actual = endMenu.Render(); + var expected = "Play['P'], Exit['E']"; + + Assert.Equal(expected, actual); + } + + [Fact] + public void Listelenen_menulerin_bas_harfine_basildiginda_basilan_menu_calistirilir() + { + var menuOption = Exit(); + var endMenu = AnEndMenu(menuOption); + + endMenu.Option(AKey(ConsoleKey.E)); + + Mock.Get(menuOption).Verify(m => m.Select(), Times.Once()); + } + } +} diff --git a/src/learn-console/Learn.Hangman.Test/HangmanTest.cs b/src/learn-console/Learn.Hangman.Test/HangmanTest.cs index 3e55627..370bf8c 100644 --- a/src/learn-console/Learn.Hangman.Test/HangmanTest.cs +++ b/src/learn-console/Learn.Hangman.Test/HangmanTest.cs @@ -218,12 +218,12 @@ public void Geri_sayim_animasyonu_ustte_cizilir() var actual = game.Render(); - Assert.StartsWith($"first{Environment.NewLine}", actual); + Assert.StartsWith($"first", actual); game.ProcessKey(ConsoleKey.A); actual = game.Render(); - Assert.StartsWith($"first{Environment.NewLine}", actual); + Assert.StartsWith($"first", actual); } } } diff --git a/src/learn-console/Learn.Hangman.Test/MainMenuTest.cs b/src/learn-console/Learn.Hangman.Test/MainMenuTest.cs index b9bec69..8b8b272 100644 --- a/src/learn-console/Learn.Hangman.Test/MainMenuTest.cs +++ b/src/learn-console/Learn.Hangman.Test/MainMenuTest.cs @@ -22,9 +22,9 @@ public void Kullanici_ilk_secenek_secili_degilken_sol_ok_tusuna_basar_bir_onceki var menu = AMenu( Play(), Exit()); - menu.Right(); - menu.Left(); + menu.Option(AKey(ConsoleKey.RightArrow)); + menu.Option(AKey(ConsoleKey.LeftArrow)); Assert.Contains("Play", menu.Render()); } @@ -36,7 +36,7 @@ public void Kullanici_ilk_secenek_secili_degilken_sol_ok_tusuna_basar_bir_onceki Play(), Exit()); - menu.Left(); + menu.Option(AKey(ConsoleKey.LeftArrow)); Assert.Contains("Play", menu.Render()); } @@ -48,7 +48,7 @@ public void Kullanici_sondaki_secenek_secili_degilken_sag_ok_tusuna_basar_sonrak Play(), Exit()); - menu.Right(); + menu.Option(AKey(ConsoleKey.RightArrow)); Assert.Contains("Exit", menu.Render()); } @@ -59,9 +59,8 @@ public void Kullanici_sondaki_secenek_secili_degilken_sag_ok_tusuna_basar_sonrak var menu = AMenu( Play(), Exit()); - menu.Right(); - menu.Right(); + menu.Option(AKey(ConsoleKey.RightArrow)); Assert.Contains("Exit", menu.Render()); } @@ -69,28 +68,28 @@ public void Kullanici_sondaki_secenek_secili_degilken_sag_ok_tusuna_basar_sonrak [Fact] public void Kullanici_Play_secenegini_secer_secenekteki_action_calisir() { - var game = AGame(); + var playMenu = Play(); var menu = AMenu( - Play(game, AConsole(lastKey: ConsoleKey.Enter)), + playMenu, Exit()); - menu.Enter(); + menu.Option(AKey(ConsoleKey.Enter)); - Mock.Get(game).Verify(g => g.Render(), Times.AtLeast(1)); + Mock.Get(playMenu).Verify(m => m.Select(), Times.AtLeast(1)); } [Fact] public void Kullanici_Exit_secenegini_secer_program_sonlanir() { - var console = AConsole(lastKey: ConsoleKey.Enter); + var exit = Exit(isClickEnter: true); var menu = AMenu( Play(), - Exit(console)); - menu.Right(); + exit); + menu.Option(AKey(ConsoleKey.RightArrow)); - menu.Enter(); + menu.Option(AKey(ConsoleKey.Enter)); - Mock.Get(console).Verify(g => g.Exit(), Times.AtLeast(1)); + Mock.Get(exit).Verify(g => g.Select(), Times.AtLeast(1)); } } } diff --git a/src/learn-console/Learn.Hangman.Test/MenuRunnerTest.cs b/src/learn-console/Learn.Hangman.Test/MenuRunnerTest.cs index e61ff76..546b033 100644 --- a/src/learn-console/Learn.Hangman.Test/MenuRunnerTest.cs +++ b/src/learn-console/Learn.Hangman.Test/MenuRunnerTest.cs @@ -1,7 +1,5 @@ -using Learn.Hangman.MenuOptions; -using Moq; +using Moq; using System; -using System.Collections.Generic; using Xunit; namespace Learn.Hangman.Test @@ -11,15 +9,15 @@ public class MenuRunnerTest : TestBase [Fact] public void MenuRunner_calisir_verilen_menu_render_olur() { - var game = AGame(); - var menu = AMenu( - Play(game, AConsole()), - Exit(AConsole())); - var menuRunner = new MenuRunner(menu, AConsole(lastKey: ConsoleKey.Enter)); + var menu = Menu(); + var menuRunner = new MenuRunner( + menu, + menu, + AConsole(keys: new[] { ConsoleKey.Escape })); menuRunner.Run(); - Mock.Get(game).Verify(t => t.Render(), Times.AtLeastOnce()); + Mock.Get(menu).Verify(m => m.Render(), Times.AtLeastOnce()); } } } diff --git a/src/learn-console/Learn.Hangman.Test/TestBase.cs b/src/learn-console/Learn.Hangman.Test/TestBase.cs index fa18f5e..52e96de 100644 --- a/src/learn-console/Learn.Hangman.Test/TestBase.cs +++ b/src/learn-console/Learn.Hangman.Test/TestBase.cs @@ -1,5 +1,4 @@ -using Learn.Hangman.MenuOptions; -using Moq; +using Moq; using System; using System.Collections.Generic; @@ -18,18 +17,71 @@ protected virtual MainMenu AMenu(params IMenuOption[] options) return new MainMenu(menuOptions); } - protected virtual IConsole AConsole(ConsoleKey lastKey = ConsoleKey.Enter) + protected ConsoleKeyInfo AKey(ConsoleKey key = default, char charKey = '0') => new ConsoleKeyInfo(charKey, key, false, false, false); + + protected virtual IMenu Menu() + { + var key = AKey(); + var mock = new Mock(); + mock.Setup(m => m.Render()); + mock.Setup(m => m.Option(key)); + + return mock.Object; + } + + protected virtual IConsole AConsole(ConsoleKey[] keys) { var mock = new Mock(); - var key = new ConsoleKeyInfo(keyChar: 'a', key: lastKey, false, false, false); - mock.Setup(t => t.ReadKey()).Returns(key); + mock.Setup(t => t.Exit()); + var setup = mock.SetupSequence(t => t.ReadKey()); + foreach (var key in keys) + { + setup = setup.Returns(new ConsoleKeyInfo(keyChar: 'k', key: key, false, false, false)); + } + return mock.Object; + } + + protected virtual IGame AGame(int remainingRounds = 1, GameStatus lastStatus = GameStatus.Won) + { + var mock = new Mock(); + var setup = mock.SetupSequence(t => t.GameStatus); + for (var i = 0; i < remainingRounds; i++) + { + setup = setup.Returns(GameStatus.Play); + } + + setup.Returns(lastStatus); + return mock.Object; } - protected virtual IGame AGame() => new Mock().Object; + protected virtual EndMenu AnEndMenu(params IMenuOption[] options) + { + var menuOptions = new List(); + foreach (var option in options) + { + menuOptions.Add(option); + } + + return new EndMenu(menuOptions); + } - protected virtual IMenuOption Play(IGame game = null, IConsole console = null) => new Play(new GameRunner(game ?? AGame(), console ?? AConsole())); + protected virtual IMenuOption Play(bool isClickEnter = false) + { + var mock = new Mock(); + mock.Setup(m => m.Title).Returns("Play"); + if (isClickEnter) mock.Setup(m => m.Select()); + + return mock.Object; + } - protected virtual IMenuOption Exit(IConsole console = null) => new Exit(console ?? AConsole()); + protected virtual IMenuOption Exit(bool isClickEnter = false) + { + var mock = new Mock(); + mock.Setup(m => m.Title).Returns("Exit"); + if (isClickEnter) mock.Setup(m => m.Select()); + + return mock.Object; + } } } diff --git a/src/learn-console/Learn.Hangman/Assets/gameplay.gif b/src/learn-console/Learn.Hangman/Assets/gameplay.gif index dafe573..29af9ec 100644 Binary files a/src/learn-console/Learn.Hangman/Assets/gameplay.gif and b/src/learn-console/Learn.Hangman/Assets/gameplay.gif differ diff --git a/src/learn-console/Learn.Hangman/Consoles/SystemConsole.cs b/src/learn-console/Learn.Hangman/Consoles/SystemConsole.cs index bfb0701..1712415 100644 --- a/src/learn-console/Learn.Hangman/Consoles/SystemConsole.cs +++ b/src/learn-console/Learn.Hangman/Consoles/SystemConsole.cs @@ -1,15 +1,14 @@ -namespace Learn.Hangman.Consoles +namespace Learn.Hangman.Consoles; + +public class SystemConsole : IConsole { - public class SystemConsole : IConsole - { - public void Clear() => Console.Clear(); + public void Clear() => Console.Clear(); - public void Exit() => Environment.Exit(0); + public void Exit() => Environment.Exit(0); - public ConsoleKeyInfo ReadKey() => Console.ReadKey(); + public ConsoleKeyInfo ReadKey() => Console.ReadKey(); - public void Sleep(int milliseconds) => Thread.Sleep(milliseconds); + public void Sleep(int milliseconds) => Thread.Sleep(milliseconds); - public void WriteLine(string message) => Console.WriteLine(message); - } + public void WriteLine(string message) => Console.WriteLine(message); } diff --git a/src/learn-console/Learn.Hangman/EndMenu.cs b/src/learn-console/Learn.Hangman/EndMenu.cs new file mode 100644 index 0000000..aa509aa --- /dev/null +++ b/src/learn-console/Learn.Hangman/EndMenu.cs @@ -0,0 +1,19 @@ +namespace Learn.Hangman; + +public class EndMenu: IMenu +{ + private readonly List menuOptions; + public EndMenu(List menuOptions) + { + this.menuOptions = menuOptions; + } + + public void Option(ConsoleKeyInfo keyInfo) + { + var option = menuOptions.Where(x => char.ToLower(x.Title[0]) == char.ToLower(keyInfo.Key.ToString().First())); + + option.First().Select(); + } + + public string Render() => string.Join(", ", menuOptions.Select(x => x.Title + $"['{x.Title.First()}']")); +} diff --git a/src/learn-console/Learn.Hangman/Enums.cs b/src/learn-console/Learn.Hangman/Enums.cs index 4733c5f..315c3f1 100644 --- a/src/learn-console/Learn.Hangman/Enums.cs +++ b/src/learn-console/Learn.Hangman/Enums.cs @@ -1,16 +1,16 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public enum GameStatus { - public enum GameStatus - { - MainMenu, - Exit, - Play, - Over, - Won - } - public enum MainMenuStatus - { - Play, - Exit - } + MainMenu, + Exit, + Play, + Over, + Won +} +public enum MenuStatus +{ + OnOptions, + OnMenu, + Done } diff --git a/src/learn-console/Learn.Hangman/Extensions.cs b/src/learn-console/Learn.Hangman/Extensions.cs index 3526462..66bef76 100644 --- a/src/learn-console/Learn.Hangman/Extensions.cs +++ b/src/learn-console/Learn.Hangman/Extensions.cs @@ -1,15 +1,14 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public static class Extensions { - public static class Extensions + public static IEnumerable ForEach(this IEnumerable source, Action action) { - public static IEnumerable ForEach(this IEnumerable source, Action action) + foreach (var item in source) { - foreach (var item in source) - { - action(item); - } - - return source; + action(item); } + + return source; } } diff --git a/src/learn-console/Learn.Hangman/Game.cs b/src/learn-console/Learn.Hangman/Game.cs index 4d3915a..bfd96aa 100644 --- a/src/learn-console/Learn.Hangman/Game.cs +++ b/src/learn-console/Learn.Hangman/Game.cs @@ -1,60 +1,59 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public class Game : IGame { - public class Game : IGame - { - private readonly int maxGuesses; - private readonly IText text; - private readonly string[] countDown; - private readonly List letters; - private int remainingGuesses; + private readonly int maxGuesses; + private readonly IText text; + private readonly string[] countDown; + private List letters; + private int remainingGuesses; - public GameStatus GameStatus { get; private set; } + public GameStatus GameStatus { get; private set; } - private bool NoMoreGuesses => remainingGuesses <= 0; + private bool NoMoreGuesses => remainingGuesses <= 0; - public Game(string challenge, int maxGuesses, IText text, string[] countDown) - { - this.maxGuesses = maxGuesses; - this.text = text; - this.countDown = countDown; - GameStatus = GameStatus.Play; + public Game(string challenge, int maxGuesses, IText text, string[] countDown) + { + this.maxGuesses = maxGuesses; + this.text = text; + this.countDown = countDown; + GameStatus = GameStatus.Play; - letters = challenge.Select(c => new Letter(c)).ToList(); - remainingGuesses = maxGuesses; - } + letters = challenge.Select(c => new Letter(c)).ToList(); + remainingGuesses = maxGuesses; + } - public void ProcessKey(ConsoleKey key) - { - var hit = letters - .Where(l => l.Is(key)) - .ForEach(l => l.Reveal()) - .Any(); - - if (!hit) remainingGuesses--; - - GameStatus = NoMoreGuesses - ? GameStatus.Over - : letters.All(l => l.Revealed) - ? GameStatus.Won - : GameStatus.Play; - } + public void ProcessKey(ConsoleKey key) + { + var hit = letters + .Where(l => l.Is(key)) + .ForEach(l => l.Reveal()) + .Any(); + + if (!hit) remainingGuesses--; + + GameStatus = NoMoreGuesses + ? GameStatus.Over + : letters.All(l => l.Revealed) + ? GameStatus.Won + : GameStatus.Play; + } - public string Render() + public string Render() + { + switch (GameStatus) { - switch (GameStatus) - { - case GameStatus.Play: - { - var yuzde = 1 - (double)remainingGuesses / maxGuesses; - var countDownIndex = (int)Math.Round(yuzde * (countDown.Length - 1)); - - return $"{countDown[countDownIndex]}{Environment.NewLine}{string.Join(' ', letters)}"; - } - case GameStatus.Won: - return text.GameFinishText(); - default: - return $"{countDown.Last()}{Environment.NewLine}{text.GameOverText()}"; - } + case GameStatus.Play: + { + var yuzde = 1 - (double)remainingGuesses / maxGuesses; + var countDownIndex = (int)Math.Round(yuzde * (countDown.Length - 1)); + + return $"{countDown[countDownIndex]} -------> {string.Join(' ', letters)}{Environment.NewLine}REMAINING GUESSES:{remainingGuesses}"; + } + case GameStatus.Won: + return text.GameFinishText(); + default: + return $"{countDown.Last()}{Environment.NewLine}{text.GameOverText()}"; } } } diff --git a/src/learn-console/Learn.Hangman/GameFactory.cs b/src/learn-console/Learn.Hangman/GameFactory.cs index 368f43c..b28cb2f 100644 --- a/src/learn-console/Learn.Hangman/GameFactory.cs +++ b/src/learn-console/Learn.Hangman/GameFactory.cs @@ -1,18 +1,17 @@ using Learn.Hangman.Models; using Learn.Hangman.Texts; -namespace Learn.Hangman +namespace Learn.Hangman; + +public class GameFactory : IGameFactory { - public class GameFactory - { - public Game CreateDefault() => - new Game("I AM IRONMAN", - maxGuesses: 8, - text: new MixedText( - new EliteText(), - new BloodyText() - ), - Man.Animation - ); - } + public Game CreateDefault() => + new Game("I AM IRONMAN", + maxGuesses: 8, + text: new MixedText( + new EliteText(), + new BloodyText() + ), + Man.Animation + ); } diff --git a/src/learn-console/Learn.Hangman/GameRunner.cs b/src/learn-console/Learn.Hangman/GameRunner.cs index 28411e5..4b31d20 100644 --- a/src/learn-console/Learn.Hangman/GameRunner.cs +++ b/src/learn-console/Learn.Hangman/GameRunner.cs @@ -1,28 +1,27 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public class GameRunner { - public class GameRunner + private readonly IGameFactory gameFactory; + private readonly IConsole console; + public GameRunner(IGameFactory gameFactory, IConsole console) { - private readonly IGame game; - private readonly IConsole console; - public GameRunner(IGame game, IConsole console) - { - this.game = game; - this.console = console; - } + this.gameFactory = gameFactory; + this.console = console; + } - public void Run() + public void Run() + { + var game = gameFactory.CreateDefault(); + while (game.GameStatus == GameStatus.Play) { - while (game.GameStatus == GameStatus.Play) - { - console.Clear(); - console.WriteLine(game.Render()); - game.ProcessKey(console.ReadKey().Key); - console.Sleep(500); - } - console.Clear(); console.WriteLine(game.Render()); - console.Sleep(300); + game.ProcessKey(console.ReadKey().Key); + console.Sleep(500); } + + console.Clear(); + console.WriteLine(game.Render()); } } diff --git a/src/learn-console/Learn.Hangman/IConsole.cs b/src/learn-console/Learn.Hangman/IConsole.cs index b5f8319..6df3a5c 100644 --- a/src/learn-console/Learn.Hangman/IConsole.cs +++ b/src/learn-console/Learn.Hangman/IConsole.cs @@ -1,11 +1,10 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public interface IConsole { - public interface IConsole - { - void WriteLine(string message); - void Clear(); - ConsoleKeyInfo ReadKey(); - void Sleep(int milliseconds); - void Exit(); - } + void WriteLine(string message); + void Clear(); + ConsoleKeyInfo ReadKey(); + void Sleep(int milliseconds); + void Exit(); } diff --git a/src/learn-console/Learn.Hangman/IGame.cs b/src/learn-console/Learn.Hangman/IGame.cs index 621980e..6a23698 100644 --- a/src/learn-console/Learn.Hangman/IGame.cs +++ b/src/learn-console/Learn.Hangman/IGame.cs @@ -1,15 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Learn.Hangman; -namespace Learn.Hangman +public interface IGame { - public interface IGame - { - GameStatus GameStatus { get; } - void ProcessKey(ConsoleKey key); - string Render(); - } + GameStatus GameStatus { get; } + void ProcessKey(ConsoleKey key); + string Render(); } diff --git a/src/learn-console/Learn.Hangman/IGameFactory.cs b/src/learn-console/Learn.Hangman/IGameFactory.cs new file mode 100644 index 0000000..9ebc234 --- /dev/null +++ b/src/learn-console/Learn.Hangman/IGameFactory.cs @@ -0,0 +1,6 @@ +namespace Learn.Hangman; + +public interface IGameFactory +{ + public Game CreateDefault(); +} \ No newline at end of file diff --git a/src/learn-console/Learn.Hangman/IMenu.cs b/src/learn-console/Learn.Hangman/IMenu.cs new file mode 100644 index 0000000..428072a --- /dev/null +++ b/src/learn-console/Learn.Hangman/IMenu.cs @@ -0,0 +1,7 @@ +namespace Learn.Hangman; + +public interface IMenu +{ + void Option(ConsoleKeyInfo keyInfo); + string Render(); +} diff --git a/src/learn-console/Learn.Hangman/IMenuOption.cs b/src/learn-console/Learn.Hangman/IMenuOption.cs index 4e0adeb..fd6ec6c 100644 --- a/src/learn-console/Learn.Hangman/IMenuOption.cs +++ b/src/learn-console/Learn.Hangman/IMenuOption.cs @@ -1,9 +1,8 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public interface IMenuOption { - public interface IMenuOption - { - string Title { get; } + string Title { get; } - void Select(); - } + void Select(); } diff --git a/src/learn-console/Learn.Hangman/IText.cs b/src/learn-console/Learn.Hangman/IText.cs index e5e796b..0a3f6b2 100644 --- a/src/learn-console/Learn.Hangman/IText.cs +++ b/src/learn-console/Learn.Hangman/IText.cs @@ -1,8 +1,7 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public interface IText { - public interface IText - { - string GameOverText(); - string GameFinishText(); - } + string GameOverText(); + string GameFinishText(); } diff --git a/src/learn-console/Learn.Hangman/Letter.cs b/src/learn-console/Learn.Hangman/Letter.cs index 95030fb..dc7a39f 100644 --- a/src/learn-console/Learn.Hangman/Letter.cs +++ b/src/learn-console/Learn.Hangman/Letter.cs @@ -1,22 +1,21 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public class Letter { - public class Letter - { - public char Original { get; private set; } - public char Masked { get; private set; } + public char Original { get; private set; } + public char Masked { get; private set; } - public Letter(char letter) - { - Original = letter; - Masked = '_'; + public Letter(char letter) + { + Original = letter; + Masked = '_'; - if (letter == ' ') Reveal(); - } + if (letter == ' ') Reveal(); + } - public bool Is(ConsoleKey key) => Original == (char)key; - public void Reveal() => Masked = Original; - public bool Revealed => Masked == Original; + public bool Is(ConsoleKey key) => Original == (char)key; + public void Reveal() => Masked = Original; + public bool Revealed => Masked == Original; - public override string ToString() => $"{Masked}"; - } + public override string ToString() => $"{Masked}"; } diff --git a/src/learn-console/Learn.Hangman/MainMenu.cs b/src/learn-console/Learn.Hangman/MainMenu.cs index 64fb39b..fdec251 100644 --- a/src/learn-console/Learn.Hangman/MainMenu.cs +++ b/src/learn-console/Learn.Hangman/MainMenu.cs @@ -1,39 +1,33 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public class MainMenu : IMenu { - public class MainMenu + private readonly List menuOptions; + private int currentIndex = 0; + public MainMenu(List menuOptions) { - private readonly List menuOptions; - private int currentIndex = 0; - public MainMenu(List menuOptions) - { - this.menuOptions = menuOptions; - } + this.menuOptions = menuOptions; + } - public void Enter() - { - menuOptions[currentIndex].Select(); - } + private readonly int MAX_MENU_OPTION_SIZE = 9; - public void Left() - { - if (currentIndex > 0) currentIndex--; - } - - public void Right() - { - if (currentIndex < menuOptions.Count() - 1) currentIndex++; - } + public string Render() + { + var leftArrow = currentIndex == 0 ? " " : "<"; + var rightArrow = currentIndex == menuOptions.Count() - 1 ? " " : ">"; - private readonly int MAX_MENU_OPTION_SIZE = 9; + return leftArrow + new string(' ', (MAX_MENU_OPTION_SIZE - menuOptions[currentIndex].Title.Length) / 2) + + menuOptions[currentIndex].Title + + new string(' ', (MAX_MENU_OPTION_SIZE - menuOptions[currentIndex].Title.Length) / 2) + rightArrow; + } - public string Render() + public void Option(ConsoleKeyInfo keyInfo = default) + { + if (keyInfo.Key == ConsoleKey.RightArrow && currentIndex < menuOptions.Count() - 1) currentIndex++; + else if (keyInfo.Key == ConsoleKey.LeftArrow && currentIndex > 0) currentIndex--; + else if (keyInfo.Key == ConsoleKey.Enter) { - var leftArrow = currentIndex == 0 ? " " : "<"; - var rightArrow = currentIndex == menuOptions.Count() - 1 ? " " : ">"; - - return leftArrow + new string(' ', (MAX_MENU_OPTION_SIZE - menuOptions[currentIndex].Title.Length) / 2) + - menuOptions[currentIndex].Title + - new string(' ', (MAX_MENU_OPTION_SIZE - menuOptions[currentIndex].Title.Length) / 2) + rightArrow; + menuOptions[currentIndex].Select(); } } } diff --git a/src/learn-console/Learn.Hangman/MenuOptions/Exit.cs b/src/learn-console/Learn.Hangman/MenuOptions/Exit.cs index 40c67e5..6bc7302 100644 --- a/src/learn-console/Learn.Hangman/MenuOptions/Exit.cs +++ b/src/learn-console/Learn.Hangman/MenuOptions/Exit.cs @@ -1,13 +1,12 @@ -namespace Learn.Hangman.MenuOptions +namespace Learn.Hangman.MenuOptions; + +public class Exit : IMenuOption { - public class Exit : IMenuOption - { - private readonly IConsole console; + private readonly IConsole console; - public Exit(IConsole console) => this.console = console; + public Exit(IConsole console) => this.console = console; - public string Title => nameof(Exit); + public string Title => nameof(Exit); - public void Select() => console.Exit(); - } + public void Select() => console.Exit(); } diff --git a/src/learn-console/Learn.Hangman/MenuOptions/Main.cs b/src/learn-console/Learn.Hangman/MenuOptions/Main.cs new file mode 100644 index 0000000..908abbb --- /dev/null +++ b/src/learn-console/Learn.Hangman/MenuOptions/Main.cs @@ -0,0 +1,8 @@ +namespace Learn.Hangman.MenuOptions; + +public class Main : IMenuOption +{ + public string Title => nameof(Main); + + public void Select() { } +} diff --git a/src/learn-console/Learn.Hangman/MenuOptions/Play.cs b/src/learn-console/Learn.Hangman/MenuOptions/Play.cs index d43eb61..3a56e3b 100644 --- a/src/learn-console/Learn.Hangman/MenuOptions/Play.cs +++ b/src/learn-console/Learn.Hangman/MenuOptions/Play.cs @@ -1,13 +1,12 @@ -namespace Learn.Hangman.MenuOptions +namespace Learn.Hangman.MenuOptions; + +public class Play : IMenuOption { - public class Play : IMenuOption - { - private readonly GameRunner gameRunner; + private readonly GameRunner gameRunner; - public Play(GameRunner gameRunner) => this.gameRunner = gameRunner; + public Play(GameRunner gameRunner) => this.gameRunner = gameRunner; - public string Title => nameof(Play); + public string Title => nameof(Play); - public void Select() => gameRunner.Run(); - } + public void Select() => gameRunner.Run(); } diff --git a/src/learn-console/Learn.Hangman/MenuRunner.cs b/src/learn-console/Learn.Hangman/MenuRunner.cs index 00f318e..25feaf2 100644 --- a/src/learn-console/Learn.Hangman/MenuRunner.cs +++ b/src/learn-console/Learn.Hangman/MenuRunner.cs @@ -1,30 +1,33 @@ -namespace Learn.Hangman +namespace Learn.Hangman; + +public class MenuRunner { - public class MenuRunner + private readonly IMenu menu; + private readonly IMenu end; + private readonly IConsole console; + + public MenuRunner(IMenu menu, IMenu end, IConsole console) { - private readonly MainMenu menu; - private readonly IConsole console; + this.console = console; + this.menu = menu; + this.end = end; + } - public MenuRunner(MainMenu menu, IConsole console) + public void Run() + { + var showMustGoOn = true; + while (showMustGoOn) { - this.console = console; - this.menu = menu; - } + console.Clear(); + console.WriteLine(menu.Render()); + var keyInfo = console.ReadKey(); - public void Run() - { - while (true) - { - console.Clear(); - console.WriteLine(menu.Render()); - var key = console.ReadKey().Key; - if (key == ConsoleKey.LeftArrow) menu.Left(); - else if (key == ConsoleKey.RightArrow) menu.Right(); - else if (key == ConsoleKey.Enter) - { - menu.Enter(); - break; - } + if (keyInfo.Key == ConsoleKey.Escape) showMustGoOn = false; + + menu.Option(keyInfo); + if (keyInfo.Key == ConsoleKey.Enter) { + console.WriteLine(end.Render()); + end.Option(console.ReadKey()); } } } diff --git a/src/learn-console/Learn.Hangman/Models/Man.cs b/src/learn-console/Learn.Hangman/Models/Man.cs index b420058..779dafe 100644 --- a/src/learn-console/Learn.Hangman/Models/Man.cs +++ b/src/learn-console/Learn.Hangman/Models/Man.cs @@ -1,9 +1,9 @@ -namespace Learn.Hangman.Models +namespace Learn.Hangman.Models; + +public static class Man { - public static class Man - { - public static readonly string[] Animation = new[]{ - @" + public static readonly string[] Animation = new[]{ + @" ___________.._______ | .__________))______| | | / / @@ -27,7 +27,7 @@ public static class Man | | | | : : : : . . . . ", - @" + @" ___________.._______ | .__________))______| | | / / @@ -51,7 +51,7 @@ public static class Man | | | | : : : : . . . . ", - @" + @" ___________.._______ | .__________))______| | | / / @@ -75,7 +75,7 @@ public static class Man | | | | : : : : . . . . ", - @" + @" ___________.._______ | .__________))______| | | / / @@ -99,7 +99,7 @@ public static class Man | | | | : : : : . . . . ", - @" + @" ___________.._______ | .__________))______| | | / / @@ -123,7 +123,7 @@ public static class Man | | | | : : : : . . . .", - @" + @" ___________.._______ | .__________))______| | | / / @@ -147,7 +147,7 @@ public static class Man | | | | : : : : . . . .", - @" + @" ___________.._______ | .__________))______| | | / / @@ -171,7 +171,7 @@ public static class Man | | | | : : : : . . . .", - @" + @" ___________.._______ | .__________))______| | | / / || @@ -195,7 +195,7 @@ public static class Man | | | | : : : : . . . .", - @" + @" ___________.._______ | .__________))______| | | / / || @@ -219,6 +219,5 @@ public static class Man | | \ \ | | : : \ \ : : . . `' . ." - }; - } + }; } diff --git a/src/learn-console/Learn.Hangman/Program.cs b/src/learn-console/Learn.Hangman/Program.cs index a135231..174306d 100644 --- a/src/learn-console/Learn.Hangman/Program.cs +++ b/src/learn-console/Learn.Hangman/Program.cs @@ -3,10 +3,14 @@ using Learn.Hangman.MenuOptions; var console = new SystemConsole(); -List options = new List(); -options.Add(new Play(new GameRunner(new GameFactory().CreateDefault(), console))); -options.Add(new Exit(console)); +List mainMenuOptions = new(); +mainMenuOptions.Add(new Play(new GameRunner(new GameFactory(), console))); +mainMenuOptions.Add(new Exit(console)); -var menuRunner = new MenuRunner(new MainMenu(options), console); +List endMenuOptions = new(); +endMenuOptions.Add(new Main()); +endMenuOptions.Add(new Exit(console)); + +var menuRunner = new MenuRunner(new MainMenu(mainMenuOptions), new EndMenu(endMenuOptions), console); menuRunner.Run(); diff --git a/src/learn-console/Learn.Hangman/Texts/BloodyText.cs b/src/learn-console/Learn.Hangman/Texts/BloodyText.cs index f861aa2..0445174 100644 --- a/src/learn-console/Learn.Hangman/Texts/BloodyText.cs +++ b/src/learn-console/Learn.Hangman/Texts/BloodyText.cs @@ -1,10 +1,10 @@ -namespace Learn.Hangman.Texts +namespace Learn.Hangman.Texts; + +public class BloodyText : IText { - public class BloodyText : IText + public string GameFinishText() { - public string GameFinishText() - { - return @" + return @" █ █░▓█████ ██▓ ██▓ ▓█████▄ ▒█████ ███▄ █ ▓█████ ▄▄▄▄ ▒█████ ▓██ ██▓ ▓█░ █ ░█░▓█ ▀ ▓██▒ ▓██▒ ▒██▀ ██▌▒██▒ ██▒ ██ ▀█ █ ▓█ ▀ ▓█████▄ ▒██▒ ██▒▒██ ██▒ @@ -18,11 +18,11 @@ public string GameFinishText() ░ ░ ░ ░ "; - } + } - public string GameOverText() - { - return @" + public string GameOverText() + { + return @" ▄████ ▄▄▄ ███▄ ▄███▓▓█████ ▒█████ ██▒ █▓▓█████ ██▀███ ██▒ ▀█▒▒████▄ ▓██▒▀█▀ ██▒▓█ ▀ ▒██▒ ██▒▓██░ █▒▓█ ▀ ▓██ ▒ ██▒ @@ -36,6 +36,5 @@ public string GameOverText() ░ "; - } } } diff --git a/src/learn-console/Learn.Hangman/Texts/EliteText.cs b/src/learn-console/Learn.Hangman/Texts/EliteText.cs index 5cf523e..1578841 100644 --- a/src/learn-console/Learn.Hangman/Texts/EliteText.cs +++ b/src/learn-console/Learn.Hangman/Texts/EliteText.cs @@ -1,10 +1,10 @@ -namespace Learn.Hangman.Texts +namespace Learn.Hangman.Texts; + +public class EliteText : IText { - public class EliteText : IText + public string GameFinishText() { - public string GameFinishText() - { - return @" + return @" ▄▄▌ ▐ ▄▌▄▄▄ .▄▄▌ ▄▄▌ ·▄▄▄▄ ▐ ▄ ▄▄▄ . ▄▄▄▄· ▄· ▄▌ ██· █▌▐█▀▄.▀·██• ██• ██▪ ██ ▪ •█▌▐█▀▄.▀· ▐█ ▀█▪▪ ▐█▪██▌ @@ -13,11 +13,11 @@ public string GameFinishText() ▀▀▀▀ ▀▪ ▀▀▀ .▀▀▀ .▀▀▀ ▀▀▀▀▀• ▀█▄▀▪▀▀ █▪ ▀▀▀ ·▀▀▀▀ ▀█▄▀▪ ▀ • "; - } + } - public string GameOverText() - { - return @" + public string GameOverText() + { + return @" ▄▄ • ▄▄▄· • ▌ ▄ ·. ▄▄▄ . ▌ ▐·▄▄▄ .▄▄▄ ▐█ ▀ ▪▐█ ▀█ ·██ ▐███▪▀▄.▀· ▪ ▪█·█▌▀▄.▀·▀▄ █· @@ -26,6 +26,5 @@ public string GameOverText() ·▀▀▀▀ ▀ ▀ ▀▀ █▪▀▀▀ ▀▀▀ ▀█▄▀▪. ▀ ▀▀▀ .▀ ▀ "; - } } } diff --git a/src/learn-console/Learn.Hangman/Texts/MixedText.cs b/src/learn-console/Learn.Hangman/Texts/MixedText.cs index bb1b96a..54fe1bd 100644 --- a/src/learn-console/Learn.Hangman/Texts/MixedText.cs +++ b/src/learn-console/Learn.Hangman/Texts/MixedText.cs @@ -1,14 +1,13 @@ -namespace Learn.Hangman.Texts +namespace Learn.Hangman.Texts; + +public class MixedText : IText { - public class MixedText : IText - { - private readonly IText gameFinish; - private readonly IText gameOver; + private readonly IText gameFinish; + private readonly IText gameOver; - public MixedText(IText gameFinish, IText gameOver) => - (this.gameFinish, this.gameOver) = (gameFinish, gameOver); + public MixedText(IText gameFinish, IText gameOver) => + (this.gameFinish, this.gameOver) = (gameFinish, gameOver); - public string GameFinishText() => gameFinish.GameFinishText(); - public string GameOverText() => gameOver.GameOverText(); - } + public string GameFinishText() => gameFinish.GameFinishText(); + public string GameOverText() => gameOver.GameOverText(); } diff --git a/work/hangman/ana-menu.md b/work/hangman/done/ana-menu.md similarity index 100% rename from work/hangman/ana-menu.md rename to work/hangman/done/ana-menu.md diff --git a/work/hangman/animasyon-gecikmeleri.md b/work/hangman/done/animasyon-gecikmeleri.md similarity index 100% rename from work/hangman/animasyon-gecikmeleri.md rename to work/hangman/done/animasyon-gecikmeleri.md