现在的位置: 首页 > 综合 > 正文

推箱子小游戏–控制台

2014年01月06日 ⁄ 综合 ⁄ 共 6765字 ⁄ 字号 评论关闭
public class BoxGame {
	public static boolean isWin = false;
	private int row, col;// 有多少行,多少列
	private int box_count;// 箱子个数
	private int[][] gameCore;// 游戏核心 二维地图
	static private final Random RANDOM = new Random();
	public static final Scanner scanner = new Scanner(System.in);

	public BoxGame() {
		init();
	}
	private void init(){
		System.out.println("请输入地图行数,以回车键结束.");
		setRow(scanner.nextInt()+2);
		System.out.println("请输入地图列数,以回车键结束.");
		setCol(scanner.nextInt()+2);
		System.out.println("请输入需完成目标数,以回车键结束.");
		setBox_count(scanner.nextInt());
		gameCore = new int[this.row][this.col];
		createBoxGame();
		player();
	}
	public BoxGame(int row, int col, int box_count) {
		if (row < 2 || col < 2 || box_count < 1)
			throw new RuntimeException("参数异常,导致地图创建失败!");
		this.row = row + 2;// 多加二行,用于制造墙壁
		this.col = col + 2;// 多加二列,用于制造墙壁
		this.box_count = box_count;
		gameCore = new int[this.row][this.col];
	}

	public void createBoxGame() {
		int type = 0;
		for (int i = 0; i < this.row; i++) {
			for (int j = 0; j < this.col; j++) {
				type = RANDOM.nextInt(10);
				// 初始化地图
				if (type < 1 || i == 0 || i == this.row - 1 || j == 0
						|| j == this.col - 1) {
					gameCore[i][j] = 1;// 墙壁
				} else {
					gameCore[i][j] = 0;// 空地
				}
			}
		}
		//人
		gameCore[1][1] = 5;
		// 往地图上放星星
		makeStar();
		// 往地图上放箱子
		makeBox();

	}

	// 往地图上放星星
	private void makeStar() {
		int row = 0;
		int col = 0;
		for (int i = 0; i < box_count; i++) {
			row = RANDOM.nextInt(this.row);
			col = RANDOM.nextInt(this.col);
			// 判断构建的星星在地图上是否合法
			if (gameCore[row][col] == 0) {
				// 合法
				gameCore[row][col] = 2;// 记录星星的位置
			} else {
				i--;//添加失败,次数还原
			}
		}
	}

	// 往地图上放箱子
	private void makeBox() {
		int row = -1;
		int col = -1;
		int type = -1;
		for (int i = 0; i < box_count; i++) {
			row = RANDOM.nextInt(this.row);
			col = RANDOM.nextInt(this.col);
			// 检查箱子的合法性及可行性
			type = checkBox(row, col);
			if(type != -1){
				gameCore[row][col]=type;
			}else{
				i--;
			}
		}
	}

	private int checkBox(int row, int col) {
		if (gameCore[row][col] == 1 || gameCore[row][col] == 3 || gameCore[row][col] == 4 ||gameCore[row][col] == 5)
			return -1;
		if(gameCore[row][col] == 2)
			return 4;
			
		// 判断是否是死角
		if ((gameCore[row + 1][col] == 1 || gameCore[row - 1][col] == 1)
				&& (gameCore[row][col + 1] == 1 || gameCore[row][col - 1] == 1))
			return -1;
		
		return 3;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getCol() {
		return col;
	}

	public void setCol(int col) {
		this.col = col;
	}

	public int getBox_count() {
		return box_count;
	}

	public void setBox_count(int box_count) {
		this.box_count = box_count;
	}


	public int[][] getGameCore() {
		return gameCore;
	}

	public void setGameCore(int[][] gameCore) {
		this.gameCore = gameCore;
	}

	public void player(){
		Person p = new Person(getGameCore(), 1, 1,box_count);
		p.printMap();
		int dir = -1;
		System.out.println("游戏说明:");
		System.out.println("	〓:墙壁	£:is you	◎:箱子	☆:目标	★:成功");
		System.out.println("	上:8		下:5 or 2	左:4		右:6");
		System.out.println("	退出:7		下一关:9");
		System.out.println("请输入你接下来的操作:");
		do {
			if(isWin){
				isWin=false;
				init();
				break;
			}
			dir = scanner.nextInt();
			if(dir == 7)
				break;
			else if(dir == 9){
				init();
				break;
			}
			p.move(dir);
		} while (true);
	}
	
	public static void main(String[] args) {
		new BoxGame();
	}
	
}

class Person {
	private int row, col;// 小人所在的位置,注意这里的row、col和BoxGame中row、col的区别,完全不是一个概念
	private int[][] gameCore;// 游戏核心 二维地图
	private int box_count;//目标数
	private boolean flag = false;// 标记人物是否与目标重合
	
	public Person(int[][] gameCore, int row, int col,int box_count) {
		this.gameCore = gameCore;
		this.row = row;
		this.col = col;
		this.gameCore[row][col] = 5;
		this.box_count = box_count;
	}

	public void move(int dir) {
		switch (dir) {
		case 8:
			up();
			break;
		case 2:
		case 5:
			down();
			break;
		case 4:
			left();
			break;
		case 6:
			right();
			break;
		}
		printMap();
	}

