Java猜数字的问題

各位好,不好意思,想請教各位,这游戏和自己输入猜数字的那种不同。

想问要如何寫出能产生每回合的猜题数字且缩短平均猜题数的程式码。

以下是程式碼,一共有三個資料夾,前二個不能修改,第三個可,拜托大家帮忙谢谢!

 我有几个想法是

1.如果猜到0A0B就把那四个数字删掉(不在使用)

2.如猜到0A4B就用这四个数字去排列

/*第一个资料夹 (主程式)*/

public class BullsCowsGamePlayTest
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        AIBullsCows ABC = new AIBullsCows();
        
        int GameTestTimes = 1000;
        
        int OneGameRounds = 0, SumOfRounds = 0;
        for(int i = 1; i <= GameTestTimes; i++)
        {
            OneGameRounds = ABC.RunNewGame();
            SumOfRounds = SumOfRounds + OneGameRounds;
        }
        
        float AverageRound = (float)SumOfRounds / (float)GameTestTimes;
        
        System.out.println("The average rounds of your Bulls and Cows game = "
                         + AverageRound);
    }
}

/*第二个资料夹*/

public abstract class BasicBullsCows
{
    //the total round number
    int RoundNumber;
    //the input number (Inputs[i][0~3]) for each round
    int [][] Inputs;
    //the bulls results for each round
    int [] Bulls;
    //the cows results for each round
    int [] Cows;
    
    public BasicBullsCows()
    {
        this.RoundNumber = 0;
        this.Inputs = new int[100000][4];
        this.Bulls = new int[100000];
        this.Cows = new int[100000];
    }
    
    public abstract int [] GenerateInputNumber();

    public int GetRoundNumber()
    {
        return this.RoundNumber;
    }
    
    public int [][] GetInputs()
    {
        return this.Inputs;
    }
    
    public int [] GetBulls()
    {
        return this.Bulls;
    }
    
    public int [] GetCows()
    {
        return this.Cows;
    }
    
    public boolean CheckInputNumber(int [] InputNumber)
    {
        //1.check input string length
        boolean lengthCheck;

        if(InputNumber.length == 4)
            lengthCheck = true;
        else
            lengthCheck = false;
        
        //2.check whether the four numbers are between 1~9(1st) or 0~9(others)
        boolean digitCheck;
        
        int numberOfDigit = 0;
        
        if( (InputNumber[0]>=1) && (InputNumber[0]<=9) )
            numberOfDigit++;
        
        for(int i = 1; i <= 3; i++)
        {
            if( (InputNumber[i]>=0) && (InputNumber[i]<=9) )
                numberOfDigit++;
        }
        
        if(numberOfDigit == 4)
            digitCheck = true;
        else
            digitCheck = false;
        
        //3.check whether the four digits are different
        boolean differentCheck;
        
        int numberOfDifferent = 0;
        //check the first Input Number digit
        int count = 0;
        for(int j = 0; j <= 3; j++)
        {
            if( InputNumber[0] != InputNumber[j] )
                count++;
        }
        if(count == 3)
            numberOfDifferent++;

        //check the 2nd Input Number digit
        count = 0;
        for(int j = 0; j <= 3; j++)
        {
            if( InputNumber[1] != InputNumber[j] )
                count++;
        }
        if(count == 3)
            numberOfDifferent++;

        //check the 3rd Input Number digit
        count = 0;
        for(int j = 0; j <= 3; j++)
        {
            if( InputNumber[2] != InputNumber[j] )
                count++;
        }
        if(count == 3)
            numberOfDifferent++;

        //check the fourth Input Number digit
        count = 0;
        for(int j = 0; j <= 3; j++)
        {
            if( InputNumber[3] != InputNumber[j] )
                count++;
        }
        if(count == 3)
            numberOfDifferent++;
        
        if(numberOfDifferent == 4)
            differentCheck = true;
        else
            differentCheck = false;

        //4.return check result
        if(lengthCheck && digitCheck && differentCheck)
            return true;
        else
            return false;
    }
    
