游戏“牛与牛”。第1部分

你好!检疫工作取得了丰硕的成果,因此决定写一个时间杀手玩具“牛与牛”。游戏并不是那么困难,但是很有趣。通常,游戏使用需要猜测的4个数字。计算机猜测数字,用户输入他的4个数字,如果用户数字中的数字与隐藏数字中的位置重合,则说明这是牛,但如果不在其位置,则表示牛。在互联网上很容易找到更详细的规则。在这一部分中,我们将处理BackEnd,将来我们将添加FrontEnd,猜测用户的号码,也许仅此而已。好吧,让我们开始吧!



首先,我创建了一个BackEnd类,它将负责程序的所有工作。它具有以下字段:



    private int length;             // 
    private int[] mas;              //  
    private int[] inputArray;       //,  


想法如下:计算机生成一个长度为质量的数字,用户输入数字inputArray并查看其中包含多少头公牛和头牛,然后获胜或继续比赛。首先,我们将输出mas数组以查看数字并调试代码。那么我们将删除此行。让我们写一个任意长度的数组的输出:



    private void printMas(int[] mas) {
        for (int i = 0; i < length; i++) {
            System.out.print(mas[i] + "   ");
        }
    } 


我们将数组传递给此方法,然后通过for循环将其输出。可以通过该字段完成此操作,但是由于我们将首先输出mas,然后输出inputArray,因此我决定这样做。



现在我们需要创建一个数字代。您可以使用标准功能执行此操作,但不要忘记我们需要数字中的不同数字。这意味着号码45566将不起作用,但45367将是正确的。让我们编写一种通过数字验证来生成这样的数字的方法,但是首先,让我们找出用户想要猜测数字的时间:



private void getMas() {
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("    1  10");
            while (!scanner.hasNextInt()) {
                System.out.println(" ");
                scanner.next();
            }
            length = scanner.nextInt();
        } while (length <= 0 || length > 10);
        setMas(createMas());
    }


我们创建一个扫描程序对象,通过该对象我们将从控制台获取编号。接下来,我们使用带有do while后置条件的循环。我会解释为什么要过一会儿。在循环的主体中,我们看到一个while循环。它用于验证是否在控制台中输入了数字,而不是小数,字母等。确认将数字输入到控制台后,我们将其长度记为数值。现在,使用do while后置条件,我们检查它是否属于间隔[0,10)。如果输入的数字不包含在间隔中,则我们再次要求您输入长度。如果一次输入的数字正确,那么我们使用createMas方法生成数字并更改mas字段的值:



 public void setMas(int[] mas) {
        this.mas = mas;
    }


private int[] createMas() {
        int[] arr = new int[length];
        for (int i = 0; i < this.length; i++) {
            arr[i] = (int) (Math.random() * 10);
        }
        boolean checkMas = false;
        while (!checkMas) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length; j++) {
                    if (i != j) {
                        if (arr[i] == arr[j]) {
                            arr[j] = (int) (Math.random() * 10);
                        }
                    }

                }
            }


            boolean check = false;
            for (int i = 0; i < arr.length && !check; i++) {
                for (int j = 0; j < arr.length && !check; j++) {

                    if (i != j) {
                        if (arr[i] == arr[j]) {
                            check = true;
                            checkMas = false;
                        } else checkMas = true;
                    }
                }
            }

        }
        return arr;
    }


让我解释一下createMas的工作原理。首先,我们创建一个长度为length的数组,并用间隔[0,10)中的随机数填充它。 Math.random()从间隔[0,1)生成一个随机数,并将其乘以10得到一个在间隔[0,10)中的数。现在,我们有了一个数组arr,它由随机数字组成。下一步是检查它是否重复数字。我决定使用布尔变量来执行此操作。首先,我们将每个元素进行比较,如果匹配,则将我们正在比较的元素更改为随机数。比较完所有元素后,我们检查数组中数字的唯一性。为此,我还创建了check变量。然后,我们仅将每个元素进行比较。如果我们找到2个相同的元素,则退出进行比较(因为!检查将返回false,循环将结束),然后返回for循环以更改相同的数字。在数组中的所有数字均不同之后,checkMas变量变为true,并且数组的检查结束。现在,我们返回arr数组,并使字段mas = arr。



现在,我们有了一个计算机定义的号码。让我们来实现用户对这个数字的猜测。为此,让我们编写checkInput方法:



