-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (125 loc) · 3.95 KB
/
Program.cs
File metadata and controls
149 lines (125 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
namespace QuadaxProgrammingExcercise
{
class Program
{
static void Main(string[] args)
{
string answer = GenerateAnswer();
Boolean guessedCorrectly = PlayGame(answer);
Finish(guessedCorrectly);
return;
}
static string GenerateAnswer()
{
int digitToAdd;
string answer = "";
Random random = new Random();
for (int i = 0; i < 4; i++)
{
digitToAdd = random.Next(1, 7);
answer += digitToAdd.ToString();
}
return answer;
}
static Boolean PlayGame(string answer)
{
int numberOfTries = 0;
string guess;
string results;
Boolean guessedCorrectly = false;
while (!guessedCorrectly && numberOfTries <= 10)
{
results = "";
numberOfTries++;
guess = GetGuess();
if (guess.Equals(answer))
{
guessedCorrectly = true;
}
else
{
results += GetPlusses(answer, guess);
results += GetMinuses(answer, guess, results);
}
Console.WriteLine(ResultsReversed(results));
}
return guessedCorrectly;
}
static string GetGuess()
{
Console.WriteLine("Enter your guess:");
string guess = Console.ReadLine();
return guess;
}
static string GetPlusses(string answer, string guess)
{
string plusses = "";
for (int i = 0; i < 4; i++)
{
if (answer[i].Equals(guess[i]))
{
plusses += '+';
}
}
return plusses;
}
static string GetMinuses(string answer, string guess, string plusses)
{
int numberOfMinusesToAdd = 0;
string minuses = "";
List<char> distinctDigitsInGuess = new List<char>();
distinctDigitsInGuess.Add(guess[0]);
//use only the distinct digits in the guess to check with so that there are no repeated - for the same digit
for (int i = 0; i < 4; i++)
{
for (int k = i; k < 4; k++)
{
if (guess[i] != guess[k] && !distinctDigitsInGuess.Contains(guess[k]))
{
distinctDigitsInGuess.Add(guess[k]);
}
}
}
for (int i = 0; i < distinctDigitsInGuess.Count; i++)
{
for (int k = 0; k < 4; k++)
{
if (answer[k].Equals(distinctDigitsInGuess[i]))
{
numberOfMinusesToAdd++;
}
}
}
//remove instances of - being added when it should be a +
if (!plusses.Equals(""))
{
numberOfMinusesToAdd -= plusses.Length;
}
for (int i = 1; i <= numberOfMinusesToAdd; i++)
{
minuses += '-';
}
return minuses;
}
//reverse the results so -/+ are in right order
static string ResultsReversed(string results)
{
char[] arrayOfPlussesAndMinuses = results.ToCharArray();
Array.Reverse(arrayOfPlussesAndMinuses);
return new string(arrayOfPlussesAndMinuses);
}
static void Finish(Boolean guessedCorrectly)
{
if (guessedCorrectly)
{
Console.WriteLine("You win!");
}
else
{
Console.WriteLine("You lost!");
}
}
}
}