this struct can help
public struct SudokuCell
{
private int value;
private int x;
private int y;
public SudokuCell(int x, int y)
{
if (((x < 0) || (x >= 9)) || ((y < 0) || (y >= 9)))
{
throw new ArgumentException("the values of the state of cell(0-8)");
}
this.x = x;
this.y = y;
this.value = 0;
}
public SudokuCell(int x, int y, int value) : this(x, y)
{
if ((value < 1) || (value > 9))
{
throw new ArgumentException("the values of the cells (1-9)");
}
this.value = value;
}
public int X
{
get
{
return this.x;
}
}
public int Y
{
get
{
return this.y;
}
}
public int Value
{
get
{
return this.value;
}
}
}
and one class to work with the struct
public class SudokuGameState
{
private SudokuCell[] originalProblem;
private SudokuCell[] playerCells;
public SudokuGameState(SudokuCell[] originalProblem, SudokuCell[] playerCells)
{
if (originalProblem == null)
{
throw new ArgumentNullException("originalProblem");
}
this.originalProblem = originalProblem;
this.playerCells = playerCells;
}
public SudokuCell[] OriginalProblem
{
get
{
return this.originalProblem;
}
}
public SudokuCell[] PlayerCells
{
get
{
return this.playerCells;
}
}
}
Finally i implement this to save and load the game:
public class SudokuIOHelper
{
public static SudokuGameState LoadFromFile(string filePath)
{
SudokuGameState state;
using (StreamReader reader = new StreamReader(filePath))
{
try
{
int num = int.Parse(reader.ReadLine());
SudokuCell[] originalProblem = new SudokuCell[num];
for (int i = 0; i < num; i++)
{
string[] strArray = reader.ReadLine().Split(new char[] { ',' });
originalProblem[i] = new SudokuCell(int.Parse(strArray[0]), int.Parse(strArray[1]), int.Parse(strArray[2]));
}
num = int.Parse(reader.ReadLine());
SudokuCell[] playerCells = new SudokuCell[num];
for (int j = 0; j < num; j++)
{
string[] strArray2 = reader.ReadLine().Split(new char[] { ',' });
playerCells[j] = new SudokuCell(int.Parse(strArray2[0]), int.Parse(strArray2[1]), int.Parse(strArray2[2]));
}
state = new SudokuGameState(originalProblem, playerCells);
}
catch (Exception exception)
{
throw new Exception("Ha ocurido lgun error leyendo el archivo. Verifique que tega el formato correcto", exception);
}
}
return state;
}
public static void SaveProblem(string filePath, int[,] initialProblem)
{
int num = 0;
for (int i = 0; i < initialProblem.GetLength(0); i++)
{
for (int j = 0; j < initialProblem.GetLength(1); j++)
{
if (initialProblem[i, j] != 0)
{
num++;
}
}
}
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(num);
for (int k = 0; k < initialProblem.GetLength(0); k++)
{
for (int m = 0; m < initialProblem.GetLength(1); m++)
{
if (initialProblem[k, m] != 0)
{
writer.WriteLine("{0},{1},{2}", k, m, initialProblem[k, m]);
}
}
}
writer.WriteLine(0);
}
}
public static void SaveToFile(string filePath, SudokuGameState gameState)
{
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(gameState.OriginalProblem.Length);
foreach (SudokuCell cell in gameState.OriginalProblem)
{
writer.WriteLine("{0},{1},{2}", cell.X, cell.Y, cell.Value);
}
if (gameState != null)
{
writer.WriteLine(gameState.PlayerCells.Length);
foreach (SudokuCell cell2 in gameState.PlayerCells)
{
writer.WriteLine("{0},{1},{2}", cell2.X, cell2.Y, cell2.Value);
}
}
else
{
writer.WriteLine(0);
}
}
}
}
I think that is all maybe very sun i finish the idea and do a sudoku game.
but before i try to implement the generator of sudoku with minimal cost...
So, Mathew Good appetite.
|