黃金點遊戲(結對項目)
阿新 • • 發佈:2018-01-03
data utm back length sed x86 提交數據 數據 alert
黃金點遊戲
基本要求:
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 }
黃金點遊戲(結對項目)