Advertisement
Guest User

Mastermind Backup

a guest
Nov 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Mastermind_v2
  8. {
  9.     class Backend
  10.     {
  11.         public static string[] Randomizer()
  12.         {
  13.             Random r = new Random();
  14.             var randomizedNumbers = new int[4];
  15.             for (int i = 0; i < 4; i++)
  16.             {
  17.                 randomizedNumbers[i] = r.Next(0, 6);
  18.             }
  19.             string[] newRandom = randomizedNumbers.Select(x => x.ToString()).ToArray();
  20.            
  21.             for (int i = 0; i < 4; i++)
  22.             {
  23.                 newRandom[i] = newRandom[i].Replace("0", "red");
  24.                 newRandom[i] = newRandom[i].Replace("1", "blue");
  25.                 newRandom[i] = newRandom[i].Replace("2", "green");
  26.                 newRandom[i] = newRandom[i].Replace("3", "pink");
  27.                 newRandom[i] = newRandom[i].Replace("4", "cyan");
  28.                 newRandom[i] = newRandom[i].Replace("5", "yellow");
  29.             }
  30.            
  31.             return newRandom;
  32.         }
  33.  
  34.         static string[] Format(string rawInput)
  35.         {
  36.             //delar upp inputen i en ny array med varje färg på sin plats
  37.             var newInput = rawInput.Split();
  38.  
  39.             //en liten funktion som fångar in fel
  40.             //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
  41.             //
  42.             var errorMessage = new string[] { "error", "", "", "" };
  43.             if (newInput.Length != 4)
  44.             {
  45.                 return errorMessage;
  46.             }
  47.             foreach (var word in newInput)
  48.             {
  49.                 switch (word)
  50.                 {
  51.                     case "red":
  52.                     case "blue":
  53.                     case "green":
  54.                     case "pink":
  55.                     case "cyan":
  56.                     case "yellow":
  57.                         break;
  58.                     default:
  59.                         return errorMessage;
  60.                 }
  61.             }
  62.  
  63.             return newInput;
  64.         }
  65.  
  66.         //en funktion som räknar antalet av en viss färg in
  67.         static int CountColors(string color, string[] randomColors)
  68.         {
  69.             var amount =
  70.                 from colors in randomColors
  71.                 where colors == color
  72.                 select colors;
  73.             int intAmount = Convert.ToInt32(amount.Count());
  74.             return intAmount;
  75.         }
  76.  
  77.         public static int[] CheckColors(string rawInput, string[] allRandomColors)
  78.         {
  79.             var input = Format(rawInput);
  80.  
  81.             //efter att ha formaterat inmatningen kollar den om det blivit något fel
  82.             //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
  83.             var foundError = 0;
  84.             if (input[0] == "error")
  85.             {
  86.                 foundError = 1;
  87.             }
  88.  
  89.             //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
  90.             var random = allRandomColors.Distinct().ToArray();
  91.  
  92.             int trueCorrectColors = 0;
  93.             int correctColors = 0;
  94.             int correctPositions = 0;
  95.  
  96.             //kolla efter korrekta färger
  97.             foreach (var randomColor in random)
  98.             {
  99.                 foreach (var inputColors in input)
  100.                 {
  101.                     //helt ärligt vet jag inte riktigt varför det här funkar, om man tar bort && correctColors < etc etc så funkar det inte
  102.                     //så no touchy
  103.                     while ((randomColor == inputColors) && correctColors < CountColors(randomColor, allRandomColors))
  104.                     {
  105.                         correctColors++;
  106.                     }
  107.                 }
  108.                 trueCorrectColors += correctColors;
  109.                 correctColors = 0;
  110.             }
  111.  
  112.             //kolla efter korrekta platser
  113.             int counter = 0;
  114.             foreach (var randomColor in allRandomColors)
  115.             {
  116.                 if (randomColor == input[counter])
  117.                 {
  118.                     correctPositions++;
  119.                 }
  120.                 counter++;
  121.             }
  122.  
  123.             //räkna och returnera resultatet
  124.             trueCorrectColors = Math.Abs(correctPositions - trueCorrectColors);
  125.             return new int[] { correctPositions, trueCorrectColors, foundError };
  126.         }
  127.     }
  128.  
  129.     class Interface
  130.     {
  131.         static void Board(string input, int[] result)
  132.         {
  133.             //print # of correct positions
  134.             Console.ForegroundColor = ConsoleColor.DarkRed;
  135.             Console.CursorLeft = 38;
  136.             Console.Write("" + result[0]);
  137.             Console.ForegroundColor = ConsoleColor.Gray;
  138.             Console.Write("  |  ");
  139.  
  140.             //print the written pegs
  141.             var inputColors = input.Split();
  142.             foreach (string word in inputColors)
  143.             {
  144.                 switch (word)
  145.                 {
  146.                     case "red":
  147.                         Console.ForegroundColor = ConsoleColor.Red;
  148.                         Console.Write("[*] ");
  149.                         Console.ForegroundColor = ConsoleColor.Gray;
  150.                         break;
  151.                     case "blue":
  152.                         Console.ForegroundColor = ConsoleColor.Blue;
  153.                         Console.Write("[*] ");
  154.                         Console.ForegroundColor = ConsoleColor.Gray;
  155.                         break;
  156.                     case "green":
  157.                         Console.ForegroundColor = ConsoleColor.Green;
  158.                         Console.Write("[*] ");
  159.                         Console.ForegroundColor = ConsoleColor.Gray;
  160.                         break;
  161.                     case "pink":
  162.                         Console.ForegroundColor = ConsoleColor.Magenta;
  163.                         Console.Write("[*] ");
  164.                         Console.ForegroundColor = ConsoleColor.Gray;
  165.                         break;
  166.                     case "cyan":
  167.                         Console.ForegroundColor = ConsoleColor.Cyan;
  168.                         Console.Write("[*] ");
  169.                         Console.ForegroundColor = ConsoleColor.Gray;
  170.                         break;
  171.                     case "yellow":
  172.                         Console.ForegroundColor = ConsoleColor.DarkYellow;
  173.                         Console.Write("[*] ");
  174.                         Console.ForegroundColor = ConsoleColor.Gray;
  175.                         break;
  176.                 }
  177.             }
  178.  
  179.             //print # of correct colors
  180.             Console.Write(" |  ");
  181.             Console.ForegroundColor = ConsoleColor.White;
  182.             Console.Write(result[1]);
  183.             Console.ForegroundColor = ConsoleColor.Gray;
  184.             Console.Write("\n");
  185.         }
  186.  
  187.         static void NewGame()
  188.         {
  189.             Console.Clear();
  190.             Console.Write("\n" +
  191.                 "\n\n                                              Select difficulty" +
  192.                 "\n\n                                           [");
  193.             Console.ForegroundColor = ConsoleColor.Green;
  194.             Console.Write("EASY");
  195.             Console.ForegroundColor = ConsoleColor.Gray;
  196.             Console.Write("] [");
  197.             Console.ForegroundColor = ConsoleColor.DarkYellow;
  198.             Console.Write("MEDIUM");
  199.             Console.ForegroundColor = ConsoleColor.Gray;
  200.             Console.Write("] [");
  201.             Console.ForegroundColor = ConsoleColor.DarkRed;
  202.             Console.Write("HARD");
  203.             Console.ForegroundColor = ConsoleColor.Gray;
  204.             Console.Write("]" +
  205.                 "\n                                                   ");
  206.             bool diffLoop = true;
  207.             int numberOfTurns = 0;
  208.             string difficulty = "";
  209.             while (diffLoop)
  210.             {
  211.                 difficulty = Console.ReadLine().ToLower();
  212.                 switch (difficulty)
  213.                 {
  214.                     case "easy":
  215.                         numberOfTurns = 20;
  216.                         diffLoop = false;
  217.                         Console.Write("\n                                                                                                        \r");
  218.                         Console.CursorLeft = 40;
  219.                         Console.Write("Difficulty selected: [");
  220.                         Console.ForegroundColor = ConsoleColor.Green;
  221.                         Console.Write(difficulty.ToUpper());
  222.                         Console.ForegroundColor = ConsoleColor.Gray;
  223.                         Console.Write("]");
  224.                         break;
  225.                     case "medium":
  226.                         numberOfTurns = 15;
  227.                         diffLoop = false;
  228.                         Console.Write("\n                                                                                                        \r");
  229.                         Console.CursorLeft = 40;
  230.                         Console.Write("Difficulty selected: [");
  231.                         Console.ForegroundColor = ConsoleColor.DarkYellow;
  232.                         Console.Write(difficulty.ToUpper());
  233.                         Console.ForegroundColor = ConsoleColor.Gray;
  234.                         Console.Write("]");
  235.                         break;
  236.                     case "hard":
  237.                         numberOfTurns = 10;
  238.                         diffLoop = false;
  239.                         Console.Write("\n                                                                                                        \r");
  240.                         Console.CursorLeft = 40;
  241.                         Console.Write("Difficulty selected: [");
  242.                         Console.ForegroundColor = ConsoleColor.DarkRed;
  243.                         Console.Write(difficulty.ToUpper());
  244.                         Console.ForegroundColor = ConsoleColor.Gray;
  245.                         Console.Write("]");
  246.                         break;
  247.                     default:
  248.                         Console.Write("\n                           Something went wrong, please select a valid difficulty");
  249.                         Console.CursorTop = 6;
  250.                         Console.Write("\r                                                                                                        ");
  251.                         Console.CursorTop = 6;
  252.                         Console.CursorLeft = 51;
  253.                         break;
  254.                 }
  255.             }
  256.             System.Threading.Thread.Sleep(1500);
  257.             Console.Clear();
  258.             int turnsPlayed = 0;
  259.             Console.CursorTop = 6;
  260.             for (int i = numberOfTurns; i > 0; i--)
  261.             {
  262.                 Console.CursorLeft = 41;
  263.                 Console.Write("|   *   *   *   *   |\n");
  264.             }
  265.             Console.CursorTop = 4;
  266.             Console.CursorLeft = 41;
  267.             Console.Write("|  [?] [?] [?] [?]  |");
  268.             Console.CursorTop = 5;
  269.             Console.CursorLeft = 41;
  270.             Console.Write("|-------------------|");
  271.  
  272.             Console.CursorTop = 3;
  273.             Console.CursorLeft = 41;
  274.             string input = "";
  275.  
  276.             var randomizedColors = Backend.Randomizer();
  277.             for (turnsPlayed = 0; turnsPlayed < numberOfTurns; turnsPlayed++)
  278.             {
  279.                 input = Console.ReadLine();
  280.                 var result = Backend.CheckColors(input, randomizedColors);
  281.                 if (result[2] == 0)
  282.                 {
  283.                     Console.CursorTop = 6 + turnsPlayed;
  284.                     Board(input, result);
  285.                     Console.CursorLeft = 41;
  286.                     Console.CursorTop = 3;
  287.                     Console.Write("                                                                ");
  288.                     Console.CursorLeft = 41;
  289.                     Console.CursorTop = 3;
  290.                 }
  291.                 else
  292.                 {
  293.                     Console.CursorTop = 2;
  294.                     Console.CursorLeft = 30;
  295.                     Console.Write("Something went wrong, please check your input.");
  296.                     Console.CursorLeft = 41;
  297.                     Console.CursorTop = 3;
  298.                     Console.Write("                                                                ");
  299.                     Console.CursorLeft = 41;
  300.                     Console.CursorTop = 3;
  301.                     turnsPlayed--;
  302.                 }
  303.             }
  304.         }
  305.  
  306.         static void Main(string[] args)
  307.         {
  308.             Console.Write(
  309.                 "\n\n\n" +
  310.                 "                           __  __           _            __  __ _           _ \n" +
  311.                 "                          |  \\/  |         | |          |  \\/  (_)         | |\n" +
  312.                 "                          | \\  / | __ _ ___| |_ ___ _ __| \\  / |_ _ __   __| |\n" +
  313.                 "                          | |\\/| |/ _` / __| __/ _ \\ '__| |\\/| | | '_ \\ / _` |\n" +
  314.                 "                          | |  | | (_| \\__ \\ ||  __/ |  | |  | | | | | | (_| |\n" +
  315.                 "                          |_|  |_|\\__,_|___/\\__\\___|_|  |_|  |_|_|_| |_|\\__,_|\n" +
  316.                 "\n                                                 By Dag\n\n" +
  317.                 "                                             Press Any Key");
  318.             Console.ReadKey();
  319.             NewGame();
  320.             Console.ReadLine();
  321.         }
  322.     }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement