From 9fed104d24daa98b298b028ff7d25fbbd9609036 Mon Sep 17 00:00:00 2001 From: AUnicornWithNoLife <55228370+AUnicornWithNoLife@users.noreply.github.com> Date: Thu, 25 May 2023 12:50:53 +0100 Subject: [PATCH] better --- Master Mind/Game.cs | 36 ++++++++++++++++++++++++++++++++++++ Master Mind/Render.cs | 41 +++++++++++++++++++++-------------------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/Master Mind/Game.cs b/Master Mind/Game.cs index 57b135b..b6a2b0c 100644 --- a/Master Mind/Game.cs +++ b/Master Mind/Game.cs @@ -54,6 +54,42 @@ namespace Master_Mind this.go++; } + + public int[] ContainsCalcIHateThis(int row) + { + int[] retVal = new int[4]; + int[] seqCache = new int[4]; + + this.sequence.CopyTo(seqCache, 0); + + for (int i = 0; i < 4; i++) + { + if (this.board[row, i] == seqCache[i]) + { + seqCache[i] = -1; + + retVal[i] = 1; + } + } + + for (int i = 0; i < 4; i++) + { + if (retVal[i] == 1) + continue; + + for (int s = 0; s < 4; s++) + { + if (this.board[row, i] == seqCache[s]) + { + seqCache[s] = -1; + + retVal[i] = 2; + } + } + } + + return retVal; + } } public static void Play() diff --git a/Master Mind/Render.cs b/Master Mind/Render.cs index 1e750f9..73b9c02 100644 --- a/Master Mind/Render.cs +++ b/Master Mind/Render.cs @@ -10,7 +10,7 @@ namespace Master_Mind { public static void RenderBoard(Game.GameData board) { - for (int x = 0; x < 12; x++) + for (int x = 0; x < (board.go); x++) { for (int y = 0; y < 4; y++) { @@ -22,38 +22,39 @@ namespace Master_Mind Console.Write(" "); + int[] checks = board.ContainsCalcIHateThis(x); + + Console.BackgroundColor = ConsoleColor.White; + Console.Write(" "); + for (int y = 0; y < 4; y++) { - if (board.board[x, y] == board.sequence[y]) + switch (checks[y]) { - Console.BackgroundColor = ConsoleColor.DarkGreen; - } - else if (board.sequence.Contains(board.board[x,y])) - { - Console.BackgroundColor = ConsoleColor.DarkRed; - } - else - { - Console.BackgroundColor = ConsoleColor.Black; + case 1: + Console.BackgroundColor = ConsoleColor.DarkGreen; + break; + + case 2: + Console.BackgroundColor = ConsoleColor.DarkRed; + break; + + case 0: + Console.BackgroundColor = ConsoleColor.Black; + break; } Console.Write(" "); } - Console.BackgroundColor = ConsoleColor.Black; + Console.BackgroundColor = ConsoleColor.White; + 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()