diff --git a/src/main/java/info/istlab/Zemi01/sudoku/AbstractDataStore.java b/src/main/java/info/istlab/Zemi01/sudoku/AbstractDataStore.java new file mode 100644 index 0000000..af3d718 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/sudoku/AbstractDataStore.java @@ -0,0 +1,101 @@ +package info.istlab.Zemi01.sudoku; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import javax.swing.JFileChooser; + +public class AbstractDataStore { + + + public static String selectfile(boolean issave, String ext){ + // �t�@�C���_�C�A���O���J�� + String cdir = System.getProperty("user.dir"); + JFileChooser fd = new JFileChooser(cdir); + NullFileFilter nff = new NullFileFilter(ext); + fd.setFileFilter(nff); + int returnval; + if (issave) returnval = fd.showSaveDialog(null); + else returnval = fd.showOpenDialog(null); + if (returnval == JFileChooser.APPROVE_OPTION){ + // �g���q���‚��Ă��Ȃ��Ƃ��C�ނ���‚��� + StringBuffer path = new StringBuffer(fd.getSelectedFile().getAbsolutePath()); + if (!path.toString().endsWith(ext)){ + path.append(ext); + } + return path.toString(); + }else{ + return null; + } + } + public static void saveToFile(String fn, ArrayList ary, String dotextension) { + if (fn == null) fn = selectfile(true, dotextension /*".satn"*/); + if (fn == null) return;//�����t�@�C������I�����Ȃ������� + FileReadWriter.writeBytesToFile(fn, byteSerializeExport(ary)); + File f = new File(fn); + System.out.println(f.getAbsolutePath()+"に保存しました"); + } + public static ArrayList loadFromFile(String fn, String dotextension){ + if (fn == null) fn = selectfile(false, dotextension /*".satn"*/); + if (fn == null) return null;//�����t�@�C������I�����Ȃ������� + byte[] ba = FileReadWriter.readBytesFromFile(fn); + File f = new File(fn); + System.out.println(f.getAbsolutePath()+"から読み込みました"); + return byteSerializeImport(ba); + } + + public static byte[] byteSerializeExport(ArrayList ary) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStream gzipos = null; + ObjectOutputStream pobjos = null; // PNode��ۑ�����Ƃ���PObjectOutputStream + + try { + gzipos = new GZIPOutputStream(baos); + pobjos = new ObjectOutputStream(gzipos); + pobjos.writeObject(ary); + pobjos.close(); + gzipos.close(); + } catch (IOException iex) { + iex.printStackTrace(System.out); + } + System.out.println("save size: " + baos.size() + " bytes."); + return baos.toByteArray(); + } + + @SuppressWarnings("unchecked") + public static ArrayList byteSerializeImport(byte[] ba){ + ByteArrayInputStream bais = new ByteArrayInputStream(ba); + InputStream gzipis = null; + ObjectInputStream ois = null; + ArrayList ret = null; + try { + gzipis = new GZIPInputStream(bais); + ois = new ObjectInputStream(gzipis); + ret = (ArrayList) ois.readObject(); + ois.close(); + gzipis.close(); + bais.close(); + } catch (EOFException eofex) { + eofex.printStackTrace(); + } catch (IOException excep) { + excep.printStackTrace(System.err); + } catch (ClassNotFoundException excnf) { + System.err.println("ClassNotFound Error"); + } finally { + } + return ret; + } + + + +} diff --git a/src/main/java/info/istlab/Zemi01/sudoku/FileReadWriter.java b/src/main/java/info/istlab/Zemi01/sudoku/FileReadWriter.java new file mode 100644 index 0000000..2207a3c --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/sudoku/FileReadWriter.java @@ -0,0 +1,192 @@ +package info.istlab.Zemi01.sudoku; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.util.ArrayList; + +public class FileReadWriter { + + public static ArrayList getLinesListFromResource(String path){ + ClassLoader cl = FileReadWriter.class.getClassLoader(); + + if (cl == null) { + System.err.println("Resource not found: " + path); + return null; + } else { + InputStream ris = cl.getResourceAsStream(path); + return getLinesListFromInputStream(ris, path); + } + } + public static ArrayList getLinesListFromInputStream(InputStream is, String name){ + ArrayList list = new ArrayList(); + try { + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String line = null; + /* ファイル読み込み */ + while ((line = br.readLine()) != null) { + list.add(line); + } + /* ファイルを閉じます */ + br.close(); + } catch (IOException err) { + System.out.println("ReadError: "+name); + } + return list; + } + + public static ArrayList getLinesList(String fn, boolean createIfNotExist){ + File f = new File(fn); + if (createIfNotExist){ + if (!f.exists()){ + try { + if (f.createNewFile()){ + System.out.println("New file "+fn+" Created."); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return getLinesList(fn); + } + + public static ArrayList getLinesList(String fn){ + try { + return getLinesListFromInputStream(new FileInputStream(fn), fn); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return null; + } + /* ファイルを読み込む */ + public static String[] getLines(String fn) { + + ArrayList list = getLinesList(fn); + return (String[]) list.toArray(new String[list.size()]); + } + + public static String[] getLinesFromResource(String path){ + ArrayList list = getLinesListFromResource(path); + return (String[]) list.toArray(new String[list.size()]); + } + + /** 配列をファイルに書き込む + * + * @param fn ファイル名 + * @param data 文字列の配列(各行が要素) + */ + public static void putLines(String fn, String data[]) { + try { + FileWriter filewriter = new FileWriter(fn, false); + for (int i = 0; i < data.length; i++) { + /* 改行文字追加して */ + /* 文字列を書き込みます */ + filewriter.write(data[i]+System.getProperty("line.separator")); + } + /* ファイルを閉じます */ + filewriter.close(); + } catch (IOException e) { + System.out.println("WriteError:" + fn); + } + } + + /* ファイルに書き込む */ + public static void putLines(String fn, ArrayList data) { +// System.out.println(fn); + try { + FileWriter filewriter = new FileWriter(fn, false); + for (String d : data) { + /* 改行文字追加 */ + d = d + "\r\n"; + /* 文字列を書き込みます */ + filewriter.write(d); + } + /* ファイルを閉じます */ + filewriter.close(); + } catch (IOException e) { + System.out.println("WriteError:" + fn); + } + } + + /* ファイルに書き込む */ + public static void putStringToFile(String fn, String out) { +// System.out.println(fn); + try { + FileWriter filewriter = new FileWriter(fn, false); + filewriter.write(out); + /* ファイルを閉じます */ + filewriter.close(); + } catch (IOException e) { + System.out.println("WriteError:" + fn); + } + } + + /* 文字列を分割して配列で返します */ + public static String[] split(String delim, String text) { + int index = -1; + ArrayList list = new ArrayList(); + while ((index = text.indexOf(delim)) != -1) { + list.add(text.substring(0, index)); + text = text.substring(index + delim.length()); + } + list.add(text); + String[] ret = (String[]) list.toArray(new String[list.size()]); + return ret; + } + + public static void writeBytesToFile(String filepath, byte[] ba){ + OutputStream fos = null; + DataOutputStream dos = null; + try{ + fos = new FileOutputStream(filepath); + dos = new DataOutputStream(fos); + dos.write(ba); + dos.close(); + fos.close(); + }catch(IOException iex){ + iex.printStackTrace(System.out); + } + } + + public static byte[] readBytesFromFile(String path){ + File f = new File(path); + if (!f.exists()) System.out.println("No file "+path); + + BufferedInputStream in; + ByteArrayOutputStream varyBuf = new ByteArrayOutputStream(); + final int LS = 1024; + int b; + try { + in = new BufferedInputStream(new FileInputStream(path)); + byte buf[] = new byte[LS]; + while((b = in.read(buf, 0, buf.length)) != -1 ) { + varyBuf.write(buf,0,b) ; + } + varyBuf.close(); + in.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return varyBuf.toByteArray(); + } + public static ArrayList buffer; + public static void bufclear() { + if (buffer == null) buffer = new ArrayList(); + else buffer.clear(); + } + public static void bufSave(String fn){ + FileReadWriter.putLines(fn, buffer); + } +} diff --git a/src/main/java/info/istlab/Zemi01/sudoku/Model3x3.java b/src/main/java/info/istlab/Zemi01/sudoku/Model3x3.java new file mode 100644 index 0000000..7ca3569 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/sudoku/Model3x3.java @@ -0,0 +1,74 @@ +package info.istlab.Zemi01.sudoku; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; + +public class Model3x3 implements Serializable { + private static final long serialVersionUID = 4652161354049162573L; + + public int[][] m3x3; + public boolean[][] error3x3; + public Model3x3(){ + reset(); + setIA(iagen()); + } + public void paintComponent(Graphics2D g2d){ + for(int i=0;i<3;i++){ + for(int j=0;j<3;j++){ + if (error3x3[i][j]) g2d.setColor(Color.red); + else g2d.setColor(Color.black); + g2d.drawString(String.valueOf(m3x3[i][j]), (i+1)*20, (j+1)*20); + } + } + + } + public void reset(){ + m3x3 = new int[3][3]; + error3x3 = new boolean[3][3]; + } + public int[] col(int c){ + int col[] = new int[3]; + for(int i=0;i<3;i++){ + col[i] = m3x3[c][i]; + } + return col; + } + public int[] row(int r){ + int row[] = new int[3]; + for(int i=0;i<3;i++){ + row[i] = m3x3[i][r]; + } + return row; + } + public void setIA(ArrayList ia){ + for(int i=0;i<3;i++){ + for(int j=0;j<3;j++){ + m3x3[i][j] = ia.get(i*3+j); + } + } + } + public ArrayList iagen(){ + ArrayList ia = new ArrayList(); + for(int i=1;i<=9;i++){ + ia.add(i); + } + Collections.shuffle(ia); + return ia; + } + public static void printIA(int[] a){ + for(int i=0;i ia){ + for(int i=0;i<9;i++){ + for(int j=0;j<9;j++){ + m9x9[i][j] = ia.get(i*9+j); + } + } + } + public ArrayList iagen(){ + ArrayList ia = new ArrayList(); + for(int i=1;i<=9;i++){ + ia.add(i); + } + Collections.shuffle(ia); + return ia; + } + public static void printIA(int[] a){ + for(int i=0;i1) markErrorCol_Val(c,i); + } + private void errorcheck_row(int r) { + int row[] = row(r); + Arrays.sort(row); + int count[] = new int[10]; + for(int i=0;i<9;i++) count[row[i]]++; + for(int i=1;i<10;i++) if (count[i]>1) markErrorRow_Val(r,i); + } + private void markErrorCol_Val(int c, int v){ + for(int i=0;i<9;i++){ + if (m9x9[c][i]==v) error9x9[c][i]=true; + } + } + private void markErrorRow_Val(int r, int v){ + for(int i=0;i<9;i++){ + if (m9x9[i][r]==v) error9x9[i][r]=true; + } + } +} diff --git a/src/main/java/info/istlab/Zemi01/sudoku/Sudoku.java b/src/main/java/info/istlab/Zemi01/sudoku/Sudoku.java new file mode 100644 index 0000000..50c7837 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/sudoku/Sudoku.java @@ -0,0 +1,80 @@ +package info.istlab.Zemi01.sudoku; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +import info.istlab.Zemi01.Launcher; + +public class Sudoku extends JFrame { + private static final long serialVersionUID = -8995261455025975788L; + + public static Sudoku theapp; + + public static void main(String[] args){ + theapp = new Sudoku(); + } + + SudokuPanel mainP; + + JPanel buttonP; + + public Sudoku(){ + super("Sudoku"); + getContentPane().add(mainP = new SudokuPanel(this)); + + buttonP = new JPanel(); + + JButton jb = new JButton("Shuffle"); + jb.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + mainP.shuffle(); +// mainP.repaint(); + } + }); + buttonP.add(jb); + JButton jb2 = new JButton("check"); + jb2.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + //mainP.test(); + mainP.check(); + } + }); + buttonP.add(jb2); + + JButton jb3 = new JButton("save"); + jb3.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + //mainP.test(); + mainP.save(); + } + }); + buttonP.add(jb3); + + JButton jb4 = new JButton("load"); + jb4.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + //mainP.test(); + mainP.load(); + } + }); + buttonP.add(jb4); + + + + getContentPane().add(buttonP, BorderLayout.SOUTH); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + pack(); + setVisible(true); + setLocation(Launcher.centerOfScreen(this.getSize())); + + } + + + +} diff --git a/src/main/java/info/istlab/Zemi01/sudoku/SudokuModel.java b/src/main/java/info/istlab/Zemi01/sudoku/SudokuModel.java new file mode 100644 index 0000000..dc0d272 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/sudoku/SudokuModel.java @@ -0,0 +1,90 @@ +package info.istlab.Zemi01.sudoku; + +import java.awt.Graphics2D; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; + +public class SudokuModel implements Serializable { + private static final long serialVersionUID = 9118224938421550662L; + public Model3x3[][] model3x3; + + public SudokuModel(){ + model3x3 = new Model3x3[3][3]; + for(int i=0;i<3;i++){ + for(int j=0;j<3;j++){ + model3x3[i][j] = new Model3x3(); + } + } + + Tane t = new Tane(true); + Tane[] ta = new Tane[9]; + ta[0] = t; + ta[1] = ta[0].colrotT(); + ta[2] = ta[1].colrotT(); + ta[3] = ta[0].rowrotT(); + ta[4] = ta[1].rowrotT(); + ta[5] = ta[2].rowrotT(); + ta[6] = ta[3].rowrotT(); + ta[7] = ta[4].rowrotT(); + ta[8] = ta[5].rowrotT(); + + ArrayList ia = new ArrayList(); + for(int i=1;i<=9;i++){ + ia.add(i); + } + Collections.shuffle(ia); + + for(int i=0;i<9;i++){ + ta[i].setValues(ia.get(i),model3x3); + } + } + // public SudokuModel(){ + // model3x3 = new Model3x3[3][3]; + // for(int i=0;i<3;i++){ + // for(int j=0;j<3;j++){ + // model3x3[i][j] = new Model3x3(); + // } + // } + // } + /** + * Create Same Model Converted + * @param sdModel9x9 + */ + public SudokuModel(SDModel9x9 sdModel9x9) { + model3x3 = new Model3x3[3][3]; + for(int c=0;c<3;c++){ + for(int r=0;r<3;r++){ + model3x3[c][r] = sdModel9x9.get3x3Model(c, r); + } + } + } + public SDModel9x9 toSDModel9x9(){ + SDModel9x9 ninemodel = new SDModel9x9(this); + return ninemodel; + } + public void paintComponent(Graphics2D g2d){ + int tx = 70; + for(int i=0;i<3;i++){ + for(int j=0;j<3;j++){ + g2d.translate(i*tx, j*tx); + model3x3[i][j].paintComponent(g2d); + g2d.translate(i*-tx, j*-tx); + } + } + + } + public void debugprint(int px, int py) { + model3x3[px][py].debugprint(); + } + public int getInt(int c, int r) { + int mc = c/3; + int mr = r/3; + return model3x3[mc][mr].m3x3[c%3][r%3]; + } + public boolean getError(int c, int r) { + int mc = c/3; + int mr = r/3; + return model3x3[mc][mr].error3x3[c%3][r%3]; + } +} diff --git a/src/main/java/info/istlab/Zemi01/sudoku/SudokuPanel.java b/src/main/java/info/istlab/Zemi01/sudoku/SudokuPanel.java new file mode 100644 index 0000000..f98584b --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/sudoku/SudokuPanel.java @@ -0,0 +1,85 @@ +package info.istlab.Zemi01.sudoku; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.util.ArrayList; + +import javax.swing.JPanel; + + +public class SudokuPanel extends JPanel implements MouseListener { + private static final long serialVersionUID = -1611925332039214586L; + + Sudoku sudoku; + +// int[][] numAry = new int[10][10]; + SudokuModel model; + + public SudokuPanel(Sudoku _sudoku){ + sudoku = _sudoku; + model = new SudokuModel(); + addMouseListener(this); + } + + public void paintComponent(Graphics g){ + Graphics2D g2d = (Graphics2D)g; + g.clearRect(0,0,getWidth(),getHeight()); + model.paintComponent(g2d); + } + public Dimension getPreferredSize(){ + return new Dimension(230,230); + } + + @Override + public void mouseClicked(MouseEvent e) { + int px = e.getX()/70; + int py = e.getY()/70; +// System.out.println(px+" "+py); + model.debugprint(px,py); + } + @Override + public void mousePressed(MouseEvent e) { + } + @Override + public void mouseReleased(MouseEvent e) { + } + @Override + public void mouseEntered(MouseEvent e) { + } + @Override + public void mouseExited(MouseEvent e) { + } + + public void test() { + SDModel9x9 m2 = model.toSDModel9x9(); + m2.debugprint(); + } + + public void shuffle() { + model = new SudokuModel(); + repaint(); + } + public void check(){ + SDModel9x9 m2 = model.toSDModel9x9(); + m2.errorcheck(); + model = m2.toSudokuModel(); + repaint(); + } + + public void save() { + ArrayList sdkary = new ArrayList(); + sdkary.add(model); + AbstractDataStore.saveToFile(null, sdkary, ".sdk"); + + } + + public void load() { + ArrayList assary = AbstractDataStore.loadFromFile(null, ".sdk"); + + model = assary.get(0); + repaint(); + } +} diff --git a/src/main/java/info/istlab/Zemi01/sudoku/Tane.java b/src/main/java/info/istlab/Zemi01/sudoku/Tane.java new file mode 100644 index 0000000..b80fc90 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/sudoku/Tane.java @@ -0,0 +1,116 @@ +package info.istlab.Zemi01.sudoku; + +import java.util.Random; + +public class Tane { + + int[][] a = new int[][]{{1,9,5},{8,4,3},{6,2,7}}; + public Tane(){ + } + public Tane(boolean isrand){ + if (isrand){ + Random r = new Random(); + int c; + c = r.nextInt(3); + for(int i=0;i numlist){ + for(int i: numlist) System.out.print(i+" "); + System.out.println(""); + } + + public static ArrayList numShuffle(ArrayList numlist){ + Collections.shuffle(numlist); + return numlist; + } + + public static ArrayList generate(){ + ArrayList numlist = new ArrayList(); + for(int i=1;i<=9;i++){ + numlist.add(i); + } + return numlist; + } + // posは0,1,2 + public static ArrayList three(ArrayList list, int pos){ + ArrayList s = new ArrayList(); + s.add(list.get(pos*3)); + s.add(list.get(pos*3+1)); + s.add(list.get(pos*3+2)); + return s; + } + public static void avoidOverlap(ArrayList l1, ArrayList l2, int block3){ + ArrayList l1s = three(l1, block3); + for(int i=block3*3;i