龟兔赛跑:
--普通格,共100格
龟:每次随机移动1至3格
兔子:每次随机移动1至6格
每20格会随机出现以下功能格各一次(不会在同一格内出现两种或两种以上功能)
功能格:(**和^^可以连续执行,@@和==不能连续执行)
幸运星格:选手可再行动一次
@@地雷格:兔子回到上数两个地雷格,龟回到上一地雷格(若后方无地雷格则回到起点)
==传送格:兔子到达下一传送格,龟到达下数两个传送格(若前方无传送格则到达终点)
||树格:兔子停止三次行动
^^下坡格:龟前进10格
Anmial类
package race; public abstract class Animal { protected int position; protected String name; protected int maxStep; public void play(){ run(); check(); lucky(); check(); dilei(); down(); check(); tree(); chuansong(); check(); printMap(); } public void check(){ if (position>=99){ System.out.println("游戏结束"+name+"获胜"); System.exit(0); } } public void run(){ int rand = (int)(Math.random() * maxStep)+1; position = position + rand; System.out.println(name+"本次走"+rand+"格,当前位置在"+position); } public void lucky(){ if (Map.map[position].equals("**")){ System.out.println(name+"踩到幸运星,再走一次"); run(); } } public abstract void dilei(); public abstract void chuansong(); public void tree(){ } public void down(){ } public void printMap(){ for (int i = 0; i < Map.map.length; i++) { if (i == position){ System.out.println(name); }else{ System.out.print(Map.map[i]); } } System.out.println(); } }
Game类
package race; import java.util.Scanner; public class Game { public static void main(String[] args) { Animal r = new Rabbit(); Animal t = new Turtle(); Scanner sc = new Scanner(System.in); while (true) { r.play(); t.play(); sc.nextLine(); } } }
Map类
package race; import java.util.Arrays; public class Map { static String[] map = new String[100]; public static boolean check(int[] arr, int rand) { for (int a : arr) { if (a == rand) { return true; } } return false; } static { Arrays.fill(map, "--"); String[] function = {"**", "==", "@@", "||", "^^"}; for (int i = 0; i < 5; i++) { int[] arr = new int[5]; for (int j = 0; j < 5; j++) { int rand = (int) (Math.random() * 20 + i * 20); while (check(arr, rand)) { rand = (int) (Math.random() * 20 + i * 20); } arr[j] = rand; map[rand] = function[j]; } } } } Rabbit类
package race; public class Rabbit extends Animal { private int stopCount; private boolean sleep = false; public Rabbit() { name = "兔子"; maxStep = 6; } @Override public void dilei() { if (Map.map[position].equals("@@")) { if (position < 40) { position = 0; System.out.println(name + "遇到了地雷,被炸回起点"); return; } int count = 0; for (int i = position - 1; i > 0; i--) { if (Map.map[i].equals("@@")) { count++; } if (count == 2) { position = i; break; } } System.out.println(name + "遇到了地雷,被炸回第" + position + "格"); } } @Override public void chuansong() { if (Map.map[position].equals("==")) { if (position >= 80) { position = 99; System.out.println(name + "遇到了传送格,直接传送到了终点"); return; } for (int i = position + 1; i < 100; i++) { if (Map.map[i].equals("==")) { position = i; break; } } System.out.println(name + "遇到了传送格,被传送到第" + position + "格"); } } public void tree() { if (Map.map[position].equals("||")) { if (!sleep){ stopCount = 3; System.out.println(name + "遇到了树,停止三次移动"); sleep = true; } } } public void run() { if (stopCount > 0) { stopCount--; System.out.println(name+"正在睡觉,这是第"+(3-stopCount)+"次"); }else { sleep = false; super.run(); } } }
Turtle类
package race; public class Turtle extends Animal { public Turtle() { name = "乌龟"; maxStep = 3; } @Override public void dilei() { if (Map.map[position].equals("@@")) { if (position < 20) { position = 0; System.out.println(name + "遇到了地雷,被炸回起点"); return; } for (int i = position - 1; i > 0; i--) { if (Map.map[i].equals("@@")) { position = i; break; } } System.out.println(name + "遇到了地雷,被炸回第" + position + "格"); } } @Override public void chuansong() { if (Map.map[position].equals("==")) { if (position >= 60) { position = 99; System.out.println(name + "遇到了传送格,直接传送到了终点"); return; } int count =0; for (int i = position + 1; i < 100; i++) { if (Map.map[i].equals("==")) { count++; } if (count == 2) { position = i; break; } } System.out.println(name + "遇到了传送格,被传送到第" + position + "格"); } } public void down() { if (Map.map[position].equals("^^")) { position = position + 10; System.out.println(name+"遇到了下坡路,前进10格"); } } }标签:赛跑,name,项目,int,void,龟兔,System,position,public From: https://blog.csdn.net/2301_79612384/article/details/140714239