Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class Main {
- private static Random Rnd = new Random();
- private static Cell[][] Board = new Cell[9][9];
- private static int[][][] Blks = new int[][][]
- {{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}},
- {{0, 3}, {0, 4}, {0, 5}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}},
- {{0, 6}, {0, 7}, {0, 8}, {1, 6}, {1, 7}, {1, 8}, {2, 6}, {2, 7}, {2, 8}},
- {{3, 0}, {3, 1}, {3, 2}, {4, 0}, {4, 1}, {4, 2}, {5, 0}, {5, 1}, {5, 2}},
- {{3, 3}, {3, 4}, {3, 5}, {4, 3}, {4, 4}, {4, 5}, {5, 3}, {5, 4}, {5, 5}},
- {{3, 6}, {3, 7}, {3, 8}, {4, 6}, {4, 7}, {4, 8}, {5, 6}, {5, 7}, {5, 8}},
- {{6, 0}, {6, 1}, {6, 2}, {7, 0}, {7, 1}, {7, 2}, {8, 0}, {8, 1}, {8, 2}},
- {{6, 3}, {6, 4}, {6, 5}, {7, 3}, {7, 4}, {7, 5}, {8, 3}, {8, 4}, {8, 5}},
- {{6, 6}, {6, 7}, {6, 8}, {7, 6}, {7, 7}, {7, 8}, {8, 6}, {8, 7}, {8, 8}}};
- public static void main(String[] args) {
- while (true) {
- for (int x = 0; x < 9; x++) {
- for (int y = 0; y < 9; y++) {
- Board[x][y] = new Cell();
- }
- }
- List<Integer> ValidBlks = new ArrayList<Integer>();
- for (int i =0; i < 9; i++) { ValidBlks.add(i); }
- Boolean Retry = false;
- do {
- List<Integer> ValidCells = new ArrayList<Integer>();
- for (int i =0; i < 9; i++) { ValidCells.add(i); }
- Integer B = ValidBlks.get(Rnd.nextInt(ValidBlks.size()));
- ValidBlks.remove(B);
- do {
- Integer C = ValidCells.get(Rnd.nextInt(ValidCells.size()));
- int X = Blks[B][C][0]; int Y = Blks[B][C][1];
- Integer R[] = Board[X][Y].Process(Rnd);
- if (R[0] == 1) {
- Retry = true;
- break;
- } else {
- ValidCells.remove(C);
- for (int i = 0; i < 9; i++) {
- Board[X][i].ValidNbrs.remove(R[1]); Board[i][Y].ValidNbrs.remove(R[1]);
- Board[Blks[B][i][0]][Blks[B][i][1]].ValidNbrs.remove(R[1]);
- }
- }
- } while (!(ValidCells.size() == 0));
- if (Retry) break;
- } while (!(ValidBlks.size() == 0));
- if (!(Retry)) break;
- }
- System.out.print("\r\n");
- for (int x = 0; x < 3; x++) {
- System.out.print(" ");
- for (int y = 0; y < 3; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print(" ");
- for (int y = 3; y < 6; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print(" ");
- for (int y = 6; y < 9; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print("\r\n");
- }
- System.out.print("\r\n");
- for (int x = 3; x < 6; x++) {
- System.out.print(" ");
- for (int y = 0; y < 3; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print(" ");
- for (int y = 3; y < 6; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print(" ");
- for (int y = 6; y < 9; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print("\r\n");
- }
- System.out.print("\r\n");
- for (int x = 6; x < 9; x++) {
- System.out.print(" ");
- for (int y = 0; y < 3; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print(" ");
- for (int y = 3; y < 6; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print(" ");
- for (int y = 6; y < 9; y++) { System.out.print(Board[x][y].Value + " "); }
- System.out.print("\r\n");
- }
- }
- }
- public class Cell {
- public int Value = 0;
- public List<Integer> ValidNbrs = new ArrayList<Integer>();
- public Cell() { for (int i = 1; i < 10; i++) ValidNbrs.add(i); }
- public Integer[] Process(Random Rnd) {
- Integer[] R = new Integer[2];
- if (ValidNbrs.size() == 0) {
- R[0] = 1;
- return R;
- }
- Value = ValidNbrs.get(Rnd.nextInt(ValidNbrs.size()));
- R[0] = 0; R[1] = Value;
- return R;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement