如何添加"欺骗"功能到Java游戏策划 [英] How to add a "cheat" function to a Java mastermind game

查看:122
本文介绍了如何添加"欺骗"功能到Java游戏策划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个策划游戏,这里有我的codeS:

 进口的java.util。*;公共类毫米{
    公共静态无效的主要(字串[] args){
        的System.out.println(我想一个4位数字code的。);
        INT [] =随机numberGenerator();
        确切的诠释= 0,关闭= 0;
        而(精确!= 4){
            INT [] =猜测userinput();
            确切= 0;
            关闭= 0;
            的for(int i = 0; I< guess.length;我++){
                如果(猜[I] ==随机[I]){
                    确切++;
                }
                否则如果(随机[Ⅰ] ==猜[0] ||随机[Ⅰ] ==猜[1] ||随机[Ⅰ] ==猜[2] ||随机[Ⅰ] ==猜[3]) {
                    接近++;
                }
            }
            如果(精确== 4){
                的System.out.println(你猜对了!);
            }
            其他{
                的System.out.println(精确+准确+关闭:+关闭);
            }
        }
    }公共静态INT [] userinput(){
    的System.out.println(你的猜测:);
    扫描仪用户=新的扫描仪(System.in);
    字符串输入= user.nextLine();
    INT [] =猜测新INT [4];
    对(INT I = 0; I&下; 4;我++){
        猜[I] =的Integer.parseInt(将String.valueOf(input.charAt(I)));
    }
    返回的猜测;
}公共静态INT [] numberGenerator(){
    随机RND =新的随机();
    INT [] = randArray {10,10,10,10};
    的for(int i = 0; I< randArray.length;我++){
        INT TEMP = rnd.nextInt(9);
        而(温度== randArray [0] ||温度== randArray [1] ||温度== randArray [2] ||温度== randArray [3]){
            TEMP = rnd.nextInt(9);
        }
        randArray [I] =温度;
    }
    返回randArray;
}
}

现在的程序工作。不过,我想增加一个功能,如果用户输入的是*,该程序打印作弊code输入的秘密code是:XXXX(//生成随机数),然后继续问。我试着写了一个单独的欺骗()功能,以实现这一目标。但是,再次调用 numbergenerator()这样的秘密code修改每次。如何避免这个问题?还是有什么其他的方式来实现这个功能?

BTW,这里是作弊功能的逻辑:

 如果(guess.equals(*)){
    System.out.format(作弊code输入的秘密code是:)
    的for(int i = 0; I< guess.length;我++){
            System.out.print(猜[I]);
        }
}


解决方案

作弊是这样的:

 如果(guess.equals(*)){
    System.out.format(作弊code输入的秘密code是:)
    的for(int i = 0; I< random.length;我++){
            System.out.print(随机[I]);
        }
}

编辑:两种方法可以从 userinput获得随机的()

A)传递随机 userinput()作为参数

 公共静态INT [] userinput(INT []随机){
   ...

B)做随机一个成员变量(可能是更好的方式来做到这一点)

 公共类毫米{
    静态INT []随机的;
    公共静态无效的主要(字串[] args){
        的System.out.println(我想一个4位数字code的。);
        随机= numberGenerator();
        ...

I am writing a mastermind game, here are my codes:

import java.util.*;

public class mm {
    public static void main(String[] args) {      
        System.out.println("I'm thinking of a 4 digit code.");
        int[] random=numberGenerator();
        int exact=0, close=0;
        while(exact!=4){
            int[] guess=userinput();
            exact=0;
            close=0;
            for(int i=0;i<guess.length;i++){
                if(guess[i]==random[i]){
                    exact++;
                }
                else if(random[i]==guess[0] || random[i]==guess[1] || random[i]==guess[2] || random[i]==guess[3]){
                    close++;
                }
            }
            if(exact==4){
                System.out.println("YOU GOT IT!");
            }
            else{
                System.out.println("Exact: "+exact+" Close: "+close);
            }
        }   
    }

public static int[] userinput(){
    System.out.println("Your guess: ");
    Scanner user = new Scanner(System.in);
    String input = user.nextLine();
    int[] guess = new int[4];
    for (int i = 0; i < 4; i++) {
        guess[i] = Integer.parseInt(String.valueOf(input.charAt(i)));
    }
    return guess;
}

public static int[] numberGenerator() {
    Random rnd = new Random();
    int[] randArray = {10,10,10,10};
    for(int i=0;i<randArray.length;i++){
        int temp = rnd.nextInt(9);
        while(temp == randArray[0] || temp == randArray[1] || temp == randArray[2] || temp == randArray[3]){
            temp=rnd.nextInt(9);
        }
        randArray[i]=temp;
    }
    return randArray;
}
}

Now the program works. However, I want to add a function that if user's input is"*", the program prints "cheat code entered. The secret code is: XXXX(//the random number that generated)" then continue asking. I've tried to wrote a separate cheat() function in order to achieve this. But it calls thenumbergenerator()again so the secret code change everytime. How to avoid this problem? Or are there any other way to achieve this function?

BTW, here is the logic of the cheat function:

if (guess.equals("*")){
    System.out.format("cheat code entered. The secret code is:")
    for(int i=0;i<guess.length;i++){
            System.out.print(guess[i]);
        }
}

解决方案

Cheating goes like this:

if (guess.equals("*")){
    System.out.format("cheat code entered. The secret code is:")
    for(int i=0;i<random.length;i++){
            System.out.print(random[i]);
        }
}

EDIT: Two ways to get access to random from userinput()

A) Pass random to userinput() as a parameter

public static int[] userinput(int[] random){
   ...

B) make random a member variable (probably the better way to do it)

public class mm {
    static int[] random;
    public static void main(String[] args) {      
        System.out.println("I'm thinking of a 4 digit code.");
        random=numberGenerator();
        ...

这篇关于如何添加&QUOT;欺骗&QUOT;功能到Java游戏策划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