想當年高中時常常和小夥伴在紙上或者黑板上或者學習機上玩猜數字的遊戲,在當年那個手機等娛樂設備在咱們那還不是很廣泛的時候是很好的一個消遣的遊戲,去年的時候便寫了一個Android版的猜數字遊戲,只是當時沒寫完,最近又拿出來改了一下,完善了一些功能,修正了不少bug,終於將V0.1版作了出來,現貼出來分享一下。android
鑑於竟然有不少人都不會玩這個遊戲,我仍是簡單介紹下規則吧:git
猜數字: 系統每次隨機產生一個4位數字,4位數的每一位都不相同,且都爲0~9,而後你有5次機會去猜出那個數字,每次你猜一個4位數,都會獲得一個反饋,反饋以 nAmB 的形式給出,有n個'A'表示你猜的數的4位中與答案有n位是徹底相同的,可是你可能不知道這n個徹底相同的位是那幾位,有m個'B'表示你猜的數字與答案有共同的m個數,可是你猜的那m個數位置是錯的。app
好比:答案爲1234,你猜的是2184,那麼會返回1A2B,說明4是公共的且位置放對了,1和2是公共的,可是位置不對。dom
而後根據提示,通過本身的分析推理,儘量快地猜出答案。ide
本遊戲由4個數位,9個數碼(1~9)組成。函數
-----------------------------------------規則介紹完畢------------------------------------------------佈局
軟件組件:學習
1、1個文本輸入框EditText,用來輸入數字,限制使用數字輸入法測試
2、3個Button: ui
1.肯定: 輸入猜的數字後按肯定提交得到返回結果
2.繼續:進行一輪後繼續下一輪,全部輸出內容所有清空,從新產生隨機數
3.退出:退出程序
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
歡迎你們下載來玩,或者提出建議哦。