Java版 简易大富翁

2023-10-18 13:40
文章标签 java 简易 大富翁

本文主要是介绍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版 简易大富翁的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/232924

相关文章

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再