Java Puzzle项目

1、PuzzleNode类

public class PuzzleNode {
    
    
    private int value;
    private int index;
    private boolean isLeft;//左边是否可走
    private boolean isRight;//右边是否可走

    public PuzzleNode(int value, int index) {
    
    
        this.value = value;
        this.index = index;
    }

    public int getValue() {
    
    
        return value;
    }

    public void setValue(int value) {
    
    
        this.value = value;
    }

    public int getIndex() {
    
    
        return index;
    }

    public void setIndex(int index) {
    
    
        this.index = index;
    }

    public boolean isLeft() {
    
    
        return isLeft;
    }

    public void setLeft(boolean left) {
    
    
        isLeft = left;
    }

    public boolean isRight() {
    
    
        return isRight;
    }

    public void setRight(boolean right) {
    
    
        isRight = right;
    }
}

2、Puzzle类

import java.util.Scanner;
import java.util.Stack;

public class Puzzle {
    
    
    private PuzzleNode[] puzzleNodes;
    private static final int NODESIZE = 10;
    private Stack<PuzzleNode> stack = new Stack<>();
    private static Scanner scanner = new Scanner(System.in);

    public Puzzle() {
    
    
        this.puzzleNodes =new PuzzleNode[NODESIZE];
    }
    private void initPuzszle(){
    
    //初始化数组
        //随机生成数组
        System.out.println("请输入方格中的值:");
        for(int i=0;i<puzzleNodes.length;i++){
    
    
            puzzleNodes[i] = new PuzzleNode(scanner.nextInt(),i);
        }
    }
    private void adjustDirection(){
    
    //调整每个节点左右方向是否可走
        for (int i=0;i<puzzleNodes.length;i++){
    
    
            //左边
            if(i-puzzleNodes[i].getValue()>=0){
    
    
                puzzleNodes[i].setLeft(true);
            }
            //右边
            if (i + puzzleNodes[i].getValue()<puzzleNodes.length){
    
    
                puzzleNodes[i].setRight(true);
            }
        }
    }
    public void goPuzzle(){
    
    
        initPuzszle();
        adjustDirection();

        stack.push(puzzleNodes[0]);
        while (!stack.isEmpty()){
    
    
            //下一个
            PuzzleNode top = stack.peek();
            int value = top.getValue();
            int index = top.getIndex();
            if(value == 0 && index == puzzleNodes.length-1){
    
    
                System.out.println("成功找到路径");
                break;
            }
            if(top.isRight()){
    
    //右边可走
                //将右边节点入栈
                stack.push(puzzleNodes[index+value]);
                top.setRight(false);
            }else if(top.isLeft()){
    
    
                //将左边方向入栈
                stack.push(puzzleNodes[index-value]);
                top.setLeft(false);
            }else{
    
    //左右不可走
                stack.pop();
            }
        }
        if(stack.isEmpty()){
    
    
            System.out.println("无路可走");
        }else{
    
    
            show();
        }
    }
    private void show(){
    
    
        while (!stack.isEmpty()){
    
    
            System.out.print(stack.peek().getValue()+"⬅");
            stack.pop();
        }
    }



}

3、TestDemo测试类

public class TestDemo {
    
    
    public static void main(String[] args) {
    
    
        Puzzle puzzle = new Puzzle();
        puzzle.goPuzzle();
    }


}

猜你喜欢

转载自blog.csdn.net/Super_Powerbank/article/details/106460303