Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Mastermind_v2
- {
- class Backend
- {
- public static string[] Randomizer()
- {
- Random r = new Random();
- var randomizedNumbers = new int[4];
- for (int i = 0; i < 4; i++)
- {
- randomizedNumbers[i] = r.Next(0, 6);
- }
- string[] newRandom = randomizedNumbers.Select(x => x.ToString()).ToArray();
- for (int i = 0; i < 4; i++)
- {
- newRandom[i] = newRandom[i].Replace("0", "red");
- newRandom[i] = newRandom[i].Replace("1", "blue");
- newRandom[i] = newRandom[i].Replace("2", "green");
- newRandom[i] = newRandom[i].Replace("3", "pink");
- newRandom[i] = newRandom[i].Replace("4", "cyan");
- newRandom[i] = newRandom[i].Replace("5", "yellow");
- }
- return newRandom;
- }
- static string[] Format(string rawInput)
- {
- //delar upp inputen i en ny array med varje färg på sin plats
- var newInput = rawInput.Split();
- //en liten funktion som fångar in fel
- //skapar en stringarray som returnernas om det blir något fel i inmatningen, t.ex om man skriver för många eller för få färger, eller om man stavar fel
- //
- var errorMessage = new string[] { "error", "", "", "" };
- if (newInput.Length != 4)
- {
- return errorMessage;
- }
- foreach (var word in newInput)
- {
- switch (word)
- {
- case "red":
- case "blue":
- case "green":
- case "pink":
- case "cyan":
- case "yellow":
- break;
- default:
- return errorMessage;
- }
- }
- return newInput;
- }
- //en funktion som räknar antalet av en viss färg in
- static int CountColors(string color, string[] randomColors)
- {
- var amount =
- from colors in randomColors
- where colors == color
- select colors;
- int intAmount = Convert.ToInt32(amount.Count());
- return intAmount;
- }
- public static int[] CheckColors(string rawInput, string[] allRandomColors)
- {
- var input = Format(rawInput);
- //efter att ha formaterat inmatningen kollar den om det blivit något fel
- //foundError används senare när den returnerar ett resultat, 1 betyder att något gått fel och 0 betyder att allt funkar som det ska
- var foundError = 0;
- if (input[0] == "error")
- {
- foundError = 1;
- }
- //om det finns flera av samma färg i den slumpade sekvensen gör reducerar detta det till en av varje i en ny stringarray
- var random = allRandomColors.Distinct().ToArray();
- int trueCorrectColors = 0;
- int correctColors = 0;
- int correctPositions = 0;
- //kolla efter korrekta färger
- foreach (var randomColor in random)
- {
- foreach (var inputColors in input)
- {
- //helt ärligt vet jag inte riktigt varför det här funkar, om man tar bort && correctColors < etc etc så funkar det inte
- //så no touchy
- while ((randomColor == inputColors) && correctColors < CountColors(randomColor, allRandomColors))
- {
- correctColors++;
- }
- }
- trueCorrectColors += correctColors;
- correctColors = 0;
- }
- //kolla efter korrekta platser
- int counter = 0;
- foreach (var randomColor in allRandomColors)
- {
- if (randomColor == input[counter])
- {
- correctPositions++;
- }
- counter++;
- }
- //räkna och returnera resultatet
- trueCorrectColors = Math.Abs(correctPositions - trueCorrectColors);
- return new int[] { correctPositions, trueCorrectColors, foundError };
- }
- }
- class Interface
- {
- static void Board(string input, int[] result)
- {
- //print # of correct positions
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.CursorLeft = 38;
- Console.Write("" + result[0]);
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write(" | ");
- //print the written pegs
- var inputColors = input.Split();
- foreach (string word in inputColors)
- {
- switch (word)
- {
- case "red":
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("[*] ");
- Console.ForegroundColor = ConsoleColor.Gray;
- break;
- case "blue":
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.Write("[*] ");
- Console.ForegroundColor = ConsoleColor.Gray;
- break;
- case "green":
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("[*] ");
- Console.ForegroundColor = ConsoleColor.Gray;
- break;
- case "pink":
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.Write("[*] ");
- Console.ForegroundColor = ConsoleColor.Gray;
- break;
- case "cyan":
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("[*] ");
- Console.ForegroundColor = ConsoleColor.Gray;
- break;
- case "yellow":
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.Write("[*] ");
- Console.ForegroundColor = ConsoleColor.Gray;
- break;
- }
- }
- //print # of correct colors
- Console.Write(" | ");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(result[1]);
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("\n");
- }
- static void NewGame()
- {
- Console.Clear();
- Console.Write("\n" +
- "\n\n Select difficulty" +
- "\n\n [");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("EASY");
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("] [");
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.Write("MEDIUM");
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("] [");
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.Write("HARD");
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("]" +
- "\n ");
- bool diffLoop = true;
- int numberOfTurns = 0;
- string difficulty = "";
- while (diffLoop)
- {
- difficulty = Console.ReadLine().ToLower();
- switch (difficulty)
- {
- case "easy":
- numberOfTurns = 20;
- diffLoop = false;
- Console.Write("\n \r");
- Console.CursorLeft = 40;
- Console.Write("Difficulty selected: [");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write(difficulty.ToUpper());
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("]");
- break;
- case "medium":
- numberOfTurns = 15;
- diffLoop = false;
- Console.Write("\n \r");
- Console.CursorLeft = 40;
- Console.Write("Difficulty selected: [");
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.Write(difficulty.ToUpper());
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("]");
- break;
- case "hard":
- numberOfTurns = 10;
- diffLoop = false;
- Console.Write("\n \r");
- Console.CursorLeft = 40;
- Console.Write("Difficulty selected: [");
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.Write(difficulty.ToUpper());
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.Write("]");
- break;
- default:
- Console.Write("\n Something went wrong, please select a valid difficulty");
- Console.CursorTop = 6;
- Console.Write("\r ");
- Console.CursorTop = 6;
- Console.CursorLeft = 51;
- break;
- }
- }
- System.Threading.Thread.Sleep(1500);
- Console.Clear();
- int turnsPlayed = 0;
- Console.CursorTop = 6;
- for (int i = numberOfTurns; i > 0; i--)
- {
- Console.CursorLeft = 41;
- Console.Write("| * * * * |\n");
- }
- Console.CursorTop = 4;
- Console.CursorLeft = 41;
- Console.Write("| [?] [?] [?] [?] |");
- Console.CursorTop = 5;
- Console.CursorLeft = 41;
- Console.Write("|-------------------|");
- Console.CursorTop = 3;
- Console.CursorLeft = 41;
- string input = "";
- var randomizedColors = Backend.Randomizer();
- for (turnsPlayed = 0; turnsPlayed < numberOfTurns; turnsPlayed++)
- {
- input = Console.ReadLine();
- var result = Backend.CheckColors(input, randomizedColors);
- if (result[2] == 0)
- {
- Console.CursorTop = 6 + turnsPlayed;
- Board(input, result);
- Console.CursorLeft = 41;
- Console.CursorTop = 3;
- Console.Write(" ");
- Console.CursorLeft = 41;
- Console.CursorTop = 3;
- }
- else
- {
- Console.CursorTop = 2;
- Console.CursorLeft = 30;
- Console.Write("Something went wrong, please check your input.");
- Console.CursorLeft = 41;
- Console.CursorTop = 3;
- Console.Write(" ");
- Console.CursorLeft = 41;
- Console.CursorTop = 3;
- turnsPlayed--;
- }
- }
- }
- static void Main(string[] args)
- {
- Console.Write(
- "\n\n\n" +
- " __ __ _ __ __ _ _ \n" +
- " | \\/ | | | | \\/ (_) | |\n" +
- " | \\ / | __ _ ___| |_ ___ _ __| \\ / |_ _ __ __| |\n" +
- " | |\\/| |/ _` / __| __/ _ \\ '__| |\\/| | | '_ \\ / _` |\n" +
- " | | | | (_| \\__ \\ || __/ | | | | | | | | | (_| |\n" +
- " |_| |_|\\__,_|___/\\__\\___|_| |_| |_|_|_| |_|\\__,_|\n" +
- "\n By Dag\n\n" +
- " Press Any Key");
- Console.ReadKey();
- NewGame();
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement