想当年高中时经常和小伙伴在纸上或者黑板上或者学习机上玩猜数字的游戏,在当年那个手机等娱乐设备在我们那还不是很普遍的时候是很好的一个消遣的游戏,去年的时候便写了一个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>