import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { private static String[][] sudoku; private static String[][] sudokuRespuesta; public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); sudoku = new String[9][9]; sudokuRespuesta = new String[9][9]; rellenar(); System.out.println("Juego de Sudoku:"); visualizar(); System.out.println("Intro las coordenadas:"); String e; do { try { e = br.readLine(); if (e.equalsIgnoreCase("fin")) { System.out.println("Me despido!!!"); } else { if (jugar(e)) { visualizar(); if (victoria()) { System.out.println("Has ganado!!!"); e = "fin"; } else { System.out.println("Intro las coordenadas:"); } } } } catch (IOException | NumberFormatException | StringIndexOutOfBoundsException ex) { System.out.println("Error, repite"); e = ""; } } while (!e.equalsIgnoreCase("fin")); } private static void rellenar() { // sudoku resuelto sudokuRespuesta[0] = "534678912".split(""); sudokuRespuesta[1] = "672195348".split(""); sudokuRespuesta[2] = "198342567".split(""); sudokuRespuesta[3] = "859761423".split(""); sudokuRespuesta[4] = "426853791".split(""); sudokuRespuesta[5] = "713924856".split(""); sudokuRespuesta[6] = "961537284".split(""); sudokuRespuesta[7] = "287419635".split(""); sudokuRespuesta[8] = "345286179".split(""); // rellenar aleatorio for (int x = 1; x <= 9; x++) { for (int y = 0; y < 5; y++) { int z = (int) (Math.random() * 8) + 1; jugar(x + "" + z + sudokuRespuesta[x - 1][z - 1]); } } } private static boolean victoria() { boolean es = true; for (int x = 0; x < sudoku.length && es; x++) { for (int y = 0; y < sudoku[x].length && es; y++) { if (sudoku[x][y] == null) { es = false; } } } return es; } private static boolean jugar(String e) { int x = Integer.parseInt(e.substring(0, 1)) - 1; int y = Integer.parseInt(e.substring(1, 2)) - 1; return entrada(x, y, e.substring(2, 3)); } private static void visualizar() { System.out.print(" 1 2 3 4 5 6 7 8 9 "); for (int x = 0; x < sudoku.length; x++) { if (x == 0 || x == 3 || x == 6) { System.out.println(" "); } System.out.print((x + 1) + "| "); for (int y = 0; y < sudoku[x].length; y++) { if (y == 2 || y == 5) { System.out.print((sudoku[x][y] == null) ? "_ | " : sudoku[x][y] + " | "); } else { System.out.print((sudoku[x][y] == null) ? "_ " : sudoku[x][y] + " "); } } System.out.println("|"); } } private static boolean entrada(int x, int y, String v) { boolean es = false; if (sudokuRespuesta[x][y].equalsIgnoreCase(v)) { sudoku[x][y] = v; es = true; } else { System.out.println("Valor no válido"); } return es; } }[/aux_code]
Sudoku
con
No hay comentarios
[aux_code language=»javascript» theme=»tomorrow» title=»Sudoku» extra_classes=»»]