JAVA-飞行棋(抢手棋)

JAVA 小白在成长!

这个飞行棋的代码可以说是2.0版本了,
之前写的有个问题(特殊位置执行结束,做不到飞行棋位置的累加)。
现在写的是改良版的。

public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int i=0;
		int j=0;
		boolean black=true;
		while(true) {
			System.out.println("是否掷骰子");
			String str=in.nextLine();
			if("退出".equals(str)) {
				System.out.println("游戏结束!欢迎下次使用");
				break;
			}
			if("否".equals(str)) {
				System.out.println("游戏结束!欢迎下次使用");
				break;
			}
			int a=(int)(Math.random()*6+1);
			System.out.println((black?"你":"机器人")+"前进了"+a+"步");	
			if(black==true) {
				i=i+a;
				System.out.println((black?"你":"机器人")+"的位置是"+i);
				int[] kl = sse(i, j);
				i=kl[0];
				j=kl[1];
				
			}else if(black==false){
				j=j+a;
				System.out.println((black?"你":"机器人")+"的位置是"+j);
				int[] ks=sseh(j,i);
				j=ks[0];
				i=ks[1];
			}
			if(i>=41) {
				System.out.println("恭喜你,你赢了!");
				break;
			}else if(j>=41) {
				System.out.println("很遗憾,机器人获胜!");
				break;
			}
			black=!black;
		}
	}
	public static int[] sse(int sum, int num) {
		switch (sum) {
		case 4:
			System.out.println("你前进两步");
			sum = sum + 2;
			System.out.println("你当前位置是:" + sum);
			break;
		case 9:
			System.out.println("你后退三步");
			sum = sum - 3;
			System.out.println("你当前位置是:" + sum);
			break;
		case 13:
			System.out.println("你暂停一次");
			System.out.println("你当前位置是:" + sum);
			int f = (int) (Math.random() * 6 + 1);
			num = num + f;
			System.out.println("机器人前进了" + f);
			System.out.println("机器人当前位置" + num);
			break;
		case 18:
			System.out.println("你奖励再掷一次骰子");
			Scanner in = new Scanner(System.in);
			System.out.println("是否掷筛子?");
			String str = in.nextLine();
			sum = sum + (int) (Math.random() * 6 + 1);
			System.out.println("你当前位置是:" + sum);
			break;
		case 21:
			System.out.println("你滚回原点");
			sum = 0;
			System.out.println("你当前位置是:" + sum);
			break;
		case 25:
			System.out.println("你前进五步");
			sum = sum + 5;
			System.out.println("你当前位置是:" + sum);
			break;
		case 29:
			System.out.println("你后退六步");
			sum = sum - 6;
			System.out.println("你当前位置是:" + sum);
			break;
		case 32:
			System.out.println("你前进五步");
			sum = sum + 5;
			System.out.println("你当前位置是:" + sum);
		case 37:
			System.out.println("你后退十步");
			sum = sum - 10;
			System.out.println("你当前位置是:" + sum);
			break;

		}
		int gg[]=new int[2];
		gg[0]=sum;
		gg[1]=num;
		return gg;

	}

	public static int[] sseh(int sum, int num) {
		switch (sum) {
		case 4:
			System.out.println("机器人前进两步");
			sum = sum + 2;
			System.out.println("机器人当前位置是:" + sum);
			break;
		case 9:
			System.out.println("机器人后退三步");
			sum = sum - 3;
			System.out.println("机器人当前位置是:" + sum);
			break;
		case 13:
			System.out.println("机器人暂停一次");
			System.out.println("机器人当前位置是:" + sum);
			Scanner in = new Scanner(System.in);
			System.out.println("是否掷筛子?");
			String str = in.nextLine();
			num = num + (int) (Math.random() * 6 + 1);
			System.out.println("你当前位置是:" + num);
			break;
		case 18:
			System.out.println("机器人奖励再掷一次骰子");
			int f = (int) (Math.random() * 6 + 1);
			num = num + f;
			System.out.println("机器人前进了" + f);
			System.out.println("机器人当前位置" + num);
			break;
		case 21:
			System.out.println("机器人滚回原点");
			sum = 0;
			System.out.println("机器人当前位置是:" + sum);
			break;
		case 25:
			System.out.println("机器人前进五步");
			sum = sum + 5;
			System.out.println("机器人当前位置是:" + sum);
			break;
		case 29:
			System.out.println("机器人后退六步");
			sum = sum - 6;
			System.out.println("机器人当前位置是:" + sum);
			break;
		case 32:
			System.out.println("机器人前进五步");
			sum = sum + 5;
			System.out.println("机器人当前位置是:" + sum);
		case 37:
			System.out.println("机器人后退十步");
			sum = sum - 10;
			System.out.println("机器人当前位置是:" + sum);
			break;
		}
		int gg[]=new int[2];
		gg[0]=sum;
		gg[1]=num;
		return gg;
	}
}

你可能感兴趣的:(JAVA)