为了账号安全,请及时绑定邮箱和手机立即绑定

猜字母游戏!会写会玩才是真本事!

标签:
PHP Java Html/CSS

猜字母游戏!欢迎鉴赏!初学者可以了解一下!

游戏截图1
游戏截图3
游戏截图2

package com.jc1;
import java.util.*;
/**
 * 
 * @author JavaLover
 * 猜字符游戏
 *
 */
public class GuessGame3 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("====================欢迎来到猜字母游戏====================");
        System.out.println("游戏规则:\n" 
                + "1、游戏中的字符种类可自定义选择(字母和数字),并且区分大小写;\n" 
                + "2、输入的字符与位置和随机生成的字符与位置一一对应才算正确;\n"
                + "3、游戏总分1000分,每猜1次扣除10分,分数为0则退出游戏;\n"
                + "4、游戏每运行一次,系统都会随机生成不重复的字符;\n" 
                + "5、游戏中字符长度可自由设置;\n" 
                + "6、猜字符过程中输入“==”退出游戏。\n\n"
                + "------------------游戏开始!Ready!Go!------------------\n");
        int score = 1000; // 总分
        int chs = 0, loc = 0; // 声明猜对字符的个数及位置正确的字符个数
        String choiceStr = null, countStr = null;
        int choice = 0, count = 0;
        char[] answer = null;
        while (choice < 1 || choice > 5) {
            System.out.println("请设置字符类型:(输入类型前面的序号进行选择)\n" 
                    + "1.数字    2.小写字母    3.大写字母    4.字母组合    5.字母与数字组合");
            choiceStr = scan.nextLine();
            if (isNumer(choiceStr)) {
                choice = Integer.parseInt(choiceStr);
                if (choice < 1 || choice > 5) {
                    System.out.println("你的选择有误!请重新输入!");
                    continue;
                }
            } else {
                System.out.println("你输入了非法字符!请重新输入!");
            }
        }
        while (count < 1 || count > 26) {
            System.out.println("请设置字符长度:(数字类型长度为1~10,其它类型长度为1~26)");
            countStr = scan.nextLine(); // 自定义字符的个数
            if (isNumer(countStr)) {
                count = Integer.parseInt(countStr); //将输入的字符串转换成整型
                if (choice == 1) {
                    if (count < 1 || count > 10) {
                        System.out.println("你选择猜测的字符类型为数字,与长度不匹配,请重新设置字符长度!\n");
                        count = 0;
                        continue;
                    }
                } else {
                    if (count < 1 || count > 26) {
                        System.out.println("你选择猜测的字符类型长度不匹配,请重新设置字符长度!\n");
                    }
                }
            } else {
                System.out.println("你输入了非法字符!请重新输入!");
            }
        }
        // 调用生成随机字符方法(共有五种随机方法)
        if (choice == 1) {
            answer = generate1(count);
        } else if (choice == 2) {
            answer = generate2(count);
        } else if (choice == 3) {
            answer = generate3(count);
        } else if (choice == 4) {
            answer = generate4(count);
        } else {
            answer = generate5(count);
        }
        // System.out.println(answer); // 输出系统随机生成的字符
        do {
            System.out.println("猜猜看吧!请输入" + count + "个字符:");
            String s = scan.nextLine();
            // String s = scan.next().trim().toUpperCase(); //
            // 接收用户输入的字母并自动将小写转换成大写(即不区分大小写)
            // 检查用户输入==退出游戏
            if (s.equals("==")) {
                System.out.println("------------------退出游戏!欢迎继续挑战!------------------");
                break;
            }
            if (s.length() != count) {
                System.out.println("你的输入有误!字符长度必须是" + count + "位!请重新输入!");
                continue;
            }
            char[] input = s.toCharArray();
            int[] var = check(answer, input); // 调用比较方法返回有chs与loc信息的数组check
            chs = var[0];
            loc = var[1];
            System.out.println("你猜对字符个数:" + chs + ",其中位置正确的字符个数:" + loc);
            score -= 10;
            if (score == 0) {
                System.out.println("\nYOU LOSE!唉。。。真替你的智商着急!");
                break;
            }
            if (chs == count && loc == count) {
                rank(count, score); // 调用rank方法输出段位
            }
        } while (chs != count || loc != count);
        scan.close(); // 关闭输出

    }

    // 效率非常高且常用的生成随机字符方法一(纯数字)
    public static char[] generate1(int n) {
        // 将10个数字放到一个数组里面,然后随机生成这个数组的索引(下标),通过随机索引得到数字
        char[] allLetters = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        boolean[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
        char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
        int temp; // 声名数组索引(下标)
        for (int i = 0; i < letter.length; i++) {
            do {
                temp = new Random().nextInt(allLetters.length); // 生成随机数方法一
                // temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
                letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
            } while (isRep[temp]);
            isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
        }
        return letter;
    }

    // 效率非常高且常用的生成随机字符方法二(小写字母)
    public static char[] generate2(int n) {
        // 将26个小写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过随机索引得到小写字母
        char[] allLetters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        boolean[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
        char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
        int temp; // 声名数组索引(下标)
        for (int i = 0; i < letter.length; i++) {
            do {
                temp = new Random().nextInt(allLetters.length); // 生成随机数方法一
                // temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
                letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
            } while (isRep[temp]);
            isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
        }
        return letter;
    }

    // 效率非常高且常用的生成随机字符方法三(大写字母)
    public static char[] generate3(int n) {
        // 将26个大写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机大写字母
        char[] allLetters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        boolean[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
        char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
        int temp; // 声名数组索引(下标)
        for (int i = 0; i < letter.length; i++) {
            do {
                temp = new Random().nextInt(allLetters.length); // 生成随机数方法一
                // temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
                letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
            } while (isRep[temp]);
            isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
        }
        return letter;
    }

    // 效率非常高且常用的生成随机字符方法四(大小写字母)
    public static char[] generate4(int n) {
        // 将26个大写字母和26个小写字母放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机字母
        char[] allLetters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        boolean[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
        char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
        int temp; // 声名数组索引(下标)
        for (int i = 0; i < letter.length; i++) {
            do {
                temp = new Random().nextInt(allLetters.length); // 生成随机数方法一
                // temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
                letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
            } while (isRep[temp]);
            isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
        }
        return letter;
    }

    // 效率非常高且常用的生成随机字符方法五(大小写字母与数字组合)
    public static char[] generate5(int n) {
        // 将26个大写字母、小写字母、数字放到一个数组里面,然后随机生成这个数组的索引(下标),通过索引得到随机字母和数字
        char[] allLetters = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
                'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                'Y', 'Z' };
        boolean[] isRep = new boolean[allLetters.length]; // 创建一个boolean型数组,与allLetters长度一致,所有元素默认为false
        char[] letter = new char[n]; // 创建数组接收随机生成的n个字符
        int temp; // 声名数组索引(下标)
        for (int i = 0; i < letter.length; i++) {
            do {
                temp = new Random().nextInt(allLetters.length); // 生成随机数方法一
                // temp = (int) (Math.random() * allLetters.length); // 生成随机数方法二
                letter[i] = allLetters[temp]; // 将allLetters数组中下标为temp的元素赋值给letter数组中索引为i的元素
            } while (isRep[temp]);
            isRep[temp] = true; // letter每一次赋值完成后,将与allLetters数组下标对应的isRep数组下标所对应的元素值改为true
        }
        return letter;
    }

    // 判断输入是否为数字
    public static boolean isNumer(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    // 比较系统随机生成的字符与用户输入的字符
    public static int[] check(char[] answer, char[] input) {
        int m = 0, n = 0;
        for (int i = 0; i < answer.length; i++) {
            for (int j = 0; j < input.length; j++) {
                if (answer[i] == input[j]) {
                    m++; // 猜中的字符个数
                    if (i == j) {
                        n++; // 位置对应的字符个数
                    }
                    break;
                }
            }
        }
        return new int[] { m, n }; // 将猜中的字符个数与位置对应的字符个数存放到数组中
    }

    // 输出得分
    public static void rank(int n, int score) {
        if (n >= 4) {
            if (score >= 950) {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========最强王者========");
            } else if (score >= 900) {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========超凡大师========");
            } else if (score >= 850) {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========璀璨钻石========");
            } else if (score >= 800) {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========华贵铂金========");
            } else if (score >= 750) {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========荣耀黄金========");
            } else if (score >= 700) {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========不屈白银========");
            } else if (score >= 600) {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n段位:========英勇黄铜========");
            } else {
                System.out.println("\n恭喜你!猜对了!你的得分:" + score + "分!\n\n======加油吧!骚年!======");
            }
        } else {
            System.out.println("\n大神!恭喜你!猜对了!你的得分:" + score + "分!\n\n=======这太小儿科了!你需要更大的挑战!=======");
        }
    }
}
点击查看更多内容
19人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消