本文主要是介绍Java版 简易大富翁,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//game类:
package com.fs.DaFuWen;import java.util.*;public class Game {//声明地图Map map;//声明对战中玩家1的当前位置int playerPos1;//声明对战中玩家2的当前位置int playerPos2;//声明走或停标识设置String[] goAndStop = new String[2];//声明对战角色String[] playerName = new String[4];/*** 初始化游戏的一局*/public void init() {//创建Map对象map = new Map();//生成地图map.createMap();//设置玩家1起始位置playerPos1 = 0;//设置玩家2起始位置playerPos2 = 0;//记录玩家1下一次走或停goAndStop[0] = "on";//设置玩家2下一次走或停goAndStop[1] = "on";}/*** 开始游戏*/public void start() {//调用初始化方法init();//显示游戏界面System.out.println("~~~~~~双人对战~~~~~~");System.out.println();System.out.println();//角色设置Scanner scanner = new Scanner(System.in);System.out.println("请选择角色:1,钱夫人 2,大老千 3, 阿土伯 4,贝哥");for (int no = 1; no < 3; no++) {System.out.println("请玩家" + no + "输入数字选择角色:");int role = scanner.nextInt();setRole(no, role);}//开始游戏play();}/*** 设置对战角色** @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 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")) {//玩家1掷骰子step = throwShifter(1); //掷骰子System.out.println("\n-----------------"); //显示结果信息System.out.println("骰子数: " + step);playerPos1 = getCurPos(1, playerPos1, step); //计算这一次移动后的当前位置System.out.println(playerName[0] + "\n您当前位置: " + (playerPos1 + 1));System.out.println(playerName[1] + "当前位置:" + (playerPos2 + 1));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(playerName[1] + "\n您当前位置: " + (playerPos2 + 1));System.out.println(playerName[0] + "当前位置:" + (playerPos1 + 1));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;//提示玩家启动掷骰子Scanner sc = new Scanner(System.in);Random rd = new Random();System.out.println();System.out.println(playerName[no - 1] + "请你按任意字母开始投掷骰子");sc.nextLine();//模拟掷骰子:产生一个1~6的数字作为玩家掷的骰子数目step = rd.nextInt(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 (position == playerPos2) { //添加条件:玩家1与对方骑兵相遇//添加代码实现:踩到对方,对方回到起点playerPos2 = 0;System.out.println(":-D 哈哈哈哈...踩到了!");}if (position == playerPos1) { //添加条件:玩家2与对方骑兵相遇//添加代码实现:踩到对方,对方回到起点playerPos1 = 0;System.out.println(":-D 哈哈哈哈...踩到了!");}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) {//添加代码实现交换:position与playerPos2数值互换temp = position;position = playerPos2;playerPos2 = temp;} else if (no == 2) {//添加代码实现交换:position与playPos1数值互换temp = position;position = playerPos1;playerPos1 = temp;}break;case 2: //轰炸if (no == 1 && playerPos2 < 6) { //no为1并且玩家2位置小于6//添加代码实现:计算玩家2当前位置playerPos2 = 0;break;} else {//添加代码实现:计算玩家2当前位置playerPos2 = 0;}if (no == 2 && playerPos1 < 6) { //no为2并且玩家1位置小于6//添加代码实现: 计算玩家1当前位置playerPos1 = 0;break;} else {//添加代码实现:计算玩家1当前位置playerPos1 = 0;}break;}break;case 2: //踩到地雷//添加代码实现:踩到地雷退6步position = position - 6;System.out.println("~:-( " + "踩到地雷,气死了...");break;case 3: //下一次暂停一次//添加代码实现:设置下次暂停掷骰子goAndStop[no - 1] = "step";System.out.println("~~>_<~~ 要停战一局了。");break;case 4: //时空隧道//添加代码实现:进入时空隧道,加走10步position = position + 10;System.out.println("|-P " + "进入时空隧道, 真爽!");break;}//返回此次掷骰子后玩家的位置坐标if (position < 0) {return 0;} else if (position > 99) {return 99;} else {return position;}}/*** 显示对战结果*/public void judge() {//添加代码if (playerPos1==99){System.out.println("gameOver!赢家是"+playerName[0]);}if (playerPos2==99){System.out.println("gameOver!赢家是"+playerName[1]);}}
}
//地图创建,和当前位置获取类:
package com.fs.DaFuWen;public class Map {int[] map = new int[100]; //对战地图int[] luckyTurn = {6, 23, 40, 55, 69, 83}; //幸运轮盘 ¤int[] landMine = {5, 13, 17, 33, 38, 50, 64, 80, 94}; //地雷位置 ★int[] pause = {9, 27, 60, 93}; //暂停 ■int[] timeTunnel = {20, 25, 45, 63, 72, 88, 90}; //时空隧道 〓/*** 生成地图:* 关卡代号为:1:幸运轮盘 2:地雷 3: 暂停 4:时空隧道 0:普通 ∷*/public void createMap() {int i = 0;//在对战地图上设置幸运轮盘for (i = 0; i < luckyTurn.length; i++) {map[luckyTurn[i]] = 1;}//添加代码实现在对战地图上设置地雷for (i = 0; i < landMine.length; i++) {map[landMine[i]] = 2;}//添加代码实现在对战地图上设置暂停for (i = 0; i < pause.length; i++) {map[pause[i]] = 3;}//添加代码实现在对战地图上设置时空隧道for (i = 0; i < timeTunnel.length; i++) {map[timeTunnel[i]] = 4;}}/*** 显示地图关卡对应的图形** @param i 地图当前位置的关卡代号* @param index 当前地图位置编号* @param playerPos1 玩家1的当前位置* @param playerPos2 玩家2的当前位置* @return 地图当前位置的对应图片* 显示规则如下: @@ 两人重合时* A 玩家1* B 玩家2* ¤ 幸运轮盘* ★ 地雷* ■ 暂停* 〓 时空隧道* ∷ 普通格* 实现逻辑: 先判断playerPos1与playerPos2及index是否是同一位置,如果是,则重合,显示@@* 如果不是,则判断playerPos1是否与index重合,是则显示A* 如果不是,则判断playerPos2是否与index重合,是则显示B* 如果不是,则判断value为什么值,显示对应的关卡图形.*/public String getGraph(int i, int index, int playerPos1, int playerPos2) {String graph = "";//添加代码if (playerPos1 == index && playerPos2==index) {graph = "@@";} else if (playerPos1 == index) {graph = "A";} else if (playerPos2 == index) {graph = "B";} else {for (int j = 0; j < map.length; j++) {//i = map[j];switch (i) {case 1:graph = "¤";break;case 2:graph = "★";break;case 3:graph = "■";break;case 4:graph = "〓";break;default:graph = "::";break;}}}return graph;}/*** 输出地图的偶数行(第2行)** @param start 输出的起始点在地图上的位置* @param end 输出的结束点在地图上的位置* @param playerPos1 玩家1的当前位置* @param playerPos2 玩家2的当前位置*/public void showLine2(int start, int end, int playerPos1, int playerPos2) {for (int i = end - 1; i >= start; i--) {System.out.print(getGraph(map[i], i, playerPos1, playerPos2));}}/*** 输出地图的右竖列** @param start 输出的起始点在地图上的位置* @param end 输出的结束点在地图上的位置* @param playerPos1 玩家1的当前位置* @param playerPos2 玩家2的当前位置*/public void showRLine(int start, int end, int playerPos1, int playerPos2) {for (int i = start; i < end; i++) {for (int j = 28; j > 0; j--) { //输出29个空格System.out.print(" ");}System.out.print(getGraph(map[i], i, playerPos1, playerPos2));System.out.println();}}/*** 输出地图的奇数行(第1、3行)** @param start 输出的起始点在地图上的位置* @param end 输出的结束点在地图上的位置* @param playerPos1 玩家1的当前位置* @param playerPos2 玩家2的当前位置*/public void showLine1(int start, int end, int playerPos1, int playerPos2) {//添加代码for (int i =start; i <end; i++) {System.out.print(getGraph(map[i], i, playerPos1, playerPos2));}}/*** 输出地图的左竖列** @param start 输出的起始点在地图上的位置* @param end 输出的结束点在地图上的位置* @param playerPos1 玩家1的当前位置* @param playerPos2 玩家2的当前位置*/public void showLLine(int start, int end, int playerPos1, int playerPos2) {//添加代码for (int i = start; i < end; i++) {System.out.println(getGraph(map[i], i, playerPos1, playerPos2));}}/*** 显示对战地图** @param playerPos1 玩家1的当前位置* @param playerPos2 玩家2的当前位置*/public void showMap(int playerPos1, int playerPos2) {//显示地图第一行showLine1(0,30,playerPos1,playerPos2);//换行System.out.println();//显示地图右竖行showRLine(30,35,playerPos1,playerPos2);//显示地图第二行showLine2(35,65,playerPos1,playerPos2);//换行System.out.println();//显示地图左竖行showLLine(65,70,playerPos1,playerPos2);//显示地图第3行showLine1(70,100,playerPos1,playerPos2);}
}
//测试类:
package com.fs.DaFuWen;public class Test {public static void main(String[] args) {Game game = new Game();game.init();game.start();game.play();}
}
这篇关于Java版 简易大富翁的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!