Labyrinth7/Labyrinth7Game — pull request #19 by Enlightment (2014-06-01 08:22:02+00:00)
Title: Labyrinth Generator as Labyrinth Factory creating instance of class Labyrinth 

Summary: Labyrinth array from type string[,] to type char[,]; Replacing array values with constants WallSymbol, BlankSymbol, PlayerSymbol; Convert static class Labyrinth to an instanceable class. Move labyrinth array in Labyrinth class and rename it matrix. Create an instance of type Labyrinth with name GameBoard; Move methods DisplayLabyrinth and labyrinthGenerator. Create Labyrinthfactory class where labyrinth gets validated

Full:

Change labyrinth array type from string[,] to char[,]. Make symbols ‘*’ and ‘-’ and ‘x’ as contants.

public const char wallSymbol = 'x';
public const char blankSymbol = '-';
public const char playerSymbol = '*';

Change Labyrinth static class to class that can create an instance of type Labyrinth. It needs to contain indexer. Remove static attributes to class and methods. Add constructor receiving number ot rows and cols as well the current game player.

Hide char[,] labyrinth inside as private field and rename it to matrix.

LabyrinthGame.cs:
Remove public bool isInLabyrinth;
Replace private char[,] labyrinth with property named GameBoard with type Labyrinth
public Labyrinth GameBoard
{
get;
private set;
}
Replace (incl. static calls):
l	labyrinth = new char[7, 7];

Labyrinth.isLabyrinthValid = false;

while (Labyrinth.isLabyrinthValid == false)
{
Labyrinth.LabyrinthGenerator(labyrinth, initialPlayerPositionX, initialPlayerPositionY);
// Check is the generated labyrinth has a right path
Labyrinth.SolutionChecker(labyrinth, initialPlayerPositionX, initialPlayerPositionY);
}

WITH:
this.GameBoard = new Labyrinth(labyrinthRows, labyrinthCols, this.Player);

Rename in Labyrinth.cs
public static void DisplayLabyrinth(string[,] labyrinth)
To:
public void Display()

Refactor Display() method to use this.matrix / Use StringBuilder before outpurt using Console.WriteLine(). Add Player position on the labyrinth.

Rename in LabyrinthGame.cs:
Labyrinth.DisplayLabyrinth(this.labyrinth);
To:

this.GameBoard.Display();

Labyrinth.cs:
Remove public static bool isLabyrinthValid -> the validation is done independently from the class using its method.
Rename LabyrinthGenerator() to FillMatrix();

public void LabyrinthGenerator(char[,] labyrinth, int x, int y)
To:
public void FillMatrix()

public void SolutionChecker(string[,] labyrinth, int x, int y)
To:
public void CheckIfAnyExit(char[,] labyrinth, int x, int y)

Refactor wrong logic of FillMatrix()
Remove labyrinth[i, j] = Convert.ToString(randomInt.Next(2)); and Replace it with check what is the next random int and assign a symbol to the matrix according to the result. The matrix is already referred by this.matrix (rename labyrinth).
Rename i to rows and j to cols and replace magic number 7 to const.
The matrix doesn’t have to know about the player’s position, but it has to make the center blank. Replace
labyrinth[x, y] = playerSymbol;
to
this.matrix[initialPlayerPositionX, initialPlayerPositionY] = '-';

VALIDATION of the labyrinth now is done in Labyrinth constructor using:
while (!CheckIfAnyExit((char[,])this.matrix.Clone(), initialPlayerPositionX, initialPlayerPositionY))
{
FillMatrix();
}
This way the matrix gets filled with new symbols until it gets suitable.

Add properties for Matrix Center position - X and Y - calculated in the constructor. They are the same as the player initial position x and y, but this way the two classes get separated.

In the same way CheckIfAnyExit() -> gets only one parameter of type Labyrinth.

CREATING LabyrinthFactory. Exporting labyrinth validation logic from LabyrinthGame.cs and Labyrinth.cs to this file.

Moving method public bool CheckIfAnyExit(Labyrinth labyrinth) to the newly created factory and refactor it to return a boolean:

Labyrinth needs to be cloned for CheckIfAnyExit() method to work without harming the actual Labyrinth object, but Labyrinth major variable is a private field matrix which cannot be cloned in the traditional way implementing ICloneable interface. Thats why we use serialization.
Because we use clone of the object Labyrinth we don’t need to change its matrix values back to 0, so we remove both loops in Catch(Exception).