• 黄金点游戏(结对项目)


    黄金点游戏

    基本要求:
    N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同学得到-2分,其他同学得0分。

    主要功能:

    每位玩家输入名字和数字(必须大于两位玩家),最后一位玩家点击结束游戏,由程序计算出G值,并判断每位玩家的得分情况。

    设计思想:

    我们组是在android平台上开发的这个游戏,因为android提供了更好的图形界面,能使游戏变得更加有趣。

    小组成员:

    我和室友陈亮一起完成了此次编程练习,我负责代码查找与修改,他负责测试以及优化。

    我们的代码来自https://coding.net/u/rayshea/p/android-based-golden-point/git,经过我们自己修改优化,增加了保存数据,删除数据等方法,将这个小游戏做得更加完善。代码地址:https://github.com/xiaofancheng/goldenPoint。

    下面是我们项目的主要代码以及效果图:

    MainActivity.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    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.java:

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

    showResult.java:

    复制代码
     1 package com.example.shea.goldenpoint2rd;
     2 
     3 import android.content.Intent;
     4 import android.content.SharedPreferences;
     5 import android.support.v7.app.AppCompatActivity;
     6 import android.os.Bundle;
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.TextView;
    10 import android.widget.Toast;
    11 
    12 public class showResult extends AppCompatActivity {
    13     private int round=0;
    14     private String info="";
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_showresult);
    20         SharedPreferences tempData=getSharedPreferences("tempData",MODE_APPEND);
    21         SharedPreferences.Editor editData=tempData.edit();
    22         TextView tv= (TextView) findViewById(R.id.textView);
    23         Button back= (Button) findViewById(R.id.back);
    24         round=tempData.getInt("round", 0);
    25         if (round==0)
    26             Toast.makeText(showResult.this,"无数据!",Toast.LENGTH_LONG).show();
    27         else {
    28             for (int i = 1; i <round ; i++) {
    29                 info+='\n'+"第"+i+"轮的数据"+'\n'+tempData.getString(Integer.toString(i),"");
    30             }
    31             tv.setText(info);
    32 
    33         }
    34         back.setAlpha((float) 0.6);
    35         back.setOnClickListener(new View.OnClickListener() {
    36             @Override
    37             public void onClick(View v) {
    38                 Intent intent=new Intent(showResult.this,MainActivity.class);
    39                 startActivity(intent);
    40             }
    41         });
    42     }
    43 }
    复制代码

  • 相关阅读:
    [quote] Rough Notes on Linux Networking Stack
    All my miscellaneous assembly
    chrome浏览器屏幕网页内嵌的flash广告?
    [quote] How to determine what target is given in Makefile from the make command-line?
    [quote] colour your linux command line
    python 3 函数
    python 2 控制流
    薛定谔的猫 转载
    using distance to keep track of the min in a stack
    Java String
  • 原文地址:https://www.cnblogs.com/XGQXGQ/p/8179754.html
Copyright © 2020-2023  润新知