简体   繁体   中英

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

I am writing a Sudoku solver in java and wonder how to assign the value in a string to a 2d array by using charAt or something else? for example, this is the printboard looks like:

     1  2  3    4  5  6      7  8  9      
  +-----------+-----------+-----------+
A |  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  |
I |  0  0  0  |  4  0  9  |  0  6  0  |
  +-----------+-----------+-----------+

and this is the way I am using to assign values to the printboard so far:

public void printBoard() {

        System.out.print("   ");
        for (int j = 0; j < 3; j++)
            System.out.print("  " + (j+1));
        System.out.print("  ");
        for (int j = 3; j < 6; j++)
            System.out.print("  " + (j+1)); 
        System.out.print("    ");
        for (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';
        for (int i = 0; i < 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");
        for (int i = 3; i < 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");
        for (int i = 6; i < 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 to know how can I use a 81 digit string like :"000005090140000670080002451063070010900000003007200080026000035000409060" to assign values to printboard in certain position, 0 represents unsolved.

To strictly answer the question, the formula

81 / 9 * row + col

will give you the character position in the string that represents the board position that you want, as long as you use zero indexed rows and columns. So something like

public String GetBoardValue(int row, int col)
{
    return boardString.CharAt(81 / 9 * row + col);
}

Be careful though, code like this is very fragile and prone to break. The numbers 81 and 9 used here only work when the string is exactly 81 characters long. 'Magic numbers' in programming are bad.

As markus has mentioned you need to think about what you are trying to achieve here. A pure string representation of the board in this manner has no intrinsic value or meaning. At least the 2D array is a data representation that more closely matches the Sudoku game board. However creating your own object to encapsulate the board data and provide accessors specific to the Sudoku board would be a much better way to go. It would give the data meaning and be much easier to work with. The goal of programming is to write code that makes sense so that someone else reading your code understands it and can work with it. Because in many cases that person will be you a couple of months down the track when you haven't looked at this code for a while. If it takes you time to read the code just to understand what you meant, then there are better ways to write it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM