黄金点第一次博客

现代软件工程课程设计——黄金点游戏

 

 

一. 课设要求

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

  2. 采用单机方式实现,需要为用户提供便利的输入界面。

  3. 该游戏每次至少可以运行10轮以上,并能够保留各轮比赛结果。

  4. 后续在此基础上迭代开发。

 

 

 

二. 相关资料

  1. 结对编程和两人合作:https://www.cnblogs.com/xinz/archive/2011/08/07/2130332.html

  2. 创新的时机——黄金点游戏:https://www.cnblogs.com/xinz/archive/2011/08/08/2130505.html

 

 

三. 开发环境

  系统为win10,项目拟采用 java 语言编写,集成环境为 IntelliJ IDEA。交互界面用 java swing 界面编写。

 

四. 思路

  首先理解该程序的逻辑,将各个部分封装并且建立联系。其次对该程序进行编写,并且测试数据。最后添加可视化界面。

 

五. 程序大致流程图

 

六. 命令行程序代码及展示

  1. 代码实现(c++)

    1.1. 游戏大类

 1 class Gold_point{
 2     public:
 3         Gold_point();
 4         ~Gold_point();
 5         void Show_history();  // 历史分数
 6         void Show_result();  // 显示结果
 7         void Welcome();  // 欢迎界面
 8         void Gaming();  // 进行游戏
 9         void Name_input();  // 输入姓名
10         int player_num, round_num, now_round_num = 1;  // 分别为玩家人数、游戏总轮数、当前所在轮数
11         double now_result = 0;  // 即时结果
12         int point_choice[10000];  // 每一轮每人的分数选择
13         vector<vector<int> > history_scores;  // 存储历史分数
14         double now_distance[10000];  // 存储每一轮每位玩家到黄金点的距离
15         vector<double> round_distance;  // 距离复用
       // 玩家的结构体数组
16 struct Player{ 17 string name; 18 int scores, index; // 分数和身份id 19 }player[10000]; 20 21 };

 

     1.2. 姓名输入

1 void Gold_point::Name_input(){
2     for(int i = 0; i < player_num; i++){
3         cout << "请输入 " << i + 1 << " 号玩家的姓名:" ;
4         cin >> player[i].name;
5     }    
6 }

    1.3. 历史成绩

 1 void Gold_point::Show_history(){
 2     for(;;){
 3         cout << "您想要查看历史的每轮成绩吗?Y / N:";
 4         char choice;
 5         cin >> choice;
 6         if(choice == 'Y' || choice == 'y'){
 7             cout << "您想查看第几轮的成绩?输入数字则查看指定轮成绩,输入 0 则查看全部历史成绩:";
 8             int tempNum;
 9             cin >> tempNum;
10             if(tempNum == 0){
11                 for(int i = 0; i < history_scores.size(); i++){
12                     cout << "" << i + 1 << " 轮的成绩是:" << endl;
13                     for(int j = 0; j < player_num; j++)    cout << player[j].name << "" << history_scores[i][j] << endl;
14                 }
15             }else if(tempNum >= 1 && tempNum <= history_scores.size()){
16                 cout << "" << tempNum << " 轮的成绩是:" << endl;
17                 for(int j = 0; j < player_num; j++)    cout << player[j].name << "" << history_scores[tempNum - 1][j] << endl;
18             }else{
19                 cout << "输入数据有误!";
20             }
21             cout << "是否继续?Y / N:";
22             cin >> choice;
23             if(choice == 'Y' || choice == 'y')    continue;
24             else    return; 
25         }
26         return;
27     }
28 }

 

    1.4. 游戏主程序

 1 void Gold_point::Show_history(){
 2     for(;;){
 3         cout << "您想要查看历史的每轮成绩吗?Y / N:";
 4         char choice;
 5         cin >> choice;
 6         if(choice == 'Y' || choice == 'y'){
 7             cout << "您想查看第几轮的成绩?输入数字则查看指定轮成绩,输入 0 则查看全部历史成绩:";
 8             int tempNum;
 9             cin >> tempNum;
10             if(tempNum == 0){
11                 for(int i = 0; i < history_scores.size(); i++){
12                     cout << "" << i + 1 << " 轮的成绩是:" << endl;
13                     for(int j = 0; j < player_num; j++)    cout << player[j].name << "" << history_scores[i][j] << endl;
14                 }
15             }else if(tempNum >= 1 && tempNum <= history_scores.size()){
16                 cout << "" << tempNum << " 轮的成绩是:" << endl;
17                 for(int j = 0; j < player_num; j++)    cout << player[j].name << "" << history_scores[tempNum - 1][j] << endl;
18             }else{
19                 cout << "输入数据有误!";
20             }
21             cout << "是否继续?Y / N:";
22             cin >> choice;
23             if(choice == 'Y' || choice == 'y')    continue;
24             else    return; 
25         }
26         return;
27     }
28 }

 

 

    1.5. 完整代码

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define gold_point 0.618
  5 
  6 class Gold_point{
  7     public:
  8         Gold_point();
  9         ~Gold_point();
 10         void Show_history();
 11         void Show_result();
 12         void Welcome();
 13         void Gaming();
 14         void Name_input();
 15         int player_num, round_num, now_round_num = 1;
 16         double now_result = 0;
 17         int point_choice[10000];
 18         vector<vector<int> > history_scores;
 19         double now_distance[10000];
 20         vector<double> round_distance;
 21         struct Player{
 22             string name;
 23             int scores, index;
 24         }player[10000];
 25         
 26 };
 27 
 28 Gold_point::Gold_point(){}
 29 
 30 Gold_point::~Gold_point(){}
 31 
 32 void Gold_point::Welcome(){
 33     cout << "欢迎来到黄金点游戏!" << endl;
 34 }
 35 
 36 void Gold_point::Name_input(){
 37     for(int i = 0; i < player_num; i++){
 38         cout << "请输入 " << i + 1 << " 号玩家的姓名:" ;
 39         cin >> player[i].name;
 40     }    
 41 }
 42 
 43 void Gold_point::Show_history(){
 44     for(;;){
 45         cout << "您想要查看历史的每轮成绩吗?Y / N:";
 46         char choice;
 47         cin >> choice;
 48         if(choice == 'Y' || choice == 'y'){
 49             cout << "您想查看第几轮的成绩?输入数字则查看指定轮成绩,输入 0 则查看全部历史成绩:";
 50             int tempNum;
 51             cin >> tempNum;
 52             if(tempNum == 0){
 53                 for(int i = 0; i < history_scores.size(); i++){
 54                     cout << "" << i + 1 << " 轮的成绩是:" << endl;
 55                     for(int j = 0; j < player_num; j++)    cout << player[j].name << "" << history_scores[i][j] << endl;
 56                 }
 57             }else if(tempNum >= 1 && tempNum <= history_scores.size()){
 58                 cout << "" << tempNum << " 轮的成绩是:" << endl;
 59                 for(int j = 0; j < player_num; j++)    cout << player[j].name << "" << history_scores[tempNum - 1][j] << endl;
 60             }else{
 61                 cout << "输入数据有误!";
 62             }
 63             cout << "是否继续?Y / N:";
 64             cin >> choice;
 65             if(choice == 'Y' || choice == 'y')    continue;
 66             else    return; 
 67         }
 68         return;
 69     }
 70 }
 71 
 72 void Gold_point::Gaming(){
 73     cout << "请输入游戏人数:";
 74     cin >> player_num;
 75     cout << "请输入游戏轮数:";
 76     cin >> round_num; 
 77     Name_input();
 78     cout << player[1].scores << endl;
 79     while(now_round_num <= round_num){
 80         vector<int> round_scores(player_num, 0);
 81         cout << "请输入每个人的分数:" << endl;
 82         for(int i = 0; i < player_num; i++)    cin >> point_choice[i];
 83         for(int i = 0; i < player_num; i++)    now_result += point_choice[i];
 84         cout << "当前和为" << now_result << endl;  
 85         now_result = (now_result / player_num) * gold_point;
 86         for(int i = 0; i < player_num; i++)    now_distance[i] = abs(point_choice[i] - now_result);
 87         for(int i = 0; i < player_num; i++)    round_distance.push_back(now_distance[i]);
 88         for(int i = 0; i < player_num; i++)    cout << player[i].name << "的距离是" << round_distance[i] << endl;
 89         double max = *max_element(round_distance.begin(), round_distance.end()), min = *min_element(round_distance.begin(), round_distance.end());
 90         for(int i = 0; i < player_num; i++){
 91             if(now_distance[i] == max)    player[i].scores += player_num, round_scores[i] = player_num;
 92             else if(now_distance[i] == min)    player[i].scores -= 2, round_scores[i] = -2;
 93             else    round_scores[i] = 0;
 94         }
 95         cout << "当前轮的成绩是:" << endl;
 96         for(int j = 0; j < player_num; j++)    cout << player[j].name << "" << round_scores[j] << endl;
 97         history_scores.push_back(round_scores);
 98         Show_history();
 99         now_round_num++;
100         round_distance.clear();
101     }
102 }
103 
104 void Gold_point::Show_result(){
105     cout << "游戏结束!成绩如下:" << endl;
106     for(int i = 0; i < player_num; i++){
107         cout << player[i].name << "的成绩是" << player[i].scores << endl;
108     }
109 }
110 
111 int main(){
112     Gold_point g;
113     g.Welcome();
114     g.Gaming();
115     g.Show_result();
116     return 0;
117 }

 

  2. 界面展示

 

 

七. 总结

  目前已经完成了命令行界面的开发,已基本实现所有要求功能。后续将在此基础上继续进行迭代开发。

posted @ 2020-10-19 18:41  软工课设第29组  阅读(40)  评论(0)    收藏  举报