JAVA 贪吃蛇 控制台版 自动移动 set LinkedList实现

嗯。这是我写的最简单的贪吃蛇了。代码一部分参考一位大神的。但是我现在找不到了。非原创供大家参考用。另外我加了一些功能。

package com;

import java.util.*;

class Node {
    private int i;
    private int j;

    public Node() {
    }

    public Node(int i, int j) {
        this.i = i;
        this.j = j;
    }

    public void setI(int i) {
        this.i = i;
    }

    public void setJ(int j) {
        this.j = j;
    }

    public int getI() {
        return this.i;
    }

    public int getJ() {
        return this.j;
    }

    public int hashCode() {
        return 6;
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else {
            Node n = (Node) obj;
            return n.i == this.i && n.j == this.j;
        }

    }
}

class WormPanel {
    private Worm worm;
    private HashSet<Node> food;

    public Worm getWorm() {
        return worm;
    }

    class Worm implements Runnable {
        private int dirInt = -10;
        private LinkedList<Node> nodes;// 蛇由点组成

        public Worm() {
            nodes = new LinkedList<Node>();// 初始化蛇
            nodes.add(new Node(2, 9));
            nodes.add(new Node(3, 9));
            nodes.add(new Node(4, 9));
            nodes.add(new Node(5, 9));
            nodes.add(new Node(5, 10));
            nodes.add(new Node(5, 11));
            nodes.add(new Node(6, 11));
            nodes.add(new Node(7, 11));
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                step();
                shiTu();
                try {
                    Thread.sleep(600);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public void step() {
            Node n, new1;
            Scanner sc = new Scanner(System.in);
            // while(true){
            n = nodes.get(0);
            int x = n.getI() + dirInt / 10;
            int y = n.getJ() + dirInt % 10;
            switch(x){
            case 0:
                x=9;break;
            case 9:
                x=0;break;
            }
            switch(y){
            case 0:
                y=31;break;
            case 31:
                y=0;break;
            }

            new1 = new Node(x,y );
            if (nodes.contains(new1)) {
                throw new RuntimeException("吃自己了");
            }
            nodes.add(0, new1);

            if (food.remove(new1)) {
                randFood();
                return;
            }
            nodes.remove(nodes.size() - 1);
        }

        public void step(char dir) {
            Node n, new1;
            switch (dir) {
            case 'a':

                if ((this.dirInt - 1) == 0) {
                    break;
                }
                this.dirInt = -1;
                step();
                break;
            case 'w':
                if ((this.dirInt - 10) == 0) {
                    break;
                }
                this.dirInt = -10;
                step();
                break;
            case 's':
                if ((this.dirInt + 10) == 0) {
                    break;
                }
                this.dirInt = 10;
                step();

                break;
            case 'd':
                if ((this.dirInt + 1) == 0) {
                    break;
                }
                this.dirInt = 1;
                step();

                break;
            default:
                break;
            }
        }

        public boolean contains(int x, int y) {// 判断蛇体包含该坐标点吗
            return nodes.contains(new Node(x, y));
        }
    }

    public WormPanel() {
        worm = new Worm();
        Thread t = new Thread(worm);
        t.start();
        food = new HashSet<Node>();
        randomFood();// 随机生成食物
    }

    public void randFood() {
        Random rand = new Random();
        int x = rand.nextInt(9);
        int y = rand.nextInt(31);
        food.add(new Node(x, y));
    }

    public void randomFood() {
        Random rand = new Random();
        int i = 0;
        int x = rand.nextInt(9);
        int y = rand.nextInt(31);
        while (i < 3) {
            food.add(new Node(x, y));
            x = rand.nextInt(9);
            y = rand.nextInt(31);
            i++;
        }
    }

    public void shiTu() {// 用来绘画出当前的情况
        for (int j = 0; j < 31; j++) {
            System.out.print("-");
        }
        System.out.println();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 32; j++) {
                if (j == 0 || j == 31) {
                    System.out.print("|");
                } else if (worm.contains(i, j)) {
                    System.out.print("#");
                } else if (food.contains(new Node(i, j))) {
                    System.out.print("0");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        for (int j = 0; j < 31; j++) {
            System.out.print("-");
        }
        System.out.println();

    }

}
class demo1 {
    public static void main(String[] args) {
        WormPanel w = new WormPanel();
        w.shiTu();
        Scanner sc = new Scanner(System.in);
        while (true) {
            char c = sc.next().charAt(0);
            w.getWorm().step(c);
            w.shiTu();

        }
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值