private void checkInput() {
        Scanner scanner = new Scanner(System.in);
        int[] arr = new int[length];
        if (length == 1) System.out.println(" " + length + "   ");
        else {
            if (length > 1 && length < 5) {
                System.out.println(" " + length + "   ");
            } else {
                if (length > 4 && length < 11) {
                    System.out.println(" " + length + "   ");
                }
            }
        }
        boolean checkMas = false;
        while (!checkMas) {
            for (int i = 0; i < length; i++) {
                do {
                    System.out.println("  ");
                    while (!scanner.hasNextInt()) {
                        System.out.println(" ");
                        scanner.next();
                    }
                    arr[i] = scanner.nextInt();
                    if(arr[i] < 0 || arr[i]>=10) System.out.println("   0  9 ");
                } while (arr[i] < 0 || arr[i] >= 10);
            }
            
            boolean check = checkInputArray(arr);
            if (check) {
                checkMas = true;
            } else {
                System.out.println("    ");
                System.out.println("  ");
            }
        }
        setInputArray(arr);

    }


同样,我们创建扫描器和一个长度为length的辅助数组arr。接下来是一堆if,它们只负责匹配数字和短语。在他们之后,可以直接输入并验证用户的号码。为了在逻辑上与用户没有分歧,我决定这样做,以便用户分别输入每个数字。



for (int i = 0; i < length; i++) {
                do {
                    System.out.println("  ");
                    while (!scanner.hasNextInt()) {
                        System.out.println(" ");
                        scanner.next();
                    }
                    arr[i] = scanner.nextInt();
                    if(arr[i] < 0 || arr[i]>=10) System.out.println("   0  9 ");
                } while (arr[i] < 0 || arr[i] >= 10);
            }


在代码的此部分中,输入并检查了号码。该工作类似于输入数组的长度,因此我认为没有理由对其进行解释。然后,检查用户数组是否存在相同的数字。为此,让我们编写checkInputArray方法:



private boolean checkInputArray(int[] arr) {
        boolean checkMas = false;
        boolean check = false;
        for (int i = 0; i < arr.length && !check; i++) {
            for (int j = 0; j < arr.length && !check; j++) {

                if (i != j) {
                    if (arr[i] == arr[j]) {
                        check = true;
                        checkMas = false;
                    } else checkMas = true;
                }
            }
        }
        return checkMas;
    }


检查与检查隐藏数组相似,因此我不再赘述。如果用户号码包含重复的号码,则我们要求您重新输入整个号码。依此类推,直到正确输入为止。然后,将inputArray字段的值更改为辅助数组arr。以防万一,我将给出代码:



public void setInputArray(int[] inputArray) {
        this.inputArray = inputArray;
    }


此时,我们有两个数组:隐藏数组和输入数组。现在是时候找出用户发现了多少头公牛和多少头母牛。



我们将使用checkCowAndBull方法找出这一点:



private int[] checkCowAndBull() {
        int[] arr = new int[2];
        int cow = 0;
        int bull = 0;
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {

                if (mas[i] == inputArray[j]) {
                    if (i == j) bull++;
                    else cow++;
                }
            }
        }
        arr[0] = cow;
        arr[1] = bull;
        return arr;
    }


我们正在创建一个辅助2元素数组。第一个是母牛的数量,第二个是公牛的数量。然后,我们遍历两个数组,比较元素。如果元素相等且它们的索引相等,则我们增加公牛的数量,否则我们增加牛的数量。之后,我们将值写入数组并返回它。



现在是时候弄清楚BackEnd类的构造函数中有什么。



public BackEnd() {
        getMas();
        //printMas(mas);
        boolean win = false;
        while (!win) {
            checkInput();
            System.out.println(" :");
            printMas(inputArray);
            System.out.println();
            int[] arr = checkCowAndBull();
            if (arr[1] == length) {
                win = true;
                System.out.println("!  !");
            } else {
                System.out.println("   " + arr[0] + " , " + arr[1] + " ");
                System.out.println("  ");
            }
        }
    }


首先,我们生成预期的数组。我输出它用于调试,但游戏不需要此功能。由于我们不知道用户将通过什么尝试来猜测数字,因此我们开始while循环直到合适的时刻。然后,我们要求用户输入他的编号,显示输入的数组,检查多头和多头牛的数量。如果公牛的数量与数组的长度匹配,则猜测数字,游戏结束。否则,将显示公牛和母牛的数量,然后继续游戏。



剩下的就是将创建类的实例添加到man中:



public class main {
    public static void main(String[] args) {
        BackEnd bk = new BackEnd();
    }
}


我们启动,检查,播放。



感谢您的关注,第二部分即将推出。



All Articles