Saturday, 15 March 2014

sudoku - C# Read int numbers to array -



sudoku - C# Read int numbers to array -

using system; using system.io; namespace sudoku { class game { private int[,] puzzle = new int[9, 9]; public void savetofile() { streamwriter str = new streamwriter("sudoku.txt"); (int = 0; < 9; ++i) { (int j = 0; j < 9; ++j) { str.write(puzzle[i, j] + " "); } str.write("\t\n"); } str.close(); } public void readfromfile() { clear(); streamreader str = new streamreader("sudoku.txt"); (int = 0; < 9; ++i) { (int j = 0; j < 9; ++j) { puzzle[i, j] = convert.toint32(str.read()); } } str.close(); } } }

i can not download info file. saving works fine , has view of txt file:

1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 1 4 3 6 5 8 9 7 3 6 5 8 9 7 2 1 4 8 9 7 2 1 4 3 6 5 5 3 1 6 4 2 9 7 8 6 4 2 9 7 8 5 3 1 9 7 8 5 3 1 6 4 2

how written in array 9x9 skipping gaps info written correctly?

instead of using str.read() require read single characters (or buffer specified), seek using str.readline() read single line each iteration of i.

public void readfromfile() { streamreader str = new streamreader("sudoku.txt"); (int = 0; < 9; ++i) { string[] linenumbers = str.readline().split(' '); (int j = 0; j < 9; ++j) { puzzle[i, j] = convert.toint32(linenumbers[j]); } } str.close(); }

this reads single line @ time (each iteration of i), splits line linenumbers separating current line space characters. each number on current line can accessed linenumbers[j] within inner loop (each iteration of j).

c# sudoku

No comments:

Post a Comment