完全用链表实现的贪吃蛇

来源:互联网 发布:unity3d教程微盘 编辑:程序博客网 时间:2024/05/18 01:30

1.链表设计

同事突然说想实现一个贪吃蛇,这使我想起了几年前实现的一个很糟糕的贪吃蛇程序,代码可以在《一个java写的贪吃蛇程序》里面找到。如今,突然想再实现一个贪吃蛇,不过这次绝对不能再那么糟糕了。
     用链表实现并且只用链表实现贪吃蛇是一个不错的主意,于是初步的打算就是先规划出到底需要什么链表,图示如下:

游戏面板上的所有的元素都处于一条或者多条链表之中,这样整个游戏的操作就简单了,无非就是将元素在链表之间移动来移动去。另外一个要点就是将“位置”信息作为静态数据保存,将元素本身和元素的位置完全分离,只是将位置当成元素的一个属性信息。

2.数据结构

到底用什么链表呢?Linux内核的“侵入式链表”list_head是一个不错的选择,这样就不用管理大量的链表节点内存了,因为“本身即链表”。因此设计以下的数据结构:

2.1.位置结构

[cpp] view plaincopy
  1. struct posision {  
  2.     int x;  
  3.     int y;  
  4. };  

很简单,没什么好说的,坐标而已,也可以扩展成三维坐标。需要注意,方向信息也作为一个posision保存,之所以可以这样做是因为posision是一个坐标,而一个坐标在坐标系中可以表示一个失量,矢量的含义就是有方向的数量,因此posision既可以表示位置,又可以表示方向。

2.2.节点结构

[cpp] view plaincopy
  1. struct body {  
  2.     struct list_head list;  
  3.     struct posision pos;  
  4.     struct posision direct;  
  5.     void (*go_on) (struct body* this);  
  6.     void *private;  
  7. };  

其中go_on回调函数实现了蛇的前进动作,但是由于像障碍,边墙,食物本身也是链表元素,因此需要一个private数据来做扩展。需要指明的是,整个游戏中还有一个隐含的链表,那就是“拐弯”处的节点,这个链表记录了蛇身体该在哪个地方拐弯,因此很显然,拐弯链表是需要和键盘按键侦听器联系在一起的。

2.3.游戏面板

[cpp] view plaincopy
  1. struct pad {  
  2.     struct list_head snake_body;  
  3.     struct list_head changes;  
  4.     struct list_head kill_body;  
  5.     struct list_head food;  
  6.     unsigned int curr_status;  
  7.     void (*game_handler) (struct pad* this);  
  8.     void (*key_handler) (struct pad* thisstruct posision key);  
  9.     void (*random_gen_food) (struct pad* this);  
  10.     void (*random_gen_fence) (struct pad* this);  
  11.     void (*eat) (struct pad* this);  
  12. };  

该数据结构囊括了整个游戏的面板以及操作。

2.4.代码初步实现

有了上述的数据结构,实现代码就很简单了
首先,go_on的逻辑就是当前位置加上方向矢量
[cpp] view plaincopy
  1. void gen_go_on(struct body* this)  
  2. {  
  3.     sb->pos.x += sb->direct.x;      
  4.     sb->pos.y += sb->direct.y;      
  5. }  

键盘处理就是根据不同的按键来生成方向信息
[cpp] view plaincopy
  1. void gen_key_handler(struct pad* game, unsigned int key)  
  2. {  
  3.     //{0,-1},{0,1},{-1,0},{1,0} //这几个是静态的方向数据,全部是posision结构体  
  4.     //ALLOC body -> cb  
  5.     list_add (cb, &(game->changes));      
  6. }  

食物生成比较复杂,但是最简单的实现就是随机在面板的空闲链表中取出一个加入食物链表,我的这个贪吃蛇实现中,不但食物一次可以有多份,体现蛇的贪婪,并且还可以设置物体障碍,表现蛇玩命贪吃。
[cpp] view plaincopy
  1. void gen_random_gen_food(struct pad* game)  
  2. {  
  3.     //random pos avoid snake_body or kill_body  
  4.     //ALLOC body -> fb  
  5.     list_add (fb, &(game->food));  
  6. }  

最重要的就是以下这个game_handler了,它处理游戏的中心逻辑
[cpp] view plaincopy
  1. unsigned int game_handler()  
  2. {  
  3.     struct list_head *snake;  
  4.     //以下的遍历判断蛇节点是否需要拐弯  
  5.     list_for_each(snake, &(g_pad->snake_body)){  
  6.         struct body *sb = list_entry(snake, struct body, list);  
  7.         struct list_head *change;  
  8.         list_for_each(change, &(g_pad->changes)){      
  9.             struct body *cp = list_entry(change, struct body, list);  
  10.             if (sb->pos.x == cp->pos.x && sb->pos.y == cp->pos.y) {  
  11.                 sb->direct = cp->direct;  
  12.                 if (snake->next == &snake_body) {  
  13.                     list_del (change);  
  14.                 }  
  15.                 break;  
  16.             }  
  17.         }  
  18.         (*sb->go_on)(sb);  
  19.     }  
  20.     //以下的遍历判断蛇头是否碰到自身或者边墙  
  21.     struct body *head = snake_body.next;  
  22.     struct list_head *kb;  
  23.     list_for_each(kb, &(g_pad->kill_body)){      
  24.         struct body *cp = list_entry(kb, struct body, list);  
  25.         if (head->pos.x == cp->pos.x && head->pos.y == cp->pos.y) {  
  26.             g_pad->curr_status = 1;  
  27.         }  
  28.     }  
  29.     //以下的遍历判断蛇头是否碰到了食物,然后吃掉它  
  30.     struct list_head *fd;  
  31.     list_for_each(fd, &(g_pad->food)){      
  32.         struct body *cp = list_entry(fd, struct body, list);  
  33.         if (head->pos.x == cp->pos.x && head->pos.y == cp->pos.y) {  
  34.             list_del (cp);  
  35.             list_add (cp, &(g_pad->snake_body));      
  36.             (*g_pad->eat)(g_pad);  
  37.             (*g_pad->random_gen_food)(g_pad);  
  38.         }  
  39.     }  
  40. }  

最后需要一个处理按键的函数
[cpp] view plaincopy
  1. void key_handler()  
  2. {  
  3.     //get key  
  4.     (*g_pad->key_handler)(g_pad, key_pos);  
  5. }  

最终我们发现还缺点什么,那就是这个实现测试起来很麻烦,还需要单独抽取linux内核的list.h。

3.Java实现

由于用C语言编写的代码在测试的时候需要图形库,而我最烦的就是GUI编程了,关键是因为自己太懒了,而GUI环境部署又需要一定的工作量,因此难耐了。在别人看来,部署一个qt开发环境是小菜一碟,在我看来比登天还难...
     后来想在Windows平台搞,可以编译起来出了那么多的问题,不是缺这就是少那...最终,还是java是避开GUI环境的好办法。java的好处是环境部署最简单,因此还是用java来实现吧,和2004年实现的那个一样。虽然java中没有list_head,但是由于其本身就是面向对象的,且容器支持泛型,因此使用LinkedList也是不错的。
     由于贪吃蛇的中心逻辑不在UI,因此也就只是简单的实现了一个UI,代码如下:
