java涉嫌蛇和梯子的游戏

技术分享


/**
 * @Author jiangfq
 * 
 */
package com.test;


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


/**
 * @author jiangfq
 *
 */
public class Game {
private static int[][] me = new int[1][1];
private static List<Ladder> ladders = new ArrayList<Ladder>(4);
private static List<Snake> snakes = new ArrayList<Snake>(4);
private static int[][] datas = new int[5][5];

/**
* @Author jiangfq
*
*/
public static void main(String[] args) {
init();
startGame();

}


private static void init() {
me[0][0] = 1;
Random random = new Random();

int count = 1;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
datas[i][j] = count++; 
}
}

for(int i = 0; i < 4; i++) {
int[][] ladderHeader = new int[1][1];
int[][] ladderTail = new int[1][1];
int hx = random.nextInt(5);
while(hx == 0) {
hx = random.nextInt(5);
}
int hy = random.nextInt(5);

int tx = random.nextInt(5);
while(tx >= hx) {
tx = random.nextInt(5);
}
int ty = random.nextInt(5);

ladderHeader[0][0] = datas[hx][hy];
ladderTail[0][0] = datas[tx][ty];

Ladder ladder = new Ladder(ladderHeader,ladderTail);
ladders.add(ladder);
}

for(int i = 0; i < 4; i++) {
int[][] snakeHeader = new int[1][1];
int[][] snakeTail = new int[1][1];
int hx = random.nextInt(5);
while(hx == 0) {
hx = random.nextInt(5);
}
int hy = random.nextInt(5);

int tx = random.nextInt(5);
while(tx >= hx) {
tx = random.nextInt(5);
}
int ty = random.nextInt(5);
snakeHeader[0][0] = datas[hx][hy];
snakeTail[0][0] = datas[tx][ty];
Snake snake = new Snake(snakeHeader,snakeTail);
snakes.add(snake);
}

}

private static void startGame() {
Random random = new Random();
boolean flag = false;

while(!flag) {
int x = random.nextInt(7);
while(x == 0) {
x = random.nextInt(7);
}
System.out.println("你可以跳:" + x + "步! 现在在" + me[0][0] + "  即将去" + (x + me[0][0]));
int t = x + me[0][0];
for(int i = 0; i < datas.length; i++) {
boolean isRun = false;
for(int j = 0; j < datas[i].length; j++) {
if(t >= 25) {
flag = true;
break;
}
if(datas[i][j] == t) {
for(Snake snake : snakes) {
int[][] headers = snake.getHeader();
if(headers[0][0] == t) {
int[][] tails = snake.getTail();
System.out.println("走" + x + "步太背了,遇到蛇头了:" + t + " 我要滑到蛇尾:" +tails[0][0]);
me[0][0] = tails[0][0];
isRun = true;
break;
}
}
for(Ladder ladder : ladders) {
int[][] tails = ladder.getTail();
if(tails[0][0] == t) {
int[][] headers = ladder.getHeader();
System.out.println("走" +x +"步太好了,遇到梯子了:" + t + " 我要爬上去了:" +headers[0][0]);
me[0][0] = headers[0][0];
isRun = true;
break;
}
}
if(!isRun) {
me[0][0] = t;
break;
}
}
}
if(flag || isRun) {
break;
}
}
}
System.out.println("我成功了!");
}
}


class Ladder {
private int[][] header;
private int[][] tail;
public Ladder(int[][] header,int[][] tail) {
this.header = header;
this.tail = tail;
}
public int[][] getHeader() {
return header;
}
public void setHeader(int[][] header) {
this.header = header;
}
public int[][] getTail() {
return tail;
}
public void setTail(int[][] tail) {
this.tail = tail;
}



}


class Snake {
private int[][] header;
private int[][] tail;
public Snake(int[][] header,int[][] tail) {
this.header = header;
this.tail = tail;
}
public int[][] getHeader() {
return header;
}
public void setHeader(int[][] header) {
this.header = header;
}
public int[][] getTail() {
return tail;
}
public void setTail(int[][] tail) {
this.tail = tail;
}
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。