我开通博客啦

2015新的一年,我找到了工作,正式的成为了职场人,我会努力的大笑。今天2015年1月6号,我也有自己的博客啦哈哈


package com.hitworth.month1.day06;

public class Point {
 private int x;
 private int y;
 public Point(){
  System.out.print("- ");
 }
 public Point(int x,int y){
  this.x = x;
  this.y = y;
 }
 public int getX() {
  return x;
 }
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + x;
  result = prime * result + y;
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Point other = (Point) obj;
  if (x != other.x)
   return false;
  if (y != other.y)
   return false;
  return true;
 }
 public void setX(int x) {
  this.x = x;
 }
 public int getY() {
  return y;
 }
 public void setY(int y) {
  this.y = y;
 }
 @Override
 public String toString() {
  return "Point [x=" + x + ", y=" + y + "]";
 }
}





package com.hitworth.month1.day06;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import org.junit.Test;

/**
 * 贪吃蛇小游戏---控制台版本
 * version 1.0
 *
 * @author yeqiang
 * Create_Time:2015.01.06
 */
public class SnackGame {
 String[][] board = new String[12][12];
 static List<Point> listp = new ArrayList<>();
 static Random r = new Random();
     static int x;
     static int y;
     static Point pfood = new Point() ;
 
     
    //主函数 ----游戏入口
 public static void main(String[] args){
  SnackGame tm = new SnackGame();
  tm.init(pfood);
  tm.start(listp);
 }
 
 
 public SnackGame(){
  //设计一个蛇的身子
  Point p1 = new Point(3,4);
  Point p2 = new Point(3,5);
  Point p3 = new Point(3,6);
  listp.add(p1);
  listp.add(p2);
  listp.add(p3);
 
  //随机一个食物
   x = r.nextInt(board.length);
   y = r.nextInt(board[0].length);
  pfood.setX(x);
  pfood.setY(y);
 }
 
 //重新生成食物
 public Point madefood(List<Point> list){
  //判断蛇的身子是否包含到食物
   while(list.contains(pfood)){
     x = r.nextInt(board.length);
     y = r.nextInt(board[0].length);
     pfood.setX(x);
     pfood.setY(y);
   }
    return pfood;
 }
 
 //布置棋盘
 public void init(Point pfood){
    for(int i=0;i<board.length;i++){
      for(int j=0;j<board[0].length;j++){
         board[i][j] = "-";
         for(Point p : listp){
            if(p.getX() == i && p.getY()==j){
               if(i == listp.get(listp.size()-1).getX() && j == listp.get(listp.size()-1).getY() ){
                    board[i][j] = "*";
               }else {
                        board[i][j] = "@";
                   }
             }    
            }
           }
       }
       if(listp.contains(pfood)){
       board[pfood.getX()][pfood.getY()] = "%";
    }else if(!listp.contains(pfood)){
    board[pfood.getX()][pfood.getY()] = "#";
   }
  initframe();
 }
 
 //打印面板
 public void initframe(){
  for(int i=0;i<board.length;i++){
    for(int j=0;j<board[0].length;j++){
    System.out.print(board[i][j]+" ");
    }
    System.out.println();
   }
 }
 
 //开始
 public void start(List<Point> list){
  System.out.println("开始:");
  Scanner sc = new Scanner(System.in);
  while(true){
   System.out.println("请输入方向:");
   String str = sc.next();
   Point food= tomove(str,list);
   init(food);
  }
 }
 
 //根据运动方向进行走动
 public Point tomove(String m,List<Point> list){
  Point p = new Point();
  if(m.equals("s")){
   p = move1(list);
  }
  else if(m.equals("d")){
   p = move2(list);
  }else if(m.equals("a")){
   p = move3(list);
  }else if(m.equals("w")){
   p = move4(list);
  }
  return p;
 }

 //向下
 public Point move1(List<Point> list){
  int step = 1;
  System.out.println("move1:"+pfood);
  if(list.get(list.size()-1).getX()>=0 && list.get(list.size()-1).getX()<board.length-1){
   list.add(new Point(list.get(list.size()-1).getX()+step,list.get(list.size()-1).getY()));
    if(!list.contains(pfood)){
     list.remove(list.get(0));
    }else if(list.contains(pfood)){
    pfood = madefood(list);
    }
  }
  return pfood;
 }
 
 //向右
 public Point move2(List<Point> list){
  int step = 1;
   if(list.get(list.size()-1).getY()>=0 && list.get(list.size()-1).getY()<board.length-1){
   if(list.contains(pfood)){
    pfood = madefood(list);
    list.add(new Point(list.get(list.size()-1).getX(),list.get(list.size()-1).getY()+step));
   }else{
    list.add(new Point(list.get(list.size()-1).getX(),list.get(list.size()-1).getY()+step));
    list.remove(list.get(0));
   }
   }
   return pfood;
 }
 
 
 //向左
 public Point move3(List<Point> list){
  int step = -1;
   if(list.get(list.size()-1).getY()>=1 && list.get(list.size()-1).getY()<= board.length-1){
   if(list.contains(pfood)){
    pfood = madefood(list);
     list.add(new Point(list.get(list.size()-1).getX(),list.get(list.size()-1).getY()+step));
   }else{
    list.remove(list.get(0));
    list.add(new Point(list.get(list.size()-1).getX(),list.get(list.size()-1).getY()+step));
   }
   }
   return pfood;
 }


 //向上
 public Point move4(List<Point> list){
  int step = -1;
   if(list.get(list.size()-1).getX()>=1 && list.get(list.size()-1).getX()<=board.length-1){
    boolean flag = listp.contains(new Point(pfood.getX(),pfood.getY()));
   if(flag == true){
    pfood = madefood(list);
    list.add(new Point(list.get(list.size()-1).getX()+step,list.get(list.size()-1).getY()));
   }else if(flag == false){
    list.remove(list.get(0));
    list.add(new Point(list.get(list.size()-1).getX()+step,list.get(list.size()-1).getY()));
   }
   }
   return pfood;
 }

 
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、5资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值