java飞行棋实现思路
本文实例为大家分享了java飞行棋的注释版,供大家参考,具体内容如下
可以直接用:
import java.util.Scanner;public class Fly3 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int all1 = 0;// 记录A的步数 int all2 = 0;// 记录B的步数 int flag1 = 1;// 对于A的暂停情况进行判断 int flag2 = 1;// 对于B的暂停情况进行判断 int first1 = 0;// 进行初始化判断 int first2 = 0; System.out.print('|||||||||✈|||||||||||||||||||||||' + 'n||||✈||||||飞行棋Beta版||||||||||||n' + '|||||||||||||||||||||||||||✈|||||n'+ '||||||||✈||||||||||||||||||||||||');// 标题显示 System.out.println(); System.out.println(); System.out.println(); System.out.println('t}}}}图形展示{{{{'); System.out.println('✈为传送门,一次10格' + 'n💣为炸弹,一次退回6格' + 'n⚡为被雷劈,一次直接返回原点 ' + 'n💩为幸运轮盘,踩上去可选择相关' + 'n🕔为暂停,踩上后下一次行动无法进行' + 'n注:玩家与玩家的位置相同时,后一个玩家将会将上一个玩家挤退2格'); System.out.println(); System.out.println(); String A = '玩家A';// 用户选择角色 A = login(A); String B = '玩家B'; B = login(B); if (A.equals(B)) { B = B + '2号'; } maps(icon(all1, all2)); for (int i = 0;; i++) { // A玩家视角👇 System.out.println(A + '开始投骰子'); int random1 = (int) (Math.random() * 6 + 1); String msg = sc.nextLine(); System.out.println('少女祈祷中。。。'); System.out.println(A + '走' + random1 + '步'); all1 += random1; if (first1 > 0) {// 判断是否为第一次运行地图(由于数组坐标重复的原因) if (flag1 % 2 != 0) {// 判断为第几次踩到了暂停格子 all1 = all1 - random1;// 如果是第二次则将前面走的随机步数减回去 System.out.println('但是' + A + '不能走!因为'); } } first1++; all2 = samepoint(all1, all2, B, A);// 判断二者坐标相同时(当A挤到B的位置时B怎么办(一种为后退四个格子一种为回到原点)) all1 = walk(A, all1);// 得到A所在数组下标位置,接下来对A的位置进行一次是否暂停的判定 flag1 = check(all1, flag1); /* * if (all1 == 15 || all1 == 28 || all1 == 85 || all1 == 90) * {//(在输出地图之后对A当前所在的位置进行判定,如果满足则让flag自加1) * flag1++;}此时flag在暂停判定模块中满足条件,进入判定,当第二次结束后flag则不会满足上面暂停模块的判定条件 else { flag1 = * 0; }//正常情况时flag被赋值为0;则不会走到上面的暂停判定模块 * */ if (all1 == 55 || all1 == 22 || all1 == 10) {// 对幸运转盘进行操作判定 System.out.println('请选择' + A + '要执行的操作!n1.和' + B + '换个位置n2.让' + B + '退后个4格子'); int choice = sc.nextInt(); if (choice == 1) { int temp = all1; all1 = all2; all2 = temp; System.out.println(A + '和' + B + '的位置交换了!'); } else { if (all2 < 4) { all2 = 0; System.out.println('直接把' + B + '送回原点了!'); } else { all2 -= 4; } } } maps(icon(all1, all2));// 判断A是否符合条件获胜 System.out.println(A+'的位置是'+all1+'n'+B+'的位置是'+all2+'n'); if (all1 >= 100) { System.out.println(A + '赢啦'); return; } // 到此为止,对A的一轮判定结束 // B玩家地图视角👇 System.out.println(B + '开始投骰子'); int random2 = (int) (Math.random() * 6 + 1); String msg2 = sc.nextLine(); System.out.println('少女祈祷中。。。'); System.out.println(B + '走' + random2 + '步'); all2 += random2; if (first2 > 0) {// 判断是否初始化 if (flag2 % 2 != 0) {// 判断第几次踩到了暂停格子 all2 = all2 - random2; System.out.println('但是' + B + '不能走!因为'); } } first2++; all1 = samepoint(all2, all1, A, B);// 判断二者坐标相同时(当B挤到A的位置时A怎么办(一种为后退四个格子一种为回到原点)) all2 = walk(B, all2); flag2 = check(all2, flag2); if (all2 == 55 || all2 == 22 || all2 == 10) { System.out.println('请选择' + B + '要执行的操作!n1.和' + A + '换个位置n2.让' + A + '退后个4格子'); int choice = sc.nextInt(); if (choice == 1) { int temp = all2; all2 = all1; all1 = temp; System.out.println(B + '和' + A + '的位置交换了!'); } else { if (all1 < 4) { all1 = 0; System.out.println('直接把' + A + '送回原点了!'); } else { all1 -= 4; } } } maps(icon(all1, all2)); System.out.println(A+'的位置是'+all1+'n'+B+'的位置是'+all2+'n'); if (all2 >= 100) {// 判断B是否符合条件获胜 System.out.println(B + '赢啦'); return; } // 到此位置,对B的一轮判定结束 } } public static void maps(String[] a) {// 加空格是为了美观 for (int i = 0; i < 32; i++) { System.out.print(a[i] + ' '); } System.out.println();// 第一排地图图形输出 for (int i = 0; i < 32; i++) { System.out.print(' '); } System.out.println(' ' + a[32]);// 第二排地图图形输出 for (int i = 0; i < 32; i++) { System.out.print(' '); } System.out.println(' ' + a[33]);// 第三排地图图形输出 for (int i = 65; i > 33; i--) { System.out.print(a[i] + ' ');// 第四排地图图形输出 } System.out.println(); System.out.println(a[66]); System.out.println(a[67]);// 第五第六排地图输出 for (int i = 68; i < 100; i++) { System.out.print(a[i] + ' ');// 第七排地图图形输出 } for (int i = 100; i < 105; i++) {// 结尾小旗子图像输出 System.out.print(a[i]); } System.out.println(); } public static String[] icon(int a, int b) { String[] map = new String[105]; for (int i = 0; i < 105; i++) { if (i == 32 || i == 33 || i == 66 || i == 67) { map[i] = '||';// 竖向输出道路 } else if (i == 3 || i == 9 || i == 23 || i == 40) { map[i] = '✈';// 传送门logo } else if (i == 75 || i == 62 || i == 48 || i == 37 || i == 98) { map[i] = '💣';// 炸弹logo } else if (i == 15 || i == 28 || i == 85 || i == 90) { map[i] = '🕔';// 暂停logo } else if (i == 55 || i == 22 || i == 10) { map[i] = '💩';// 幸运转盘logo } else if (i == 100 || i == 101 || i == 102 || i == 103 || i == 104) { map[i] = '🚩';// 结尾处旗帜logo } else if (i == 99) { map[i] = '⚡';// 结尾处闪电logo } else { map[i] = '=';// 其余为横向的道路 } } map[b] = 'B'; map[a] = 'A'; return map; } public static int walk(String player, int a) {// 对当前角色应该走到的数组下标进行判断 int num = a; switch (a) { case 3: case 9: case 23: case 40: System.out.println(player + '进入传送门,传送10格!'); num = a + 10; break; case 75: case 62: case 48: case 37: case 98: System.out.println(player + '危!!n踩到炸弹了,退6格!'); num = a - 6; return num; case 15: case 28: case 85: case 90: num = a; System.out.println(player + '踩到了暂停格子!'); break; case 55: case 22: case 10: num = a; System.out.println(player + '踩到了幸运转盘!!!!'); break; case 99: num = 0; System.out.println(player + '危!!!n被雷劈了,直接送回原点'); break; default: num = a; break; } return num; } public static int check(int a, int b) {// a为位置,b为状态判断 if (a == 15 || a == 28 || a == 85 || a == 90) { b++; } else { b = 0; } return b; } public static String login(String a) { String[] names = { '劳拉', '不知火舞', '春丽' }; Scanner sc = new Scanner(System.in); System.out.println('可选角色:1.劳拉t2.不知火舞t3.春丽'); System.out.println('请' + a + '选择你的角色');// 角色选择 int aplayer = sc.nextInt(); String player = names[aplayer - 1]; return player; } public static int samepoint(int a, int b, String A, String B) {// 输入的A为受害者,B为幸运玩家 if (a == b && a >= 2 && a != 0) { b = b - 2; System.out.println(A + '玩家被' + B + '玩家挤回去了2格!'); return b; } else if (a == b && a < 2 && a > 0) { b = 0; System.out.println(A + '玩家被' + B + '玩家挤回去了原点!'); } return b; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章: