软件工程结对项目---黄金点游戏
Posted 忆杉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程结对项目---黄金点游戏相关的知识,希望对你有一定的参考价值。
github源码和工程文件地址:https://github.com/HuChengLing/GoldPointGame
基本要求:黄金点游戏是一个数字小游戏。
其游戏规则是:N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同学得到-2分,其他同学得0分。玩了几天以后,大家发现了一些很有意思的现象,比如黄金点在逐渐地往下移动。
主要功能:由用户自主输入10个数字,然后由程序自主判断哪些得分和不得分并将结果进行排行。
设计思想:将小游戏移植到安卓上,有图形界面更吸引人。
人员:本次结对编程,我的队友是易聪,他是我的室友。他做事比较低调,默默勤劳工作。我们这次编程我们明确分工协同完成此次项目。并分别编写了博客和上传工程文件到GitHub上。
工程部分源码及效果图
1.activity——main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.bignerdranch.android.goldpoint.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="黄金点游戏" android:textSize="20sp" android:textColor="#000000" android:layout_marginTop="100sp" android:layout_gravity="center"/> <TextView android:id="@+id/content_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:text=" 共十名玩家,每个玩家输入一个0-100的整数,不包括0或100,系统会算出这10个数的平均数然后乘0.618以此为G值,提交的数字最靠近G的玩家得到2分,离G点最远的玩家得到-1分,其他玩家得0分" /> <Button android:id="@+id/start_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" android:textColor="#000000" android:background="@drawable/setbar_bg" android:layout_gravity="center" android:layout_marginTop="15dp"/> <LinearLayout android:id="@+id/play_linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <Button android:id="@+id/play1_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:text="Play_1" android:textColor="#ffffff" android:textSize="16sp"/> <Button android:id="@+id/play2_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:text="Play_2" android:textColor="#ffffff" android:textSize="16sp"/> <Button android:id="@+id/play3_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:text="Play_3" android:textColor="#ffffff" android:textSize="16sp"/> <Button android:id="@+id/play4_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:text="Play_4" android:textColor="#ffffff" android:textSize="16sp"/> <Button android:id="@+id/play5_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:text="Play_5" android:textColor="#ffffff" android:textSize="16sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <Button android:id="@+id/play6_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:textColor="#ffffff" android:text="Play_6" android:layout_marginLeft="5dp" android:textSize="16sp"/> <Button android:id="@+id/play7_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:textColor="#ffffff" android:text="Play_7" android:layout_marginLeft="5dp" android:textSize="16sp"/> <Button android:id="@+id/play8_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:textColor="#ffffff" android:text="Play_8" android:layout_marginLeft="5dp" android:textSize="16sp"/> <Button android:id="@+id/play9_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:textColor="#ffffff" android:text="Play_9" android:layout_marginLeft="5dp" android:textSize="16sp"/> <Button android:id="@+id/play10_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/holo_blue_light" android:textColor="#ffffff" android:text="Play_10" android:layout_marginLeft="5dp" android:textSize="16sp"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="35dp"> <Button android:id="@+id/inputEnd_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright" android:text="输入结束" android:textColor="#ffffff" android:textSize="16sp" android:layout_marginLeft="80dp"/> <Button android:id="@+id/checkResult_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright" android:text="查看结果" android:textColor="#ffffff" android:textSize="16sp" android:layout_marginLeft="40dp"/> </LinearLayout> </LinearLayout>
2.mainactivity.java
package com.bignerdranch.android.goldpoint; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Button mPlayer1; private Button mPlayer2; private Button mPlayer3; private Button mPlayer4; private Button mPlayer5; private Button mPlayer6; private Button mPlayer7; private Button mPlayer8; private Button mPlayer9; private Button mPlayer10; private Button mInputEnd; private Button mCheckResult; private Button mStart; private LinearLayout mPlayer; private TextView mContent; private String mNumber; private List<Player> mPlayerList= new ArrayList<>(); private double[] mValue = new double[10]; private int[]mScore = new int[10]; private SharedPreferences mPref; private SharedPreferences.Editor mEditor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setupListeners(); } public void initView() { mPlayer1 = (Button)findViewById(R.id.play1_btn); mPlayer2 = (Button)findViewById(R.id.play2_btn); mPlayer3 = (Button)findViewById(R.id.play3_btn); mPlayer4 = (Button)findViewById(R.id.play4_btn); mPlayer5 = (Button)findViewById(R.id.play5_btn); mPlayer6 = (Button)findViewById(R.id.play6_btn); mPlayer7 = (Button)findViewById(R.id.play7_btn); mPlayer8 = (Button)findViewById(R.id.play8_btn); mPlayer9 = (Button)findViewById(R.id.play9_btn); mPlayer10 = (Button)findViewById(R.id.play10_btn); mPlayer = (LinearLayout)findViewById(R.id.play_linearLayout); mStart = (Button)findViewById(R.id.start_btn); mCheckResult = (Button)findViewById(R.id.checkResult_btn); mInputEnd = (Button)findViewById(R.id.inputEnd_btn); mContent = (TextView) findViewById(R.id.content_textView); mPlayer.setVisibility(View.GONE); mInputEnd.setVisibility(View.GONE); mCheckResult.setVisibility(View.GONE); mPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); for(int i=0;i<10;i++) { Player player = new Player(null,"0"); mPlayerList.add(i,player); } Log.i("TAG","test"); for(int i=0;i<10;i++){ mScore[i] = mPref.getInt(Integer.toString(i),0); } } public void setupListeners() { mStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mPlayer.setVisibility(View.VISIBLE); mInputEnd.setVisibility(View.VISIBLE); mCheckResult.setVisibility(View.VISIBLE); mStart.setVisibility(View.GONE); mContent.setVisibility(View.GONE); } }); mPlayer1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(0,new Player("1",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer1.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if(mPlayerList.get(0).getNumber()!="0") Toast.makeText(MainActivity.this, mPlayerList.get(0).getNumber(), Toast.LENGTH_SHORT).show(); else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(1,new Player("2",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer2.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(1).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(1).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(2,new Player("3",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer3.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(2).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(2).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(3,new Player("4",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer4.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(3).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(3).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.add(4,new Player("5",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer5.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(4).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(4).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(5,new Player("6",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer6.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(5).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(5).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(6,new Player("7",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer7.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(6).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(6).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(7,new Player("8",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer8.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(7).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(7).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.set(8,new Player("9",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer9.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(8).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(8).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字",Toast.LENGTH_SHORT).show(); } return false; } }); mPlayer10.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setMessage("请输入你的数字"); View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.input_number_dialog,null); final EditText mEditText = (EditText)mView.findViewById(R.id.dialog_editText); dialog.setView(mView); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mNumber = mEditText.getText().toString(); mPlayerList.add(9,new Player("10",mNumber)); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); } }); mPlayer10.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (mPlayerList.get(9).getNumber()!="0"){ Toast.makeText(MainActivity.this,mPlayerList.get(9).getNumber(),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"还没有输入数字"结对项目——黄金分割点游戏