java版迷宫

当年学数据结构,正好java也刚刚学会gui编程,就想着结合起来做个小东西,然后这个自动走路的小球就出来了。

一个方向的枚举Dir.java

public enum Dir {
    L,U,R,D
}

结束标志EndFlag.java

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;


public class EndFlag implements MyDraw{
    private int row,col;
    private Random random = new Random();
    private MyMaze mm;
    private int h = MyMaze.BORDER_SIZE;
    private int w = MyMaze.BORDER_SIZE;
    Color c;
    public EndFlag(MyMaze m,Color c){
        this.row = 27;
        this.col = 27;
        this.mm = m;
        this.c = c;
    }
    
    public void draw(Graphics g){
        Color color = g.getColor();
        g.setColor(this.c);
        g.fillOval(col*MyMaze.BORDER_SIZE, row*MyMaze.BORDER_SIZE, w, h);
        g.setColor(color);
    }
}

所有能绘图的对象的抽象接口

import java.awt.Graphics;


public interface MyDraw {
    public void draw(Graphics g);
}

主窗体

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class MyMaze extends Frame {

    public static final int BORDER_SIZE = 20;
    public static final int ROW = 30;
    public static final int COL = 30;
    public static final int x = 300;
    public static final int y = 100;
    private static Color wallColor = Color.BLACK;
    private static Color endFlagColor = Color.RED;
    private static int success = 2;
    private static int fail = 1;
    
    private Image offScreenImage = null;
    private Font font = new Font("宋体", Font.BOLD,40 );
    public Wall w = new Wall(this,wallColor);
    
    public EndFlag ef = new EndFlag(this,endFlagColor);
    public MyMethod mmd = new MyMethod(this, Color.BLUE,1,"小蓝");
    public MyMethod mmd2 = new MyMethod(this, Color.YELLOW,2,"大黄");
    public MyMethod mymmd = new MyMethod(this, Color.ORANGE,0,"手动");
     private boolean pause = false;//是否暂停重画线程
    
    private Graphics g;
    
    public void launch(){
        this.setLocation(x, y);
        this.setBackground(Color.WHITE);
        this.setSize(COL*BORDER_SIZE, ROW*BORDER_SIZE);
        this.setTitle("迷宫");
        //this.setAlwaysOnTop(false);
        this.setUndecorated(true);
        this.addKeyListener(new KeyMonitor());
        
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            
        });
        this.setResizable(false);                
        this.setVisible(true);
        
        new Thread(new PaintThread()).start();
    }
    
    public static void main(String[] args) {
        new MyMaze().launch();
    }
    
    public void draw(MyDraw d){
        d.draw(g);
    }
    
    public boolean isSuccess(MyMethod m){
        if(m.isSuccess() == success){
            return true;
        }
        return false;
    }
    
    public boolean isFail(MyMethod m){
        if(m.isSuccess() == fail){
            return true;
        }
        return false;
    }
    
    @Override
    public void paint(Graphics g) {
        this.g = g;
        if(isSuccess(mmd) && isSuccess(mmd2)){
            g.setColor(Color.RED);
            g.setFont(font);
            g.drawString("游戏成功!", 225, 325);
            return;
        }
        
        if(isFail(mmd) && isFail(mmd2)){
            g.setColor(Color.RED);
            g.setFont(font);
            g.drawString("游戏失败!", 225, 325);
            return;
        }
        
        Color color = g.getColor();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, COL*BORDER_SIZE, ROW*BORDER_SIZE);
        g.setColor(color);
        draw(w);
        draw(ef);
        draw(mmd);
        draw(mmd2);
        draw(mymmd);
    }

    @Override
    public void update(Graphics g) {
        if(offScreenImage == null){
            offScreenImage = this.createImage(COL*BORDER_SIZE, ROW*BORDER_SIZE);
        }
        Graphics gra = offScreenImage.getGraphics();
        paint(gra);
        g.drawImage(offScreenImage, 0, 0, null);
    }
    
    private class PaintThread implements Runnable {
        public void run() {
            while(true){
                if(pause){
                    continue;
                }
                repaint();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    private class KeyMonitor extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if(key == KeyEvent.VK_SPACE){
                pause = !pause;
            }else if(key == KeyEvent.VK_ESCAPE){
                System.exit(0);
            }
            mymmd.keyPressed(e);
        }
        
    }

}

