您的位置:首页 > 其它

我开通博客啦

2015-01-06 16:07 155 查看
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: