由于工作的关系,已经不怎么写代码了,最近和同事聊起以前的工作的时候,无意中想起还写过一些Java的小游戏。
想着练练手,就把前两天看到的一个简单的猜数字的小游戏,写成了Java,供大家参考,也供我怀旧怀旧。
游戏规则如下,
- 系统随机生成一个4位数,每个位数之间数字不重复,如1233不可用
- 用户每次猜测一个4位数,系统会根据用户的猜测数字,给出猜对几个数字,猜对几个位置
- 用户一共有10次猜测的机会,超过10次或者10次内猜测正确,游戏结束
梳理一下逻辑,这个游戏的关键,一共有3点
- 4位随机数的生成
- 猜对几个数字的判定
- 猜对几个位置的判定
首先看4位随机数的生成
//preset the number as the number set to guess
public static ArrayList<String> preset(){
Random random = new Random();
int firstIndex = random.nextInt(9) + 1;
int secondIndex = random.nextInt(8);
int thirdIndex = random.nextInt(7);
int fourthIndex = random.nextInt(6);
ArrayList<String> numSet = new ArrayList<String>();
int i;
int j = 0;
for(i = 0;i<10;i++){
numSet.add(i,inttoString(j));
j++;
}
ArrayList<String> preNum = new ArrayList<String>();
preNum.add(0,numSet.get(firstIndex));
numSet.remove(firstIndex);
preNum.add(1,numSet.get(secondIndex));
numSet.remove(secondIndex);
preNum.add(2,numSet.get(thirdIndex));
numSet.remove(thirdIndex);
preNum.add(3,numSet.get(fourthIndex));
numSet.remove(fourthIndex);
//System.out.println(preNum);
/*test part
ArrayList<String> test = new ArrayList<String>();
test.add(0,"1");
test.add(1,"2");
test.add(2,"3");
test.add(3,"4");*/
return preNum;
}
这里利用ArrayList<String> 作为我们4位随机数的容器,由于这里是4位数,所以为了方便,就进行了hard code,如果猜测的位数变多的话,可以考虑利用循环语句进行动态生成。
这里需要注意的是,第一位数字不可为0,不然不符合4位数的定义,所以在随机数生成的时候,需要+1来保证获取的数字是0-9
此外,每个数字只能出现一次,因此选中的数字需要从原有的numSet中移除
完成了四位随机数的生成,下面来看猜中几个数字的判定
//count how many numbers are contained in preset number by compre preset number and playernumber
public static int numberContain(ArrayList<String> playerNum, ArrayList<String>presetNum){
int numContain = 0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(playerNum.get(i).equals(presetNum.get(j))){
//System.out.println(playerNum.get(i));
numContain++;
}
}
}
return numContain;
}
这里我们采用一个2层循环语句,进行数据对比,只要任意的玩家猜测数字等于预设数字,numCatain的计数器就要加一,这里不用在意位置的分布,只要数字包含即可。
完成了猜中几个数字的判定,下面进行猜中位数的判定
public static int positionCheck(ArrayList<String> playerNum, ArrayList<String>presetNum){
int positioncounter = 0;
int j = 0;
for(int i = 0;i<4;i++){
if(playerNum.get(i).equals(presetNum.get(j))){
positioncounter++;
}
j++;
}
return positioncounter;
}
这里有一个比较容易错的逻辑点,j的计数是在if条件之外,但却在循环里面的因为这里是位置确认,所以需要数字一一对应,j需要随着i变化而变化。
至此,小游戏的基本逻辑已经完成,剩下的就是一些帮助函数和运行主函数,包含输入输出的等。
Help Function
/*********************************************
*This part is the help function
*********************************************/
//convert type int to type String
public static String inttoString(int i){
String intNum = String.valueOf(i);
return intNum;
}
//conver type Character to String
public static String chartoString(Character c){
String s = Character.toString(c);
return s;
}
//split the string one by one and convert to arrayList, each arraylist item contain one number(string type)
public static ArrayList<String> stringtoArrayList(String s){
ArrayList<String> playerNum = new ArrayList<String>();
for(int i=0;i<s.length();i++){
playerNum.add(i, chartoString(s.toCharArray()[i]));
}
return playerNum;
}
Game Main & Game Reset
public static void main(String[] args) throws Exception {
System.out.println("猜数字游戏");
System.out.println("****************");
System.out.println("0-9,10个数字,每个数字只会出现一次,每次猜测需完整猜测一个4位数,每次猜测之后,系统会告知猜对数字的个数,以及猜对数字的位数,一共有10次机会");
reset();
System.exit(0);
}
public static void reset(){
Scanner getNumber = new Scanner(System.in);
String numberBox = getNumber.nextLine();
int counter = 1;
ArrayList<String> numberBoxList = stringtoArrayList(numberBox);
ArrayList<String> presetNumberBox = preset();
int countNum = numberContain(numberBoxList,presetNumberBox);
int positionCount = positionCheck(numberBoxList,presetNumberBox);
while(true){
if(counter == 11){
System.out.println("对不起,您已经尝试超过10次,游戏失败");
break;
}else{
if(countNum == 4 && positionCount == 4){
System.out.println("真棒,你在"+counter+"次猜中了数字,数字是" + presetNumberBox);
getNumber.close();
break;
}else{
System.out.println("玩家猜测的数字" + numberBoxList + "; " + "猜中了" + countNum + "个数字; " + "猜中了" + positionCount + "个位置");
counter++;
numberBox = getNumber.nextLine();
numberBoxList = stringtoArrayList(numberBox);
countNum = numberContain(numberBoxList,presetNumberBox);
positionCount = positionCheck(numberBoxList,presetNumberBox);
//System.out.println("玩家猜测的数字" + numberBoxList + "; " + "猜中了" + countNum + "个数字; " + "猜中了" + positionCount + "个位置");
}
}
}
}
此篇文章仅供参考,如需转载,请注明出处,切勿抄袭,谢谢合作!