自动寻路小球的走法

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

import org.w3c.dom.Node;


public class MyMethod implements MyDraw{
    private int step = 0;
    private int choose;
    
    int success = 0;
    public int isSuccess() {
        return success;
    }

    private int row = 3;
    private int col = 3;
    private Dir dir = Dir.R;
    private int w = MyMaze.BORDER_SIZE;
    private int h = MyMaze.BORDER_SIZE;
    private String name;
    Color c;
    MyMaze mm;
    Stack moveStack = new Stack();
    Stack recordStack = new Stack();
    
    public MyMethod(MyMaze mm,Color c,int choose,String name){
        this.c = c;
        this.mm = mm;
        this.choose = choose;
        this.name = name;
        keepMoveRecord();
    }
    
    public void draw(Graphics g){
        move();
        Color color = g.getColor();
        g.setColor(c);
        g.fillOval(col*MyMaze.BORDER_SIZE, row*MyMaze.BORDER_SIZE, w, h);
        g.setColor(color);
    }
    
    void move(){
        if(this.row == 27 && this.col == 27){
            success = 2;//成功
            return;
        }
        if(moveStack.isEmpty()){
            success = 1;//无路可走
            return;
        }
step ++;
        switch (choose) {
        case 0:
            return;
        case 1:
            if(RisOpen()){
                goRight();
                return;
            }
            if(DisOpen()){
                goDown();
                return;
            }
            if(UisOpen()){
                goUp();
                return;
            }
            if(LisOpen()){
                goLeft();
                return;
            }
            break;
        case 2:
            if(DisOpen()){
                goDown();
                return;
                }
            if(RisOpen()){
                goRight();
                return;
            }
            if(UisOpen()){
                goUp();
                return;
            }
            if(LisOpen()){
                goLeft();
                return;
            }
            break;
        }
        goBack();
    }
    
    boolean LisOpen(){
        if(checkWall(this.row, this.col-1) || checkRecordRepeat(this.row, this.col-1)){
            return false;
        }
        return true;
    }
    
    boolean UisOpen(){
        if(checkWall(this.row-1, this.col) || checkRecordRepeat(this.row-1, this.col)){
            return false;
        }
        return true;
    }
    
    boolean RisOpen(){
        if(checkWall(this.row, this.col+1) || checkRecordRepeat(this.row, this.col+1)){
            return false;
        }
        return true;
    }
    
    boolean DisOpen(){
        if(checkWall(this.row+1, this.col) || checkRecordRepeat(this.row+1, this.col)){
            return false;
        }
        return true;
    }
    
    void keepMoveRecord(){
        keepMove();
        keepRecord();
    }
    
    void keepMove(){
        moveStack.push(row, col, dir);
    }
    
    void keepRecord(){
        recordStack.push(row, col, dir);
    }
    
    boolean checkRecordRepeat(int r,int c){
        return    recordStack.isInStack(r, c);
    }
    
    boolean checkWall(int row,int col){
        if(mm.w.wallIsSet(row, col)){
            return true;
        }
        return false;
    }
    
    void goRight(){
        System.out.println(this.name + ": 向右走一步 + step:"+step);
        this.row = row;
        this.col = col+1;
        keepMoveRecord();
    }
    
    void goDown(){
        System.out.println(this.name + ": 向下走一步 + step:"+step);
        this.row = row+1;
        this.col = col;
        keepMoveRecord();
    }
    
    void goLeft(){
        System.out.println(this.name + ": 向左走一步 + step:"+step);
        this.row = row;
        this.col = col-1;
        keepMoveRecord();
    }
    
    void goUp(){
        System.out.println(this.name + ": 向上走一步 + step:"+step);
        this.row = row-1;
        this.col = col;
        keepMoveRecord();
    }
    
    void goBack(){
        moveStack.pop();
        System.out.println(this.name + ": 向后退一步。。。");
        this.row = moveStack.getTopRow();
        this.col = moveStack.getTopCol();
    }
    
