各位大大晚安~
事情是這樣的我現在有一個1A2B遊戲
但是我怎麼找都找不到BUG的地方 QQ(BUG已解決...現在卡在降不到50次以下~已經想了一個晚上....)
三個程式碼:
------------------
/*
/**
*
@author wayway
*/
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;
}
}
--------------------
/*
/**
*
@author wayway
*/
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);
}
}
可是我看都沒有QQ
老師指定說:
使回合數降到至少50回合以內(寫在AlBullsCows.class裡面的public int [ ] GenerateNumber( ){ },僅能改這個部分,其它的部分是題目預設的,不能改)
但是我還卡在BUG的地方~不知道有沒有大大可以指點...QQ
![https://ithelp.ithome.com.tw/upload/images/20210625/20119035MTuqP8Z0EY.png](https://ithelp.ithome.com.tw/upload/images/20210625/20119035MTuqP8Z0EY.png)
感激不盡
-----------------
重貼之後可以RUN了...
但是...還是不在50回內
![https://ithelp.ithome.com.tw/upload/images/20210625/201190353eIL9FuIPr.png](https://ithelp.ithome.com.tw/upload/images/20210625/201190353eIL9FuIPr.png)
老師最原始的code是
package bullscowsgameplaytest;
/**
*
@author wayway
/
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;
}
}
![https://ithelp.ithome.com.tw/upload/images/20210626/201190357RSaRk0LN4.png](https://ithelp.ithome.com.tw/upload/images/20210626/201190357RSaRk0LN4.png)
public class AIBullsCows extends BasicBullsCows
{
public int [] GenerateInputNumber()
{
int [][]answerBase=new int[5040][4];
//generate input number using random digit
int [] candidateNumber = new int[4];
//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");
}
//**********************************************************************************
int tmp=0;
for(int i=1023;i<9999;i++){ //將1023~9999可能的答案放入answerBase二維矩陣裡
int unit_1 = (i/1)%10; //以下四行把四位數拆成四個各位數分別存到四個變數
int unit_10 = (i/10)%10;
int unit_100 = (i/100)%10;
int unit_1000 = (i/1000)%10;
if(unit_1!=unit_10 && unit_1!=unit_100 && unit_1!=unit_1000 && unit_10!=unit_100 && unit_10!=unit_1000 && unit_100!=unit_1000){//判斷是否重複
answerBase[tmp][0] = unit_1000;
answerBase[tmp][1] = unit_100;
answerBase[tmp][2] = unit_10;
answerBase[tmp][3] = unit_1;
//------------------
int Bull = 0;
int Cow = 0;
boolean bValid = true;
for(int r = 0; r < RoundNumber; r++) {
Bull = 0;
Cow = 0;
for (int m=0; m<4; m++) {
for (int n=0; n<4; n++) {
if (answerBase[tmp][m]==Inputs[r][n]) {
if (m==n) {
Bull = Bull + 1;
} else {
Cow = Cow + 1;
}
}
}
}
if ((Bull==Bulls[r])&&(Cow==Cows[r])) {
//valid answer
} else {
bValid = false;
break;
}
}
if (bValid) {
candidateNumber[0] = answerBase[tmp][0];
candidateNumber[1] = answerBase[tmp][1];
candidateNumber[2] = answerBase[tmp][2];
candidateNumber[3] = answerBase[tmp][3];
break;
}
//------------------
tmp++;
}
}
return candidateNumber;
}
}
大大 我剛剛試著改成
package bullscowsgameplaytest;
/**
*
* @author wayway
*/
public class AIBullsCows extends BasicBullsCows
{
int [][]answerBase=new int[5040][4];
public int [] GenerateInputNumber()
{
//access game record****************************************************************
int RoundNumber = this.GetRoundNumber();
int [][] Inputs = this.GetInputs();
int [] Bulls = this.GetBulls();
int [] Cows = this.GetCows();
int remain=5040;
//tmp只是一個拿來計數變數
int isOver=0; //判斷結束與否
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];
if(RoundNumber==0){//回合數為零
int tmp=0;
for(int i=1023;i<9999;i++){ //將1023~9999可能的答案放入answerBase二維矩陣裡
int unit_1 = (i/1)%10; //以下四行把四位數拆成四個各位數分別存到四個變數
int unit_10 = (i/10)%10;
int unit_100 = (i/100)%10;
int unit_1000 = (i/1000)%10;
if(unit_1!=unit_10 && unit_1!=unit_100 && unit_1!=unit_1000 && unit_10!=unit_100 && unit_10!=unit_1000 && unit_100!=unit_1000){//判斷是否重複
answerBase[tmp][0] = unit_1000;
answerBase[tmp][1] = unit_100;
answerBase[tmp][2] = unit_10;
answerBase[tmp][3] = unit_1;
tmp++;
}};/*for(int i=0;i<=5040;i++){
for(int j=0;j<4;j++){
System.out.println(answerBase[i][j]);
}
}*/
int q=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[q][0];
candidateNumber[1] = answerBase[q][1];
candidateNumber[2] = answerBase[q][2];
candidateNumber[3] = answerBase[q][3];
}
else if(RoundNumber>0){//回合數大於1
boolean passCheck = false;
int y=RoundNumber-1;
while(passCheck==false){
int m=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[m][0];
candidateNumber[1] = answerBase[m][1];
candidateNumber[2] = answerBase[m][2];
candidateNumber[3] = answerBase[m][3];
if(Bulls[y]==0 && Cows[y]==0 ){//如果前一項A為0且B為0,則執行此段程式
for(int i=0;i<5040;i++){
if(answerBase[i][0]==Inputs[y][0] || answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]==Inputs[y][3]||answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][1]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]==Inputs[y][3]||answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]==Inputs[y][2]||answerBase[i][2]==Inputs[y][3]||answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]==Inputs[y][2]||answerBase[i][3]==Inputs[y][3])//answerBase答案庫裡只要有任何一個數和前一項相同,就把它等於0000
{
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int s=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[s][0];
candidateNumber[1] = answerBase[s][1];
candidateNumber[2] = answerBase[s][2];
candidateNumber[3] = answerBase[s][3];
}
int coun=0;
if(Cows[y]==4){//如果前一項B為4,則執行此段程式
for(int i=0;i<5040;i++){
if( answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]!=Inputs[y][3])//answerBase答案庫裡只要有任何一個數和前一項不同位置的值相等,則Coun++
{
coun++;}
if( answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]!=Inputs[y][3]){
coun++;}
if( answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]!=Inputs[y][3]){
coun++;}
if( answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]!=Inputs[y][2]){
coun++;}
if(coun==0){//當coun=0,1,2,3時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun==1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun==2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun==3){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int w=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[w][0];
candidateNumber[1] = answerBase[w][1];
candidateNumber[2] = answerBase[w][2];
candidateNumber[3] = answerBase[w][3];
} int count=0;
if(Bulls[y]==3){//如果前一項A為3,則執行此段程式
for(int i=0;i<5040;i++){
if(answerBase[i][0]==Inputs[y][0]) {//answerBase答案庫裡一個元素裡的值和前一項相同位置的值相等,則Count++
count++;
}
if(answerBase[i][1]==Inputs[y][1]){
count++;
}
if(answerBase[i][2]==Inputs[y][2]){
count++;
}
if(answerBase[i][3]==Inputs[y][3]){
count++;
}
if(count==0){//當count=0,1,2時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
} if(count==1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(count==2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}
int n=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[n][0];
candidateNumber[1] = answerBase[n][1];
candidateNumber[2] = answerBase[n][2];
candidateNumber[3] = answerBase[n][3];
}
int countt=0;
if(Cows[y]==3){//如果前一項B為4,則執行此段程式
for(int i=0;i<5040;i++){
if( answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]!=Inputs[y][3]){//answerBase答案庫裡只要有任何一個數和前一項不同位置的值相等,則Countt++
countt++;}
if( answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]!=Inputs[y][3]){
countt++;}
if( answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]!=Inputs[y][3]){
countt++;}
if( answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]!=Inputs[y][2]){
countt++;}
if(countt!=0){//當count=0,1,2時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(countt!=1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(countt!=2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int w=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[w][0];
candidateNumber[1] = answerBase[w][1];
candidateNumber[2] = answerBase[w][2];
candidateNumber[3] = answerBase[w][3];
}
passCheck = CheckInputNumber(candidateNumber);}
}
return candidateNumber;
}
}
結果都run不出來~還在跑~我是不是搞錯了..qq
改成
package bullscowsgameplaytest;
/**
*
* @author wayway
*/
public class AIBullsCows extends BasicBullsCows
{
int [][]answerBase=new int[5040][4];
public int [] GenerateInputNumber()
{
//access game record****************************************************************
int RoundNumber = this.GetRoundNumber();
int [][] Inputs = this.GetInputs();
int [] Bulls = this.GetBulls();
int [] Cows = this.GetCows();
int remain=5040;
//tmp只是一個拿來計數變數
int isOver=0; //判斷結束與否
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];
if(RoundNumber==0){//回合數為零
int tmp=0;
for(int i=1023;i<9999;i++){ //將1023~9999可能的答案放入answerBase二維矩陣裡
int unit_1 = (i/1)%10; //以下四行把四位數拆成四個各位數分別存到四個變數
int unit_10 = (i/10)%10;
int unit_100 = (i/100)%10;
int unit_1000 = (i/1000)%10;
if(unit_1!=unit_10 && unit_1!=unit_100 && unit_1!=unit_1000 && unit_10!=unit_100 && unit_10!=unit_1000 && unit_100!=unit_1000){//判斷是否重複
answerBase[tmp][0] = unit_1000;
answerBase[tmp][1] = unit_100;
answerBase[tmp][2] = unit_10;
answerBase[tmp][3] = unit_1;
tmp++;
}};/*for(int i=0;i<=5040;i++){
for(int j=0;j<4;j++){
System.out.println(answerBase[i][j]);
}
}*/
int q=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[q][0];
candidateNumber[1] = answerBase[q][1];
candidateNumber[2] = answerBase[q][2];
candidateNumber[3] = answerBase[q][3];
}
else if(RoundNumber>0){//回合數大於1
boolean passCheck = false;
int y=RoundNumber-1;
while(passCheck==false){
int m=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[m][0];
candidateNumber[1] = answerBase[m][1];
candidateNumber[2] = answerBase[m][2];
candidateNumber[3] = answerBase[m][3];
if(Bulls[y]!=0 && Cows[y]!=0 ){//如果前一項A為0且B為0,則執行此段程式
for(int i=0;i<5040;i++){
if(answerBase[i][0]==Inputs[y][0] || answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]==Inputs[y][3]||answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][1]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]==Inputs[y][3]||answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]==Inputs[y][2]||answerBase[i][2]==Inputs[y][3]||answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]==Inputs[y][2]||answerBase[i][3]==Inputs[y][3])//answerBase答案庫裡只要有任何一個數和前一項相同,就把它等於0000
{
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int s=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[s][0];
candidateNumber[1] = answerBase[s][1];
candidateNumber[2] = answerBase[s][2];
candidateNumber[3] = answerBase[s][3];
}
int coun=0;
if(Cows[y]!=4){//如果前一項B為4,則執行此段程式
for(int i=0;i<5040;i++){
if( answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]!=Inputs[y][3])//answerBase答案庫裡只要有任何一個數和前一項不同位置的值相等,則Coun++
{
coun++;}
if( answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]!=Inputs[y][3]){
coun++;}
if( answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]!=Inputs[y][3]){
coun++;}
if( answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]!=Inputs[y][2]){
coun++;}
if(coun!=0){//當coun=0,1,2,3時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun!=1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun!=2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun!=3){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int w=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[w][0];
candidateNumber[1] = answerBase[w][1];
candidateNumber[2] = answerBase[w][2];
candidateNumber[3] = answerBase[w][3];
} int count=0;
if(Bulls[y]!=3){//如果前一項A為3,則執行此段程式
for(int i=0;i<5040;i++){
if(answerBase[i][0]==Inputs[y][0]) {//answerBase答案庫裡一個元素裡的值和前一項相同位置的值相等,則Count++
count++;
}
if(answerBase[i][1]==Inputs[y][1]){
count++;
}
if(answerBase[i][2]==Inputs[y][2]){
count++;
}
if(answerBase[i][3]==Inputs[y][3]){
count++;
}
if(count!=0){//當count=0,1,2時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
} if(count!=1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(count!=2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}
int n=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[n][0];
candidateNumber[1] = answerBase[n][1];
candidateNumber[2] = answerBase[n][2];
candidateNumber[3] = answerBase[n][3];
}
int countt=0;
if(Cows[y]!=3){//如果前一項B為4,則執行此段程式
for(int i=0;i<5040;i++){
if( answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]!=Inputs[y][3]){//answerBase答案庫裡只要有任何一個數和前一項不同位置的值相等,則Countt++
countt++;}
if( answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]!=Inputs[y][3]){
countt++;}
if( answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]!=Inputs[y][3]){
countt++;}
if( answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]!=Inputs[y][2]){
countt++;}
if(countt!=0){//當count=0,1,2時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(countt!=1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(countt!=2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int w=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[w][0];
candidateNumber[1] = answerBase[w][1];
candidateNumber[2] = answerBase[w][2];
candidateNumber[3] = answerBase[w][3];
}
passCheck = CheckInputNumber(candidateNumber);}
}
return candidateNumber;
}
}
也是跑很久跑不出來qq
改成這樣也是run很久...今天回應的次數該不會要沒了
package bullscowsgameplaytest;
/**
*
* @author wayway
*/
public class AIBullsCows extends BasicBullsCows
{
int [][]answerBase=new int[5040][4];
public int [] GenerateInputNumber()
{
//access game record****************************************************************
int RoundNumber = this.GetRoundNumber();
int [][] Inputs = this.GetInputs();
int [] Bulls = this.GetBulls();
int [] Cows = this.GetCows();
int remain=5040;
//tmp只是一個拿來計數變數
int isOver=0; //判斷結束與否
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];
if(RoundNumber==0){//回合數為零
int tmp=0;
for(int i=1023;i<9999;i++){ //將1023~9999可能的答案放入answerBase二維矩陣裡
int unit_1 = (i/1)%10; //以下四行把四位數拆成四個各位數分別存到四個變數
int unit_10 = (i/10)%10;
int unit_100 = (i/100)%10;
int unit_1000 = (i/1000)%10;
if(unit_1!=unit_10 && unit_1!=unit_100 && unit_1!=unit_1000 && unit_10!=unit_100 && unit_10!=unit_1000 && unit_100!=unit_1000){//判斷是否重複
answerBase[tmp][0] = unit_1000;
answerBase[tmp][1] = unit_100;
answerBase[tmp][2] = unit_10;
answerBase[tmp][3] = unit_1;
tmp++;
}};/*for(int i=0;i<=5040;i++){
for(int j=0;j<4;j++){
System.out.println(answerBase[i][j]);
}
}*/
int q=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[q][0];
candidateNumber[1] = answerBase[q][1];
candidateNumber[2] = answerBase[q][2];
candidateNumber[3] = answerBase[q][3];
}
else if(RoundNumber>0){//回合數大於1
boolean passCheck = false;
int y=RoundNumber-1;
while(passCheck==false){
int m=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[m][0];
candidateNumber[1] = answerBase[m][1];
candidateNumber[2] = answerBase[m][2];
candidateNumber[3] = answerBase[m][3];
if(Bulls[y]!=0 && Cows[y]!=0 ){//如果前一項A為0且B為0,則執行此段程式
for(int i=0;i<5040;i++){
if(answerBase[i][0]==Inputs[y][0] || answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]==Inputs[y][3]||answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][1]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]==Inputs[y][3]||answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]==Inputs[y][2]||answerBase[i][2]==Inputs[y][3]||answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]==Inputs[y][2]||answerBase[i][3]==Inputs[y][3])//answerBase答案庫裡只要有任何一個數和前一項相同,就把它等於0000
{
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int s=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[s][0];
candidateNumber[1] = answerBase[s][1];
candidateNumber[2] = answerBase[s][2];
candidateNumber[3] = answerBase[s][3];
}
int coun=0;
if(Cows[y]!=4){//如果前一項B為4,則執行此段程式
for(int i=0;i<5040;i++){
if( answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]!=Inputs[y][3])//answerBase答案庫裡只要有任何一個數和前一項不同位置的值相等,則Coun++
{
coun++;}
if( answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]!=Inputs[y][3]){
coun++;}
if( answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]!=Inputs[y][3]){
coun++;}
if( answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]!=Inputs[y][2]){
coun++;}
if(coun!=0){//當coun=0,1,2,3時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun!=1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun!=2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(coun!=3){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int w=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[w][0];
candidateNumber[1] = answerBase[w][1];
candidateNumber[2] = answerBase[w][2];
candidateNumber[3] = answerBase[w][3];
} int count=0;
if(Bulls[y]!=4){//如果前一項A不為4,則執行此段程式
for(int i=0;i<5040;i++){
if(answerBase[i][0]==Inputs[y][0]) {//answerBase答案庫裡一個元素裡的值和前一項相同位置的值相等,則Count++
count++;
}
if(answerBase[i][1]==Inputs[y][1]){
count++;
}
if(answerBase[i][2]==Inputs[y][2]){
count++;
}
if(answerBase[i][3]==Inputs[y][3]){
count++;
}
if(count!=0){//當count=0,1,2時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
} if(count!=1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(count!=2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}
int n=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[n][0];
candidateNumber[1] = answerBase[n][1];
candidateNumber[2] = answerBase[n][2];
candidateNumber[3] = answerBase[n][3];
}
int countt=0;
if(Cows[y]!=4){//如果前一項B不為4,則執行此段程式
for(int i=0;i<5040;i++){
if( answerBase[i][0]==Inputs[y][1]||answerBase[i][0]==Inputs[y][2]||answerBase[i][0]!=Inputs[y][3]){//answerBase答案庫裡只要有任何一個數和前一項不同位置的值相等,則Countt++
countt++;}
if( answerBase[i][1]==Inputs[y][0]||answerBase[i][1]==Inputs[y][2]||answerBase[i][1]!=Inputs[y][3]){
countt++;}
if( answerBase[i][2]==Inputs[y][0]||answerBase[i][2]==Inputs[y][1]||answerBase[i][2]!=Inputs[y][3]){
countt++;}
if( answerBase[i][3]==Inputs[y][0]||answerBase[i][3]==Inputs[y][1]||answerBase[i][3]!=Inputs[y][2]){
countt++;}
if(countt!=0){//當count=0,1,2時,answerBase答案庫裡那個數,等於0000
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(countt!=1){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
if(countt!=2){
answerBase[i][0]=0;
answerBase[i][1]=0;
answerBase[i][2]=0;
answerBase[i][3]=0;
}
}int w=(int)(Math.random()*5040);
candidateNumber[0] = answerBase[w][0];
candidateNumber[1] = answerBase[w][1];
candidateNumber[2] = answerBase[w][2];
candidateNumber[3] = answerBase[w][3];
}
passCheck = CheckInputNumber(candidateNumber);}
}
return candidateNumber;
}
}
老師最原始的code是
package bullscowsgameplaytest;
/**
*
* @author wayway
*/
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;
}
}
我寫了一個版本如答案
這個CheckInputNumber怎麼感覺有點笨...
所以是要寫一個程式去猜結果?
看起來4個數字是不重複的(跟我原本的印象一樣),
如果是用人來猜的話,
我記得平均大約10次就可以解出來.
因為老師說只能改寫在AlBullsCows.class裡面的public int [ ] GenerateNumber( ){ },僅能改這個部分,其它的部分是題目預設的,不能改..qq
那個GenerateInputNumber也有點奇怪,
為什麼看起來像是先寫數字,
然後最後才產生答案??
這是老師最原始的code~
package bullscowsgameplaytest;
/**
*
* @author wayway
*/
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;
}
}
已經想了一個晚上都想不出來~
誰可以救救我~QQ
所以我才說奇怪,
照理說應該是先產生4個數字,
然後才去猜數字吧...
一開始的Code執行起來正常嗎?
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];
// 代入答案清單, 找到可用的dummy數字, 如果沒有就用自己
// 在這個階段, Bulls, Cows, 的1-10筆(length)資料會被視為是推算dummy的過程, 長度超過或加起來不是0A0B = 猜dummy失敗
int dummyNumber = 0;
while( dummyNumber < RoundNumber && (Bulls[dummyNumber]+ Cows[dummyNumber])> 0 ){
dummyNumber++;
};
// 還未找到dummy, 猜dummy 先
if(dummyNumber >= RoundNumber){
return new int[]{dummyNumber,dummyNumber,dummyNumber,dummyNumber};
}
System.out.println("dummyGet:" + dummyNumber);
// 能走到這邊, 代表dummy的數字已經是決定的狀態, 由0->9 一路猜題, Bulls每進一格就往後走
// 當前的狀態可能正猜到一半, 或剛猜玩前一個位數
// 找最近的一次猜題結果, 取得當前進度, 譬如說 如果dummy 是數字D
// 那上一次猜題可能是 1 D D D
// 如果回傳 1A, 那下一次就改猜 1 0 D D, 1 1 D D, ..., 直到猜到2A
// Bulls[RoundNumber-1]決定當前猜到哪位數
int increaseNumber = 0;
for(int i = 0; i<= 3; i++){
if(Bulls[RoundNumber-1]>i){ // 如果是已猜到的, 延用
candidateNumber[i] = Inputs[RoundNumber-1][i];
}else if(Bulls[RoundNumber-1]<i){ // 不是在猜的項目 補dummy就好
candidateNumber[i] = dummyNumber;
}else{
if(dummyNumber == RoundNumber-1){// 剛取得dummy, 該從0開始
increaseNumber = 0;
}else if(Bulls[RoundNumber-1] > Bulls[RoundNumber-2] ){ // 剛進位, 該從0開始
increaseNumber = 0;
}else{ // 前一次的數字+1
increaseNumber = Inputs[RoundNumber-1][i]+1;
}
// 避免dummy再次被拿去猜題
if(increaseNumber==dummyNumber) increaseNumber++;
candidateNumber[i] = increaseNumber;
}
}
return candidateNumber;
}
我把我說的方法實作完了, 你參考看看
其實不太需要管你老師寫的其他部份, 你需要思考的是如何利用現有的3個array去推算已知的訊息, 當然這不是好的解答, 因為我完全沒用到B的相關訊息, 給你作個參考
本堂課助教到此一遊...可以偷偷跟我說你是哪一位同學嗎?