Sunday 15 June 2014

java - How to assign the value in a string to a 2d array? -



java - How to assign the value in a string to a 2d array? -

i writing sudoku solver in java , wonder how assign value in string 2d array using charat or else? example, printboard looks like:

1 2 3 4 5 6 7 8 9 +-----------+-----------+-----------+ | 0 0 0 | 0 0 5 | 0 9 0 | b | 1 4 0 | 0 0 0 | 6 7 0 | c | 0 8 0 | 0 0 2 | 4 5 1 | +-----------+-----------+-----------+ d | 0 6 3 | 0 7 0 | 0 1 0 | e | 9 0 0 | 0 0 0 | 0 0 3 | f | 0 1 0 | 0 9 0 | 5 2 0 | +-----------+-----------+-----------+ g | 0 0 7 | 2 0 0 | 0 8 0 | h | 0 2 6 | 0 0 0 | 0 3 5 | | 0 0 0 | 4 0 9 | 0 6 0 | +-----------+-----------+-----------+

and way using assign values printboard far:

public void printboard() { system.out.print(" "); (int j = 0; j < 3; j++) system.out.print(" " + (j+1)); system.out.print(" "); (int j = 3; j < 6; j++) system.out.print(" " + (j+1)); system.out.print(" "); (int j = 6; j < 9; j++) system.out.print(" " + (j+1)); system.out.print(" "); system.out.println(); system.out.print(" +-----------+-----------+-----------+\n"); char row_letter = 'a'; (int = 0; < 3; i++) { system.out.print(row_letter + " |"); row_letter++; system.out.print(" " + board[i][0] + " " + board[i][1] + " " + board[i][2] + " |" + " " + board[i][3] + " " + board[i][4] + " " + board[i][5] + " |" + " " + board[i][6] + " " + board[i][7] + " " + board[i][8] + " |"); system.out.println(""); } system.out.print(" +-----------+-----------+-----------+\n"); (int = 3; < 6; i++) { system.out.print(row_letter + " |"); row_letter++; system.out.print(" " + board[i][0] + " " + board[i][1] + " " + board[i][2] + " |" + " " + board[i][3] + " " + board[i][4] + " " + board[i][5] + " |" + " " + board[i][6] + " " + board[i][7] + " " + board[i][8] + " |"); system.out.println(""); } system.out.print(" +-----------+-----------+-----------+\n"); (int = 6; < 9; i++) { system.out.print(row_letter + " |"); row_letter++; system.out.print(" " + board[i][0] + " " + board[i][1] + " " + board[i][2] + " |" + " " + board[i][3] + " " + board[i][4] + " " + board[i][5] + " |" + " " + board[i][6] + " " + board[i][7] + " " + board[i][8] + " |"); system.out.println(""); } system.out.print(" +-----------+-----------+-----------+\n"); } public static void main(string[] args) { int[][] board = new int[9][9]; board[0][3] = 1; board[0][5] = 5; board[1][0] = 1; board[1][1] = 4; board[1][6] = 6; board[1][7] = 7; board[2][1] = 8; board[2][5] = 2; board[2][6] = 4; board[3][1] = 6; board[3][2] = 3; board[3][4] = 7; board[3][7] = 1; board[4][0] = 9; board[4][8] = 3; board[5][1] = 1; board[5][4] = 9; board[5][6] = 5; board[5][7] = 2; board[6][2] = 7; board[6][3] = 2; board[6][7] = 8; board[7][1] = 2; board[7][2] = 6; board[7][7] = 3; board[7][8] = 5; board[8][3] = 4; board[8][5] = 9;

i want know how can utilize 81 digit string like:"000005090140000670080002451063070010900000003007200080026000035000409060" assign values printboard in position, 0 represents unsolved.

to strictly reply question, formula

81 / 9 * row + col

will give character position in string represents board position want, long utilize 0 indexed rows , columns. like

public string getboardvalue(int row, int col) { homecoming boardstring.charat(81 / 9 * row + col); }

be careful though, code fragile , prone break. numbers 81 , 9 used here work when string 81 characters long. 'magic numbers' in programming bad.

as markus has mentioned need think trying accomplish here. pure string representation of board in manner has no intrinsic value or meaning. @ to the lowest degree 2d array info representation more closely matches sudoku game board. creating own object encapsulate board info , provide accessors specific sudoku board much improve way go. give info meaning , much easier work with. goal of programming write code makes sense else reading code understands , can work it. because in many cases person couple of months downwards track when haven't looked @ code while. if takes time read code understand meant, there improve ways write it.

java arrays sudoku

No comments:

Post a Comment