About a week ago Keith and I went on a ‘tramp’.. thats trekking. We discussed Sudoku (apologies to Helen, Phil, Nora and others for starting the challenge without you..there was lots of time while walking to discuss strategy).
A single test project is VS2010. Everything in one file (!) and static methods to make starting easier.
So I wanted a method to check if Array has only 1 instance of each number.
An array is Enumerable.
[TestMethod]
public void AllHorizonalLinesPass()
{
int[,] sudokuArray = new int[9, 9]{ {1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9}
};
bool result = DoAllHorizontalRowsPass(sudokuArray);
Assert.AreEqual(true, result);
}
[TestMethod]
public void AllVerticalColsPass()
{
int[,] sudokuArray = new int[9, 9]{ {1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2,2},
{3,3,3,3,3,3,3,3,3},
{4,4,4,4,4,4,4,4,4},
{5,5,5,5,5,5,5,5,5},
{6,6,6,6,6,6,6,6,6},
{7,7,7,7,7,7,7,7,7},
{8,8,8,8,8,8,8,8,8},
{9,9,9,9,9,9,9,9,9}
};
bool result = DoAllVerticalColsPass(sudokuArray);
Assert.AreEqual(true, result);
}
[TestMethod]
public void AllSquaresPass()
{
int[,] sudokuArray = new int[9, 9]{ {1,2,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{7,8,9,7,8,9,7,8,9},
{7,8,9,7,8,9,7,8,9},
{4,5,6,4,5,6,4,5,6},
{1,2,3,1,2,3,1,2,3},
{4,5,6,4,5,6,4,5,6},
{1,2,3,1,2,3,1,2,3},
{7,8,9,7,8,9,7,8,9},
};
bool result = DoAllSquaresPass(sudokuArray);
Assert.AreEqual(true, result);
}
[TestMethod]
public void LookAtOtherNumbersInRowAndTakeThoseOffTheListOfNumbersToTry()
{
int row = 2;
List<int> listOfNumbersToTry = new List<int>();
for (int i = 1; i < 10; i++)
listOfNumbersToTry.Add(i);
int[,] trySudokuArray = new int[9, 9]{ {1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,0,4,5,6,0,8,0}, // this one
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9},
{1,2,3,4,5,6,7,8,9}
};
LookAtOtherNumbersInRowAndTakeOutOnesWeDontNeedToCheck(trySudokuArray, row, listOfNumbersToTry);
Assert.AreEqual(3, listOfNumbersToTry.Count);
Assert.IsTrue(listOfNumbersToTry.Contains(3));
Assert.IsTrue(listOfNumbersToTry.Contains(7));
Assert.IsTrue(listOfNumbersToTry.Contains(9));
}