From: David Tran Date: 2005-08-25T03:50:39+09:00 Subject: Re: [QUIZ] Sodoku Solver (#43) The way I translate the Java's continue label is not "ruby way". The java's continue label is like: static void ShowAllAns() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (puzzle[i][j] <= 0) { next_value: for (int value = 1; value <= 9; value++) { // On the same column, make sure no any row already used it // On the same row, make sure no any column already used it for (int k = 0; k < 9; k++) { if ((puzzle[k][j] == value) || (puzzle[i][k] == value)) { continue next_value; // continue with label } } // On the 3x3 box, make sure no other cell already used it int p = (i / 3) * 3; int q = (j / 3) * 3; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (puzzle[p+x][q+y] == value) { continue next_value; // continue with label } } } puzzle[i][j] = value; ShowAllAns(); puzzle[i][j] = 0; } return; // tried all possible value, just return } } } PrintPuzzle(); } ---------------------------- I translated like : def solve 9.times do |row| 9.times do |col| next if @ary[row][col] # find next possible value for @ary[row][col] 1.upto(9) do |v| # check on same row/col, no any col/row already used it next unless 9.times { |i| break if @ary[i][col] == v || @ary[row][i] ==v } # check on 3x3 box, no other cell already used it next unless 3.times do |i| break unless 3.times do |j| break if @ary[i + row / 3 * 3][j + col / 3 * 3] == v end end @ary[row][col] = v if solve return true else @ary[row][col] = nil # backtracking end end return false end end true end ------------------------------- Using too many breaks to simulate Java's continue label... I almost forget Ruby's throw-catch. I think translate Java's continue label the "Ruby" way maybe like this: def solve 9.times do |row| 9.times do |col| next if @ary[row][col] # find next possible value for @ary[row][col] 1.upto(9) do |v| catch :next do # check on same row/col, no any col/row already used it 9.times { |i| throw :next if @ary[i][col] == v || @ary[row][i] ==v } # check on 3x3 box, no other cell already used it 3.times do |i| 3.times do |j| throw :next if @ary[i + row / 3 * 3][j + col / 3 * 3] == v end end @ary[row][col] = v if solve return true else @ary[row][col] = nil # backtracking end end end return false end end true end