    public int RunNewGame()
    {
        //Generate true number
        int [] TrueNumber = GenerateTrueNumber();
        
        //start game from round 0
        this.RoundNumber = 0;   //total round number
        boolean endGame = false;
        while(endGame == false)
        {
            //generate input number
            int [] InputNumber = GenerateInputNumber();

            //match result at current round
            endGame = RoundMatchResult(this.RoundNumber, InputNumber, TrueNumber);
            
            this.RoundNumber++;
            
            if(this.RoundNumber >= 100000)  break;
        }

        return this.RoundNumber;
    }
    
    private int [] GenerateTrueNumber()
    {
        int [] candidateNumber = new int[4];

        boolean passCheck = false;
        while(passCheck == false)
        {
            candidateNumber[0] = (int)(Math.random()*9);
            candidateNumber[1] = (int)(Math.random()*9);
            candidateNumber[2] = (int)(Math.random()*9);
            candidateNumber[3] = (int)(Math.random()*9);
            
            passCheck = CheckInputNumber(candidateNumber);
        }

        return candidateNumber;
    }
    
    private boolean RoundMatchResult(int Round, int [] InputNumber, int [] TrueNumber)
    {
        //assign InputNumber to Inputs
        this.Inputs[Round] = InputNumber;
        
        //reset bull and cow for RoundNumber round
        this.Bulls[Round] = 0;
        this.Cows[Round] = 0;
        
        //match first InputNumber digit
        for(int i = 0; i <= 3; i++)
        {
            if( InputNumber[0] == TrueNumber[i] )
            {
                if(i == 0)
                    this.Bulls[Round]++;
                else
                    this.Cows[Round]++;
                
                break;
            }
        }
        
        //match 2nd InputNumber digit
        for(int i = 0; i <= 3; i++)
        {
            if( InputNumber[1] == TrueNumber[i] )
            {
                if(i == 1)
                    this.Bulls[Round]++;
                else
                    this.Cows[Round]++;
                
                break;
            }
        }
        
        //match 3rd InputNumber digit
        for(int i = 0; i <= 3; i++)
        {
            if( InputNumber[2] == TrueNumber[i] )
            {
                if(i == 2)
                    this.Bulls[Round]++;
                else
                    this.Cows[Round]++;
                
                break;
            }
        }
        
        //match 4th InputNumber digit
        for(int i = 0; i <= 3; i++)
        {
            if( InputNumber[3] == TrueNumber[i] )
            {
                if(i == 3)
                    this.Bulls[Round]++;
                else
                    this.Cows[Round]++;
                
                break;
            }
        }
        
        //if result is 4A0B, return true
        if(this.Bulls[Round] == 4)
            return true;
        else
            return false;
    }
}

/*第三个资料夹*/

public class AIBullsCows extends BasicBullsCows
{
    public int [] GenerateInputNumber()
    {
        //access game record****************************************************************
        int RoundNumber = this.GetRoundNumber();
        int [][] Inputs = this.GetInputs();
        int [] Bulls = this.GetBulls();
        int [] Cows = this.GetCows();
        
        System.out.println();
        for(int i = 0; i < RoundNumber; i++)
        {
           System.out.print("Round " + i + ": "
                    + Inputs[i][0] + Inputs[i][1] + Inputs[i][2] + Inputs[i][3]
                   + " " + Bulls[i] + "A" + Cows[i] + "B\n");
        } 
      

        //generate input number using random digit
      
        int [] candidateNumber = new int[4];

        boolean passCheck = false;
        while(passCheck == false)
        {
            candidateNumber[0] = (int)(Math.random()*9);
            candidateNumber[1] = (int)(Math.random()*9);
            candidateNumber[2] = (int)(Math.random()*9);
            candidateNumber[3] = (int)(Math.random()*9);
            
            passCheck = CheckInputNumber(candidateNumber);
        }

        return candidateNumber;
    }
        
        
    }

 

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^