Java 8 digital (graphical interface)
I recently learned how to program the Java graphic interface and wrote an octal digital bfs code to share with you.
This code is also my final job.
Logic layer
package com.EightNumber.view;import java.util.*;public class EightNumPath {final static int dx[] = {-1, 1, 0, 0};final static int dy[] = { 0, 0,-1, 1};final static String dir = "UDLR";static int maxstate = 400000;static int [][]st = new int[maxstate][9];static int []goal = {1,2,3,4,5,6,7,8,0};static int []dist = new int[maxstate];static int []fa = new int[maxstate];static int []move = new int[maxstate];static boolean []vis = new boolean[maxstate];static int []fact = new int[9];static StringBuffer path;public static boolean isok(int []a) { int sum=0; for(int i=0; i < 9; i++) for(int j=i+1; j < 9; j++) if(a[j] != 0 && a[i] != 0 && a[i] > a[j]) sum++; if(sum % 2 == 0) { return true; } return false;} private static void init_lookup_table() {fact[0] = 1;for(int i = 1; i < 9; i++) {fact[i] = fact[i-1] * i;}Arrays.fill(vis, false);}private static boolean try_to_insert(int s) {int code = 0;for(int i = 0; i < 9; i++) {int cnt = 0;for(int j = i+1; j < 9; j++) {if(st[s][j] < st[s][i]) {cnt++;}}code += fact[8-i] * cnt;}if(vis[code]) {return false;}return vis[code] = true;}private static void print_path(int cur) {while(cur != 1) {path.insert(0,dir.charAt(move[cur]));cur = fa[cur];}}private static int bfs() {init_lookup_table();int front = 1 , rear = 2;try_to_insert(front);while(front < rear) {if(Arrays.equals(st[front], goal)) {return front;}int z;for(z = 0; z < 9; z++) {if(st[front][z] == 0) {break;}}int x = z/3, y = z%3;for(int d = 0; d < 4; d++) {int newx = x + dx[d];int newy = y + dy[d];int newz = newx * 3 + newy;if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3) {st[rear] = Arrays.copyOf(st[front], st[front].length);st[rear][newz] = st[front][z];st[rear][z] = st[front][newz];dist[rear] = dist[front] + 1;if(try_to_insert(rear)) {fa[rear] = front;move[rear] = d;rear++;}}}front++;}return 0;}public static String solve(String state) {path = new StringBuffer();for(int i = 0; i < state.length(); i++) {st[1][i] = Integer.valueOf(state.charAt(i)) - '0';}int ans = bfs();print_path(ans);return path.toString();}}
Interface Layer
Package com. eightNumber. view; import java. awt. *; import javax. swing. *; import java. awt. event. *; import java. util. *; public class EightNumFrame extends Frame implements ActionListener, KeyListener {MenuBar menubar = new MenuBar (); Menu menu_file = new Menu ("file (F )"); menuItem restart = new MenuItem ("restart"); MenuItem nextPath = new MenuItem ("prompt"); MenuItem printPath = new MenuItem ("Restore "); menuItem exit = new MenuItem ("exit"); Button [] button; Panel panel; int row, col; private static int position, cellNum; final int dr [] = {0, -1, 0, 1}; final int dc [] = {-1, 0, 1, 0}; public EightNumFrame (int row, int col) {super. setMenuBar (menubar); // Windows style try {UIManager. setLookAndFeel ("com. sun. java. swing. plaf. windows. windowsLookAndFeel ");} catch (Exception e) {e. printStackTrace ();} this. row = row; this. col = col; cellNum = row * col; restart. addActionListener (this); exit. addActionListener (this); nextPath. addActionListener (this); printPath. addActionListener (this); menu_file.add (restart); menu_file.add (nextPath); menu_file.add (printPath); menu_file.add (exit); menubar. add (menu_file); panel = new Panel (new GridLayout (row, col); button = new Button [cellNum]; for (int I = 0; I <cellNum; I ++) {if (I = cellNum-1) {button [I] = new Button ("");} else {button [I] = new Button (String. valueOf (I + 1);} button [I]. setFont (new Font ("Courier", 1, 20); button [I]. addActionListener (this); button [I]. addKeyListener (this); panel. add (button [I]);} position = cellNum-1; this. add (BorderLayout. CENTER, panel); this. setTitle ("Eight Digital"); this. setVisible (true); this. setSize (300,300); Toolkit kit = Toolkit. getdefatooltoolkit (); Dimension screenSize = kit. getScreenSize (); int screenWidth = screenSize. width/2; int screenHeight = screenSize. height/2; int height = this. getHeight (); int width = this. getWidth (); this. setLocation (screenWidth-width/2, screenHeight-height/2); this. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}) ;}void start () {int a [] = new int [9]; do {int k = 0; random random = new Random (); Set
Set = new HashSet
(); While (set. size () <cellNum-1) {int n = random. nextInt (cellNum-1) + 1; if (! Set. contains (n) {set. add (n); a [k ++] = n ;}} a [k] = 0 ;}while (! EightNumPath. isok (a); for (int I = 0; I <9; I ++) button [I]. setLabel (String. valueOf (a [I]); button [cellNum-1]. setLabel (""); position = cellNum-1;} boolean win () {for (int I = 0; I <cellNum-1; I ++) {if (button [I]. getLabel (). equals ("") {return false;} else if (Integer. valueOf (button [I]. getLabel ())! = I + 1) {return false ;}} return true;} private boolean judge (Button a, Button B) {for (int I = 0; I <4; I ++) {if (. getX () = B. getX () + dr [I] *. getWidth () & (. getY () = B. getY () + dc [I] *. getHeight () {return true;} return false;} public void actionreceivmed (ActionEvent e) {StringBuffer state = new StringBuffer (); if (e. getSource () = restart) {start (); return;} else if (e. getSource () = exit) {System. Exit (0); return;} else if (e. getSource () = nextPath) {for (int I = 0; I <cellNum; I ++) {if (button [I]. getLabel (). equals ("") {state. append ('0');} else {state. append (button [I]. getLabel () ;}string path = EightNumPath. solve (state. toString (); JOptionPane. showMessageDialog (this, "recommended path:" + path); System. out. println (path); return;} else if (e. getSource () = printPath) {for (int I = 0; I <cellNum; I ++) {if (button [I]. getLabel (). equals ("") {state. append ('0');} else {state. append (button [I]. getLabel () ;}string path = EightNumPath. solve (state. toString (); for (int I = 0; I <path. length (); I ++) {switch (path. charAt (I) {case 'U': go (KeyEvent. VK_UP); break; case 'D': go (KeyEvent. VK_DOWN); break; case 'l': go (KeyEvent. VK_LEFT); break; case 'r': go (KeyEvent. VK_RIGHT); break;} try {Thread. sleep (1000);} catch (InterruptedExcepti On e1) {e1.printStackTrace () ;}}for (int I = 0; I <cellNum; I ++) {if (e. getSource () = button [I]) {if (! Button [I]. getLabel (). equals ("") & judge (button [I], button [position]) {button [position]. setLabel (button [I]. getLabel (); button [I]. setLabel (""); position = I ;}} if (win () {JOptionPane. showMessageDialog (this, "Congratulations") ;}} void go (int dir) {int x = position/col; int y = position % col; switch (dir) {case KeyEvent. VK_UP: if (x! = 0) {button [position]. setLabel (button [position-col]. getLabel (); button [position-col]. setLabel (""); position-= col;} break; case KeyEvent. VK_DOWN: if (x! = Row-1) {button [position]. setLabel (button [position + col]. getLabel (); button [position + col]. setLabel (""); position + = col;} break; case KeyEvent. VK_LEFT: if (y! = 0) {button [position]. setLabel (button [position-1]. getLabel (); button [position-1]. setLabel (""); position-= 1;} break; case KeyEvent. VK_RIGHT: if (y! = Col-1) {button [position]. setLabel (button [position + 1]. getLabel (); button [position + 1]. setLabel (""); position + = 1;} break;} public void keyPressed (KeyEvent e) {go (e. getKeyCode (); if (win () {JOptionPane. showMessageDialog (this, "Congratulations") ;}} public void keyReleased (KeyEvent e) {} public void keyTyped (KeyEvent e) {} public static void main (String [] args) {new EightNumFrame (3, 3 );}}