diff --git a/Master Mind/Game.cs b/Master Mind/Game.cs new file mode 100644 index 0000000..a2220d5 --- /dev/null +++ b/Master Mind/Game.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Master_Mind +{ + public class Game + { + public class GameData + { + public int[,] board = new int[12, 4]; + public int[] sequence = new int[4]; + + public int go = 0; + + public int won + { + get + { + if (go >= 12) + { + return -1; + } + + + for (int i = 0; i < 4; i++) + { + if (board[go - 1, i] != sequence[i]) + { + return 0; + } + } + + return 1; + } + } + + public GameData() + { + this.board = new int[12, 4]; + this.sequence = new int[4]; + + this.go = 0; + } + + public void AddRow(int[] dat) + { + for (int i = 0; i < 4; i++) + { + this.board[this.go, i] = dat[i]; + } + + this.go++; + } + } + + public static void Play() + { + Console.Clear(); + int opt = Menu.NumberMenu(new string[] { "You Guesses", "Computer Guess" }); + Console.Clear(); + + Console.WriteLine("Loading..."); + + GameData game = new GameData(); + + if (opt == 1) + { + Random rand = new Random(); + + for (int i = 0; i < 4; i++) + { + int col = rand.Next(6); + + game.sequence[i] = col; + } + } + else + { + Console.WriteLine("Please select your colors"); + + return; + } + + do + { + game.AddRow(Render.GetColorInput()); + + Render.RenderBoard(game); + + Console.ReadLine(); + } while (game.won == 0); + } + } +} diff --git a/Master Mind/Master Mind.csproj b/Master Mind/Master Mind.csproj new file mode 100644 index 0000000..9a37dca --- /dev/null +++ b/Master Mind/Master Mind.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + Master_Mind + enable + enable + + + diff --git a/Master Mind/Master Mind.sln b/Master Mind/Master Mind.sln new file mode 100644 index 0000000..b047c11 --- /dev/null +++ b/Master Mind/Master Mind.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32901.215 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Master Mind", "Master Mind.csproj", "{D7479AA6-5B44-4974-9792-DC72EFB9C692}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D7479AA6-5B44-4974-9792-DC72EFB9C692}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7479AA6-5B44-4974-9792-DC72EFB9C692}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7479AA6-5B44-4974-9792-DC72EFB9C692}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7479AA6-5B44-4974-9792-DC72EFB9C692}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {40CE4916-036F-4226-9EF8-C7C88E5F6DEF} + EndGlobalSection +EndGlobal diff --git a/Master Mind/Menu.cs b/Master Mind/Menu.cs new file mode 100644 index 0000000..71aaa1f --- /dev/null +++ b/Master Mind/Menu.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Master_Mind +{ + public class Menu + { + private static ConsoleKey[] inputKeys = + { + ConsoleKey.D1, + ConsoleKey.D2, + ConsoleKey.D3, + ConsoleKey.D4, + ConsoleKey.D5, + ConsoleKey.D6, + ConsoleKey.D7, + ConsoleKey.D8, + ConsoleKey.D9, + }; + + public static int NumberMenu(string[] menuItems, string inputPrompt = "", ConsoleKey? menuReturn = null) + { + int width = 0; + + foreach (string menuItem in menuItems) + { + if (menuItem.Length > width) + width = menuItem.Length; + } + + for (int i = 1; i <= menuItems.Length; i++) + { + int expanse = (width - menuItems[i - 1].Length) + 1; + + Console.WriteLine("[ " + i.ToString() + ": " + menuItems[i - 1] + String.Concat(Enumerable.Repeat(" ", expanse)) + "]"); + } + + Console.WriteLine(); + + if (menuReturn != null) + { + Console.WriteLine("Press [" + menuReturn.ToString() + "] to go back"); + Console.WriteLine(); + } + + Console.Write((inputPrompt == "") ? "Option: " : inputPrompt); + + while (true) + { + ConsoleKey input = Console.ReadKey().Key; + + if (input == menuReturn) + return -1; + + for (int i = 1; i <= menuItems.Length; i++) + { + if (input == inputKeys[i - 1]) + return i; + } + } + } + + public static int ChooseNumber(int min = 0, int max = 0) + { + if (max <= 0) + max = 10; + + if (min == 0) + min = 1; + + while (true) + { + ConsoleKey input = Console.ReadKey().Key; + + for (int i = min; i < max; i++) + { + if (input == inputKeys[i - 1]) + return i; + } + } + } + + public static int ChooseOption(ConsoleKey[] options) + { + while (true) + { + ConsoleKey input = Console.ReadKey().Key; + + for (int i = 0; i < options.Length; i++) + { + if (input == inputKeys[i]) + return i; + } + } + } + } +} diff --git a/Master Mind/Program.cs b/Master Mind/Program.cs new file mode 100644 index 0000000..fc7c41e --- /dev/null +++ b/Master Mind/Program.cs @@ -0,0 +1,19 @@ +namespace Master_Mind +{ + internal class Program + { + static void Main(string[] args) + { + int opt = Menu.NumberMenu(new string[] { "PLAY", "EXIT" }); + + switch (opt) + { + case 0: + return; + case 1: + Game.Play(); + break; + } + } + } +} \ No newline at end of file diff --git a/Master Mind/Render.cs b/Master Mind/Render.cs new file mode 100644 index 0000000..1e750f9 --- /dev/null +++ b/Master Mind/Render.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Master_Mind +{ + public class Render + { + public static void RenderBoard(Game.GameData board) + { + for (int x = 0; x < 12; x++) + { + for (int y = 0; y < 4; y++) + { + Console.BackgroundColor = Values.colors[board.board[x, y]]; + Console.Write(" "); + Console.BackgroundColor = ConsoleColor.Black; + Console.Write(" "); + } + + Console.Write(" "); + + for (int y = 0; y < 4; y++) + { + if (board.board[x, y] == board.sequence[y]) + { + Console.BackgroundColor = ConsoleColor.DarkGreen; + } + else if (board.sequence.Contains(board.board[x,y])) + { + Console.BackgroundColor = ConsoleColor.DarkRed; + } + else + { + Console.BackgroundColor = ConsoleColor.Black; + } + + Console.Write(" "); + } + + Console.BackgroundColor = ConsoleColor.Black; + + Console.WriteLine("\n"); + } + + Console.WriteLine("\n"); + + for (int y = 0; y < 4; y++) + { + Console.BackgroundColor = Values.colors[board.sequence[y]]; + Console.Write(" "); + Console.BackgroundColor = ConsoleColor.Black; + Console.Write(" "); + } + } + + public static int[] GetColorInput() + { + int[] colors = new int[4]; + + int selected = 0; + + while (true) + { + Console.Clear(); + + for (int x = 0; x < 4; x++) + { + Console.Write(" "); + + for (int y = 0; y < 4; y++) + { + Console.BackgroundColor = Values.colors[colors[y]]; + Console.Write((y == selected) ? "# #" : " "); + + Console.BackgroundColor = ConsoleColor.Black; + Console.Write(" "); + } + + Console.WriteLine(); + } + + ConsoleKey ck = Console.ReadKey().Key; + + switch (ck) + { + case ConsoleKey.LeftArrow: + if (--selected < 0) + selected = 3; + + break; + case ConsoleKey.RightArrow: + if (++selected > 3) + selected = 0; + + break; + case ConsoleKey.UpArrow: + if (++colors[selected] > 5) + colors[selected] = 0; + + break; + case ConsoleKey.DownArrow: + if (--colors[selected] < 0) + colors[selected] = 5; + + break; + case ConsoleKey.Enter: + return colors; + } + } + } + } +} diff --git a/Master Mind/Values.cs b/Master Mind/Values.cs new file mode 100644 index 0000000..496d8b8 --- /dev/null +++ b/Master Mind/Values.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Master_Mind +{ + internal class Values + { + public static ConsoleColor[] colors = + { + ConsoleColor.Red, + ConsoleColor.Yellow, + ConsoleColor.Green, + ConsoleColor.Cyan, + ConsoleColor.Blue, + ConsoleColor.Magenta + }; + } +}