Getting started

In any code situation like this, the first thing we must do is remove the code in question from the environment where we have no control. In this case, we can't test the code if it is sitting in Program.main. So, let's grab the whole thing and put it into a class named Mastermind. We will have a single function named Play that will run the game. This is considered a safe refactoring, because we are not changing any of the existing code, simply moving it somewhere else.

In the file Program.cs:

class Program
{
static void Main(string[] args)
{
var game = new Mastermind();
game.Play(args);
}
}

In the file Mastermind.cs:

class Mastermind
{
public void Play(string[] args)
{
char[] g;
char[] p = new[] { 'A', 'A', 'A', 'A' };
int i = 0;
int j = 0;
int x = 0;
int c = 0;
Random rand = new Random(DateTime.Now.Millisecond);
if (args.Length > 0 && args[0] != null) p = args[0].ToCharArray();
else goto randomize_password;
guess: Console.Write("Take a guess: ");
g = Console.ReadLine().ToArray();
i = i + 1;
if (g.Length != 4) goto wrong_size;
if (g == p) goto success;
x = 0;
c = 0;
check_loop:
if (g[x] > 65 + 26) g[x] = (char)(g[x] - 32);
if (g[x] == p[x]) Console.Write("+", c = c + 1);
else if (p.Contains(g[x])) Console.Write("-");
x = x + 1;
if (x < 4) goto check_loop;
Console.WriteLine();
if (c == 4) goto success;
goto guess;
success: Console.WriteLine("Congratulations you guessed the
password in " + i + " tries.");
goto end;
wrong_size: Console.WriteLine("Password length is 4.");
goto guess;
randomize_password: j = 0;
password_loop: p[j] = (char)(rand.Next(6) + 65);
j = j + 1;
if (j < 4) goto password_loop;
goto guess;
end: Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}

Running the code again at this point shows that everything still works. The next step is a cosmetic one; let's spread the Play method out into sections. This should help us determine what private methods exist inside the large public method.

In the file Mastermind.cs:

class Mastermind
{
public void Play(string[] args)
{
// Variable Declarations - Global??
char[] g;
char[] p = new[] { 'A', 'A', 'A', 'A' };
int i = 0;
int j = 0;
int x = 0;
int c = 0;
// Initialize randomness
Random rand = new Random(DateTime.Now.Millisecond);
// Determine if a password was passed in?
if (args.Length > 0 && args[0] != null) p = args[0].ToCharArray();
else goto randomize_password; // Create a password if one was not
provided
// Player move - guess the password
guess: Console.Write("Take a guess: ");
g = Console.ReadLine().ToArray();
i = i + 1;
if (g.Length != 4) goto wrong_size;
if (g == p) goto success;
x = 0;
c = 0;
// Check if the password provided by the player is correct
check_loop:
if (g[x] > 65 + 26) g[x] = (char)(g[x] - 32);
if (g[x] == p[x]) Console.Write("+", c = c + 1);
else if (p.Contains(g[x])) Console.Write("-");
x = x + 1;
if (x < 4) goto check_loop; // Still checking??
Console.WriteLine();
if (c == 4) goto success; // Password must have been correct
goto guess; // No correct, try again
// Game over you win
success: Console.WriteLine("Congratulations you guessed the
password in " + i + " tries.");
goto end;
// Password guess was wrong size - Error Message
wrong_size: Console.WriteLine("Password length is 4.");
goto guess;
// Create a random password
randomize_password: j = 0;
password_loop: p[j] = (char)(rand.Next(6) + 65);
j = j + 1;
if (j < 4) goto password_loop;
goto guess; // Start the game
// Game is complete - exit
end: Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}

We have now used whitespace to split the program into several pieces and added comments explaining what we think each piece is doing. At this point, we are almost ready to begin testing. We have just a couple things in the way, the worst of which is the Console class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.29.139