MasterMind/Master Mind/Game.cs

141 lines
3.3 KiB
C#
Raw Normal View History

2023-05-25 11:33:44 +00:00
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++;
}
2023-05-25 11:50:53 +00:00
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;
}
2023-05-25 11:33:44 +00:00
}
public static void Play()
{
Console.Clear();
int opt = Menu.NumberMenu(new string[] { "You Guesses", "Computer Guess" });
Console.Clear();
Console.WriteLine("Loading...");
2023-05-26 07:42:05 +00:00
GameData game = new GameData();
2023-05-25 11:33:44 +00:00
2023-05-26 07:42:05 +00:00
if (opt == 1)
2023-05-25 11:33:44 +00:00
{
2023-05-26 07:42:05 +00:00
Random rand = new Random();
2023-05-25 11:33:44 +00:00
2023-05-26 07:42:05 +00:00
for (int i = 0; i < 4; i++)
2023-05-25 11:33:44 +00:00
{
2023-05-26 07:42:05 +00:00
int col = rand.Next(6);
2023-05-25 12:58:09 +00:00
2023-05-26 07:42:05 +00:00
game.sequence[i] = col;
2023-05-25 11:33:44 +00:00
}
2023-05-26 07:42:05 +00:00
}
else
{
game.sequence = Render.GetColorInput();
}
2023-05-25 11:33:44 +00:00
2023-05-26 07:42:05 +00:00
do
{
if (opt == 1)
{
game.AddRow(Render.GetColorInput());
2023-05-25 12:58:09 +00:00
}
2023-05-26 07:42:05 +00:00
else
2023-05-25 12:58:09 +00:00
{
game.AddRow(AI.ShittyGuess(game));
2023-05-26 07:42:05 +00:00
}
2023-05-25 12:58:09 +00:00
2023-05-26 07:42:05 +00:00
Console.Clear();
2023-05-25 12:58:09 +00:00
2023-05-26 07:42:05 +00:00
Render.RenderBoard(game, (opt == 2));
2023-05-25 11:33:44 +00:00
2023-05-26 07:42:05 +00:00
Console.ReadLine();
} while (game.won == 0);
2023-05-25 11:33:44 +00:00
}
}
}