标签:des android style blog http io ar color os
---------------------------------------------------------------
上次做完第一个版本后,发现还有一些漏洞,并且还有一些可以添加的功能,以及一些可改进的地方,于是准备继续完善此游戏,顺便学Android了。
本次更新信息如下:
1.改正了随机数生成算法,更正了不能产生数字‘9‘的bug
2.增加了数据存储与IO的内容,使用了SharedPreferences保存数据
3.保存数据为: 总盘数,猜中的盘数
4.使用了SimpleAdapter创建ListView来显示多用户的数据
5.ListView显示的数据放在另一个Activity中,每个Activity中有一个按钮来控制Activity的切换
6.猜中时增加了一个弹出信息
7.增加了用户头像和用户签名
(用户暂时都是系统指定的,还没有实现增加用户的注册功能等)
--------------------------------------------------------------------------------------------------------------------
1.改正了随机数生成算法,更正了不能产生数字 ‘9‘ 的bug
上次程序产生随机数是这样的:
Rn[0] = 1 + ((int)(Math.random()*8))%10;
因为Math.random函数产生的是[0.0,1.0)的随机浮点数,所以((int)(Math.random()*8))%10; 是不可能产生8的,所以 Rn[0]就不能得到9, 这是我疏忽了,直接复制的以前的代码,现在改正过来了。
2.增加了数据存储与IO的内容,使用了SharedPreferences保存数据
使用SharedPreferences与Editor进行文件的输入输出,以记录盘数数据以及正确盘数数据。
SharedPreferences保存的数据主要是类似于配置信息,主要是简单类型的key-value对,value可以是String,Int等多种数据类型。
SharedPreferences负责根据key读取数据值,而SharedPreferences.Editor用于写入数据。
getSharedPreferences方法用来获取SharedPreferences实例。
preferences.getXXX() , editor.putXxx() 分别从文件中读取数据和写入数据, Xxx可以是String,Int...
存储数据总会保存在/data/data/<package name>/shared_prefs, 以xml的形式存储。
写入数据方法代码如下:
void WriteData(boolean yes, int Count) { int usercnt = preferences.getInt("TOTALUSER", 0); //这里不用管 editor.putInt("TOTALUSER",4); //预设的是4个玩家 int YES = preferences.getInt("YES", 0); //从文件中读出: 正确的盘数 int TOTAL = preferences.getInt("TOTAL", 0); //从文件中读出: 总盘数 if(yes) { //如果这盘正确 editor.putString("Username", "Whatbeg"); //写入 editor.putInt("YES", YES+1); editor.putInt("TOTAL", TOTAL+1); } else if(Count > 0) { //否则如果现在已经开始了,猜了1个及以上 editor.putString("Username", "Whatbeg"); //写入 editor.putInt("TOTAL", TOTAL+1); } editor.commit(); //提交修改 }
在猜对或者失败后立即更新数据。
在按退出键、继续键和排行键的时候,更新数据,如果此时Count==5,那么这盘的数据已经被写入,将Count赋为0,代表忽略掉。
3.使用了SimpleAdapter创建ListView来显示多用户的数据
rankmain.xml 实现了ListView界面布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ListView android:id="@+id/mylist" android:layout_width="fill_parent" android:layout_height="392dp" /> <Button android:id="@+id/ret" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="@string/rett" /> </RelativeLayout>
同时ranking实现了把数据展示到列表中的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#f0f" android:paddingLeft="10dp" /> <TextView android:id="@+id/descs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:paddingLeft="10dp" /> </LinearLayout> </LinearLayout>
实现的列表布局如下:
4.ListView显示的数据放在另一个Activity中,每个Activity中有一个按钮来控制Activity的切换
启动Activity的方式:
Intent intent = new Intent(Source.this, Destination.class); //创建需要启动的Activity对应的intent
startActivity(intent); //启动intent对应的Activity
通过两个Button进行两个Activity转换代码:
rank.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { WriteData(false,Count=(Count==5?0:Count)); //Count=5就不算失败了 Intent intent = new Intent(MainActivity.this, RankActivity.class); startActivity(intent); finish(); } });
Button ret = (Button) findViewById(R.id.ret); ret.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Intent intent = new Intent(RankActivity.this, MainActivity.class); startActivity(intent); finish(); } });
5.猜中时增加了一个弹出信息
7.增加了用户头像和用户签名
-----------------------------------------------------------------------------------------------------
源代码:
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; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; 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; SharedPreferences preferences; //IO. SharedPreferences.Editor editor; void WriteData(boolean yes, int Count) { int usercnt = preferences.getInt("TOTALUSER", 0); editor.putInt("TOTALUSER",4); int YES = preferences.getInt("YES", 0); int TOTAL = preferences.getInt("TOTAL", 0); if(yes) { editor.putString("Username", "Whatbeg"); editor.putInt("YES", YES+1); editor.putInt("TOTAL", TOTAL+1); } else if(Count > 0) { editor.putString("Username", "Whatbeg"); editor.putInt("TOTAL", TOTAL+1); } editor.commit(); } 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()*9)%9+9)%9; while(Tag[Rn[0]]>0) Rn[0]= 1 + ((int)(Math.random()*9)%9+9)%9; Tag[Rn[0]]++; Rn[1] = 1 + ((int)(Math.random()*9)%9+9)%9; while(Tag[Rn[1]]>0) Rn[1]= 1 + ((int)(Math.random()*9)%9+9)%9; Tag[Rn[1]]++; Rn[2] = 1 + ((int)(Math.random()*9)%9+9)%9; while(Tag[Rn[2]]>0) Rn[2]= 1 + ((int)(Math.random()*9)%9+9)%9; Tag[Rn[2]]++; Rn[3] = 1 + ((int)(Math.random()*9)%9+9)%9; while(Tag[Rn[3]]>0) Rn[3]= 1 + ((int)(Math.random()*9)%9+9)%9; 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); } @SuppressWarnings("deprecation") @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); final Button rank = (Button) findViewById(R.id.rank); preferences = getSharedPreferences ("rank", Context.MODE_WORLD_READABLE); editor = preferences.edit(); 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); quit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { WriteData(false,Count=(Count==5?0:Count)); finish(); } }); cont.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { WriteData(false,Count=(Count==5?0:Count)); 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 Validation....... 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 Validation...... 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++; //只有5次机会 pass.setText(""); //clear the input text if(guessed.equals(Answer)) //猜对了 { flag = true; if(Count <= 2) //2 次以内猜中 Toast.makeText(MainActivity.this, "你简直是个天才!", Toast.LENGTH_LONG).show(); else if(Count == 3) 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); WriteData(true,Count); } 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) //5次还没猜对 { ANS.setText("正确答案: " + Answer); Toast.makeText(MainActivity.this, "很遗憾,只有五次机会,你还是没有猜对.5555..", Toast.LENGTH_LONG).show(); enter.setClickable(false); WriteData(false,Count); } } } }); rank.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { WriteData(false,Count=(Count==5?0:Count)); //Count=5就不算失败了 Intent intent = new Intent(MainActivity.this, RankActivity.class); startActivity(intent); finish(); } }); } }
package com.example.guessit; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; public class RankActivity extends Activity { private String [] names = new String[] { "Whatbeg", "Hello_World", "WWWW", "GOFORIT" }; private String [] descs = new String[] { "可爱的小孩","一个擅长音乐的女孩","文艺女青年","浪漫主义诗人" }; private int [] imageids = new int[] { R.drawable.tiger, R.drawable.woman, R.drawable.boy, R.drawable.girl }; SharedPreferences preferences; //IO. SharedPreferences.Editor editor; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rankmain); preferences = getSharedPreferences ("rank", MODE_WORLD_READABLE); editor = preferences.edit(); int TotalUser = preferences.getInt("TOTALUSER", 4); List<Map<String,Object> > listItems = new ArrayList<Map<String,Object> >(); for(int i=0;i<TotalUser;i++) { Map<String,Object> li = new HashMap<String,Object>(); int YES = preferences.getInt("YES", 0); int TOTAL = preferences.getInt("TOTAL", 1); li.put("header", imageids[i]); li.put("personname",names[i] + " " + String.valueOf(YES) + "/" + String.valueOf(TOTAL)); li.put("descs", descs[i]); listItems.add(li); } SimpleAdapter simpleadapter = new SimpleAdapter(this,listItems,R.layout.ranking, new String[]{"personname","header","descs"}, new int[]{R.id.name,R.id.header,R.id.descs}); ListView list = (ListView) findViewById(R.id.mylist); list.setAdapter(simpleadapter); Button ret = (Button) findViewById(R.id.ret); ret.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Intent intent = new Intent(RankActivity.this, MainActivity.class); startActivity(intent); finish(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#f0f" android:paddingLeft="10dp" /> <TextView android:id="@+id/descs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:paddingLeft="10dp" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ListView android:id="@+id/mylist" android:layout_width="fill_parent" android:layout_height="392dp" /> <Button android:id="@+id/ret" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="@string/rett" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">猜数字游戏V1.1</string> <string name="Rank">排行榜</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> <string name="rank">排行</string> <string name="rett">返回</string> </resources>
-------------------------------------------------------------------------------------
效果:
-----------------------------------------------------------------------------------------------------
这次就先更新到这,接下来主要需要实现的功能:
用户注册与存储(最好用Sqlite数据库来存储) 及 其他优化。
----------------------------------------------------------------------------------------------------------------------
APK: http://pan.baidu.com/s/1dD9yvSH
原创Android游戏--猜数字游戏V1.1 --数据存储,Intent,SimpleAdapter的学习与应用
标签:des android style blog http io ar color os
原文地址:http://www.cnblogs.com/whatbeg/p/4161978.html