• 三、猜字符小游戏


    有猜字母游戏,其游戏规则为:程序随机产生5个按照一定顺序排列的字符作为猜测的结果,由玩家来猜测此字符串。玩家可以猜测多次,每猜测一次,则由系统提示结果。如果猜测的完全正确,则游戏结束,计算玩家的游戏得分并输出;如果没有猜对,则提示猜测的结果,如猜对了几个字符,以及猜对了几个字符的位置等信息,并提示玩家游戏继续。

    本案例要求使用交互的方式实现此游戏:由玩家在控制台输入所猜测的字符串,如果所猜测的字符串与结果并不完全相同,则在界面输出比较后的结果,并提醒玩家继续猜测。交互过程如图-7所示:

    猜字母游戏

    图-7可以看出,每次猜测后,程序将比较玩家所输入的字符串,比较字符以及字符的位置,然后提示结果:5个字符中正确的字符个数,以及位置正确的字符个数,以便于玩家判断后续如何进行猜测。

    玩家终于猜测正确后,游戏结束,并给出游戏得分,交互过程如图-8所示:

    猜字母游戏2

    其中,游戏的得分规则为:字符的个数乘以100为总分,即此游戏的总分为 500 分。玩家如果第一次就猜对,则得满分(500分);每多猜测一次,则扣10分。由图-8可以看出,玩家共猜测了5次,因此,得分为 450。

    最后,如果玩家在控制台录入 exit,则游戏中止,程序结束。交互过程如图-9所示:

    猜字母游戏2

    一、设计数据结构
    1. char[] chs; //随机生成的字符数组
    2. char[] input; //用户输入的字符数组
    3. int letterRight; //字符对的个数
    int positionRight; //位置对的个数
    int[] result; //对比的结果
    4. int score; //得分
    int count; //猜错的次数
    二、设计程序的结构
    1. 主方法
     1  public static void main(String[] args) {
     2       char[] chs = generate(); // 获取随机的字符数组
     3       char[] input = new char[5];
     4       int[] result = new int[2];
     5       int count = 0; // 猜错次数初始为0
     6       System.out.print(chs);
     7       Scanner s = new Scanner(System.in);
     8       System.out.println("游戏开始,请输入你所猜的5个字母序列:(exit---退出)");
     9       while (true) {
    10          String inputStr = s.next().toUpperCase().trim();
    11          if (inputStr.equalsIgnoreCase("exit")) {
    12             System.out.println("下次再来吧!");
    13             break;
    14          }
    15          input = inputStr.toCharArray();
    16          result = check(chs, input);
    17          if (result[0] == chs.length) {
    18             int score = 100 * chs.length - 10 * count;
    19             System.out.println("恭喜你,猜对了!得分为:" + score);
    20             break;
    21          } else {
    22             count++;
    23             System.out.println("字符对个数:" + result[1] + ",位置对个数:" + result[0]
    24                   + "(总次数=" + count + ",exit---退出)");
    25          }
    26       }
    27    }
    2. 随机生成字符数组
      1   public static char[] generate() {
     2       char[] chs = new char[5];
     3       char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
     4             'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
     5             'U', 'V', 'W', 'X', 'Y', 'Z' };
     6       boolean[] flags = new boolean[letters.length];
     7       int index = 0; // 下标
     8       for (int i = 0; i < chs.length; i++) {
     9          do {
    10             index = (int) (Math.random() * letters.length); // 生成随机下标
    11          } while (flags[index]); //当下标对应的开关为true时,表示下标已生成过,继续生产下标
    12          chs[i] = letters[index]; //基于生成的下标确定随机生成的字符
    13          flags[index] = true; //将下标对应的下标设为true
    14       }
    15           return chs;
    16    }
    3. 对比随机字符数组与用户输入的字符数组
      public static int[] check(char[] chs, char[] input) {
       int[] result = new char[2];
        for (int i = 0; i < chs.length; i++) {
             for (int j = 0; j < input.length; j++) {
               if (chs[i] == input[j]) { //字符对
                        result[1]++; // 字符对的个数加1
                        if (i == j) { //位置对
                        result[0]++; // 位置对的个数加1
                }
                break;
                }
             }
        }
        return result;
     }

    3. 完整代码如下:

     1 /**
     2  * 猜字符游戏
     3  * 
     4  * @author Administrator
     5  * 
     6  */
     7 public class GuessingGame {
     8     public static void main(String[] args) {
     9         char[] chs = generate(); // 获取随机的字符数组
    10         char[] input = new char[5];
    11         int[] result = new int[2];
    12         int count = 0; // 猜错次数初始为0
    13     System.out.print(chs);
    14         Scanner s = new Scanner(System.in);
    15         System.out.println("游戏开始,请输入你所猜的5个字母序列:(exit---退出)");
    16         while (true) {
    17             String inputStr = s.next().toUpperCase().trim();
    18             if (inputStr.equalsIgnoreCase("exit")) {
    19                 System.out.println("下次再来吧!");
    20                 break;
    21             }
    22             input = inputStr.toCharArray();
    23             result = check(chs, input);
    24             if (result[0] == chs.length) {
    25                 int score = 100 * chs.length - 10 * count;
    26                 System.out.println("恭喜你,猜对了!得分为:" + score);
    27                 break;
    28             } else {
    29                 count++;
    30                 System.out.println("字符对个数:" + result[1] + ",位置对个数:" + result[0]
    31                         + "(总次数=" + count + ",exit---退出)");
    32             }
    33         }
    34     }
    35 
    36     /**
    37      * 随机生成字符数组
    38      * 
    39      * @return
    40      */
    41     public static char[] generate() {
    42         char[] chs = new char[5];
    43         char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
    44                 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
    45                 'W', 'X', 'Y', 'Z' };
    46         boolean[] flags = new boolean[letters.length];
    47         for (int i = 0; i < chs.length; i++) {
    48             int index; // 下标
    49             do {
    50                 index = (int) (Math.random() * letters.length); // 生成随机下标
    51             } while (flags[index]); //当下标对应的开关为true时,表示已存过,则重新生成index下标
    52             chs[i] = letters[index];
    53             flags[index] = true; // 将下标对应的下标设为true
    54         }
    55         return chs;
    56     }
    57 
    58     /**
    59      * 对比:随机字符数组与用户输入的字符数组
    60      * 
    61      * @param chs
    62      * @param input
    63      * @return
    64      */
    65     public static int[] check(char[] chs, char[] input) {
    66         int[] result = new int[2];
    67         for (int i = 0; i < chs.length; i++) {
    68             for (int j = 0; j < input.length; j++) {
    69                 if (chs[i] == input[j]) { // 字符对
    70                     result[1]++; // 字符对的个数加1
    71                     if (i == j) { // 位置对
    72                         result[0]++; // 位置对的个数加1
    73           }
    74                     break;
    75         }
    76       }
    77     }
    78         return result;
    79     }
    80 }
  • 相关阅读:
    虚拟设备 ide1:0 将开始断开
    虚拟机集群启动 某一台启动失败
    jeesite1,工具类,文件介绍
    line-clamp
    js中同名的函数的调用情况
    获取子页面iframe的点击事件及iframe跨域的交互
    Docker环境搭建入门
    软件工程课后作业:论我对百度搜索的看法
    第二阶段第十天12.10
    软件工程:用户场景描述
  • 原文地址:https://www.cnblogs.com/guodong-wang/p/6938293.html
Copyright © 2020-2023  润新知