结对编程--基于android平台的黄金点游戏(2.0版本)

在昨天上传完博客之后发现一个重大的bug...故在此推出2.0版本。

博文详情见:http://www.cnblogs.com/RayShea/p/5372398.html

coding地址:https://coding.net/u/rayshea/p/android-based-golden-point/git/tree/master/Goldenpoint2rd

APK地址:http://pan.baidu.com/s/1eSsGLwQ

较先前版本的改进:

  1. 交互更加合理
  2. 存储方式更科学
  3. 代码可读性更强
  4. 修复了之前读取历史战绩时会出现读取重复的现象
  5. 增加了显示某部分数据的所属轮数

感想:deadline果然是第一生产力...在下午跟助教协商后,助教很爽快的答应给时间进行迭代,在这里表示感谢!然后回头看之前的代码,发现因为在编码的过程中临时更改数据存储方式,导致整个代码出现了各种各样的变量,各种冗余的函数...看到这里,果断弃掉,大概下午六点钟开始重新编码,重新规划了整个项目的模块,把数据的存储方式也彻底改成了以sharedpreference为主体,将各个Activity中的变量也全部重新命名,activity之间的通信也都用Intent....总之就是代码重用率不到5%...大概到九点钟,编码结束,大概400行代码,较之前版本少了100行左右,测试了一个小时,找到3个bug,并修复之。

总结:因为自己一边对android各种工具并不熟悉,导致在刚动手时就像小马过河,不知河水深浅,毕竟只到长者裤腰的河水也会淹死郭小四...蛤蛤蛤。就这样新旧技术的叠加就会使得代码无比混乱,之前设计好的框架等根本无法适用,这也导致了今天的整个代码的重写...自己还是太naive,以后还是要多学习一个的,只有技术面广阔,才能在拿到问题的第一时间内找到合适的解决策略,就先总结到这了。下面放出软件运行的截图。

首先是改动最大的地方,比赛历史记录,第一张图是第一次输入的数据的截图,本来应该是要比赛完跳转到的那个界面的图,好像误删了(吐槽一下安卓没有最近删除的照片),所以放的是没有第二次比赛时历史记录所查到的信息。图二是第二次测试的数据(比赛完显示结果的界面),图三是完成第二次测试之后的历史记录,图三是可以滚动的页面,为了不挡住信息,按钮的透明度也有调整。小生不才,必定还有很多不足之处,还请多多指教。

接下来就是改动较小的部分,1p,人数不够无法结束游戏,可以选择推出或者继续,2p,游戏主界面,在这部分处理很多异常操作,不细讲了,详情见上篇博客。

附录:代码

MainActivity

package com.example.shea.goldenpoint2rd;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private int round;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button start = (Button) findViewById(R.id.start);
        Button delete = (Button) findViewById(R.id.delete);
        Button history = (Button) findViewById(R.id.history);
        Button rules = (Button) findViewById(R.id.rules);
        SharedPreferences tempdata = getSharedPreferences("tempdata", MODE_APPEND);
        final SharedPreferences.Editor dataedit = tempdata.edit();
        round=tempdata.getInt("round",1);


        assert start != null;
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,gameinterface.class);
                startActivity(intent);

            }
        });

        assert delete != null;
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataedit.clear();
                if (round!=0){
                    for (int i = 1; i <=round ; i++) {
                        dataedit.remove(Integer.toString(i));
                        dataedit.commit();
                    }
                }
                dataedit.putInt("round",1);
                Toast.makeText(MainActivity.this,"删除成功!",Toast.LENGTH_SHORT).show();


            }
        });

        assert rules != null;
        rules.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this).
                        setMessage("请输入一个0-100的有理数,然后点击提交,如果需要更改,点击再次提交,如果完成请点击下一位玩家,并将手机传递给下一位玩家,最后一名玩家请点击结束游戏").
                        setTitle("游戏规则").setPositiveButton("懂了!开始游戏!", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent=new Intent(MainActivity.this,gameinterface.class);
                        startActivity(intent);

                    }
                }).setNegativeButton("先不游戏   ", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();

            }
        });

        assert history != null;
        history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (round==0){
                    Toast.makeText(MainActivity.this,"没有历史数据,快来完一轮吧!~",Toast.LENGTH_LONG).show();
                }
                else
                {
                    Intent intent=new Intent(MainActivity.this,showresult.class);
                    startActivity(intent);
                }

            }
        });


    }
}

  gameinterface--游戏界面

  1 package com.example.shea.goldenpoint2rd;
  2 
  3 import android.app.AlertDialog;
  4 import android.content.Context;
  5 import android.content.DialogInterface;
  6 import android.content.Intent;
  7 import android.content.SharedPreferences;
  8 import android.support.v7.app.AppCompatActivity;
  9 import android.os.Bundle;
 10 import android.text.TextUtils;
 11 import android.view.View;
 12 import android.view.inputmethod.InputMethodManager;
 13 import android.widget.Button;
 14 import android.widget.EditText;
 15 import android.widget.TextView;
 16 import android.widget.Toast;
 17 
 18 public class gameinterface extends AppCompatActivity {
 19     private String username="";
 20     private double number=0;
 21     private int score=0;
 22     private int ID=1;
 23     private double G=0;
 24     private double sum;
 25     private double distance=0;
 26     private boolean FLAG=false;
 27     private String result="";
 28     private int round=1;
 29     private information[] userinfo=new information[100];
 30 
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_gameinterface);
 35         Button next= (Button) findViewById(R.id.next);
 36         Button end= (Button) findViewById(R.id.endgame);
 37         /*final Button name= (Button) findViewById(R.id.namebtn);*/
 38         final Button submit= (Button) findViewById(R.id.inputbtn);
 39         final EditText nametext= (EditText) findViewById(R.id.name);
 40         final TextView userid= (TextView) findViewById(R.id.userid);
 41         final TextView showinfo= (TextView) findViewById(R.id.showinfo);
 42         final EditText inputnumtext= (EditText) findViewById(R.id.inputnum);
 43         SharedPreferences tempdata=getSharedPreferences("tempdata",MODE_APPEND);
 44         final SharedPreferences.Editor editdata=tempdata.edit();
 45         round=tempdata.getInt("round",1);
 46         userid.setText("您是当前第1名玩家");
 47 
 48         assert next != null;//下一个玩家
 49         next.setOnClickListener(new View.OnClickListener() {
 50             @Override
 51             public void onClick(View v) {
 52                 if (TextUtils.isEmpty(inputnumtext.getText().toString())) {
 53                     Toast.makeText(gameinterface.this, "请输入您的数值!", Toast.LENGTH_SHORT).show();
 54                 } else if (FLAG) {
 55                     Toast.makeText(gameinterface.this, "请提交数据", Toast.LENGTH_SHORT).show();
 56                 } else {
 57                     ID++;
 58                     showinfo.setText("");
 59                     nametext.setText("");
 60                     inputnumtext.setText("");
 61                     userid.setText("您是当前第" + ID + "名玩家");
 62                 }
 63 
 64             }
 65         });
 66 
 67 
 68 
 69         assert submit != null;
 70         submit.setOnClickListener(new View.OnClickListener() {
 71             @Override
 72             public void onClick(View v) {
 73                 if (TextUtils.isEmpty(inputnumtext.getText().toString()))
 74                     Toast.makeText(gameinterface.this, "请输入数据!", Toast.LENGTH_SHORT).show();
 75                 else if (TextUtils.isEmpty(nametext.getText().toString()))
 76                     Toast.makeText(gameinterface.this, "请输入昵称!", Toast.LENGTH_SHORT).show();
 77                 else
 78                     // double inputnum=Double.valueOf(inputnumtext.getText().toString())
 79                     if (Float.valueOf(inputnumtext.getText().toString()) < 100 && Float.valueOf(inputnumtext.getText().toString()) > 0) {
 80                         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 81                         if (imm.isActive() && getCurrentFocus() != null) {
 82                             if (getCurrentFocus().getWindowToken() != null) {
 83                                 imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
 84                             }
 85                         }
 86                         number = Double.valueOf(inputnumtext.getText().toString());
 87                         username = nametext.getText().toString();
 88                         userinfo[ID] = new information(username, number, ID);
 89                         String test = userinfo[ID].getinfo();
 90                         System.out.println(test);
 91                         FLAG = false;
 92                         showinfo.setText(username + ",您输入的数据是:" + number + '
' + "您可以输入框内更改输入数据,再点击提交数据");
 93                         Toast.makeText(gameinterface.this, "提交成功!", Toast.LENGTH_SHORT).show();
 94                     } else {
 95                         {Toast.makeText(gameinterface.this, "请输入一个介于0-100之间的有理数", Toast.LENGTH_SHORT).show();
 96                         inputnumtext.setText("");
 97                         }
 98                     }
 99 
100             }
101         });
102 
103         assert end != null;//结束游戏
104         end.setOnClickListener(new View.OnClickListener() {
105             @Override
106             public void onClick(View v) {
107                 if (TextUtils.isEmpty(inputnumtext.getText().toString()))
108                     Toast.makeText(gameinterface.this,"请输入数据!",Toast.LENGTH_SHORT).show();
109                 else if (TextUtils.isEmpty(nametext.getText().toString()))
110                     Toast.makeText(gameinterface.this,"请输入昵称!",Toast.LENGTH_SHORT).show();
111                 else  if (FLAG){
112                     Toast.makeText(gameinterface.this,"请提交数据!",Toast.LENGTH_SHORT).show();
113                 }else
114                 if (ID<3)
115                 {
116                     new AlertDialog.Builder(gameinterface.this).setTitle("不符合游戏规则!").setMessage("游戏结束至少需要2人,推荐人数10人").setPositiveButton("退出游戏", new DialogInterface.OnClickListener() {
117                         @Override
118                         public void onClick(DialogInterface dialog, int which) {
119                             Intent intent=new Intent(gameinterface.this,MainActivity.class);
120                             startActivity(intent);
121                         }
122                     }).setNegativeButton("继续游戏", new DialogInterface.OnClickListener() {
123                         @Override
124                         public void onClick(DialogInterface dialog, int which) {
125 
126                         }
127                     }).create().show();
128                 }
129                 else{
130                     for (int i=1;i<=ID;i++)
131                     {
132                         sum+=userinfo[i].inputnum;
133                     }
134                     G=(sum/ID)*0.618;
135                     for (int i = 1; i <=ID; i++) {
136                         distance=userinfo[i].inputnum-G;
137                         userinfo[i].setDistance(distance);
138                     }
139                     double max=userinfo[1].distance;
140                     double min=userinfo[1].distance;
141                     for (int i=2;i<=ID;i++)
142                     {
143                         if (max<userinfo[i].distance)
144                             max=userinfo[i].distance;
145                         if (min>userinfo[i].distance)
146                             min=userinfo[i].distance;
147                     }
148                     for (int i=1;i<=ID;i++)
149                     {
150                         if (userinfo[i].distance==max)
151                             userinfo[i].setScore(-2);
152                         else if (userinfo[i].distance==min)
153                             userinfo[i].setScore(ID);
154                         else
155                             userinfo[i].setScore(0);
156                         result=result+'
'+userinfo[i].getinfo();
157 
158                     }
159                     System.out.println(result);
160                     String KEY=Integer.toString(round);
161                     editdata.putString(KEY, result);
162                     round++;
163                     editdata.putInt("round", round);
164                     editdata.commit();
165                     Intent intent=new Intent(gameinterface.this,gameover.class);
166                     intent.putExtra("data",result);
167                     startActivity(intent);
168                 }
169 
170             }
171         });
172 
173     }
174 }

 information--用户信息类

package com.example.shea.goldenpoint2rd;

/**
 * Created by shea on 2016/4/11.
 */
public class information {

    public String name="";
    public double inputnum=0;
    public int ID=0;
    public int score=0;
    public double distance=0;
    public void setName(String sname){name=sname;}
    public void setInputnum(double num){inputnum=num;}
    public void setID(int id){ID=id;}
    public void setScore(int SCORE){score=SCORE;}
    public void setDistance(double sdistance){distance=sdistance;}

    public information(String sname,double num,int id)
    {
        name=sname;
        inputnum=num;
        ID=id;}

    public String getinfo()
    {
        String info="游戏ID:"+ID+"  玩家昵称:"+name+"  分数:"+score+"  输入数据:"+inputnum+'
';
        return info;}

}

  

原文地址:https://www.cnblogs.com/RayShea/p/5380836.html