Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 123 additions & 57 deletions Mastermind/Mastermind.cs
Original file line number Diff line number Diff line change
@@ -1,86 +1,152 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace Mastermind
{
class Program
{
// possible letters in code
public static char[] letters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

// size of code
public static int codeSize = 4;

// number of allowed attempts to crack the code
public static int allowedAttempts = 10;

// number of tried guesses
public static int numTry = 0;

// test solution
public static char[] solution = new char[] {'a', 'b', 'c', 'd'};

// game board
public static string[][] board = new string[allowedAttempts][];


public static void Main()
static void Main(string[] args)
{
char[] guess = new char[4];

CreateBoard();
DrawBoard();
Console.WriteLine("Enter Guess:");
guess = Console.ReadLine().ToCharArray();
Game game = new Game(GenerateSolution());
int TurnsRemaining = 0;
for (int turns = 10; turns > 0; turns--)
{
Console.WriteLine($"You have {turns} tries left");
Console.WriteLine("Choose four letters: ");
string letters = Console.ReadLine();
Ball[] balls = new Ball[4];
for (int i = 0; i < 4; i++)
{
balls[i] = new Ball(letters[i].ToString());
}
Row row = new Row(balls);
string winningString = "";
foreach (var item in game.answer)
{
winningString = winningString + item;
}
if (letters == winningString)
{
Console.Clear();
Console.WriteLine("You Win");
Thread.Sleep(3000);
TurnsRemaining = turns;
break;
}
game.AddRow(row);
Console.WriteLine(game.Rows);

// leave this command at the end so your program does not close automatically
Console.ReadLine();
}
Console.WriteLine(TurnsRemaining > 0 ? "Great Job" : "Out Of Turns");
}

public static bool CheckSolution(char[] guess)
public static string[] GenerateSolution()
{
// Your code here
Random rnd = new Random();
List<string> available = new List<string> { "a", "b", "c", "d", "e", "f" };
string[] solution = new string[4];
int one = rnd.Next(1, 6);
solution[0] = available[one];
available.RemoveRange(one, 1);
int two = rnd.Next(1, 5);
solution[1] = available[two];
available.RemoveRange(two, 1);
int three = rnd.Next(1, 4);
solution[2] = available[three];
available.RemoveRange(three, 1);
int four = rnd.Next(1, 3);
solution[3] = available[four];
available.RemoveRange(four, 1);
//uncomment to print solution Console.WriteLine(solution[0] + solution[1] + solution[2] + solution[3]);
return solution;
}
}

return false;
class Game
{
private List<Row> rows = new List<Row>();
public string[] answer { get; private set; } = new string[4];

public Game(string[] answer)
{
this.answer = answer;
}
public static string GenerateHint(char[] guess)

private string Score(Row row)
{
// Your code here
return " ";
string[] answerClone = (string[])this.answer.Clone();
int red = 0;
for (int i = 0; i < 4; i++)
{
if (answerClone[i] == row.balls[i].Letter)
{
red++;
}
}

int white = 0;
for (int i = 0; i < 4; i++)
{
int foundIndex = Array.IndexOf(answerClone, row.balls[i].Letter);
if (foundIndex > -1)
{
white++;
answerClone[foundIndex] = null;
}
}
return $" {red} - {white - red}";
}
public static void InsertCode(char[] guess)

public void AddRow(Row row)
{
// Your code here
this.rows.Add(row);
}
public static void CreateBoard()

public string Rows
{
for (var i = 0; i < allowedAttempts; i++)
get
{
board[i] = new string[codeSize + 1];
for (var j = 0; j < codeSize + 1; j++)

foreach (var row in this.rows)
{
board[i][j] = " ";
Console.Write(row.Balls);
Console.WriteLine(Score(row));
}
return "";
}
}

public static void DrawBoard()
}

class Ball
{
public string Letter { get; private set; }

public Ball(string letter)
{
for (var i = 0; i < board.Length; i++)
{
Console.WriteLine("|" + String.Join("|", board[i]));
}

this.Letter = letter;
}

public static void GenerateRandomCode() {
Random rnd = new Random();
for(var i = 0; i < codeSize; i++)
}

class Row
{
public Ball[] balls = new Ball[4];

public Row(Ball[] balls)
{
this.balls = balls;
}

public string Balls
{
get
{
solution[i] = letters[rnd.Next(0, letters.Length)];
foreach (var ball in this.balls)
{
Console.Write(ball.Letter);
}
return "";
}
}
}
}
}
26 changes: 26 additions & 0 deletions gameboy/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/gameboy.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
15 changes: 15 additions & 0 deletions gameboy/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/gameboy.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
Loading