⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.java

📁 此段源码是我自己做的一个骑士飞行棋小游戏
💻 JAVA
字号:
package s1java.xmal1;
import java.util.*;
public class Game {
	Map map;
	int playerPos1;
	int playerPos2;
	String[] goAndStop = new String[2];
	String[] playerName = new String[2];

	/**
	 * 初始化游戏
	 */
	public void init() {
		map = new Map();
		map.createMap();
		playerPos1 = 0;
		playerPos2 = 1;
		goAndStop[0] = "on"; // 设置玩家1下一次走或停
		goAndStop[1] = "on"; // 设置玩家2下一次走或停
	}

	/**
	 * 设置对战角色
	 * @param no     玩家次序 1:玩家1 2:玩家2
	 * @param role   角色代号
	 */
	public void setRole(int no, int role) {
		switch (role) {
		case 1:
			playerName[no-1] = "戴高乐";
			break;
		case 2:
			playerName[no-1] = "艾森豪威尔";
			break;
		case 3:
			playerName[no-1] = "麦克阿瑟";
			break;
		case 4:
			playerName[no-1] = "巴顿";
			break;
		default:
			break;
		}
	}

	/**
	 * 开始游戏
	 */
	public void start() {
		// 初始化
		init();
		System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※");
		System.out.println("//                                                //");
		System.out.println("//                                                //");
		System.out.println("//             骑    士    飞    行    棋               //");
		System.out.println("//                                                //");
		System.out.println("//                                                //");
		System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n\n\n");
		System.out.println("\n~~~~~~~~~~~~~~~~~~~两  人  对  战~~~~~~~~~~~~~~~~~~~");
		System.out.println("\n请选择角色: 1. 戴高乐 2. 艾森豪威尔 3. 麦克阿瑟 4. 巴顿");
		Scanner input = new Scanner(System.in);
		int role1;
		do{
			System.out.print("请玩家1选择角色: ");
			role1 = input.nextInt(); // 双方选择角色代号
			if(role1!=1&&role1!=2&&role1!=3&&role1!=4){
				System.out.println("对不起,系统不提供此角色,请重新输入!");
			}
			}while(role1!=1&&role1!=2&&role1!=3&&role1!=4);
		int role2;
		do {
			do{
			System.out.print("请玩家2选择角色: ");
			role2 = input.nextInt(); // 双方选择角色代号
			if(role2!=1&&role2!=2&&role2!=3&&role2!=4){
				System.out.println("对不起,系统不提供此角色,请重新输入!");
			}
			}while(role2!=1&&role2!=2&&role2!=3&&role2!=4);
			if(role2==role1){
				System.out.println("不可以选择相同的角色,请重新输入!");
			}
		} while (role2 == role1); 
		setRole(1, role1); // 设置玩家1代表的角色
		setRole(2, role2); // 设置玩家2代表的角色
		play();
	}

	/**
	 * 两人对战玩法
	 */
	public void play() {
		System.out.println("\n\n\n\n");
		System.out.print("\n\n*************************************\n");
		System.out.print("               Game   Start              \n");
		System.out.print("*************************************\n\n");
		// 显示对战双方士兵样式
		System.out.println("^_^" + playerName[0] + "的士兵: A");
		System.out.println("^_^" + playerName[1] + "的士兵:  B\n");
		// 显示对战地图
		System.out.println("\n图例: " + "■ 暂停  ¤ 幸运轮盘   ★ 地雷   〓 时空隧道   ∷ 普通\n");
		map.showMap(playerPos1, playerPos2);
		// 游戏开始
		int step;
		while (playerPos1 < 99 && playerPos2 < 99) {
			// 轮流掷骰子
			if (goAndStop[0].equals("on")) {
				step = throwShifter(1);
				System.out.println("\n-----------------"); // 显示结果信息
				System.out.println("骰子数: " + step);
				playerPos1 = getCurPos(1, playerPos1, step); // 计算这一次移动后的当前位置
				System.out.println("\n您当前位置:  " + playerPos1);
				System.out.println("对方当前位置:" + playerPos2);
				System.out.println("-----------------\n");
				map.showMap(playerPos1, playerPos2); // 显示当前地图
				if (playerPos1 == 99) { // 如果走到终点
					break; // 退出
				}
			} else {
				System.out.println("\n" + playerName[0] + "停掷一次!\n"); // 显示此次暂停信息
				goAndStop[0] = "on"; // 设置下次可掷状态
			}

			System.out.println("\n\n\n\n");

			if (goAndStop[1].equals("on")) {
				// 玩家2掷骰子
				step = throwShifter(2); // 掷骰子
				System.out.println("\n-----------------"); // 显示结果信息
				System.out.println("骰子数: " + step);
				playerPos2 = getCurPos(2, playerPos2, step); // 计算这一次移动后的当前位置
				System.out.println("\n您当前位置:  " + playerPos2);
				System.out.println("对方当前位置:" + playerPos1);
				System.out.println("-----------------\n");
				map.showMap(playerPos1, playerPos2);
				if (playerPos2 == 99) { // 如果走到终点
					break; // 退出
				}
			} else {
				System.out.println("\n" + playerName[1] + "停掷一次!\n"); // 显示此次暂停信息
				goAndStop[1] = "on"; // 设置下次可掷状态
			}

			System.out.println("\n\n\n\n");
		}

		// 游戏结束
		System.out.println("\n\n\n\n");
		System.out
				.print("****************************************************\n");
		System.out
				.print("                      Game  Over                    \n");
		System.out
				.print("****************************************************\n\n");
		judge();
	}

