【原创Android游戏】--猜数字游戏Version 0.1

2023-02-12,,,,

想当年高中时经常和小伙伴在纸上或者黑板上或者学习机上玩猜数字游戏,在当年那个手机等娱乐设备在我们那还不是很普遍的时候是很好的一个消遣的游戏,去年的时候便写了一个Android版的猜数字游戏,只是当时没写完,最近又拿出来改了一下,完善了一些功能,修正了很多bug,终于将V0.1版做了出来,现贴出来分享一下。

鉴于居然有很多人都不会玩这个游戏,我还是简单介绍下规则吧:

猜数字: 系统每次随机产生一个4位数字,4位数的每一位都不相同,且都为0~9,然后你有5次机会去猜出那个数字,每次你猜一个4位数,都会得到一个反馈,反馈以 nAmB 的形式给出,有n个'A'表示你猜的数的4位中与答案有n位是完全相同的,但是你可能不知道这n个完全相同的位是那几位,有m个'B'表示你猜的数字与答案有共同的m个数,但是你猜的那m个数位置是错的。

比如:答案为1234,你猜的是2184,那么会返回1A2B,说明4是公共的且位置放对了,1和2是公共的,但是位置不对。

然后根据提示,经过自己的分析推理,尽可能快地猜出答案。

本游戏由4个数位,9个数码(1~9)组成。

-----------------------------------------规则介绍完毕------------------------------------------------

软件组件:

一、1个文本输入框EditText,用来输入数字,限制使用数字输入法

二、3个Button:

1.确定: 输入猜的数字后按确定提交获得返回结果

2.继续:进行一轮后继续下一轮,所有输出内容全部清空,重新产生随机数

3.退出:退出程序

三、6个TextView : 5个用来显示反馈(最多5次),1个用来显示正确答案。

界面设计:

布局XML程序:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="@string/input"
/> <EditText
android:id="@+id/guessed"
android:hint = "@string/hint"
android:numeric="integer"
android:digits="1234567890"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:ems="15" /> <Button
android:id="@+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/guessed"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="@string/begin" /> <TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/guessed"
android:layout_below="@+id/enter"
android:textSize="18sp"
android:layout_marginTop="17dp"
android:ems="10" /> <TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/TextView01"
android:layout_below="@+id/TextView01"
android:ems="10" /> <TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/TextView02"
android:layout_below="@+id/TextView02"
android:ems="10" /> <TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/TextView03"
android:layout_below="@+id/TextView03"
android:ems="10" /> <TextView
android:id="@+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/TextView04"
android:layout_below="@+id/TextView04"
android:ems="10" /> <TextView
android:id="@+id/ANSTEXT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_alignLeft="@+id/TextView05"
android:layout_below="@+id/TextView05"
android:ems="10" /> <Button
android:id="@+id/continuebut"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/cont" /> <Button
android:id="@+id/quitbut"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/ANSTEXT"
android:layout_alignTop="@+id/continuebut"
android:text="@string/quit" /> </RelativeLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">猜数字游戏V0.1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="input">请猜数字:</string>
<string name="begin">确定</string>
<string name="hint">请输入四位数字</string>
<string name="cont">继续</string>
<string name="quit">退出</string> </resources>

逻辑设计:

1.“确定”按钮监听确定时间,

.当enter被按下时,取得输入的数字,

.判断是否为4位,是否有两个数字相同,是否包含0,是否有非数字等,

....如果是则显示错误信息,

....否则正常处理,首先看是否猜中,

.如果猜中,直接输出,

....然后小于2次说明很厉害(运气好),

.否则计算nAmB,输出,

.同时Count加1,

.猜中了或者Count到达5次还没猜中,

....则将enter设为不可点击,

....然后公布正确答案。

....等待“继续”或“退出”按钮的按下。

2.“继续”按钮被按下后,

.清空所有TextView,

.初始化变量,

.重新产生随机数,

.设置enter为可点击。

3.“退出”按钮被按下后,调用finish()函数结束游戏。

Java主代码:

package com.example.guessit;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity; public class MainActivity extends Activity
{
int Count = 0,AA,BB,k,base,NUM;
int [] Tag = new int[11];
char [] ss = new char[5];
String Answer;
boolean flag = false, Double = false, Not4Digit = false, NotDigit = false, haveZero = false, haveNonDigit = false;
String GenerateRandomNumber()
{
for(int t=1;t<=9;t++) Tag[t]=0;
Tag[0]=20;
int [] Rn = new int[5];
Rn[0] = 1 + ((int)(Math.random()*8))%10;
while(Tag[Rn[0]]>0) Rn[0]= 1 + ((int)(Math.random()*8))%10;
Tag[Rn[0]]++;
Rn[1] = 1 + ((int)(Math.random()*8))%10; while(Tag[Rn[1]]>0) Rn[1]= 1 + ((int)(Math.random()*8))%10;
Tag[Rn[1]]++;
Rn[2] = 1 + ((int)(Math.random()*8))%10; while(Tag[Rn[2]]>0) Rn[2]= 1 + ((int)(Math.random()*8))%10;
Tag[Rn[2]]++;
Rn[3] = 1 + ((int)(Math.random()*8))%10; while(Tag[Rn[3]]>0) Rn[3]= 1 + ((int)(Math.random()*8))%10;
Tag[Rn[3]]++; //can be ignored base = 1;
NUM = 0;
for(int i=3;i>=0;i--)
{
NUM += base*Rn[i];
base *= 10;
}
return String.valueOf(NUM);
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Answer = GenerateRandomNumber();
final Button enter = (Button) findViewById(R.id.enter);
final Button quit = (Button) findViewById(R.id.quitbut);
final Button cont = (Button) findViewById(R.id.continuebut);
quit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
flag = false;
Count = 0;
final TextView []show = new TextView [7];
final TextView ANS = (TextView) findViewById(R.id.ANSTEXT);
final EditText pass = (EditText) findViewById(R.id.guessed);
pass.setInputType(EditorInfo.TYPE_CLASS_PHONE);
show[1] = (TextView) findViewById(R.id.TextView01);
show[2] = (TextView) findViewById(R.id.TextView02);
show[3] = (TextView) findViewById(R.id.TextView03);
show[4] = (TextView) findViewById(R.id.TextView04);
show[5] = (TextView) findViewById(R.id.TextView05);
cont.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Answer = GenerateRandomNumber();
for(int i=1;i<=5;i++) show[i].setText("");
ANS.setText("");
pass.setText("");
flag = false;
Count = 0;
enter.setClickable(true);
Toast.makeText(MainActivity.this, "再接再厉", Toast.LENGTH_SHORT).show();
}
});
enter.setOnClickListener(new OnClickListener() // Guess the number and handle it by the Program!
{
public void onClick(View v)
{
String guessed = pass.getText().toString();
/////Check///////
Not4Digit = Double = haveZero = haveNonDigit = false;
if(guessed.length() != 4) {
Not4Digit = true;
}
else {
for(int i=0;i<4;i++)
for(int j=i+1;j<4;j++)
if(guessed.charAt(i) == guessed.charAt(j))
Double = true;
for(int i=0;i<4;i++)
if(guessed.charAt(i) == '0')
haveZero = true;
for(int i=0;i<4;i++)
if(guessed.charAt(i) < '0' || guessed.charAt(i) > '9')
haveNonDigit = true;
}
/////Check///////
if(Not4Digit) {
Toast.makeText(MainActivity.this, "请填入四位数字..", Toast.LENGTH_LONG).show();
pass.setText("");
}
else if(Double) {
Toast.makeText(MainActivity.this, "四位数字每位数字都不能相等!", Toast.LENGTH_LONG).show();
pass.setText("");
}
else if(haveNonDigit) {
Toast.makeText(MainActivity.this, "请不要输入其它非数字字符", Toast.LENGTH_LONG).show();
pass.setText("");
}
else if(haveZero) {
Toast.makeText(MainActivity.this, "数字为0~9之间哦", Toast.LENGTH_LONG).show();
pass.setText("");
}
else {
Count++; // only 5 chance!
pass.setText(""); //clear the input text
if(guessed.equals(Answer)) // Bingo!
{
flag = true;
if(Count <= 2) //2 次以内猜中
Toast.makeText(MainActivity.this, "你简直是个天才!", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "恭喜你,猜对了!", Toast.LENGTH_LONG).show();
show[Count].setText(guessed+" "+4+"A"+0+"B");
ANS.setText("正确答案: " + Answer);
enter.setClickable(false);
}
else
{
k = 0;
AA = BB = 0;
for(int i=0;i<4;i++)
{
if(guessed.charAt(i) == Answer.charAt(i))
AA++;
else
ss[k++]=guessed.charAt(i);
}
for(int j=0;j<k;j++)
{
for(int ka=0;ka<4;ka++)
{
if(ss[j] == Answer.charAt(ka))
BB++;
}
}
show[Count].setText(guessed+" "+AA+"A"+BB+"B");
}
if(!flag && Count == 5)
{
ANS.setText("正确答案: " + Answer);
Toast.makeText(MainActivity.this, "很遗憾,只有五次机会,你还是没有猜对.5555..", Toast.LENGTH_LONG).show();
enter.setClickable(false);
}
}
}
});
}
}

终端测试:

备注: 

目前还没有在别的机器上测试过,不知道有没有问题。

APK下载链接:http://pan.baidu.com/s/1i346vDN

欢迎大家下载来玩,或者提出建议哦。

原创Android游戏】--猜数字游戏Version 0.1的相关教程结束。

《【原创Android游戏】--猜数字游戏Version 0.1.doc》

下载本文的Word格式文档,以方便收藏与打印。