1. 程式人生 > 程式設計 >java飛行棋實現思路

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;
 }

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。