	/**
	 * 掷骰子
	 * 
	 * @param no
	 *            玩家次序
	 * @return step 掷出的骰子数目
	 */
	public int throwShifter(int no) {
		int step = 0;
		System.out.println("\n\n" + playerName[no - 1] + "请您按任意字母键后回车启动掷骰子:");
		Scanner input = new Scanner(System.in);
		/* String answer = */input.next();
		step = (int) (Math.random() * 10000) % 6 + 1;
		return step;
	}

	/**
	 * 计算玩家此次移动后的当前位置
	 * 
	 * @param no
	 *            玩家次序
	 * @param position
	 *            移动前位置
	 * @param step
	 *            掷的骰子数目
	 * @return position 移动后的位置
	 */
	public int getCurPos(int no, int position, int step) {
		position = position + step;
		if (position >= 99) {
			return 99;
		}
		Scanner input = new Scanner(System.in);
		switch (map.map[position]) {
		case 0: // 走到普通格
			if (no == 1 && playerPos2 == position) {
				playerPos2 = 0;
				System.out.println("^_^  哈哈哈哈...踩到了!");
			}
			if (no == 2 && playerPos1 == position) {
				playerPos1 = 0;
				System.out.println("^_^  哈哈哈哈...踩到了!");
			}
			break;
		case 1: // 幸运轮盘
			System.out.println("\n◆◇◆◇◆欢迎进入幸运轮盘◆◇◆◇◆");
			System.out.println("   请选择一种运气:");
			System.out.println("   1. 交换位置  2. 轰炸");
			System.out.println("=============================\n");
			int choice = input.nextInt();
			int temp;
			switch (choice) {
			case 1: // 交换位置
				if (no == 1) {
					temp = position;
					position = playerPos2;
					playerPos2 = temp;
				} else if (no == 1) {
					temp = position;
					position = playerPos1;
					playerPos1 = temp;
				}
				break;
			case 2: // 轰炸
				if (no == 1 && playerPos2 <= 6) {
					playerPos2 = 0;
				} else {
					playerPos2 = playerPos2 - 6;
				}
				if (no == 2 && playerPos1 <= 6) {
					playerPos1 = 0;
				} else {
					playerPos1 = playerPos1 - 6;
				}
				break;
			}
			break;
		case 2: // 踩到地雷
			position = position - 6;
			System.out.println("呜呜,踩到地雷,气死了...");
			break;
		case 3: // 暂停
			goAndStop[no - 1] = "off";
			System.out.println("真倒霉,要停战一回合了。");
			break;
		case 4: // 时空隧道
			position = position + 10;
			System.out.println("进入时空隧道, 真爽!");
			break;
		}
		// 返回此次掷骰子后玩家的位置坐标
		if (position < 0) {
			return 0;
		} else if (position > 99) {
			return 99;
		} else {

			return position;
		}
	}

	/**
	 * 显示对战结果
	 */
	public void judge() {
		if (playerPos1 > playerPos2) {
			System.out.println("\n恭喜" + playerName[0] + "将军! 您获胜了!");
		} else {
			System.out.println("\n恭喜" + playerName[1] + "将军! 您获胜了!");
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -