软工实验:黄金点游戏(双人合作)

软工实验:黄金点游戏(双人合作)


前言

黄金点游戏规则: 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结束。
以上是一个完整黄金点游戏的流程结果展示


总结

以上就是今天要讲的内容,本文仅仅简单介绍了黄金点游戏中所写的几个主要函数的使用,利用numpy包所提供的大量能使我们快速便捷地处理数据的函数和方法完成了一个小游戏:黄金点游戏实验的编写,同时这也是第一次和一位美女编程员合作所撰写的一篇博客。能隐隐约约感受到现代软件工程中分工合作的一个基本雏形,收获很多我们也将继续推进实验博客继续完善学习。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值