我怎么会遇到循环我的Mastermind游戏

时间:2015-06-02 13:07:42

标签: java bluej

我如何遇到循环我的Mastermind游戏,以便它可以播放10次,如果用户在10轮内没有猜到它就会结束,但如果他们确实猜到了程序结束而他们就赢了。我知道我可以使用do while语句,但这将需要很多,我不知道我会如何遇到它。

  // The purpose is to win a game of Mastermind against a computer player.
// 5/26/15
// Matthew Soutar

import java.util.*;
import java.awt.*;
import java.io.*;

    class Mastermind 
    {
        public static void main (String[] args)
        {
            //Welcome
            System.out.println ("Welcome to Matthew's game of Mastermind!");
            int[] Secret_code = Mystery();
            int[] Gameboard = Gameboard (Secret_code);
            int resultCount = Comparing (Secret_code, Gameboard);
            int resultCount2 = Comparing2 (Secret_code, Gameboard);
            //Goodbye
            System.out.println ("Thank you for playing.");            
            //Winner
            if (Gameboard[0] == Secret_code[0] && Gameboard[1] == Secret_code[1] && Gameboard[2] == Secret_code[2] && Gameboard[3] == Secret_code[3])
            System.out.println ("You have won this game of Mastermind!");
        }


        public static int[] Mystery () {

            //Random
            int secretcode = (int)(6*Math.random()) + 1;
            int secretcode1 = (int)(6*Math.random()) + 1;
            int secretcode2 = (int)(6*Math.random()) + 1;
            int secretcode3 = (int)(6*Math.random()) + 1;

           //Secretcode print
           System.out.println (secretcode);
           System.out.println (secretcode1);
           System.out.println (secretcode2);
           System.out.println (secretcode3);

           //Array
           int [] Secret_code;
           Secret_code = new int [4];
           Secret_code[0] = secretcode;
           Secret_code[1] = secretcode1;
           Secret_code[2] = secretcode2;
           Secret_code[3] = secretcode3;

            return Secret_code;
    }
        public static int[] Gameboard (int[] Secret_code) {
        //Variable Declaration
        int guess1, guess2, guess3, guess4;
        Scanner Guess = new Scanner (System.in);

        //User Guess's
         System.out.println ("What is the number one peg in my code?");
         guess1 = Guess.nextInt();
         System.out.println ("What is the number two peg in my code?");
         guess2 = Guess.nextInt();
         System.out.println ("What is the number three peg in my code?");
         guess3 = Guess.nextInt();
         System.out.println ("What is the number four peg in my code?");
         guess4 = Guess.nextInt();

         //Array
         int [] Gameboard;
         Gameboard = new int [4];
         Gameboard[0] = guess1;
         Gameboard[1] = guess2;
         Gameboard[2] = guess3;
         Gameboard[3] = guess4;

         return Gameboard;
}
        public static int Comparing (int[] Secret_code, int[] Gameboard){
         int resultCount = 0;
         for (int i = 0; i < 4; i++) {
         if (Gameboard[i] == Secret_code[i])
          resultCount++;
            }
    System.out.println("You got " + resultCount + " correct in the right spot."); 
    return resultCount;
    }
    public static int Comparing2 (int[] Secret_code, int[] Gameboard){
              int resultCount2 = 0;
              int i = 0;
              for (int e = 0; e < 4; e++) {
                if (Gameboard[e] == Secret_code[i])
                 resultCount2++;
                }
                System.out.println ("You got " + resultCount2 + " correct but in the wrong spot");
                return resultCount2;
    }
    }

1 个答案:

答案 0 :(得分:0)

看到这个;可能是你正在寻找的东西:

import java.util.*;

public class Mastermind {

    static int numAttempts = 10, numGuess = 4;

    public static void main(String[] args) {

        System.out.println("Welcome to Matthew's game of Mastermind! Guess : "
                + pw(numGuess) + " random numbers");

        int[] Secret_code = Mystery();
        Scanner Guess = new Scanner(System.in);

        while (numAttempts-- > 0) {
            int[] Gameboard = Gameboard(Guess);

            int resultCount = Comparing(Secret_code, Gameboard);

            if (resultCount == numGuess) {
                System.out.println("You have won this game of Mastermind!");
                break;
            } else
                compareWrongSpot(Secret_code, Gameboard);

            System.out.println();
        }

        System.out.println("Thank you for playing.");

        Guess.close();
    }

    public static int[] Mystery() {

        int[] Secret_code = new int[numGuess];

        for (int i = 0; i < numGuess; i++) {
            Secret_code[i] = (int) (6 * Math.random()) + 1;
            System.out.println(Secret_code[i]);
        }

        return Secret_code;
    }

    public static int[] Gameboard(Scanner Guess) {

        int[] Gameboard = new int[numGuess];

        for (int i = 1; i <= numGuess; i++) {
            System.out.println("What is the number " + pw(i)
                    + " peg in my code?");
            Gameboard[i - 1] = Guess.nextInt();
        }

        return Gameboard;
    }

    public static String pw(int n) {
        String one[] = { " ", "one", "two", "three", "four", "five", "six",
                "seven", "eight", "Nine", "ten", "eleven", "twelve",
                "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
                "eighteen", "nineteen" };

        return one[n];
    }

    public static int Comparing(int[] Secret_code, int[] Gameboard) {
        int resultCount = 0;

        for (int i = 0; i < numGuess; i++)
            if (Gameboard[i] == Secret_code[i])
                resultCount++;

        System.out.println("You got " + resultCount
                + " correct in the right spot.");
        return resultCount;
    }

    public static int compareWrongSpot(int[] Secret_code, int[] Gameboard) {
        int resultCount = 0;

        for (int i = 0; i < numGuess; i++)
            for (int j = 0; j < numGuess; j++)
                if (Gameboard[i] == Secret_code[j])
                    resultCount++;

        System.out.println("You got " + resultCount
                + " correct but in the wrong spot");
        return resultCount;
    }
}