    void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        switch(key){
        case KeyEvent.VK_LEFT:
            if(LisOpen()){
                goLeft();
            }
            break;
        case KeyEvent.VK_RIGHT:
            if(RisOpen()){
                goRight();
            }
            break;
        case KeyEvent.VK_UP:
            if(UisOpen()){
                goUp();
            }
            break;
        case KeyEvent.VK_DOWN:
            if(DisOpen()){
                goDown();
            }
            break;
        case KeyEvent.VK_B:
            goBack();
        }
    }
    
}

自己实现的一个栈,也可以用sun提供的替换

class Stack{
    
    private static final int STACK_LENGTH = 1000;
    private int[] posRow = new int[STACK_LENGTH];
    private int[] posCol = new int[STACK_LENGTH];
    Dir[] dirsDir = new Dir[STACK_LENGTH];
        
    int base;
    int top;
    public Stack(){
        base = 0;
        top = 0;
    }
    void push(int row,int col,Dir dir){
        if(!isFull()){
            posRow[top] = row;
            posCol[top] = col;
            dirsDir[top] = dir;
            top++;
        }
    }
    void pop(){
        if(isEmpty()) return;
        top--;
    }
        
    boolean isEmpty(){
        if(top == base){
            return true;
        }
        return false;
    }
        
    boolean isFull(){
        if(top == STACK_LENGTH){
            return true;
        }
        return false;
    }
        
    int getTopRow(){
        if(isEmpty()) return 0;
        return posRow[top-1];
    }
        
    int getTopCol(){
        if(isEmpty()) return 0;
        return posCol[top-1];
    }
        
    Dir getTopDir(){
        return dirsDir[top];
    }
        
    boolean isInStack(int row,int col){
        for(int i = 0 ; i<top ; i++){
            if(posRow[i] == row && posCol[i] == col){
                return true;
            }
        }
        return false;
        }
    
    }

墙的类

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class Wall implements MyDraw{
    
    private MyMaze mm;
    
    Color c ;
    public List<Node> nodes = new ArrayList<Node>();
    
    Random rd = new Random();
    
    public Wall(MyMaze mm,Color c){
        this.mm = mm;
        this.c = c;
        makeWall();
    }
    
    public void draw(Graphics g){
        for(int i = 0; i<nodes.size();i++){
            nodes.get(i).draw(g);
        }
    }
    
    void  makeWall(){
        boolean b = true;
        for(int i = 0 ; i<MyMaze.COL*MyMaze.ROW/3;i++){
            Node node = new Node(rd.nextInt(MyMaze.ROW-2)+1, rd.nextInt(MyMaze.COL-2)+1);
            boolean b1 = node.getCol()==3 && node.getRow()==3 ;
            boolean b2 = node.getCol()==27 && node.getRow()==27 ;
            if(b1) b=false;
            else if(b2) b=false;
            else b=true;
            if(b){
                nodes.add(node);
            }
        }
        
        for(int i = 0 ; i< MyMaze.ROW; i++){
            for(int j = 0; j<MyMaze.COL ; j++){
                if(i == 0 || j == 0 || i == MyMaze.COL-1 ||j == MyMaze.ROW-1){
                    Node n = new Node(i,j);
                    nodes.add(n);
                }
            }
        }
        
    }
    
    public boolean wallIsSet(int row,int col){
        for(int i=0; i<nodes.size();i++){
            Node node = nodes.get(i);
            if(row == node.getRow() && col == node.getCol()){
                return true;
            }
        }
        return false;
    }
    
    private class Node{
        private Toolkit tk = Toolkit.getDefaultToolkit();
        private Image image = null;
        private int row,col;
        
        public int getRow() {
            return row;
        }

        public int getCol() {
            return col;
        }

        private int w = MyMaze.BORDER_SIZE;
        private int h = MyMaze.BORDER_SIZE;
        
        public Node(int row, int col) {
            this.row = row;
            this.col = col;
            image = tk.getImage(Wall.class.getClassLoader().getResource("images/2.jpg"));
        }
        
        public void draw(Graphics g){
            g.drawImage(image, col*MyMaze.BORDER_SIZE, row*MyMaze.BORDER_SIZE, null);
        }
    }

    
}

墙的小图片

posted @ 2014-02-24 10:51  梦醒心晴  Views(467)  Comments(0Edit  收藏  举报