安卓猜数字小游戏1
来源:互联网 发布:mac spaces 编辑:程序博客网 时间:2024/04/30 12:47
先说玩法:输入四个不重复的数字,点确定按钮后,结果显示在下方的历史中。其形式为**** ?A?B。星号代表你输入的数字,你输入的四位数字中,位置和数字都正确为A,数字相同而位置不同为B,当4A时为猜对,如X次仍未猜中数字则游戏失败。
本程序实现了防止用户输入重复的数字和设置难度(其实就是设置能猜几次)也实现了历史猜的次数和正确次数,比较完善的一个小游戏,适合对安卓基本开发有所了解的初学者学习。
代码比较简单易懂,用SharedPreferences保存难度设置,用while循环判断重复,因为比较简单,就不打注释了,慢慢阅读就可以了,都可以看懂的。
import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class Caishuzi extends Activity implements View.OnClickListener { EditText cai; Button btn_del; Button btn_set; int a[]=new int[4]; Intent intent; SharedPreferences pref; TextView cai_lishi; int i,A = 0,B,k,j,cai_long,cai_win,qwe=0; int b[]=new int[4]; String s,duqu = ""; private Button btn0; private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private Button btn6; private Button btn7; private Button btn8; private Button btn9; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.caishuzi); cai=(EditText)findViewById(R.id.caishu); btn_del=(Button)findViewById(R.id.cai_del); btn_set=(Button)findViewById(R.id.cai_2); cai_lishi=(TextView)findViewById(R.id.cai_lishi); btn0=(Button)findViewById(R.id.btn_0); btn1=(Button)findViewById(R.id.btn_1); btn2=(Button)findViewById(R.id.btn_2); btn3=(Button)findViewById(R.id.btn_3); btn4=(Button)findViewById(R.id.btn_4); btn5=(Button)findViewById(R.id.btn_5); btn6=(Button)findViewById(R.id.btn_6); btn7=(Button)findViewById(R.id.btn_7); btn8=(Button)findViewById(R.id.btn_8); btn9=(Button)findViewById(R.id.btn_9); btn0.setOnClickListener(this); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); btn9.setOnClickListener(this); btn_set.setOnClickListener(this); btn_del.setOnClickListener(this);
//先生成数字并防止重复 a[0] = (int) (1 + Math.random() * 8);while(a[0]==0)a[0] = (int) (1 + Math.random() * 8); a[1] = (int) ( Math.random() * 9);while(a[1]==a[0])a[1] = (int) ( Math.random() * 9); a[2] = (int) ( Math.random() * 9); while(a[2]==a[0]||a[2]==a[1])a[2] = (int) ( Math.random() * 9); a[3] = (int) ( Math.random() * 9);while(a[3]==a[0]||a[3]==a[1]||a[3]==a[2]) a[3] = (int) ( Math.random() * 9); pref = getSharedPreferences("UserId", MODE_PRIVATE);
//获取难度,如果没取到,就为8. i=pref.getInt("nandu", 8); } @Override public void onClick(View v) { String str=cai.getText().toString(); switch (v.getId()) { case R.id.btn_0: if(str!=null&&!str.equals("")&&i>0&&!str.contains("0")){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_1: if(!str.contains("1")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_2: if(!str.contains("2")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_3: if(!str.contains("3")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_4: if(!str.contains("4")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_5: if(!str.contains("5")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_6: if(!str.contains("6")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_7: if(!str.contains("7")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_8: if(!str.contains("8")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.btn_9: if(!str.contains("9")&&i>0){ cai.setText(str + ((Button) v).getText()); } break; case R.id.cai_2: intent=new Intent(Caishuzi.this,Caiset.class);startActivity(intent); finish(); break; case R.id.cai_del:if(str!=null&&!str.equals("")&&i>0){ cai.setText(str.substring(0,str.length()-1)); } if(i<=0){ intent=new Intent(Caishuzi.this,Caishuzi.class);startActivity(intent); finish(); } break; } if(cai.getText().toString().length()==4){ gogogo(); } } private void gogogo() { i--; if(i>=0){ A=0;B=0;k=0; s=cai.getText().toString(); cai.setText(""); if(i>0) Toast.makeText(Caishuzi.this, "你还有"+i+"次机会", Toast.LENGTH_SHORT).show(); if(s.length()!=4||s.charAt(0)=='0'){ Toast.makeText(Caishuzi.this, "输入有误", Toast.LENGTH_SHORT).show(); } else { b[0]=s.charAt(0)-48; b[1]=s.charAt(1)-48; b[2]=s.charAt(2)-48; b[3]=s.charAt(3)-48; for( j=0;j<4;j++) { if(b[j]==a[j]) A++; for(k=0;k<4;k++) if(b[j]!=a[j]&&b[j]==a[k]) { B++; break; } } if(A!=4) { qwe++; if(qwe%3!=0) duqu=duqu+s+" "+A+"A"+B+"B "; else duqu=duqu+s+" "+A+"A"+B+"B\n"; cai_lishi.setText(duqu); } else { duqu=duqu+s+"\t恭喜你,答对啦!"; cai_lishi.setText(duqu); } } SharedPreferences.Editor editor = pref.edit(); if(A==4){ cai_win=pref.getInt("win",0); editor.remove("win"); editor.commit(); editor.putInt("win", cai_win + 1); editor.commit(); Toast.makeText(Caishuzi.this, "恭喜恭喜,答对啦!答案是:"+a[0]+a[1]+a[2]+a[3], Toast.LENGTH_SHORT).show(); } if(i==0||A==4) { cai_long= pref.getInt("long",0); editor.remove("long"); editor.commit(); editor.putInt("long",cai_long+1); editor.commit(); if(A!=4) Toast.makeText(Caishuzi.this, "失败了,再接再厉吧!", Toast.LENGTH_SHORT).show(); } } if(i==0||A==4){ btn_del.setText("再来一次"); } }}
阅读全文
0 0
- 安卓猜数字小游戏1
- 安卓猜数字小游戏2
- 安卓猜数字小游戏3
- 安卓猜数字小游戏4
- 猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏(MiniGUI)
- 猜数字小游戏
- 猜数字小游戏
- Flex:猜数字小游戏
- c#猜数字小游戏
- 猜数字的小游戏~
- 猜数字 小游戏
- 猜数字的小游戏
- shell猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏
- 猜数字小游戏 v1
- selector 点击样式
- 类加载器
- 团队管理者需要做到的事
- leetcode:4. Median of Two Sorted Arrays(Java实现)
- 算法导论--二叉树
- 安卓猜数字小游戏1
- Java与算法(8)
- 浅谈JSOON
- linux命令笔记
- Android笔记三:Android 接口定义语言 (AIDL)--Binder工作原理分析
- 反射
- 判断两个二叉树是否相等(函数)
- 使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换
- 210. Course Schedule II(DFS)