作者jeff10159 (小夫)
看板PLT
标题[问题] java写猜数字游戏(几A几B那种)
时间Sat Dec 19 15:54:22 2009
老师在上课有讲解了以下的部分
但说得很快 >"<
要我们说明程式码及修改以下程式码
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
1F:→ godfat:念在你在 java 板被砍文,仅 s 处理 XD 你可以改善问法 12/19 19:23
2F:→ jaiyalas:版煮大好有爱心♥ 12/21 08:44
3F:→ onlyderek:几A几B... 想当年我学组合语言的时候也写过 01/02 09:49