I have been working on this program for a good hour or two now and every time I attempt to debug it visual studio is deciding to change the values in currentGuess.guessLine and I have no idea why. They're not being passed by reference any where and the only time they should change is under the comment 'Get guess from user'. Could someone please help as it's driving me crazy? I have provided the code below: (I have removed any completely irrelevant methods to make it shorter.
I am using visual studio professional and I have re-written this code 3 times now and I still cannot figure out why it is happening. Any help would be greatly appreciated.
struct History
{
public Guess[] previousGuesses;
public int noOfPreviousGuesses;
}
struct Guess
{
public int[] guessLine;
public Hint givenHint;
}
struct Hint
{
public int blackPegs;
public int whitePegs;
}
static void Main(string[] args)
{
Mastermind(3, 3);
}
static void Mastermind(int N, int M)
{
bool gameFinished;
int playAgain;
History gameHistory;
Guess currentGuess;
int[] secretCode;
do
{
//Game start
gameFinished = false;
//Reset history
gameHistory.previousGuesses = new Guess[0];
gameHistory.noOfPreviousGuesses = 0;
//Generate secretCode
secretCode = GenerateSecretCode(N, M);
do
{
//Get guess from user
currentGuess.guessLine = GetGuessLine(N, M);
//Evaluate guess
gameFinished = EvaluateGuess(currentGuess.guessLine, secretCode, N);
if (gameFinished == false)
{
//Generate hint
currentGuess.givenHint = GenerateHint(currentGuess.guessLine, secretCode, N);
//Add guess to history
gameHistory = AddGuessToHistoryQueue(currentGuess, gameHistory);
//Output history
OutputHistory(gameHistory, N);
}
} while (gameFinished == false);
//Ask to play again
playAgain = GetValueFromUser("Enter 0 or a positive value to play again, otherwise enter a negative value: ");
} while (playAgain >= 0);
}
/// <summary>
/// Gets a guess line from the user.
/// Validates each value using above procedure.
/// </summary>
/// <param name="codeLength">The length of the code being used.</param>
/// <param name="noOfColours">The number of colours allowed.</param>
/// <returns>The line entered.</returns>
static int[] GetGuessLine(int codeLength, int noOfColours)
{
int[] guessLine;
guessLine = new int[codeLength];
for (int count = 0; count < codeLength; count++)
{
//Get guessLine[count] from user
guessLine[count] = GetValueFromUserInRange(1, noOfColours, "Please enter guess at position " + count + ": ");
}
return guessLine;
}
/// <summary>
/// Compares the given guess to the given code.
/// Returns true if guess and code match exactly otherwise
/// returns false.
/// </summary>
/// <param name="guess">The guess being compared.</param>
/// <param name="code">The code to be compared against.</param>
/// <param name="codeLength">The length of the code and guess.</param>
/// <returns></returns>
static bool EvaluateGuess(int[] guess, int[] code, int codeLength)
{
bool correctGuess;
correctGuess = true;
for (int count = 0; count < codeLength; count++)
{
if (guess[count] != code[count])
{
//Found inconsistency
correctGuess = false;
break;
}
}
return correctGuess;
}
/// <summary>
/// Generates a hint through finding all matching values,
/// changing their values and incrementing the black pegs total.
/// Then calculates white pegs by checking for matching values again.
/// </summary>
/// <param name="guess">The guess requiring a hint.</param>
/// <param name="code">The code for the guess to be compared to.</param>
/// <param name="codeLength">The length of the code/guess.</param>
/// <returns>The hint generated.</returns>
static Hint GenerateHint(int[] guess, int[] code, int codeLength)
{
Hint newHint;
newHint.blackPegs = 0;
newHint.whitePegs = 0;
//Calculate blackPegs
for (int count = 0; count < codeLength; count++)
{
if (guess[count] == code[count])
{
newHint.blackPegs = newHint.blackPegs + 1;
//Hide values
guess[count] = 0;
code[count] = 0;
}
}
//Calculate white pegs
for (int guessCount = 0; guessCount < codeLength; guessCount++)
{
//Ensure current guess value hasn't been used
if (guess[guessCount] != 0)
{
//Check for matching value in code
for (int codeCount = 0; codeCount < codeLength; codeCount++)
{
if (guess[guessCount] == code[codeCount])
{
//Found match
newHint.whitePegs = newHint.whitePegs + 1;
//Hide values
guess[guessCount] = 0;
code[codeCount] = 0;
}
}
}
}
return newHint;
}