软工实验:黄金点游戏(双人合作)
前言
黄金点游戏规则: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同学得到-2分,其他同学得0分。一、黄金点游戏实验感悟
黄金点游戏对一个python初学者来说是一个比较合适的实验项目,python较之于c和c++确实有一定方便性。在python开发中发现使用输入输出函数提示玩家继续流程较为麻烦,后续将尝试用更精简的语句继续完善。同时此次黄金点游戏实验采用了双人合作模式,初步了解到现代软件工程中所具有的分工模式,以及团队协作的力量,各司其职,分工合作,使得此次试验更加高效有序地完成
二、使用步骤
1.引入库
代码如下(示例):
import numpy as np
2.黄金点函数(Gold_Point)
def Gold_Point(people):
num = []
for i in range(len(people)):
print("请玩家"+people[i].name+"输入0-100的数字:")
num.append(int(input()))
Gpoint = np.mean(num)*0.618
print("输入完成!结果如下:")
num = [abs(x-Gpoint) for x in num] # 减去Gpoint算距离
num = [x-min(num) for x in num] # 减去距离最小值,0为最近距离的数字
farther_num = max(num) # 得到最大的数字,数字对应的下标就是该人在people数组里的下标
for i in range(len(num)):
if num[i] == 0:
people[i].change_score(len(people))
elif num[i] == farther_num:
people[i].change_score(-2)
else:
people[i].change_score(0)
for person in people:
person.show()
若存在多名距离相同的最高或最短距离玩家,那么所有最远距离玩家或最短距离玩家都将加分或减分
3.玩家类(person)
class person:
def __init__(self, name):
self.name = name
self.score_list = []
self.score = 10 # 初始分数为10
def change_score(self, num):
self.score += num
self.score_list.append(self.score)
def show(self):
print(str(self.name)+":"+str(self.score))
def show_scorelist(self):
print(str(self.name)+":"+str(self.score_list))
3.总代码
import numpy as np
class person:
def __init__(self, name):
self.name = name
self.score_list = []
self.score = 10 # 初始分数为10
def change_score(self, num):
self.score += num
self.score_list.append(self.score)
def show(self):
print(str(self.name)+":"+str(self.score))
def show_scorelist(self):
print(str(self.name)+":"+str(self.score_list))
def Gold_Point(people):
num = []
for i in range(len(people)):
print("请玩家"+people[i].name+"输入0-100的数字:")
num.append(int(input()))
Gpoint = np.mean(num)*0.618
print("输入完成!结果如下:")
num = [abs(x-Gpoint) for x in num] # 减去Gpoint算距离
num = [x-min(num) for x in num] # 减去距离最小值,0为最近距离的数字
farther_num = max(num) # 得到最大的数字,数字对应的下标就是该人在people数组里的下标
for i in range(len(num)):
if num[i] == 0:
people[i].change_score(len(people))
elif num[i] == farther_num:
people[i].change_score(-2)
else:
people[i].change_score(0)
for person in people:
person.show()
def inital():
print("请输入参与人数:")
people_num = int(input())
people = []
# 初始化
for i in range(people_num):
print("请玩家"+str(i)+"输入姓名:")
name = input()
people.append(person(name))
return people
def Main():
while 1:
print("--------游戏菜单--------")
print("1,开始新游戏")
print("2,继续游戏")
print("3,查看既往结果")
print("4,退出")
print("输入游戏选项:")
choice = input()
if choice == "1":
people = inital()
Gold_Point(people)
elif choice == "2":
Gold_Point(people)
pass
elif choice == "3":
print([x.show_scorelist() for x in people])
pass
elif choice == "4":
break
else:
print("输入无效选项,请重新输入!")
if __name__ == "__main__":
Main()
4.结果
按1游戏开始
按2紧接第一轮游戏继续
通过3得到了四名玩家的既往结果,第一列是四名玩家第一次的结果,第二列是四名玩家的第二次结果;
[None,None,None,None]本不应该打印,但不知道为什么在循环打印时却被打印出来,组员尝试过几种在有限时间内并未解决,这也将是我们亟待解决的问题
4结束。
以上是一个完整黄金点游戏的流程结果展示