	private void up() {
		// 往上走
		if (row == 1 || gameCore[row - 1][col] == 1)
			return;
		if(startMove(-1, 0))
			this.row--;
	}

	private void down() {
		// 往下走
		if (row == gameCore.length)
			return;
		if(startMove(1, 0))
			this.row++;
	}

	// 0:空地 1:墙壁 2: 目标 3:箱子 4:完成 5:小人
	private boolean startMove(int row_value, int col_value) {
		flag = gameCore[row][col] == 6;
		if (gameCore[row + row_value][col + col_value] == 0) {
			// 空地
			if (flag) {
				gameCore[row][col] = 2;
				gameCore[row + row_value][col + col_value] = 5;
			} else {
				gameCore[row][col] = 0;
				gameCore[row + row_value][col + col_value] = 5;
			}
		} else if (gameCore[row + row_value][col + col_value] == 2) {
			if (flag) {
				gameCore[row][col] = 2;
				gameCore[row + row_value][col + col_value] = 6;
			} else {
				gameCore[row][col] = 0;
				gameCore[row + row_value][col + col_value] = 6;
			}
		} else if (gameCore[row + row_value][col + col_value] == 3) {
			// 推动箱子
			// 判断箱子是否可以移动
			if (gameCore[row + row_value + row_value][col + col_value
					+ col_value] == 1
					|| gameCore[row + row_value + row_value][col + col_value
							+ col_value] == 3
					|| gameCore[row + row_value + row_value][col + col_value
							+ col_value] == 4) {
				return false;
			}
			if (flag) {
				// 空地
				if (gameCore[row + row_value + row_value][col + col_value
						+ col_value] == 0) {
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 3;
					gameCore[row + row_value][col + col_value] = 5;
					gameCore[row][col] = 2;
				} else if (gameCore[row + row_value + row_value][col
						+ col_value + col_value] == 2) {
					// 目标
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 4;
					gameCore[row + row_value][col + col_value] = 5;
					gameCore[row][col] = 2;
				}
			} else {
				// 空地
				if (gameCore[row + row_value + row_value][col + col_value
						+ col_value] == 0) {
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 3;
					gameCore[row + row_value][col + col_value] = 5;
					gameCore[row][col] = 0;
				} else if (gameCore[row + row_value + row_value][col
						+ col_value + col_value] == 2) {
					// 目标
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 4;
					gameCore[row + row_value][col + col_value] = 5;
					gameCore[row][col] = 0;
				}
			}
		} else if (gameCore[row + row_value][col + col_value] == 4) {
			// 推已经完成的.
			// 判断箱子是否可以移动
			if (gameCore[row + row_value + row_value][col + col_value
					+ col_value] == 1
					|| gameCore[row + row_value + row_value][col + col_value
							+ col_value] == 3
					|| gameCore[row + row_value + row_value][col + col_value
							+ col_value] == 4) {
				return false;
			}
			if (flag) {
				// 空地
				if (gameCore[row + row_value + row_value][col + col_value
						+ col_value] == 0) {
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 3;
					gameCore[row + row_value][col + col_value] = 6;
					gameCore[row][col] = 2;
				} else if (gameCore[row + row_value + row_value][col
						+ col_value + col_value] == 2) {
					// 目标
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 4;
					gameCore[row + row_value][col + col_value] = 6;
					gameCore[row][col] = 2;
				}
			} else {
				// 空地
				if (gameCore[row + row_value + row_value][col + col_value
						+ col_value] == 0) {
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 3;
					gameCore[row + row_value][col + col_value] = 6;
					gameCore[row][col] = 0;
				} else if (gameCore[row + row_value + row_value][col
						+ col_value + col_value] == 2) {
					// 目标
					gameCore[row + row_value + row_value][col + col_value
							+ col_value] = 4;
					gameCore[row + row_value][col + col_value] = 6;
					gameCore[row][col] = 0;
				}
			}

		}
		return true;
	}

	private void left() {
		// 往左
		if (col == 1)
			return;
		if(startMove(0, -1))
			this.col--;
	}

	private void right() {
		// 往右
		if (col == gameCore[0].length - 2)
			return;
		if(startMove(0, 1))
			this.col++;
	}

	private void isWin(){
		int count=0;
		for (int i = 0; i < gameCore.length; i++) {
			for (int j = 0; j < gameCore[0].length; j++) {
				if(gameCore[i][j] == 4)
					count++;
			}
		}
		if(count == box_count){
			System.out.println("游戏胜利.");
			BoxGame.isWin = true;
		}
	}
	
	// 0:空地 1:墙壁 2: 目标 3:箱子 4:完成 5:小人 6 人物与目标重合
	public void printMap() {
		System.out.println("\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n");
		for (int i = 0; i < gameCore.length; i++) {
			for (int j = 0; j < gameCore[0].length; j++) {
				switch (gameCore[i][j]) {
				case 0:
					System.out.print(" ");
					break;
				case 1:
					System.out.print("〓");
					break;
				case 2:
					System.out.print("☆");
					break;
				case 3:
					System.out.print("◎");
					break;
				case 4:
					System.out.print("★");
					break;
				case 5:
					System.out.print("£");
					break;
				case 6:
					System.out.print("※");
					break;
				}
			}
			System.out.println();
		}
		isWin();
	}

}

推箱子小游戏--控制台

抱歉!评论已关闭.