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

相关文章

SpringBoot实现虚拟线程的方案

《SpringBoot实现虚拟线程的方案》Java19引入虚拟线程,本文就来介绍一下SpringBoot实现虚拟线程的方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录什么是虚拟线程虚拟线程和普通线程的区别SpringBoot使用虚拟线程配置@Async性能对比H

javaSE类和对象进阶用法举例详解

《javaSE类和对象进阶用法举例详解》JavaSE的面向对象编程是软件开发中的基石,它通过类和对象的概念,实现了代码的模块化、可复用性和灵活性,:本文主要介绍javaSE类和对象进阶用法的相关资... 目录前言一、封装1.访问限定符2.包2.1包的概念2.2导入包2.3自定义包2.4常见的包二、stati

SpringBoot结合Knife4j进行API分组授权管理配置详解

《SpringBoot结合Knife4j进行API分组授权管理配置详解》在现代的微服务架构中,API文档和授权管理是不可或缺的一部分,本文将介绍如何在SpringBoot应用中集成Knife4j,并进... 目录环境准备配置 Swagger配置 Swagger OpenAPI自定义 Swagger UI 底

解决hive启动时java.net.ConnectException:拒绝连接的问题

《解决hive启动时java.net.ConnectException:拒绝连接的问题》Hadoop集群连接被拒,需检查集群是否启动、关闭防火墙/SELinux、确认安全模式退出,若问题仍存,查看日志... 目录错误发生原因解决方式1.关闭防火墙2.关闭selinux3.启动集群4.检查集群是否正常启动5.

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav

Spring Security6.3.x的使用指南与注意事项

《SpringSecurity6.3.x的使用指南与注意事项》SpringSecurity6.3.1基于现代化架构,提供简洁配置、增强默认安全性和OAuth2.1/OIDC支持,采用Lambda... 目录介绍基础配置 (Servlet 应用 - 使用 Lambda DSL)关键配置详解(Lambda DS

Java Stream 的 Collectors.toMap高级应用与最佳实践

《JavaStream的Collectors.toMap高级应用与最佳实践》文章讲解JavaStreamAPI中Collectors.toMap的使用,涵盖基础语法、键冲突处理、自定义Map... 目录一、基础用法回顾二、处理键冲突三、自定义 Map 实现类型四、处理 null 值五、复杂值类型转换六、处理

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL