Results 1 to 5 of 5
  1. #1
    Simplev_v's Avatar
    Simplev_v is offline Member
    Join Date
    Sep 2009
    Location
    Malaysia
    Posts
    10
    Rep Power
    0

    Post My Simple Hashing Program

    I have written a programme that accepts a name from the user, than extracts the first alphabet from the name and finds the hashcode for the name (the index where the name will be saved in an array).

    Can someone comment on my coding, please? It works as i have put some mental constrains (like i wont type two name that has the first alphabets :D).

    Can someone point out to me on how to make the programme into classes? I am just getting into doing java coding the OOP way.

    Thank you.

    Java Code:
    import java.io.*;   // to allow user to enter data using keyboard
    
    class CMDA_Q2 {
    
        public static void main (String[] args) throws IOException{
                
           String ch, name;
           int i=0;
    
           do {
            InputStreamReader rr = new InputStreamReader(System.in);
            BufferedReader inp = new BufferedReader(rr);
    
            System.out.println("Press [Enter] to exit the program");
            System.out.print ("Please Enter a Name:");
            name = inp.readLine();
           
            char tem1;
            int tem2 = 0;
            int chValue;
                                    // extracting the first alphabet from the input
            tem1 = name.charAt(0);
                                    // assigning a value for the alphabet
            if (tem1 == 'a' || tem1 == 'A'){
                   tem2 = 1;
            }
            if (tem1 == 'b' || tem1 == 'B') {
                      tem2 =2;
            }
            if (tem1 == 'c' || tem1 == 'C'){
                   tem2 = 3;
            }
            if (tem1 == 'd' || tem1 == 'D') {
                      tem2 = 4;
            }
            if (tem1 == 'e' || tem1 == 'E') {
               tem2 = 5;
            }
            if (tem1 == 'f' || tem1 == 'F'){
                   tem2 = 6;
            }
            if (tem1 == 'g' || tem1 == 'G') {
                      tem2 =7;
            }
            if (tem1 == 'h' || tem1 == 'H'){
                   tem2 = 8;
            }
            if (tem1 == 'i' || tem1 == 'I') {
                      tem2 = 9;
            }
            if (tem1 == 'j' || tem1 == 'J') {
               tem2 = 10;
            }
            if (tem1 == 'k' || tem1 == 'K'){
                   tem2 = 11;
            }
            if (tem1 == 'l' || tem1 == 'L') {
                      tem2 =12;
            }
            if (tem1 == 'm' || tem1 == 'M'){
                   tem2 = 13;
            }
            if (tem1 == 'n' || tem1 == 'N') {
                      tem2 = 14;
            }
            if (tem1 == 'o' || tem1 == 'O') {
               tem2 = 15;
            }
            if (tem1 == 'p' || tem1 == 'P'){
                   tem2 = 16;
            }
            if (tem1 == 'q' || tem1 == 'Q') {
                      tem2 =17;
            }
            if (tem1 == 'r' || tem1 == 'R'){
                   tem2 = 18;
            }
            if (tem1 == 's' || tem1 == 'S') {
                      tem2 = 19;
            }
            if (tem1 == 't' || tem1 == 'T') {
               tem2 = 20;
            }
            if (tem1 == 'u' || tem1 == 'U'){
                   tem2 = 21;
            }
            if (tem1 == 'v' || tem1 == 'V') {
                      tem2 =22;
            }
            if (tem1 == 'w' || tem1 == 'W'){
                   tem2 = 23;
            }
            if (tem1 == 'x' || tem1 == 'X') {
                      tem2 = 24;
            }
            if (tem1 == 'y' || tem1 == 'Y') {
               tem2 = 25;
            }
            if (tem1 == 'z' || tem1 == 'Z')  {
               tem2 = 26;
            }
                
           int hcode = 0;
                    // using modula method to find the unique key
           hcode = tem2%27;
           System.out.println("Hash Key Location in Array for " + name + " is at: "+hcode);
           System.out.println(" ");
    
           } while (name != " ");
           System.out.println ("System Exited");
          
        }
        
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Ok, as I mentioned in your other post, this guy is probably also fine as-is since it is quite a simple program. But, one way to start would be to divide the work up into methods instead of lumping it all together in the main method.

    Just for example's sake, take a look at this simple Sudoku puzzle solver I wrote. I don't expect you to be able to understand it all yet, nor did I comment my code (shame on me, but I just hacked this together for fun one afternoon for myself). You can however see how I made several classes and many methods, each with a specific task.

    Main.java
    Java Code:
    package sudoku2;
    
    /**
     * @author Steffen Gates
     */
    public class Main {
    
        public static void main(String[] args) {
            new Main();
        }
        int[][] input;
    
        Square[][] puzzle;
        boolean debug = false;
        Viewer v;
        public boolean wait = true;
        int step = 0;
        boolean automatic = true;
    
        public Main() {
            Input in = new Input(this);
            in.setVisible(true);
        }
    
        public void go(){
            v = new Viewer(this);
            puzzle = new Square[9][9];
            solve();
        }
    
        private void update() {
            v.update(puzzle);
        }
    
        private void solve() {
            //Create and fill puzzle with Square Objects
            //with blank or init values
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (input[i][j] != 0) {
                        puzzle[i][j] = new Square(input[i][j]);
                    } else {
                        puzzle[i][j] = new Square();
                    }
                }
            }
    
            update();
    
            //Print initial puzzle
            if (debug) {
                System.out.println("Init complete");
                for (int i = 0; i < 9; i++) {
                    for (int j = 0; j < 9; j++) {
                        System.out.print(puzzle[i][j].value > 0 ? puzzle[i][j].value + " " : "  ");
                    }
                    System.out.println("");
                }
            }
    
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    fillPossibles(i, j);
                }
            }
    
            update();
        }
    
        public void next() {
            if (!automatic) {
                switch (step) {
                    case 0:
                        findPuzzleSingles();
                        v.status = "Found Puzzle Singles";
                        update();
                        step++;
                        break;
                    case 1:
                        findRowOrphans();
                        v.status = "Found Row Orphans";
                        update();
                        step++;
                        break;
                    case 2:
                        findQuadOrphans();
                        v.status = "Found Quad Orphans";
                        update();
                        step = 0;
                        break;
                }
            } else {
                try {
                    findPuzzleSingles();
                    v.status = "Found Puzzle Singles";
                    update();
                    Thread.sleep(500);
                    findRowOrphans();
                    v.status = "Found Row Orphans";
                    update();
                    Thread.sleep(500);
                    findQuadOrphans();
                    v.status = "Found Quad Orphans";
                    update();
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
        }
    
        private void fillPossibles(int row, int col) {
            for (int i = 0; i < 9; i++) {
                if (puzzle[row][col].value != 0) {
                    continue;
                }
                if (!existsInRow(i + 1, row, col) && !existsInColumn(i + 1, col, row) && !existsInQuad(i + 1, getQuad(row, col), row, col)) {
                    puzzle[row][col].possibilities.add(i + 1);
                }
            }
        }
    
        private boolean existsInRow(int value, int row, int source) {
            for (int i = 0; i < 9; i++) {
                if (i == source) {
                    continue;
                }
                if (puzzle[row][i].value == value) {
                    return true;
                }
            }
            return false;
        }
    
        private boolean existsInColumn(int value, int column, int source) {
            for (int i = 0; i < 9; i++) {
                if (i == source) {
                    continue;
                }
                if (puzzle[i][column].value == value) {
                    return true;
                }
            }
            return false;
        }
    
        //Make sure that exists methods only check squares that are not the current one
        private boolean existsInQuad(int num, int quad, int sourceRow, int sourceColumn) {
            int[] c = setCount(quad);
            int il, ih = c[1], jl, jh = c[3];
    
            for (il = c[0]; il < ih; il++) {
                for (jl = c[2]; jl < jh; jl++) {
                    if (il == sourceRow && jl == sourceColumn) {
                        continue;
                    }
                    if (puzzle[il][jl].value == num) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        private void findPuzzleSingles() {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (puzzle[i][j].possibilities.size() == 1) {
                        puzzle[i][j].value = puzzle[i][j].possibilities.get(0);
                        puzzle[i][j].solved = true;
                        cleanUpColumn(j, puzzle[i][j].value);
                        cleanUpRow(i, puzzle[i][j].value);
                        cleanUpQuad(getQuad(i, j), puzzle[i][j].value);
                        puzzle[i][j].possibilities.clear();
                    }
                }
            }
        }
    
        private void findRowOrphans() {
            int count;
            for (int num = 1; num < 10; num++) {
                for (int row = 0; row < 9; row++) {
                    count = 0;
                    for (int col = 0; col < 9; col++) {
                        if (!puzzle[row][col].solved && puzzle[row][col].possibilities.contains(num)) {
                            count++;
                        }
                    }
                    if (count == 1) {
                        for (int i = 0; i < 9; i++) {
                            if (puzzle[row][i].possibilities.contains(num)) {
                                puzzle[row][i].possibilities.clear();
                                puzzle[row][i].solved = true;
                                puzzle[row][i].value = num;
                                cleanUpColumn(i, num);
                                cleanUpRow(row, num);
                                cleanUpQuad(getQuad(row, i), num);
                            }
                        }
                    }
                }
            }
        }
    
        private void findColumnOrphans() {
            int count;
            for (int num = 1; num < 10; num++) {
                for (int col = 0; col < 9; col++) {
                    count = 0;
                    for (int row = 0; row < 9; row++) {
                        if (!puzzle[row][col].solved && puzzle[row][col].possibilities.contains(num)) {
                            count++;
                        }
                    }
                    if (count == 1) {
                        for (int i = 0; i < 9; i++) {
                            if (puzzle[i][col].possibilities.contains(num)) {
                                puzzle[i][col].possibilities.clear();
                                puzzle[i][col].solved = true;
                                puzzle[i][col].value = num;
                                cleanUpRow(i, num);
                                cleanUpColumn(col, num);
                                cleanUpQuad(getQuad(col, i), num);
                            }
                        }
                    }
                }
            }
        }
    
        private void findQuadOrphans() {
            for (int quad = 1; quad < 10; quad++) {
                int[] c = setCount(quad);
                int il, ih = c[1], jl, jh = c[3];
                int count = 0;
    
                for (int num = 1; num < 10; num++) {
                    count = 0;
                    for (il = c[0]; il < ih; il++) {
                        for (jl = c[2]; jl < jh; jl++) {
                            if (puzzle[il][jl].possibilities.contains(num)) {
                                count++;
                            }
                        }
                    }
                    if (count == 1) {
                        for (il = c[0]; il < ih; il++) {
                            for (jl = c[2]; jl < jh; jl++) {
                                if (puzzle[il][jl].possibilities.contains(num)) {
                                    puzzle[il][jl].possibilities.clear();
                                    puzzle[il][jl].solved = true;
                                    puzzle[il][jl].value = num;
                                    cleanUpRow(il, num);
                                    cleanUpColumn(jl, num);
                                    cleanUpQuad(getQuad(il, jl), num);
                                }
                            }
                        }
                    }
                }
            }
        }
    
        private void cleanUpColumn(int col, int number) {
            for (int i = 0; i < 9; i++) {
                if (puzzle[i][col].possibilities.contains(number)) {
                    puzzle[i][col].possibilities.remove(puzzle[i][col].possibilities.indexOf(number));
                }
            }
        }
    
        private void cleanUpRow(int row, int number) {
            for (int i = 0; i < 9; i++) {
                if (puzzle[row][i].possibilities.contains(number)) {
                    puzzle[row][i].possibilities.remove(puzzle[row][i].possibilities.indexOf(number));
                }
            }
        }
    
        private void cleanUpQuad(int quad, int number) {
            int[] c = setCount(quad);
            int il, ih = c[1], jl, jh = c[3];
    
            for (il = c[0]; il < ih; il++) {
                for (jl = c[2]; jl < jh; jl++) {
                    if (puzzle[il][jl].possibilities.contains(number)) {
                        puzzle[il][jl].possibilities.remove(puzzle[il][jl].possibilities.indexOf(number));
                    }
                }
            }
        }
    
        /* With given x,y values representing the position of the
         * value in the array, this method returns the quad of said
         * value.
         *  1 2 3
         *  4 5 6
         *  7 8 9
         */
        private int getQuad(int x, int y) {
            int quad = 0;
    
            if (x < 3 && y < 3) {
                quad = 1;
            } else if (x < 3 && y > 2 && y < 6) {
                quad = 2;
            } else if (x < 3 && y > 5) {
                quad = 3;
            } else if (x > 2 && x < 6 && y < 3) {
                quad = 4;
            } else if (x > 2 && x < 6 && y > 2 && y < 6) {
                quad = 5;
            } else if (x > 2 && x < 6 && y > 5) {
                quad = 6;
            } else if (x > 5 && y < 3) {
                quad = 7;
            } else if (x > 5 && y > 2 && y < 6) {
                quad = 8;
            } else if (x > 5 && y > 5) {
                quad = 9;
            }
            return quad;
        }
    
        private int[] setCount(int quad) {
            int il = 0, jl = 0, ih = 0, jh = 0, set;
    
            switch (quad) {
                case 1:
                    il = 0;
                    ih = 3;
                    jl = 0;
                    jh = 3;
                    break;
                case 2:
                    il = 0;
                    ih = 3;
                    jl = 3;
                    jh = 6;
                    break;
                case 3:
                    il = 0;
                    ih = 3;
                    jl = 6;
                    jh = 9;
                    break;
                case 4:
                    il = 3;
                    ih = 6;
                    jl = 0;
                    jh = 3;
                    break;
                case 5:
                    il = 3;
                    ih = 6;
                    jl = 3;
                    jh = 6;
                    break;
                case 6:
                    il = 3;
                    ih = 6;
                    jl = 6;
                    jh = 9;
                    break;
                case 7:
                    il = 6;
                    ih = 9;
                    jl = 0;
                    jh = 3;
                    break;
                case 8:
                    il = 6;
                    ih = 9;
                    jl = 3;
                    jh = 6;
                    break;
                case 9:
                    il = 6;
                    ih = 9;
                    jl = 6;
                    jh = 9;
                    break;
            }
    
            int[] result = {il, ih, jl, jh};
            return result;
        }
    }
    Square.java
    Java Code:
    package sudoku2;
    
    import java.util.ArrayList;
    
    /**
     * @author Steffen Gates
     */
    public class Square {
        ArrayList<Integer> possibilities;
        int value;
        boolean solved;
        boolean initialValue;
    
        public Square(){
            possibilities = new ArrayList<Integer>();
            value = 0;
        }
    
        public Square(int value){
            initialValue = true;
            this.solved = true;
            this.value = value;
            possibilities = new ArrayList<Integer>();
        }
    }
    Viewer.java
    [code}
    package sudoku2;

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;

    import javax.swing.JFrame;

    public class Viewer extends JFrame {

    int offset = 22;
    BufferedImage img;
    Main m;
    int width = 600;
    int height = 650;
    int bottomBar = height-width;
    String status = "";

    public Viewer(Main m) {
    this.m = m;
    img = new BufferedImage(width, height + offset, BufferedImage.OPAQUE);
    Container content = this.getContentPane();
    this.setSize(width, height + offset);
    this.setBackground(Color.WHITE);
    this.setResizable(false);
    this.setVisible(true);
    this.addKeyListener(new KeyListener() {
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {
    if (e.getKeyChar() == ' ') {
    next();
    }
    }});
    }

    private void next(){
    m.next();
    }

    public void update(Square[][] puzzle){
    fillValues(puzzle);
    drawLines();
    Graphics g = img.getGraphics();
    g.setColor(Color.gray);
    g.fillRect(0, 600+offset+1, width, height);
    g.setColor(Color.BLACK);
    g.drawString(status, 10, 650);
    repaint();
    }

    public void repaint() {
    this.getGraphics().drawImage(img, 0, 0, this);
    }

    private void fillValues(Square[][] puzzle) {
    Graphics g = img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height+offset);
    for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
    if (puzzle[i][j].initialValue) {
    g.setColor(Color.RED);
    Font font = new Font("Arial", Font.BOLD, 36);
    g.setFont(font);
    g.drawString(puzzle[i][j].value + "", (((j + 1) * 66) - 40), ((i + 1) * 66 - 15 + offset));
    } else if (puzzle[i][j].solved) {
    g.setColor(Color.BLUE);
    Font font = new Font("Arial", Font.BOLD, 36);
    g.setFont(font);
    g.drawString(puzzle[i][j].value + "", (((j + 1) * 66) - 40), ((i + 1) * 66 - 15 + offset));
    } else {
    g.setColor(Color.GRAY);
    Font font = new Font("Arial", Font.PLAIN, 12);
    g.setFont(font);
    for (int z = 0; z < puzzle[i][j].possibilities.size(); z++) {
    switch (puzzle[i][j].possibilities.get(z)) {
    case (1):
    g.drawString(1 + "", (((j + 1) * 66) - 54), ((i + 1) * 66 - 48 + offset));
    break;
    case (2):
    g.drawString(2 + "", (((j + 1) * 66) - 38), ((i + 1) * 66 - 48 + offset));
    break;
    case (3):
    g.drawString(3 + "", (((j + 1) * 66) - 22), ((i + 1) * 66 - 48 + offset));
    break;
    case (4):
    g.drawString(4 + "", (((j + 1) * 66) - 54), ((i + 1) * 66 - 32 + offset));
    break;
    case (5):
    g.drawString(5 + "", (((j + 1) * 66) - 38), ((i + 1) * 66 - 32 + offset));
    break;
    case (6):
    g.drawString(6 + "", (((j + 1) * 66) - 22), ((i + 1) * 66 - 32 + offset));
    break;
    case (7):
    g.drawString(7 + "", (((j + 1) * 66) - 54), ((i + 1) * 66 - 16 + offset));
    break;
    case (8):
    g.drawString(8 + "", (((j + 1) * 66) - 38), ((i + 1) * 66 - 16 + offset));
    break;
    case (9):
    g.drawString(9 + "", (((j + 1) * 66) - 22), ((i + 1) * 66 - 16 + offset));
    break;
    }
    }
    }
    }
    }
    }

    private void drawLines() {
    Graphics g = img.getGraphics();
    g.setColor(Color.BLACK);

    g.drawLine(66, 0 + offset, 66, height + offset - bottomBar);
    g.drawLine(130, 0 + offset, 130, height + offset - bottomBar);
    g.drawLine(266, 0 + offset, 266, height + offset - bottomBar);
    g.drawLine(333, 0 + offset, 333, height + offset - bottomBar);
    g.drawLine(466, 0 + offset, 466, height + offset - bottomBar);
    g.drawLine(533, 0 + offset, 533, height + offset - bottomBar);

    g.drawLine(0, 66 + offset, width, 66 + offset);
    g.drawLine(0, 130 + offset, width, 130 + offset);
    g.drawLine(0, 266 + offset, width, 266 + offset);
    g.drawLine(0, 333 + offset, width, 333 + offset);
    g.drawLine(0, 466 + offset, width, 466 + offset);
    g.drawLine(0, 533 + offset, width, 533 + offset);
    g.drawLine(0, 600 + offset, width, 600 + offset);

    g.drawLine(199, 0 + offset, 199, height + offset - bottomBar);
    g.drawLine(200, 0 + offset, 200, height + offset - bottomBar);
    g.drawLine(201, 0 + offset, 201, height + offset - bottomBar);

    g.drawLine(399, 0 + offset, 399, height + offset - bottomBar);
    g.drawLine(400, 0 + offset, 400, height + offset - bottomBar);
    g.drawLine(401, 0 + offset, 401, height + offset - bottomBar);

    g.drawLine(0, 199 + offset, width, 199 + offset);
    g.drawLine(0, 200 + offset, width, 200 + offset);
    g.drawLine(0, 201 + offset, width, 201 + offset);

    g.drawLine(0, 399 + offset, width, 399 + offset);
    g.drawLine(0, 400 + offset, width, 400 + offset);
    g.drawLine(0, 401 + offset, width, 401 + offset);
    }
    }
    [/code]
    I omitted the Input.java class since it was generated by NetBeans and is very long for a web post (1600 lines). However, if you want to play with this app yourself, I would be happy to provide the last of the code. This is the meat of it though. Enjoy and good luck!

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Ok, as I mentioned in your other post, this guy is probably also fine as-is since it is quite a simple program. But, one way to start would be to divide the work up into methods instead of lumping it all together in the main method.

    Just for example's sake, take a look at this simple Sudoku puzzle solver I wrote. I don't expect you to be able to understand it all yet, nor did I comment my code (shame on me, but I just hacked this together for fun one afternoon for myself). You can however see how I made several classes and many methods, each with a specific task.

    Main.java
    Java Code:
    package sudoku2;
    
    /**
     * @author Steffen Gates
     */
    public class Main {
    
        public static void main(String[] args) {
            new Main();
        }
        int[][] input;
    
        Square[][] puzzle;
        boolean debug = false;
        Viewer v;
        public boolean wait = true;
        int step = 0;
        boolean automatic = true;
    
        public Main() {
            Input in = new Input(this);
            in.setVisible(true);
        }
    
        public void go(){
            v = new Viewer(this);
            puzzle = new Square[9][9];
            solve();
        }
    
        private void update() {
            v.update(puzzle);
        }
    
        private void solve() {
            //Create and fill puzzle with Square Objects
            //with blank or init values
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (input[i][j] != 0) {
                        puzzle[i][j] = new Square(input[i][j]);
                    } else {
                        puzzle[i][j] = new Square();
                    }
                }
            }
    
            update();
    
            //Print initial puzzle
            if (debug) {
                System.out.println("Init complete");
                for (int i = 0; i < 9; i++) {
                    for (int j = 0; j < 9; j++) {
                        System.out.print(puzzle[i][j].value > 0 ? puzzle[i][j].value + " " : "  ");
                    }
                    System.out.println("");
                }
            }
    
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    fillPossibles(i, j);
                }
            }
    
            update();
        }
    
        public void next() {
            if (!automatic) {
                switch (step) {
                    case 0:
                        findPuzzleSingles();
                        v.status = "Found Puzzle Singles";
                        update();
                        step++;
                        break;
                    case 1:
                        findRowOrphans();
                        v.status = "Found Row Orphans";
                        update();
                        step++;
                        break;
                    case 2:
                        findQuadOrphans();
                        v.status = "Found Quad Orphans";
                        update();
                        step = 0;
                        break;
                }
            } else {
                try {
                    findPuzzleSingles();
                    v.status = "Found Puzzle Singles";
                    update();
                    Thread.sleep(500);
                    findRowOrphans();
                    v.status = "Found Row Orphans";
                    update();
                    Thread.sleep(500);
                    findQuadOrphans();
                    v.status = "Found Quad Orphans";
                    update();
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
        }
    
        private void fillPossibles(int row, int col) {
            for (int i = 0; i < 9; i++) {
                if (puzzle[row][col].value != 0) {
                    continue;
                }
                if (!existsInRow(i + 1, row, col) && !existsInColumn(i + 1, col, row) && !existsInQuad(i + 1, getQuad(row, col), row, col)) {
                    puzzle[row][col].possibilities.add(i + 1);
                }
            }
        }
    
        private boolean existsInRow(int value, int row, int source) {
            for (int i = 0; i < 9; i++) {
                if (i == source) {
                    continue;
                }
                if (puzzle[row][i].value == value) {
                    return true;
                }
            }
            return false;
        }
    
        private boolean existsInColumn(int value, int column, int source) {
            for (int i = 0; i < 9; i++) {
                if (i == source) {
                    continue;
                }
                if (puzzle[i][column].value == value) {
                    return true;
                }
            }
            return false;
        }
    
        //Make sure that exists methods only check squares that are not the current one
        private boolean existsInQuad(int num, int quad, int sourceRow, int sourceColumn) {
            int[] c = setCount(quad);
            int il, ih = c[1], jl, jh = c[3];
    
            for (il = c[0]; il < ih; il++) {
                for (jl = c[2]; jl < jh; jl++) {
                    if (il == sourceRow && jl == sourceColumn) {
                        continue;
                    }
                    if (puzzle[il][jl].value == num) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        private void findPuzzleSingles() {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (puzzle[i][j].possibilities.size() == 1) {
                        puzzle[i][j].value = puzzle[i][j].possibilities.get(0);
                        puzzle[i][j].solved = true;
                        cleanUpColumn(j, puzzle[i][j].value);
                        cleanUpRow(i, puzzle[i][j].value);
                        cleanUpQuad(getQuad(i, j), puzzle[i][j].value);
                        puzzle[i][j].possibilities.clear();
                    }
                }
            }
        }
    
        private void findRowOrphans() {
            int count;
            for (int num = 1; num < 10; num++) {
                for (int row = 0; row < 9; row++) {
                    count = 0;
                    for (int col = 0; col < 9; col++) {
                        if (!puzzle[row][col].solved && puzzle[row][col].possibilities.contains(num)) {
                            count++;
                        }
                    }
                    if (count == 1) {
                        for (int i = 0; i < 9; i++) {
                            if (puzzle[row][i].possibilities.contains(num)) {
                                puzzle[row][i].possibilities.clear();
                                puzzle[row][i].solved = true;
                                puzzle[row][i].value = num;
                                cleanUpColumn(i, num);
                                cleanUpRow(row, num);
                                cleanUpQuad(getQuad(row, i), num);
                            }
                        }
                    }
                }
            }
        }
    
        private void findColumnOrphans() {
            int count;
            for (int num = 1; num < 10; num++) {
                for (int col = 0; col < 9; col++) {
                    count = 0;
                    for (int row = 0; row < 9; row++) {
                        if (!puzzle[row][col].solved && puzzle[row][col].possibilities.contains(num)) {
                            count++;
                        }
                    }
                    if (count == 1) {
                        for (int i = 0; i < 9; i++) {
                            if (puzzle[i][col].possibilities.contains(num)) {
                                puzzle[i][col].possibilities.clear();
                                puzzle[i][col].solved = true;
                                puzzle[i][col].value = num;
                                cleanUpRow(i, num);
                                cleanUpColumn(col, num);
                                cleanUpQuad(getQuad(col, i), num);
                            }
                        }
                    }
                }
            }
        }
    
        private void findQuadOrphans() {
            for (int quad = 1; quad < 10; quad++) {
                int[] c = setCount(quad);
                int il, ih = c[1], jl, jh = c[3];
                int count = 0;
    
                for (int num = 1; num < 10; num++) {
                    count = 0;
                    for (il = c[0]; il < ih; il++) {
                        for (jl = c[2]; jl < jh; jl++) {
                            if (puzzle[il][jl].possibilities.contains(num)) {
                                count++;
                            }
                        }
                    }
                    if (count == 1) {
                        for (il = c[0]; il < ih; il++) {
                            for (jl = c[2]; jl < jh; jl++) {
                                if (puzzle[il][jl].possibilities.contains(num)) {
                                    puzzle[il][jl].possibilities.clear();
                                    puzzle[il][jl].solved = true;
                                    puzzle[il][jl].value = num;
                                    cleanUpRow(il, num);
                                    cleanUpColumn(jl, num);
                                    cleanUpQuad(getQuad(il, jl), num);
                                }
                            }
                        }
                    }
                }
            }
        }
    
        private void cleanUpColumn(int col, int number) {
            for (int i = 0; i < 9; i++) {
                if (puzzle[i][col].possibilities.contains(number)) {
                    puzzle[i][col].possibilities.remove(puzzle[i][col].possibilities.indexOf(number));
                }
            }
        }
    
        private void cleanUpRow(int row, int number) {
            for (int i = 0; i < 9; i++) {
                if (puzzle[row][i].possibilities.contains(number)) {
                    puzzle[row][i].possibilities.remove(puzzle[row][i].possibilities.indexOf(number));
                }
            }
        }
    
        private void cleanUpQuad(int quad, int number) {
            int[] c = setCount(quad);
            int il, ih = c[1], jl, jh = c[3];
    
            for (il = c[0]; il < ih; il++) {
                for (jl = c[2]; jl < jh; jl++) {
                    if (puzzle[il][jl].possibilities.contains(number)) {
                        puzzle[il][jl].possibilities.remove(puzzle[il][jl].possibilities.indexOf(number));
                    }
                }
            }
        }
    
        /* With given x,y values representing the position of the
         * value in the array, this method returns the quad of said
         * value.
         *  1 2 3
         *  4 5 6
         *  7 8 9
         */
        private int getQuad(int x, int y) {
            int quad = 0;
    
            if (x < 3 && y < 3) {
                quad = 1;
            } else if (x < 3 && y > 2 && y < 6) {
                quad = 2;
            } else if (x < 3 && y > 5) {
                quad = 3;
            } else if (x > 2 && x < 6 && y < 3) {
                quad = 4;
            } else if (x > 2 && x < 6 && y > 2 && y < 6) {
                quad = 5;
            } else if (x > 2 && x < 6 && y > 5) {
                quad = 6;
            } else if (x > 5 && y < 3) {
                quad = 7;
            } else if (x > 5 && y > 2 && y < 6) {
                quad = 8;
            } else if (x > 5 && y > 5) {
                quad = 9;
            }
            return quad;
        }
    
        private int[] setCount(int quad) {
            int il = 0, jl = 0, ih = 0, jh = 0, set;
    
            switch (quad) {
                case 1:
                    il = 0;
                    ih = 3;
                    jl = 0;
                    jh = 3;
                    break;
                case 2:
                    il = 0;
                    ih = 3;
                    jl = 3;
                    jh = 6;
                    break;
                case 3:
                    il = 0;
                    ih = 3;
                    jl = 6;
                    jh = 9;
                    break;
                case 4:
                    il = 3;
                    ih = 6;
                    jl = 0;
                    jh = 3;
                    break;
                case 5:
                    il = 3;
                    ih = 6;
                    jl = 3;
                    jh = 6;
                    break;
                case 6:
                    il = 3;
                    ih = 6;
                    jl = 6;
                    jh = 9;
                    break;
                case 7:
                    il = 6;
                    ih = 9;
                    jl = 0;
                    jh = 3;
                    break;
                case 8:
                    il = 6;
                    ih = 9;
                    jl = 3;
                    jh = 6;
                    break;
                case 9:
                    il = 6;
                    ih = 9;
                    jl = 6;
                    jh = 9;
                    break;
            }
    
            int[] result = {il, ih, jl, jh};
            return result;
        }
    }
    Square.java
    Java Code:
    package sudoku2;
    
    import java.util.ArrayList;
    
    /**
     * @author Steffen Gates
     */
    public class Square {
        ArrayList<Integer> possibilities;
        int value;
        boolean solved;
        boolean initialValue;
    
        public Square(){
            possibilities = new ArrayList<Integer>();
            value = 0;
        }
    
        public Square(int value){
            initialValue = true;
            this.solved = true;
            this.value = value;
            possibilities = new ArrayList<Integer>();
        }
    }
    Viewer.java
    Java Code:
    package sudoku2;
    
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    
    public class Viewer extends JFrame {
    
        int offset = 22;
        BufferedImage img;
        Main m;
        int width = 600;
        int height = 650;
        int bottomBar = height-width;
        String status = "";
    
        public Viewer(Main m) {
            this.m = m;
            img = new BufferedImage(width, height + offset, BufferedImage.OPAQUE);
            Container content = this.getContentPane();
            this.setSize(width, height + offset);
            this.setBackground(Color.WHITE);
            this.setResizable(false);
            this.setVisible(true);
            this.addKeyListener(new KeyListener() {
                public void keyPressed(KeyEvent e) {}
                public void keyReleased(KeyEvent e) {}
                public void keyTyped(KeyEvent e) {
                    if (e.getKeyChar() == ' ') {
                        next();
                    }
                }});
        }
    
        private void next(){
            m.next();
        }
    
        public void update(Square[][] puzzle){
            fillValues(puzzle);
            drawLines();
            Graphics g = img.getGraphics();
            g.setColor(Color.gray);
            g.fillRect(0, 600+offset+1, width, height);
            g.setColor(Color.BLACK);
            g.drawString(status, 10, 650);
            repaint();
        }
    
        public void repaint() {
            this.getGraphics().drawImage(img, 0, 0, this);
        }
    
        private void fillValues(Square[][] puzzle) {
            Graphics g = img.getGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, width, height+offset);
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (puzzle[i][j].initialValue) {
                        g.setColor(Color.RED);
                        Font font = new Font("Arial", Font.BOLD, 36);
                        g.setFont(font);
                        g.drawString(puzzle[i][j].value + "", (((j + 1) * 66) - 40), ((i + 1) * 66 - 15 + offset));
                    } else if (puzzle[i][j].solved) {
                        g.setColor(Color.BLUE);
                        Font font = new Font("Arial", Font.BOLD, 36);
                        g.setFont(font);
                        g.drawString(puzzle[i][j].value + "", (((j + 1) * 66) - 40), ((i + 1) * 66 - 15 + offset));
                    } else {
                        g.setColor(Color.GRAY);
                        Font font = new Font("Arial", Font.PLAIN, 12);
                        g.setFont(font);
                        for (int z = 0; z < puzzle[i][j].possibilities.size(); z++) {
                            switch (puzzle[i][j].possibilities.get(z)) {
                                case (1):
                                    g.drawString(1 + "", (((j + 1) * 66) - 54), ((i + 1) * 66 - 48 + offset));
                                    break;
                                case (2):
                                    g.drawString(2 + "", (((j + 1) * 66) - 38), ((i + 1) * 66 - 48 + offset));
                                    break;
                                case (3):
                                    g.drawString(3 + "", (((j + 1) * 66) - 22), ((i + 1) * 66 - 48 + offset));
                                    break;
                                case (4):
                                    g.drawString(4 + "", (((j + 1) * 66) - 54), ((i + 1) * 66 - 32 + offset));
                                    break;
                                case (5):
                                    g.drawString(5 + "", (((j + 1) * 66) - 38), ((i + 1) * 66 - 32 + offset));
                                    break;
                                case (6):
                                    g.drawString(6 + "", (((j + 1) * 66) - 22), ((i + 1) * 66 - 32 + offset));
                                    break;
                                case (7):
                                    g.drawString(7 + "", (((j + 1) * 66) - 54), ((i + 1) * 66 - 16 + offset));
                                    break;
                                case (8):
                                    g.drawString(8 + "", (((j + 1) * 66) - 38), ((i + 1) * 66 - 16 + offset));
                                    break;
                                case (9):
                                    g.drawString(9 + "", (((j + 1) * 66) - 22), ((i + 1) * 66 - 16 + offset));
                                    break;
                            }
                        }
                    }
                }
            }
        }
    
        private void drawLines() {
            Graphics g = img.getGraphics();
            g.setColor(Color.BLACK);
    
            g.drawLine(66, 0 + offset, 66, height + offset - bottomBar);
            g.drawLine(130, 0 + offset, 130, height + offset - bottomBar);
            g.drawLine(266, 0 + offset, 266, height + offset - bottomBar);
            g.drawLine(333, 0 + offset, 333, height + offset - bottomBar);
            g.drawLine(466, 0 + offset, 466, height + offset - bottomBar);
            g.drawLine(533, 0 + offset, 533, height + offset - bottomBar);
    
            g.drawLine(0, 66 + offset, width, 66 + offset);
            g.drawLine(0, 130 + offset, width, 130 + offset);
            g.drawLine(0, 266 + offset, width, 266 + offset);
            g.drawLine(0, 333 + offset, width, 333 + offset);
            g.drawLine(0, 466 + offset, width, 466 + offset);
            g.drawLine(0, 533 + offset, width, 533 + offset);
            g.drawLine(0, 600 + offset, width, 600 + offset);
    
            g.drawLine(199, 0 + offset, 199, height + offset - bottomBar);
            g.drawLine(200, 0 + offset, 200, height + offset - bottomBar);
            g.drawLine(201, 0 + offset, 201, height + offset - bottomBar);
    
            g.drawLine(399, 0 + offset, 399, height + offset - bottomBar);
            g.drawLine(400, 0 + offset, 400, height + offset - bottomBar);
            g.drawLine(401, 0 + offset, 401, height + offset - bottomBar);
    
            g.drawLine(0, 199 + offset, width, 199 + offset);
            g.drawLine(0, 200 + offset, width, 200 + offset);
            g.drawLine(0, 201 + offset, width, 201 + offset);
    
            g.drawLine(0, 399 + offset, width, 399 + offset);
            g.drawLine(0, 400 + offset, width, 400 + offset);
            g.drawLine(0, 401 + offset, width, 401 + offset);
        }
    }
    I omitted the Input.java class since it was generated by NetBeans and is very long for a web post (1600 lines). However, if you want to play with this app yourself, I would be happy to provide the last of the code. This is the meat of it though. Enjoy and good luck!
    Last edited by quad64bit; 09-07-2009 at 06:09 AM.

  4. #4
    Simplev_v's Avatar
    Simplev_v is offline Member
    Join Date
    Sep 2009
    Location
    Malaysia
    Posts
    10
    Rep Power
    0

    Default

    wow... yes pls, if u dont mind i want to try to play the game in java..

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Ok, here it is then. You need to put this code into netbeans 6.7 though -- I don't think it'll compile elsewhere. To use the app, input a sudoku puzzle from the newspaper or the net (it has to be a solvable one) and then tap space bar once the viewer window is open repeatedly to advance through solving step by step. Here is a link to the entire netbeans project, which you should be able to open -- it also contains a jar file in the dist folder which you can double-click to run. Have fun!
    http://homepage.mac.com/graphic_innovations/Sudoku2.zip

    The code is too long for me to post, just use the link. I keep getting errors. The source is contained in the zip I provided :D
    Last edited by quad64bit; 09-07-2009 at 06:16 PM.

Similar Threads

  1. Hashing problem
    By etherkye in forum Java Applets
    Replies: 0
    Last Post: 07-23-2009, 12:56 PM
  2. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 02:35 PM
  3. Need Help With VERY Simple Program -- Newbie!!!
    By somethingfast in forum New To Java
    Replies: 17
    Last Post: 11-25-2008, 07:38 AM
  4. Stuck - simple program
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-24-2008, 07:44 PM
  5. Hashing in Java
    By ajaykushwaha in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •