java實(shí)現(xiàn)推箱子小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)推箱子游戲的具體代碼,供大家參考,具體內(nèi)容如下
運(yùn)行示例:
圖形界面由swing組件構(gòu)成
生成地圖的算法如下
創(chuàng)建地圖算法(produceMap):先將二維數(shù)組全置為1,代表初始狀態(tài)全為墻。先隨機(jī)產(chǎn)生兩個(gè)不重合的點(diǎn),一個(gè)作為人的起點(diǎn),另一個(gè)作為箱子的起點(diǎn)。從起點(diǎn)開(kāi)始,隨機(jī)朝四個(gè)方向走一定的步數(shù),若走到的點(diǎn)不是空地,即對(duì)應(yīng)數(shù)組元素不為0,則將該點(diǎn)設(shè)為空地,即數(shù)組元素設(shè)為0。將地圖分為左上、左下、右上和右下四個(gè)區(qū)域,并設(shè)置四個(gè)變量記錄四個(gè)區(qū)域已開(kāi)辟空地的數(shù)量。每開(kāi)辟一塊空地,代表該空地所在區(qū)域的變量加1。走完規(guī)定的步數(shù)后,在四個(gè)區(qū)域中找到開(kāi)辟空地?cái)?shù)量最小的區(qū)域,在該區(qū)域內(nèi)隨機(jī)產(chǎn)生一個(gè)點(diǎn),以該點(diǎn)作為起點(diǎn)循環(huán)執(zhí)行上述操作,直到開(kāi)辟的空地的總數(shù)大于地圖總面積的一半,創(chuàng)建完成。
解決地圖算法(solveMap):創(chuàng)建一個(gè)狀態(tài)類,將箱子的一個(gè)位置和在搜索這個(gè)箱子的過(guò)程中人的一個(gè)位置以及箱子走到這個(gè)位置的步數(shù)表示為一個(gè)狀態(tài)。建立一個(gè)優(yōu)先隊(duì)列,隊(duì)列元素為狀態(tài)類的對(duì)象,步數(shù)小的狀態(tài)的優(yōu)先級(jí)高。從起點(diǎn)開(kāi)始按照上下左右的順序進(jìn)行廣度優(yōu)先搜索。建立一個(gè)HashMap,鍵為狀態(tài)類,值為布爾類,用來(lái)保存每個(gè)狀態(tài)的是否被走過(guò),再建立一個(gè)HashMap,鍵為點(diǎn)類,值也為點(diǎn)類,保存箱子的每一個(gè)位置的前一個(gè)位置,用于最后輸出路徑。使用優(yōu)先隊(duì)列可以使推動(dòng)箱子次數(shù)多的狀態(tài)推遲出隊(duì),從而保證箱子每個(gè)能推動(dòng)的方向都被訪問(wèn)到并推動(dòng),若直接使用隊(duì)列,則無(wú)法使每種情況都走到。而當(dāng)?shù)貓D較大的時(shí)候,采用HashMap存儲(chǔ)訪問(wèn)信息可以節(jié)省較大的空間。優(yōu)先隊(duì)列每次執(zhí)行出隊(duì)操作的時(shí)候記錄出隊(duì)狀態(tài)的步數(shù),當(dāng)當(dāng)前出隊(duì)狀態(tài)的步數(shù)和上一個(gè)出隊(duì)狀態(tài)的步數(shù)不同的時(shí)候,將保存狀態(tài)訪問(wèn)信息的HashMap清空。另設(shè)置一個(gè)HashMap鍵為點(diǎn)類,值為整型類,保存每個(gè)點(diǎn)箱子訪問(wèn)過(guò)幾次,若超過(guò)四次,則不訪問(wèn)該點(diǎn)。由優(yōu)先隊(duì)列的性質(zhì)可知,出隊(duì)的狀態(tài)的步數(shù)是隊(duì)列中步數(shù)最小的狀態(tài),即當(dāng)當(dāng)前出隊(duì)的狀態(tài)的步數(shù)和前一個(gè)不同的時(shí)候,箱子前一步的狀態(tài)已全部出隊(duì)。這時(shí)箱子之前的訪問(wèn)狀態(tài)已經(jīng)不再需要,及時(shí)清空保存訪問(wèn)狀態(tài)的HashMap可以節(jié)省很大的內(nèi)存空間。當(dāng)廣搜搜到箱子在終點(diǎn)位置的狀態(tài),則搜索結(jié)束。若結(jié)束狀態(tài)的步數(shù)小于規(guī)定的最小步數(shù),則返回解決失敗,否則返回解決成功。若未搜到箱子在終點(diǎn)位置的狀態(tài)并且隊(duì)列已空,則返回解決失敗。
記錄路徑算法(recordPath):根據(jù)在解決地圖的過(guò)程中建立的記錄箱子每個(gè)位置前一個(gè)位置的一個(gè)HashMap,從終點(diǎn)開(kāi)始,一步步找回起點(diǎn),并將在這過(guò)程中找到的點(diǎn)按順序入棧,然后再將棧中的點(diǎn)逐個(gè)出棧并記錄在一個(gè)點(diǎn)的數(shù)組中,以實(shí)現(xiàn)路徑的正序輸出。
Main_Class.java
public class Main_Class { public static void main(String args[]) { PTB_Frame frame=new PTB_Frame("Push The Box"); frame.setBounds(0,0,1200,1200); } }
PTB_Frame.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") public class PTB_Frame extends JFrame { private Font font = new Font("宋體", Font.PLAIN, 23); private Map_Manager map_manager=new Map_Manager(); private ConsolePanel console = new ConsolePanel(map_manager); private JButton creat_map=new JButton("創(chuàng)建地圖"); private MapPanel map=new MapPanel(map_manager); PTB_Frame(String title) { init(); this.setTitle(title); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } void init() { this.setLayout(null); console.setBounds(0, 0, 1200,250); console.add(creat_map); this.add(console); map.setBounds(80, 250, 1200, 800); this.add(map); creat_map.setFont(font); creat_map.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.creatMap(); map.setMap(map_manager.getHeight(), map_manager.getWidth(), map_manager.getStepOfMap(), map_manager.getMap()); map.creatMap(); } }); } }
ConsolePanel.java
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @SuppressWarnings("serial") public class ConsolePanel extends JPanel { Font font = new Font("宋體", Font.PLAIN, 23); JTextField t_height = new JTextField(3); JTextField t_width = new JTextField(3); JTextField t_diff = new JTextField(5); JButton get_path = new JButton("查看最短路徑"); JTextArea show_path = new JTextArea(5, 40); int height, width; double diff; Map_Manager map_manager; ConsolePanel(Map_Manager map_manager) { this.map_manager = map_manager; UIManager.put("Label.font", font); this.add(new JLabel("地圖高度:")); t_height.setFont(font); this.add(t_height); this.add(new JLabel("(3~100的整數(shù))")); this.add(new JLabel("地圖寬度:")); t_width.setFont(font); this.add(t_width); this.add(new JLabel("(3~100的整數(shù))")); this.add(new JLabel("地圖難度:")); t_diff.setFont(font); this.add(t_diff); this.add(new JLabel("(1.0~10.0之間的小數(shù))")); this.add(new JLabel("注:地圖高度和寬度以及難度越大,生成地圖時(shí)間越長(zhǎng)")); get_path.setFont(font); this.add(get_path); show_path.setFont(font); show_path.setLineWrap(true);// 自動(dòng)換行 show_path.setWrapStyleWord(true);// 換行不斷字 JPanel show_path_panel = new JPanel(); show_path_panel.add(new JScrollPane(show_path));//滾動(dòng)窗口 this.add(show_path_panel); get_path.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Map_Manager.Point path[] = map_manager.getPath(); if (path != null) { show_path.setText(null); for (int i = 0; i<map_manager.getStepOfMap(); ++i) show_path.append(path[i] + " "); } } }); } public void creatMap() { try { height = Integer.valueOf(t_height.getText()); width = Integer.valueOf(t_width.getText()); diff = Double.valueOf(t_diff.getText()); if (height < 3 || height > 100 || width < 3 || width > 100 || diff < 1 || diff > 10) throw new NumberFormatException(); map_manager.setMap(height, width, diff); map_manager.creatMap(); show_path.setText(null); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(getRootPane(), "參數(shù)格式不正確"); } } }
MapPanel.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; @SuppressWarnings("serial") public class MapPanel extends JPanel { private Font font = new Font("宋體", Font.PLAIN, 23); private JPanel map_area; JPanel control_bar = new JPanel(); private JButton drawback, restart,start; private JLabel l_min_step, l_left_step, l_passed_step; private JLabel show_cur_point; private JLabel show_cur_box; private Point person = new Point(); private Point start_of_person = new Point(); private Point start_of_box = new Point(); private Point end = new Point(); private Point box = new Point(); private int width, height; private int map[][] = new int[100][100]; private int di[][] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; private int min_step, passed_step = 0; private Stack<Operation> operation_recorder = new Stack<Operation>(); private JButton block[][] = new JButton[100][100]; private Map_Manager map_manager; private class Point implements Cloneable { int x, y; public boolean equals(Object obj) { if (!(obj instanceof Point)) return false; Point point = (Point) obj; return this.x == point.x && this.y == point.y; } public Object clone() { Point newPoint = null; try { newPoint = (Point) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return newPoint; } public String toString() { return "(" + (this.x + 1) + "," + (this.y + 1) + ")"; } } public void setMap(int height, int width, int min_step, int map[][]) { this.height = height; this.width = width; this.min_step = min_step; this.map = map; } private class PtbKeyAdapter extends KeyAdapter { public void keyPressed(KeyEvent e) { int keycode = e.getKeyCode(); if (keycode == KeyEvent.VK_UP) { pointMove(0); } else if (keycode == KeyEvent.VK_DOWN) { pointMove(1); } else if (keycode == KeyEvent.VK_LEFT) { pointMove(2); } else if (keycode == KeyEvent.VK_RIGHT) { pointMove(3); } } } private class Operation { int stuff;// 1代表箱子,0代表人 int dir;// 0代表上,1代表下,2代表左,3代表右 Operation(int stuff, int dir) { this.stuff = stuff; this.dir = dir; } } private boolean accessible(Point point) {// point可走則返回true if (point.x < 0 || point.x >= height || point.y < 0 || point.y >= width)// 越界 return false; if (map[point.x][point.y] == 1)// 走到墻上 return false; return true; } private void pointMove(int dir) { if(passed_step>=min_step) return; // 先判斷能否進(jìn)行交換,若能,則交換兩按鈕顏色值 Point cur_point = new Point(); cur_point.x = person.x + di[dir][0]; cur_point.y = person.y + di[dir][1]; if (!accessible(cur_point))// 當(dāng)前點(diǎn)不可走 return; if (cur_point.equals(box)) { // 當(dāng)人前進(jìn)方向上前一個(gè)點(diǎn)是箱子 Point next_box = new Point(); next_box.x = box.x + di[dir][0]; next_box.y = box.y + di[dir][1]; if (!accessible(next_box))// 箱子無(wú)法推動(dòng) return; // 如果箱子能前進(jìn),則人也前進(jìn),不能則人不能前進(jìn) go(box, dir); ++this.passed_step; updateStep(); operation_recorder.push(new Operation(1, dir)); } go(person, dir); show_cur_point.setText(" "+person.toString()+" "); operation_recorder.push(new Operation(0, dir)); if (box.equals(end)) JOptionPane.showMessageDialog(this.getRootPane(), "WINNER WINNER CHICKEN DINNER!"); else if(this.passed_step==this.min_step) JOptionPane.showMessageDialog(this.getRootPane(), "江河猶在,命數(shù)已盡,悲哉!"); } private void go(Point point, int dir) {// 實(shí)現(xiàn)前進(jìn)部分的代碼 Color color = block[point.x][point.y].getBackground(); if (point.equals(end)) block[point.x][point.y].setBackground(Color.GREEN); else block[point.x][point.y].setBackground(Color.WHITE); point.x += di[dir][0]; point.y += di[dir][1]; block[point.x][point.y].setBackground(color); } private void updateStep() { l_passed_step.setText(Integer.toString(passed_step)); l_left_step.setText(Integer.toString(min_step - passed_step)); show_cur_box.setText(" "+box.toString()+" "); } public void paintMap() { map_area = new JPanel(new GridLayout(this.height, this.width)); for (int i = 0; i < this.height; i++) for (int j = 0; j < this.width; j++) { block[i][j] = new JButton(); if (map[i][j] == 0)// 數(shù)組中0為路 block[i][j].setBackground(Color.WHITE); else if (map[i][j] == 1)// 數(shù)組中1為墻 block[i][j].setBackground(Color.BLACK); else if (map[i][j] == 2)// 數(shù)組中2為箱子位置 { block[i][j].setBackground(Color.BLUE); start_of_box.x = i; start_of_box.y = j; } else if (map[i][j] == 3)// 數(shù)組中3為終點(diǎn) { block[i][j].setBackground(Color.GREEN); end.x = i; end.y = j; } else if (map[i][j] == 4) {// 數(shù)組中4為人的位置 block[i][j].setBackground(Color.RED); start_of_person.x = i; start_of_person.y = j; } map_area.add(block[i][j]); } person = (Point) start_of_person.clone(); box = (Point) start_of_box.clone(); l_min_step.setText(Integer.toString(min_step)); show_cur_point.setText(" "+person.toString()+" "); passed_step=0; updateStep(); int map_height=750,map_width=750; if(this.height>this.width) map_width=(750/this.height)*this.width; else if(this.width>this.height) map_height=(750/this.width)*this.height; map_area.setBounds(0, 0, map_width, map_height); this.add(map_area); } public MapPanel(Map_Manager map_manager) { this.map_manager = map_manager; init(); } private void init() { this.setLayout(null); map_manager.setMap(20, 20, 7.0); map_manager.creatMap(); this.height = map_manager.getHeight(); this.width = map_manager.getWidth(); this.min_step = map_manager.getStepOfMap(); this.map = map_manager.getMap(); UIManager.put("Label.font", font); drawback = new JButton("后退一步"); restart = new JButton("重新開(kāi)始"); start=new JButton("開(kāi)始"); control_bar.add(new JLabel("當(dāng)前箱子的位置")); show_cur_box=new JLabel(); control_bar.add(show_cur_box); control_bar.add(new JLabel("當(dāng)前人的位置")); show_cur_point=new JLabel(); control_bar.add(show_cur_point); control_bar.add(new JLabel("最短步數(shù):")); l_min_step = new JLabel(); l_min_step.setFont(font); control_bar.add(l_min_step); control_bar.add(new JLabel("已走步數(shù):")); l_passed_step = new JLabel(); l_passed_step.setFont(font); control_bar.add(l_passed_step); control_bar.add(new JLabel("剩余步數(shù):")); l_left_step = new JLabel(); l_left_step.setFont(font); control_bar.add(l_left_step); control_bar.add(new JLabel("注:這里的步數(shù)是")); control_bar.add(new JLabel("箱子移動(dòng)的步數(shù)")); control_bar.add(new JLabel("紅色代表人")); control_bar.add(new JLabel("藍(lán)色代表箱子")); control_bar.add(new JLabel("綠色代表終點(diǎn)")); restart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { block[person.x][person.y].setBackground(Color.WHITE); person = (Point) start_of_person.clone(); block[box.x][box.y].setBackground(Color.WHITE); box = (Point) start_of_box.clone(); block[person.x][person.y].setBackground(Color.RED); block[box.x][box.y].setBackground(Color.BLUE); block[end.x][end.y].setBackground(Color.GREEN); passed_step = 0; show_cur_point.setText(" "+person.toString()+" "); updateStep(); } }); drawback.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (operation_recorder.empty()) return; Operation cur_op = operation_recorder.peek(); operation_recorder.pop(); int dir; switch (cur_op.dir) {// 得到相反方向 case 0: dir = 1; break; case 1: dir = 0; break; case 2: dir = 3; break; default: dir = 2; } // 推箱子的時(shí)候,箱子先走,人再走 // 不推箱子的時(shí)候直接就是人走,所以棧頂元素始終是人的操作 go(person, dir);// 人相反方向走一步 show_cur_point.setText(" "+person.toString()+" "); if (!operation_recorder.empty()&&operation_recorder.peek().stuff == 1) { // 下一個(gè)是箱子,即人從該位置推動(dòng)的箱子 go(box, dir);// 箱子相反方向走一步 operation_recorder.pop(); --passed_step; updateStep(); } } }); PtbKeyAdapter ptbkeyadapter=new PtbKeyAdapter(); start.addKeyListener(ptbkeyadapter); restart.addKeyListener(ptbkeyadapter); drawback.addKeyListener(ptbkeyadapter); start.setFont(font); restart.setFont(font); drawback.setFont(font); control_bar.add(start); control_bar.add(restart); control_bar.add(drawback); control_bar.setBounds(850, 100, 200, 600); this.paintMap(); this.add(control_bar); } public void creatMap() { this.setLayout(null); this.map_area.removeAll(); this.map_area.setVisible(false); paintMap(); this.revalidate(); this.map_area.setVisible(true); } }
Map_Manager.java
import java.util.*; public class Map_Manager { private PTB_Map ptb_map = new PTB_Map(); private int di[][] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; private double search_limit; private int area[] = new int[4]; private Map<Status, Boolean> visit_of_status = new HashMap<Status, Boolean>(); private Map<Point, Integer> visit_of_box = new HashMap<Point, Integer>(); private PriorityQueue<Status> q = new PriorityQueue<Status>(new Comparator<Status>() { public int compare(Status s1, Status s2) { return s1.step - s2.step; } }); private class PTB_Map { private int width; private int height; private int step_of_map; private int accessible_point; private Point end_p = new Point(); private Status start = new Status(); private int matrix[][] = new int[100][100]; private Map<Point, Point> path_map = new HashMap<Point, Point>(); private Point[] path = new Point[1000]; private double ratio_of_space; private double ratio_of_step; } public class Point implements Cloneable { int x, y; public boolean equals(Object obj) { if (!(obj instanceof Point)) return false; Point point = (Point) obj; return this.x == point.x && this.y == point.y; } public int hashCode() { return this.x * ptb_map.width + this.y; } public Object clone() { Point newPoint = null; try { newPoint = (Point) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return newPoint; } public String toString() { return "(" + (this.x + 1) + "," + (this.y + 1) + ")"; } } private class Status implements Cloneable { Point box = new Point(); Point person = new Point(); int step; public int hashCode() { return this.box.hashCode() * this.person.hashCode() + this.step; } public boolean equals(Object obj) { if (!(obj instanceof Status)) return false; Status status = (Status) obj; return this.box.equals(status.box) && this.person.equals(status.person) && this.step == status.step; } public Object clone() { Status newStatus = null; try { newStatus = (Status) super.clone(); newStatus.box = (Point) box.clone(); newStatus.person = (Point) person.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return newStatus; } } public void setMap(int height, int width, double degree_of_difficulty) { this.ptb_map.height = height; this.ptb_map.width = width; this.ptb_map.ratio_of_space = 0.5; double ratio = 1; if (height + width < 20) ratio = 0.05 * (height + width); this.ptb_map.ratio_of_step = 0.5 + degree_of_difficulty * 0.05 * ratio; this.search_limit = (this.ptb_map.height * this.ptb_map.width) / 10; } public int getHeight() { return ptb_map.height; } public int getWidth() { return ptb_map.width; } public int getStepOfMap() { return ptb_map.step_of_map; } private boolean accessible(Point point) {// point可走則返回true if (point.x < 0 || point.x >= ptb_map.height || point.y < 0 || point.y >= ptb_map.width)// 越界 return false; if (ptb_map.matrix[point.x][point.y] == 1)// 走到墻上 return false; return true; } private void creatSpace(Point point) { Random random = new Random(); int l = 0; while (l <= search_limit) { int dir = random.nextInt(4); if (point.x + di[dir][0] < 0 || point.x + di[dir][0] >= ptb_map.height || point.y + di[dir][1] < 0 || point.y + di[dir][1] >= ptb_map.width) continue;// 若往該方向走一步越界,則換個(gè)方向 point.x += di[dir][0]; point.y += di[dir][1]; if (this.ptb_map.matrix[point.x][point.y] != 0) { if (point.y < ptb_map.width / 2) { if (point.x < ptb_map.height / 2) ++area[0];// 該點(diǎn)在左上方 else ++area[1];// 該點(diǎn)在左下方 } else { if (point.x < ptb_map.height / 2) ++area[2];// 該點(diǎn)在右上方 else ++area[3];// 該點(diǎn)在右下方 } this.ptb_map.matrix[point.x][point.y] = 0; ++this.ptb_map.accessible_point; } ++l; } } private boolean produceMap() {// 返回值為地圖是否創(chuàng)建成功 Random random = new Random(); // 重置地圖的矩陣 for (int i = 0; i < ptb_map.height; ++i) for (int j = 0; j < ptb_map.width; ++j) ptb_map.matrix[i][j] = 1; do// 隨機(jī)設(shè)置人和箱子的初始位置 { ptb_map.start.box.x = random.nextInt(ptb_map.height); ptb_map.start.box.y = random.nextInt(ptb_map.width); ptb_map.start.person.x = random.nextInt(ptb_map.height); ptb_map.start.person.y = random.nextInt(ptb_map.width); } while (ptb_map.start.box.equals(ptb_map.start.person)); ptb_map.accessible_point = 0; ptb_map.matrix[ptb_map.start.person.x][ptb_map.start.person.x] = 0; // 設(shè)置一定的墻 ptb_map.accessible_point = 0; Point start = (Point) ptb_map.start.person.clone();// 最開(kāi)始走的位置為人的初始位置 area[0] = area[1] = area[2] = area[3] = 0; creatSpace(start); while (ptb_map.accessible_point < (ptb_map.height * ptb_map.width) * ptb_map.ratio_of_space) { int min = 10000, min_area = 0; for (int i = 0; i < 4; ++i) if (area[i] < min) { min = area[i]; min_area = i; } switch (min_area) { case 0:// 左上 start.x = random.nextInt(ptb_map.height / 2); start.y = random.nextInt(ptb_map.width / 2); break; case 1:// 左下 start.x = random.nextInt(ptb_map.height / 2) + ptb_map.height / 2; start.y = random.nextInt(ptb_map.width / 2); break; case 2:// 右上 start.x = random.nextInt(ptb_map.height / 2); start.y = random.nextInt(ptb_map.width / 2) + ptb_map.width / 2; break; case 3:// 右下 start.x = random.nextInt(ptb_map.height / 2) + ptb_map.height / 2; start.y = random.nextInt(ptb_map.width / 2) + ptb_map.width / 2; break; } creatSpace(start); } ptb_map.end_p = (Point) start.clone(); ptb_map.matrix[ptb_map.start.person.x][ptb_map.start.person.y] = 4; ptb_map.matrix[ptb_map.start.box.x][ptb_map.start.box.y] = 2; ptb_map.matrix[ptb_map.end_p.x][ptb_map.end_p.y] = 3; return true; } private boolean solveMap() {// 返回值為當(dāng)前地圖是否有路徑 q.clear(); visit_of_box.clear(); ptb_map.path_map.clear(); ptb_map.step_of_map = 0; ptb_map.start.step = 0; int pre_step = -1; q.add(ptb_map.start); visit_of_status.put(ptb_map.start, true); while (!q.isEmpty()) { Status pre_Status = (Status) q.peek().clone(); if (pre_Status.step != pre_step) { visit_of_status.clear(); pre_step = pre_Status.step; } for (int i = 0; i < 4; ++i) { Status cur_Status = (Status) pre_Status.clone(); cur_Status.person.x += di[i][0]; cur_Status.person.y += di[i][1]; if (!accessible(cur_Status.person))// 該點(diǎn)不可走 continue; if (visit_of_status.containsKey(cur_Status))// 該點(diǎn)已經(jīng)走過(guò) continue; // 保存當(dāng)前點(diǎn)的前一個(gè),用于輸出路徑 if (cur_Status.person.equals(cur_Status.box))// 走到箱子上 { Point next_box = new Point();// 當(dāng)前人會(huì)把箱子推到的位置 next_box.x = cur_Status.box.x + di[i][0]; next_box.y = cur_Status.box.y + di[i][1]; if (!accessible(next_box))// 該點(diǎn)不可走 continue; if (ptb_map.path_map.containsKey(next_box)) continue; if (visit_of_box.containsKey(next_box)) { if (visit_of_box.get(next_box) > 4)// 當(dāng)前位置箱子已走過(guò)四次 continue; } // 箱子可以走到該點(diǎn),則箱子走一步 ptb_map.path_map.put(next_box, cur_Status.box); cur_Status.box = next_box; ++cur_Status.step; if (!visit_of_box.containsKey(cur_Status.box)) visit_of_box.put(cur_Status.box, 1); else { int t = visit_of_box.get(cur_Status.box); ++t; visit_of_box.put(cur_Status.box, t); } if (cur_Status.box.equals(ptb_map.end_p))// 箱子走到終點(diǎn) { ptb_map.step_of_map = cur_Status.step; q.clear(); if (ptb_map.step_of_map < (ptb_map.height + ptb_map.width) * ptb_map.ratio_of_step) return false; else return true; } } q.add(cur_Status); visit_of_status.put(cur_Status, true); } pre_Status = null; q.poll(); } return false; } public void recordPath() {// 記錄路徑 Stack<Point> output_path = new Stack<Point>();// 用于輸出路徑 Point cur_point = (Point) ptb_map.end_p.clone();// 從終點(diǎn)開(kāi)始 int step = -1; while (step != ptb_map.step_of_map) { ++step; output_path.push(cur_point); cur_point = ptb_map.path_map.get(cur_point); if (cur_point == null) break; } int i = 0; while (!output_path.empty())// 將路徑保存在點(diǎn)數(shù)組里 { ptb_map.path[i] = output_path.peek(); output_path.pop(); ++i; } } public void creatMap() { int i = 0; do { while (!produceMap()) ; ++i; } while (!solveMap()); recordPath(); printMap();// System.out.println(i);// } public int[][] getMap() { return ptb_map.matrix; } public Point getStartBoxPoint() { return ptb_map.start.box; } public Point getStartPersonPoint() { return ptb_map.start.person; } public Point getEndPoint() { return ptb_map.end_p; } private void printMap() { for (int i = 0; i < ptb_map.height; ++i) { System.out.print(ptb_map.matrix[i][0]); for (int j = 1; j < ptb_map.width; ++j) System.out.print(" " + ptb_map.matrix[i][j]); System.out.println(); } } public Point[] getPath() { return ptb_map.path; } }
更多精彩游戲,請(qǐng)參考專題《java經(jīng)典小游戲》
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Spring Boot中加載初始化數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了在Spring Boot中加載初始化數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Spring?Boot?使用觀察者模式實(shí)現(xiàn)實(shí)時(shí)庫(kù)存管理的步驟
在現(xiàn)代軟件開(kāi)發(fā)中,實(shí)時(shí)數(shù)據(jù)處理非常關(guān)鍵,本文提供了一個(gè)使用SpringBoot和觀察者模式開(kāi)發(fā)實(shí)時(shí)庫(kù)存管理系統(tǒng)的詳細(xì)教程,步驟包括創(chuàng)建項(xiàng)目、定義實(shí)體類、實(shí)現(xiàn)觀察者模式、集成Spring框架、創(chuàng)建RESTful?API端點(diǎn)和測(cè)試應(yīng)用等,這將有助于開(kāi)發(fā)者構(gòu)建能夠即時(shí)響應(yīng)庫(kù)存變化的系統(tǒng)2024-09-09詳解IDEA中MAVEN項(xiàng)目打JAR包的簡(jiǎn)單方法
本篇文章主要介紹了詳解IDEA中MAVEN項(xiàng)目打JAR包的簡(jiǎn)單方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Spring的CorsFilter會(huì)失效的原因及解決方法
眾所周知CorsFilter是Spring提供的跨域過(guò)濾器,我們可能會(huì)做以下的配置,基本上就是允許任何跨域請(qǐng)求,我利用Spring的CorsFilter做跨域操作但是出現(xiàn)報(bào)錯(cuò),接下來(lái)小編就給大家介紹一Spring的CorsFilter會(huì)失效的原因及解決方法,需要的朋友可以參考下2023-09-09Applet小應(yīng)用程序開(kāi)發(fā)簡(jiǎn)介
Applet小應(yīng)用程序開(kāi)發(fā)簡(jiǎn)介 ,用java開(kāi)發(fā)的小程序,需要的朋友可以參考下2012-09-09Spring Boot項(xiàng)目打包指定包名實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Spring Boot項(xiàng)目打包指定包名實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11