[java] view plaincopy
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import java.util.*;  
  5.   
  6. class posision {  
  7.     int x;  
  8.     int y;  
  9.     public posision(){}  
  10.     public posision(int x, int y) {  
  11.         this.x = x;  
  12.         this.y = y;  
  13.     }  
  14. }  
  15. /** 
  16.  * 静态数据,保存所有的位置和方向信息 
  17.  * 这原本是可以定义到各个使用类内部以内部类实现的 
  18.  * @author marywangran 
  19.  */  
  20. class static_pos {  
  21.     public static posision LEFT = new posision(-10);  
  22.     public static posision RIGHT = new posision(10);  
  23.     public static posision UP = new posision(0, -1);  
  24.     public static posision DOWN = new posision(01);  
  25.     public static posision HALT = new posision(00);  
  26.     public static posision pos[][];  
  27.     public static void setpos (int width, int height) {  
  28.         int i = 0, j = 0;  
  29.         int num = width*height;  
  30.         pos = new posision[width][height];  
  31.         for (i = 0; i < width; i++) {  
  32.             for (j = 0; j < height; j++) {  
  33.             pos[i][j] = new posision(i, j);  
  34.             }  
  35.         }  
  36.     }  
  37.     public static posision go_on(posision curr, posision direct) {  
  38.         return pos[curr.x + direct.x][curr.y + direct.y];  
  39.     }  
  40. }  
  41. /** 
  42.  * 游戏中所有的元素都是链表,定义为body类 
  43.  * @author marywangran 
  44.  */  
  45. class body extends Object{  
  46.     posision pos;  
  47.     posision direct;  
  48.     extension ext;  
  49.     public body(){}  
  50.     public body (posision pos, posision direct){  
  51.         this.pos = pos;  
  52.         this.direct = direct;  
  53.     }  
  54.     boolean go_on (boolean change, posision pos) {  
  55.         if (change)  
  56.             this.pos = static_pos.go_on(this.pos, this.direct);  
  57.         else {  
  58.             return (static_pos.go_on(this.pos, this.direct).x == pos.x)&&  
  59.             (static_pos.go_on(this.pos, this.direct).y == pos.y);  
  60.         }  
  61.         return true;  
  62.     }  
  63.     void set_extension(extension ext) {  
  64.         this.ext = ext;  
  65.     }  
  66.     extension get_extension() {  
  67.         return this.ext;  
  68.     }  
  69.     //void *private;  
  70. }  
  71. /** 
  72.  * 对应于C代码的void *private;每一个节点的可能有的所有属性 
  73.  * @author marywangran 
  74.  */  
  75. interface extension {  
  76.     public void set_timeout(int timeout);  
  77.     public long get_timeout();  
  78.     public long star_time();  
  79.     public void set_score(int score);  
  80.     public int get_score();  
  81. }  
  82. /** 
  83.  * 食物的属性 
  84.  * @author marywangran 
  85.  * 
  86.  */  
  87. class food_extension implements extension {  
  88.     long time_out;  
  89.     int score;  
  90.     long start;  
  91.     public void set_timeout(int timeout) {  
  92.         this.time_out = timeout*1000;  
  93.         this.start = System.currentTimeMillis();  
  94.     }  
  95.     public long get_timeout() {  
  96.         return this.time_out;  
  97.     }  
  98.     public long star_time(){  
  99.         return this.start;  
  100.     }  
  101.     public void set_score(int score) {  
  102.         this.score = score;  
  103.     }  
  104.     public int get_score() {  
  105.         return this.score;  
  106.     }  
  107. }  
  108. /** 
  109.  * 完全由链表实现的贪吃蛇游戏类,其本质就是处理body链表 
  110.  * @author marywangran 
  111.  */  
  112. class Snake_Game_Pad {  
  113.     LinkedList<body> snake_body;  
  114.     LinkedList<body> change_body;  
  115.     LinkedList<body> kill_body;  
  116.     LinkedList<body> food_body;  
  117.     LinkedList<body> free_body;  
  118.     LinkedList<posision> replace_body;  
  119.     int curr_status;  
  120.     int curr_level;  
  121.     int curr_score;  
  122.     int width, height;  
  123.     public Snake_Game_Pad () {  
  124.         snake_body = new LinkedList();  
  125.         change_body = new LinkedList();  
  126.         kill_body = new LinkedList();  
  127.         food_body = new LinkedList();  
  128.         free_body = new LinkedList();  
  129.         replace_body = new LinkedList();  
  130.     }  
  131.     /** 
  132.      * 贪吃蛇游戏初始化 
  133.      * @param width            横向元素数量 
  134.      * @param height        纵向元素数量 
  135.      * @param init_length    初始蛇长度 
  136.      */  
  137.     public void init(int width, int height, int init_length) {  
  138.         this.width = width;  
  139.         this.height = height;  
  140.         int i = 0, j = 0;  
  141.         for (i = 0; i < this.width; i++){  
  142.             for (j = 0; j < this.height; j ++) {  
  143.                   
  144.                 free_body.add(new body(static_pos.pos[j][i], static_pos.HALT));  
  145.             }  
  146.         }  
  147.         for (i = 0; i < this.width; i++){  
  148.             body kb1 = free_body.get(0);  
  149.             body kb2 = free_body.get(free_body.size()-1);  
  150.             free_body.remove(kb1);  
  151.             kill_body.add(kb1);  
  152.             free_body.remove(kb2);  
  153.             kill_body.add(kb2);  
  154.         }  
  155.         for (i = 0; i < this.height-2; i ++) {  
  156.             body kb1 = free_body.get(i*(this.width-2));  
  157.             body kb2 = free_body.get(i*(this.width-2)+this.width-1);  
  158.             free_body.remove(kb1);  
  159.             kill_body.add(kb1);  
  160.             free_body.remove(kb2);  
  161.             kill_body.add(kb2);  
  162.         }  
  163.         for (i = 0; i < init_length-1; i ++) {  
  164.             body sb = free_body.get(6);  
  165.             sb.direct = static_pos.RIGHT;  
  166.             free_body.remove(sb);  
  167.             snake_body.add(0,sb);  
  168.             kill_body.add(0,sb);  
  169.         }  
  170.         body sb = free_body.get(6);  
  171.         sb.direct = static_pos.RIGHT;  
  172.         free_body.remove(sb);  
  173.         snake_body.add(0,sb);  
  174.         random_gen_food ();      
  175.     }  
  176.     public void reset() {  
  177.         snake_body.clear();  
  178.         change_body.clear();  
  179.         kill_body.clear();  
  180.         food_body.clear();  
  181.         free_body.clear();  
  182.         replace_body.clear();  
  183.     }  
  184.     int get_curr_score() {  
  185.         return this.curr_score;  
  186.     }  
  187.       
  188.     int get_curr_level(){  
  189.         return this.curr_level;  
  190.     }  
  191.     /** 
  192.      * 贪吃蛇主处理函数 
  193.      * @return 需要重绘的free_body元素的链表 
  194.      */  
  195.     LinkedList<posision> game_handler() {  
  196.         body head = snake_body.get(0);  
  197.         body tail = snake_body.get(snake_body.size()-1);  
  198.         posision tail_pos = tail.pos;  
  199.         body replace_free;  
  200.         boolean need_gen = false;  
  201.         Iterator<body> snake_iter = snake_body.iterator();  
  202.         replace_body.clear();  
  203.         Iterator<body> food_iter = food_body.iterator();      
  204.         while (food_iter.hasNext()){  
  205.             body fb = food_iter.next();  
  206.             extension fext = fb.get_extension();  
  207.             //食物超时时间到了,食物消失。  
  208.             if ((System.currentTimeMillis()-fext.star_time()) > fext.get_timeout()) {  
  209.                 food_iter.remove();  
  210.                 free_body.add(fb);  
  211.                 replace_body.add(fb.pos);  
  212.                 need_gen = true;  
  213.                 continue;  
  214.             }  
  215.             //贪吃蛇吃食物  
  216.             if (head.go_on(false, fb.pos)) {  
  217.                 food_iter.remove();  
  218.                 fb.direct = head.direct;  
  219.                 snake_body.add(0, fb);      
  220.                 kill_body.add(head);  
  221.                 eat_food();  
  222.                 head = snake_body.get(0);  
  223.                 random_gen_food ();  
  224.             }  
  225.         }  
  226.         if (need_gen) { //食物由于超时消失了,重新生成一个  
  227.             this.random_gen_food();  
  228.         }  
  229.         while (snake_iter.hasNext()) {  
  230.             body sb = snake_iter.next();  
  231.             Iterator<body> change_iter = change_body.iterator();  
  232.             while (change_iter.hasNext()) {  
  233.                 body cb = change_iter.next();  
  234.                 if ((sb.pos.x == cb.pos.x) && (sb.pos.y == cb.pos.y)) {  
  235.                     sb.direct = cb.direct;  
  236.                     if (!snake_iter.hasNext()) {  
  237.                         change_body.remove(cb);  
  238.                     }  
  239.                     break;  
  240.                 }  
  241.             }  
  242.             sb.go_on(truenull);  
  243.         }  
  244.         Iterator<body> kill_iter = kill_body.iterator();  
  245.         while (kill_iter.hasNext()) {  
  246.             body kb = kill_iter.next();  
  247.             if ((kb.pos.x == head.pos.x) && (kb.pos.y == head.pos.y)) {  
  248.                 this.curr_status = 1;  
  249.                 /** 
  250.                  * 死了的逻辑是什么,这是一个问题。 
  251.                  */  
  252.                 //System.exit(1);  
  253.             }  
  254.         }  
  255.         replace_free = free_body.get(head.pos.y*this.width+head.pos.x);  
  256.         replace_free.pos = tail_pos;  
  257.         replace_body.add(tail_pos);  
  258.         return replace_body;  
  259.     }  
  260.     void key_handler (posision direct) {  
  261.         body pb = new body(snake_body.get(0).pos, direct);  
  262.         snake_body.get(0).direct = pb.direct;  
  263.         change_body.add(pb);  
  264.     }  
  265.     /** 
  266.      * 该方法没有完全实现,现有的实现很丑陋。TODO 
  267.      * 1.将分值设定参数化,策略化,和当前级别联动 
  268.      * 2.将超时时间和当前级别以及最近距离联动 
  269.      */  
  270.     void random_gen_food () {  
  271.         body fb;  
  272.         Random random =new java.util.Random();  
  273.         Random big_random =new java.util.Random();  
  274.         int big_score = 0;  
  275.         int size = free_body.size();  
  276.         fb = free_body.get(random.nextInt(size));  
  277.         free_body.remove(fb);  
  278.         food_extension fext = new food_extension();  
  279.         fext.set_timeout(2000);  
  280.         fext.set_score(10);  
  281.         fb.set_extension(fext);  
  282.         food_body.add(fb);  
  283.     }  
  284.     /** 
  285.      * 该方法TODO 
  286.      * 实现设置障碍物,贪吃蛇除了不能触动自身以及边墙之外,也不能触动这些障碍 
  287.      * 实现很简单,只需要将障碍添加到kill_body链表中即可 
  288.      */  
  289.     void random_gen_fence () {  
  290.     }  
  291.     void eat_food () {  
  292.      //实现加分等操作  
  293.     }  
  294. }  
  295. /** 
  296.  * 简单的一个UI,测试用的,十分不完善,什么功能都没有 
  297.  * @author marywangran 
  298.  */  
  299. class GameThread extends JFrame implements KeyListener ,Runnable {  
  300.     Snake_Game_Pad game;  
  301.     posision pos;  
  302.     JPanel mainp;  
  303.     public GameThread() {  
  304.         static_pos.setpos(200,200);  
  305.         game = new Snake_Game_Pad();  
  306.         game.init(20020020);      
  307.         mainp=new JPanel();  
  308.         mainp.addKeyListener(this);  
  309.         mainp.setFocusable(true);  
  310.         getContentPane().setLayout(null);  
  311.         getContentPane().add(mainp);  
  312.         mainp.setBounds(0,0,650,650);  
  313.         setSize(650,650);  
  314.         setResizable(false);  
  315.         setVisible(true);  
  316.         new Thread(this).start();  
  317.     }  
  318.     public void prepaint(){  
  319.         Graphics g = mainp.getGraphics();  
  320.         this.setBackground(Color.white);  
  321.         int i ,j;  
  322.         for(i=0;i<600;i+=3)  
  323.             for(j=0;j<600;j+=3) {  
  324.                 g.setColor(Color.green);  
  325.                 g.fillRect(i,j,3,3);  
  326.             }  
  327.         g.setColor(Color.blue);  
  328.     }  
  329.     public void paint(Graphics g) {  
  330.         g = mainp.getGraphics();  
  331.         Iterator<body> snake_iter = game.snake_body.iterator();  
  332.         while (snake_iter.hasNext()) {  
  333.             body sb = snake_iter.next();  
  334.             g.setColor(Color.blue);  
  335.             g.fillRect(sb.pos.x*3, sb.pos.y*3,3,3);      
  336.         }  
  337.         Iterator<body> food_iter = game.food_body.iterator();      
  338.         while (food_iter.hasNext()) {  
  339.             body fb = food_iter.next();  
  340.             g.setColor(Color.red);  
  341.             g.fillRect(fb.pos.x*3, fb.pos.y*3,3,3);      
  342.         }  
  343.     }  
  344.     public void paint_replace(LinkedList<posision> pos_body){  
  345.         Graphics g = mainp.getGraphics();  
  346.         g.setColor(Color.green);  
  347.         Iterator<posision> pos_iter = pos_body.iterator();      
  348.         while (pos_iter.hasNext()){  
  349.             posision pos = pos_iter.next();  
  350.             g.fillRect(pos.x*3, pos.y*3,3,3);      
  351.         }//打印引起的悲哀  
  352.     }  
  353.     public void run () {  
  354.         LinkedList<posision> clist = null;  
  355.         prepaint();  
  356.         while (true) {  
  357.             try {  
  358.                 clist = game.game_handler();  
  359.                 repaint();  
  360.                 paint_replace(clist);  
  361.   
  362.                 Thread.sleep(100);  
  363.             }catch (Exception e) {  
  364.                 e.printStackTrace();//System.exit(1);  
  365.             }  
  366.         }  
  367.     }  
  368.     public void keyPressed(KeyEvent ke) {  
  369.     }  
  370.     public void keyReleased(KeyEvent ke) {  
  371.     }  
  372.     public void keyTyped(KeyEvent key) {  
  373.         String kv = ""+key.getKeyChar();  
  374.         if (kv.equals("w"))  
  375.             game.key_handler(static_pos.UP);  
  376.         else if (kv.equals("s"))  
  377.             game.key_handler(static_pos.DOWN);  
  378.         else if(kv.equals("a"))  
  379.             game.key_handler(static_pos.LEFT);  
  380.         else if(kv.equals("d"))  
  381.             game.key_handler(static_pos.RIGHT);  
  382.     }  
  383. }  
  384. public class Main {  
  385.     public static void main(String[] args) {  
  386.         new GameThread();      
  387.     }  
  388. }  

在Eclipse中建立一个java project,然后把上述代码贴进Main.java中去,运行,将会出现以下的游戏画面:
0 0
原创粉丝点击