[問題] java寫猜數字遊戲(幾A幾B那種)

看板PLT (程式語言與理論)作者 (小夫)時間15年前 (2009/12/19 15:54), 編輯推噓0(003)
留言3則, 3人參與, 最新討論串1/1
老師在上課有講解了以下的部分 但說得很快 >"< 要我們說明程式碼及修改以下程式碼 Number Guess Game.java 部分的上色處 for的地方 來完成完整版的4位數猜數字遊戲 1. 可由使用者選擇所欲猜測的數字有幾位 2. 使用者可查詢過去所輸入的猜測 3. 電腦可提供提示 (例如 : 1A2B) 4. 遊戲排行榜 而這個猜數字的遊戲 包含了 Number Guess Game.java / Answer.java 及Main.java三個部分 (主要是修改Number Guess Game.java 部分及在執行時能印出步驟說明 例如: 你猜錯了 繼續加油 ) ------------------------------------- Number Guess Game.java 部分 package numberguessgame; import java.util.Scanner; import java.io.*; import java.util.Vector; import java.util.Random; public class NumberGuessGame { private int numberOfDigits; private boolean isDupDigits=true; int count=1; Answer lastAnswer=null; Vector possibleAnswerSet; NumberGuessGame() { this(4); } NumberGuessGame(int d) { setNumberOfDigits(d); possibleAnswerSet = new Vector(); } public Answer generateAnswer() { Answer temp = new Answer(numberOfDigits); temp.generate(); // System.out.println(temp.answer); return temp; } public void start(Answer answer) { boolean exit=false; Answer userGuess; String outStr=null; while(!exit && (count<100)) { System.out.println("------------------------"); System.out.println("(" + count + ")"); // userGuess = getUserGuess(); userGuess = generateGuess(answer, outStr); outStr = compare(userGuess,answer); System.out.println(outStr); if(outStr.equals("" + this.numberOfDigits + "A0B")) exit=true; count++; } System.out.println("Game Over"); } public Answer generateGuess(Answer answer, String outStr) { Answer temp = new Answer(this.numberOfDigits); System.out.print("Please Input Your Guess:"); if(count==1) { temp = generateAnswer(); lastAnswer = temp; return temp; } else if(count==2) { Answer a = generateAnAnswer(answer, outStr); lastAnswer =a; System.out.println(a.answer); return a; } else { Answer a = generateAnotherAnswer(answer, outStr); lastAnswer = a ; System.out.println(a.answer); return a; } // temp.setAnswer( string ); // return temp; } public String compare(Answer t, Answer a) { int numA = 0, numB = 0, numC = 0; String outStr = ""; for(int k = 0; k < this.numberOfDigits; k++) { if(t.answer.charAt(k) == a.answer.charAt(k)) { numA++; } } for(int i=0; i < this.numberOfDigits; i++) { for(int j=0; j < this.numberOfDigits; j++) { if(t.answer.charAt(i) == a.answer.charAt(j)) { numC++; } } } numB = numC - numA; outStr="" + numA + "A" + numB + "B"; return outStr; } public Answer getUserGuess() { Answer temp = new Answer(this.numberOfDigits); Scanner userinput = new Scanner(System.in); System.out.print("Please Input Your Guess:"); temp.setAnswer(userinput.next()); return temp; } public void setNumberOfDigits(int d) { numberOfDigits = d; } public int getNumberOfDigits() { return numberOfDigits; } public Answer generateAnAnswer(Answer answer, String outStr) { String answerStr=""; for(int w=0;w<10;w++) { for(int x=0;x<10;x++) { for(int y=0;y<10;y++) { for(int z=0;z<10;z++) { answerStr= new String(""+ w + "" + x + "" + y + "" + z); // System.out.println(answer); Answer temp = new Answer(this.numberOfDigits); temp.setAnswer(answerStr); String outStr2 = compare(temp, lastAnswer); if(outStr2.equals(outStr)) { this.possibleAnswerSet.add(temp); } } } } } Random rand =new Random(); Answer temp = (Answer)(possibleAnswerSet.get(rand.nextInt(possibleAnswerSet.size()))); return temp; } public Answer generateAnotherAnswer(Answer answer, String outStr) { String answerStr=""; for(int w=0;w<possibleAnswerSet.size();w++) { Answer temp = (Answer)(possibleAnswerSet.get(w)); String outStr2 = compare(temp, lastAnswer); if(!outStr2.equals(outStr)) { this.possibleAnswerSet.remove(temp); } } System.out.println("size of possible answer set =" + possibleAnswerSet.size()); Random rand =new Random(); Answer temp = (Answer)(possibleAnswerSet.get(rand.nextInt(possibleAnswerSet.size()))); return temp; } } Answer.java 部分 package numberguessgame; import java.lang.String; import java.util.*; public class Answer { public String answer; int numberOfDigits; Answer(int digits) { this.numberOfDigits = digits; } public void setAnswer(String s) { answer = s ; } public void generate() { Random rand =new Random(); int[] ans=new int[numberOfDigits]; int[] buff=new int[numberOfDigits]; answer=""; int i=0; int a=0; while (a < numberOfDigits) { boolean same; do { ans[a]=rand.nextInt(10); same=false; for (int count=0; count<a; count++) { if(ans[a] == buff[count]) { same = true; } } }while(same != false); buff[a]=ans[a]; a++; } for(i=0; i<numberOfDigits; i++) { answer+= new String(""+ ans); } System.out.println("" + answer ); } } Main.java部分 package numberguessgame; public class Main { public static void main(String[] args) { NumberGuessGame theGame = new NumberGuessGame(); // theGame.setNumberOfDigits(3); Answer answer = theGame.generateAnswer(); theGame.start(answer); } } ----------------------------------- 先感謝答題的各位高手~ <(_O_)> 另外,也可以回到我的信箱。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.27.211.56 jeff10159:轉錄至看板 java 12/19 15:57

12/19 19:23, , 1F
念在你在 java 板被砍文,僅 s 處理 XD 你可以改善問法
12/19 19:23, 1F

12/21 08:44, , 2F
版煮大好有愛心♥
12/21 08:44, 2F

01/02 09:49, , 3F
幾A幾B... 想當年我學組合語言的時候也寫過
01/02 09:49, 3F
文章代碼(AID): #1BB8Qmvj (PLT)
文章代碼(AID): #1BB8Qmvj (PLT)