From c42d061cfe389f8ee0ead8fc24bb67e071e5f8e8 Mon Sep 17 00:00:00 2001 From: xxMrPHDxx Date: Wed, 22 Feb 2017 07:03:16 +0800 Subject: [PATCH] Code Updated!!! --- src/grid/Cell.java | 63 +++------ src/grid/Grid.java | 309 ++++++++++--------------------------------- src/grid/Sudoku.java | 31 +++++ src/main/Game.java | 24 +--- 4 files changed, 127 insertions(+), 300 deletions(-) create mode 100644 src/grid/Sudoku.java diff --git a/src/grid/Cell.java b/src/grid/Cell.java index 5b45408..dfd0216 100644 --- a/src/grid/Cell.java +++ b/src/grid/Cell.java @@ -6,74 +6,45 @@ public class Cell { - private String c; - private int x,y; - - private int row,col; + public int value = 0; - private int p=2; - private int p2=4; - private int size=40; + private int x,y; - private boolean selected = false; + private final int p=2,p2=p * 2,size=40; - private boolean original = false; + boolean selected = false; + boolean original = false; public Cell(int i,int j){ - this.c = " "; - this.row = i; - this.col = j; this.x = p * (j - (int)(j/3)) + p2 * ((int)(j/3) + 1) + j * size; this.y = p * (i - (int)(i/3)) + p2 * ((int)(i/3) + 1) + i * size; } - public void original(){ - this.original = true; - } - - public boolean isOriginal(){ - return original; - } - public boolean contains(int x,int y){ return (x > this.x && x < this.x + size && y > this.y && y < this.y + size); } - public int getRow(){ - return row; - } - public int getCol(){ - return col; - } - - public String getRowCol(){ - return ("row " + (row+1) + ", col "+(col+1)); - } - - public void setValue(String c){ - this.c = c; - } - - public String getValue(){ - return c; - } - - public void select(){ - selected = true; + public void setValue(int value){ + this.value = value; + original = true; } - public boolean isSelected(){ - return selected; + public void clear(){ + this.value = 0; + original = false; } public void draw(Graphics g){ g.fillRect(x, y, size, size); g.setColor(Color.BLACK); - if(!original) + if(!original){ g.setFont(new Font("Century Gothic",Font.PLAIN, 30)); - else + }else{ g.setFont(new Font("Century Gothic",Font.BOLD, 30)); - g.drawString(c, x + size - 30, y + size - 10); + g.setColor(Color.BLUE); + } + if(value == 0)return; + g.drawString(String.valueOf(value), x + size - 30, y + size - 10); } } diff --git a/src/grid/Grid.java b/src/grid/Grid.java index bf12db9..cfc3d5d 100644 --- a/src/grid/Grid.java +++ b/src/grid/Grid.java @@ -4,157 +4,56 @@ import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; -import java.util.ArrayList; public class Grid { - String[] values = new String[]{"1","2","3","4","5","6","7","8","9"}; - - String[][] Example = new String[][]{ - {"5","3"," "," ","7"," "," "," "," "}, - {"6"," "," ","1","9","5"," "," "," "}, - {" ","9","8"," "," "," "," ","6"," "}, - {"8"," "," "," ","6"," "," "," ","3"}, - {"4"," "," ","8"," ","3"," "," ","1"}, - {"7"," "," "," ","2"," "," "," ","6"}, - {" ","6"," "," "," "," ","2","8"," "}, - {" "," "," ","4","1","9"," "," ","5"}, - {" "," "," "," ","8"," "," ","7","9"}}; - - public Cell[][] cells; + public final int rows = 9,cols = 9; + public Cell[][] grid = new Cell[rows][cols]; public Cell selectedCell; private int selectedRow = -1; private int selectedCol = -1; - public boolean solved = false; + public boolean done = false; public Grid(){ - cells = new Cell[9][9]; -// for(int i=0;i 8) { // Moves to next row when end of column is reached c = 0; @@ -178,159 +75,99 @@ public boolean backtrack(int r,int c){ } } - int value=0; - if (cells[r][c].getValue() != " ") { // Move to next cell if user has entered a number in current cell - if (!(validateRow(cells[r][c]) && validateCol(cells[r][c]) && validateBox(cells[r][c]))){ + if (grid[r][c].value != 0) { // Move to next cell if user has entered a number in current cell + if (!okay(r,c)){ return false; - }else{ - if(cells[r][c].isOriginal()) - if(value >= 0 && value <= 9)cells[r][c].setValue(values[value]); - } - return this.backtrack(r, c); + } + return backtrack(r, c); } else { // Goes through all possible numbers if user has left cell blank - for (int x = 0; x < 9; x++) { - cells[r][c].setValue(values[x]); - if (validateRow(cells[r][c]) && validateCol(cells[r][c]) && validateBox(cells[r][c])){ + for (int x = 1; x < 10; x++) { + grid[r][c].value = x; + if (okay(r,c)){ if (backtrack(r, c)) { return true; } } } + grid[r][c].value = 0; return false; } } - public boolean solved(){ - for(int i=0;i cells.length - 1)selectedRow = 0; - if(selectedCol > cells[0].length - 1)selectedCol = 0; - if(selectedCol < 0)selectedCol = cells[0].length - 1; - selectedCell = cells[selectedRow][selectedCol]; + if(selectedRow < 0)selectedRow = rows - 1; + if(selectedRow > rows - 1)selectedRow = 0; + if(selectedCol > cols - 1)selectedCol = 0; + if(selectedCol < 0)selectedCol = cols - 1; + selectedCell = grid[selectedRow][selectedCol]; } } public void draw(Graphics g){ - //Draw cells 9x9 - for(int i=0;i