This commit is contained in:
AUnicornWithNoLife 2023-05-25 12:33:44 +01:00
parent 43f48ea8f4
commit 0cb463fd0f
7 changed files with 388 additions and 0 deletions

97
Master Mind/Game.cs Normal file
View File

@ -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);
}
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Master_Mind</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -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

100
Master Mind/Menu.cs Normal file
View File

@ -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;
}
}
}
}
}

19
Master Mind/Program.cs Normal file
View File

@ -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;
}
}
}
}

115
Master Mind/Render.cs Normal file
View File

@ -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;
}
}
}
}
}

21
Master Mind/Values.cs Normal file
View File

@ -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
};
}
}