鱼C论坛

 找回密码
 立即注册
查看: 4706|回复: 25

[技术交流] Python: 每日一题 50

[复制链接]
发表于 2017-5-20 15:44:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ooxx7788 于 2017-5-21 09:05 编辑

在学习python的过程中,对于我这种完完全全的零基础门外汉而言,我觉得类是比较难理解的一个概念。小甲鱼给了我们一个乌龟和鱼的游戏。

虽然我也知道大佬都比较偏爱算法一类以思考为主的题目,但是毕竟这个系列总体是偏向比较基础的题库。

所以今天我也给各位出个游戏题,这样可以给各位和我一样的新手更多的参考。如果你熟悉类,这题不会很难,如果你不熟悉类,可以通过这题来熟悉。Let's go.

写一个小游戏的程序:两名武士决斗,返回出胜利者的名字。

要求:两名武士轮流攻击对方,先杀死对方的胜者。而死亡取决于其health, health<=0时死亡。

每个武士为一个Fighter实例。(需要有名字(name),生命值(health),伤害(damage_per_attack))

主函数为:declare_winner(武士1,武士2,先攻击者)
返回值为:获胜者。

示例:
  1. declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew") => "Lew"                # 此处函数中最后一个参数为先攻击者,而返回值为胜利者。

  2.   Lew attacks Harry; Harry now has 3 health.                # 这里是整个游戏的攻击过程,这也需要体现出来。
  3.   Harry attacks Lew; Lew now has 6 health.
  4.   Lew attacks Harry; Harry now has 1 health.
  5.   Harry attacks Lew; Lew now has 2 health.
  6.   Lew attacks Harry: Harry now has -1 health and is dead. Lew wins.
复制代码


好了,如果有说的不清楚的地方,请留言。

游客,如果您要查看本帖隐藏内容请回复

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-20 16:25:46 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-21 07:21:24 From FishC Mobile | 显示全部楼层
class Fighter:
  def __init__(self,name,hp,att):
    self.name = name
    self.hp = hp
    self.att = att

def declare_winner(f1,f2,first):
  if first == f1.name:
    while True:
      f2.hp -= f1.att
      print '%s attack %s, %s\'s health %d left' % (f1.name,f2.name,f2.name,f2.hp)
      if f2.hp <= 0:
        return f1.name
      f1.hp -= f2.att
      print '%s attack %s, %s\'s health %d left' % (f2.name,f1.name,f1.name,f1.hp)
      if f1.hp <= 0:
        return f2.name
  else:
    while True:
      f1.hp -= f2.att
      print '%s attack %s, %s\'s health %d left' % (f2.name,f1.name,f1.name,f1.hp)
      if f1.hp <= 0:
        return f2.name
      f2.hp -= f1.att
      print '%s attack %s, %s\'s health %d left' % (f1.name,f2.name,f2.name,f2.hp)
      if f2.hp <= 0:
        return f1.name
print '%s is the winner!' % declare_winner(Fighter('Lee',10,2),Fighter('Blu',5,4),'Blu')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-21 11:22:54 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-21 11:29:07 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-21 12:44:20 | 显示全部楼层
  1. class fighter():
  2.         def __init__(self, name, health, attack):
  3.                 self.name = name
  4.                 self.health = health
  5.                 self.attack = attack
  6.                
  7. def declare_winner(f1,f2,f):
  8.         if f1.name == f.name:
  9.                 while True:
  10.                         f2.health -= f1.attack
  11.                         print '%s-->%s,  %s now has %d health' %(f1.name, f2.name, f2.name, f2.health)
  12.                         if f2.health<=0:
  13.                                 return f1.name
  14.                         f1.health -= f2.attack
  15.                         print '%s-->%s, %s now has %d health' %(f2.name, f1.name, f1.name, f1.health)
  16.                         if f1.health<=0:
  17.                                 return f2.name
  18.         else:
  19.                 while True:
  20.                         f1.health -= f2.attack
  21.                         print '%s-->%s, %s now has %d health' %(f2.name, f1.name, f1.name,f1.health)
  22.                         if f1.health<=0:
  23.                                 return f2.name
  24.                         f2.health -= f1.attack
  25.                         print '%s-->%s, %s now has %d health' %(f1.name, f2.name, f2.name, f2.health)
  26.                         if f2.health<=0:
  27.                                 return f1.name

  28. A = fighter('len', 10, 2)
  29. B = fighter('he', 5, 5)

  30. print declare_winner(A,B,B)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-21 16:24:33 | 显示全部楼层
都好厉害呀!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-22 09:17:05 | 显示全部楼层

你好 方便加一下QQ1547298104交流点事情吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-29 20:46:03 From FishC Mobile | 显示全部楼层
looklook upup
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-8 18:49:59 | 显示全部楼层
游戏要有一定的随机性才好玩,用random加入 强力格挡和 会心一击
  1. import random

  2. class Fighter:
  3.     def __init__(self,name,health,damage):
  4.         self.name = name
  5.         self.health = health
  6.         self.damage = damage
  7.         
  8.     def get_name(self):
  9.         return self.name

  10.     def be_injured(self,num):
  11.         self.health -= num
  12.         print("%s 受到 %s 点攻击,剩余 %s health" %(self.name,num,self.health))
  13.         if self.health <= 0:
  14.             print("gg. %s is dead"%self.name)
  15.             return 0
  16.         else:
  17.             return self.health
  18.         
  19.     def attack(self,enemy):
  20.         print("%s attacking %s" %(self.name,enemy.name))
  21.         odds = random.randint(0,11)
  22.         if odds<5:
  23.             print("%s 强力格挡!"%enemy.name)
  24.         elif odds>10:
  25.             print("%s 发动会心一击!!效果拔群!!"%self.name)
  26.             odds = 20
  27.         return enemy.be_injured(odds*self.damage//10)


  28. def declare(a,b,first_pick):
  29.     if first_pick == a.get_name():
  30.         former,latter = a,b
  31.     elif first_pick ==b.get_name():
  32.         former,latter = b,a
  33.     else:
  34.         return ("nobody name %s"%first_pick)
  35.     result = 1
  36.     while result:
  37.         result = former.attack(latter)
  38.         former,latter = latter,former
  39.     print("%s wins" %latter.get_name())
复制代码

效果如下
  1. declare(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew")

  2. ##Lew attacking Harry
  3. ##Harry 受到 1 点攻击,剩余 4 health
  4. ##Harry attacking Lew
  5. ##Harry 发动会心一击!!效果拔群!!
  6. ##Lew 受到 8 点攻击,剩余 2 health
  7. ##Lew attacking Harry
  8. ##Harry 强力格挡!
  9. ##Harry 受到 0 点攻击,剩余 4 health
  10. ##Harry attacking Lew
  11. ##Lew 受到 3 点攻击,剩余 -1 health
  12. ##gg. Lew is dead
  13. ##Harry wins
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-10 14:53:28 | 显示全部楼层

  1. class Fighter(object):
  2.     def __init__(self, name, health, damage_per_attack):
  3.         self.name = name
  4.         self.health = health
  5.         self.damage_per_attack = damage_per_attack


  6. def declare_winner(f1, f2, first_pick):
  7.     if f1.name == first_pick:
  8.         while True:
  9.             f2.health -= f1.damage_per_attack
  10.             if f2.health < 0:
  11.                 print('{} attack {},{} now has {} health and is dead,{} win'.format(f1.name, f2.name, f2.name, f2.health, f1.name))
  12.                 return f1.name
  13.             else:
  14.                 print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
  15.             f1.health -= f2.damage_per_attack
  16.             if f1.health < 0:
  17.                 print('{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health, f2.name))
  18.                 return f2.name
  19.             else:
  20.                 print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
  21.     else:
  22.         while True:
  23.             f1.health -= f2.damage_per_attack
  24.             if f1.health < 0:
  25.                 print(
  26.                     '{} attack {},{} now has {} health and is dead,{} win'.format(f2.name, f1.name, f1.name, f1.health,f2.name))
  27.                 return f2.name
  28.             else:
  29.                 print('{} attack {};{} now has {} health'.format(f2.name, f1.name, f1.name, f1.health))
  30.             f2.health -= f1.damage_per_attack
  31.             if f2.health < 0:
  32.                 return f1.name
  33.             else:
  34.                 print('{} attack {};{} now has {} health'.format(f1.name, f2.name, f2.name, f2.health))
  35. if __name__ == '__main__':
  36.     f1 = Fighter('johny j', 10, 2)
  37.     f2 = Fighter('jin', 15, 3)

  38.     winner_name = declare_winner(f1, f2, 'jin')
复制代码


感觉比较臃肿,想看看大佬怎么写的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-6 10:21:35 | 显示全部楼层
本帖最后由 shigure_takimi 于 2017-12-6 10:34 编辑
  1. class Samurai:
  2.     def __init__(self, name, health, damage_per_attack):
  3.         self.name = name
  4.         self.health = health
  5.         self.damage_per_attack = damage_per_attack

  6. samurai1 = Samurai('服部半藏',150,100)
  7. samurai2 = Samurai('柳生十兵衛',200,60)

  8. def declare_winner(samurai1, samurai2, first_attack):
  9.     while not (samurai1.health <= 0 or samurai2.health <= 0):
  10.         if first_attack == samurai1:
  11.             if samurai1.health > 0:
  12.                 samurai2.health -= samurai1.damage_per_attack
  13.                 print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai1.name,samurai2.name,samurai2.name,samurai2.health))
  14.             if samurai2.health > 0:
  15.                 samurai1.health -= samurai2.damage_per_attack
  16.                 print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai2.name,samurai1.name,samurai1.name,samurai1.health))
  17.         elif first_attack == samurai2:
  18.             if samurai2.health > 0:
  19.                 samurai1.health -= samurai2.damage_per_attack
  20.                 print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai2.name,samurai1.name,samurai1.name,samurai1.health))
  21.             if samurai1.health > 0:
  22.                 samurai2.health -= samurai1.damage_per_attack
  23.                 print('{0}攻擊{1}; {2}還有{3}滴血。'.format(samurai1.name,samurai2.name,samurai2.name,samurai2.health))
  24.     winner = samurai1.name if samurai2.health<=0 else samurai2.name
  25.     if winner == samurai1.name:
  26.         print('{}有{}滴血,掛掉。{}勝利。'.format(samurai2.name, samurai2.health, samurai1.name))
  27.     else:
  28.         print('{}有{}滴血,掛掉。{}勝利。'.format(samurai1.name, samurai1.health, samurai2.name))

  29. declare_winner(samurai1, samurai2, samurai1)

  30.    

  31. ##    服部半藏攻擊柳生十兵衛; 柳生十兵衛還有100滴血。
  32. ##    柳生十兵衛攻擊服部半藏; 服部半藏還有90滴血。
  33. ##    服部半藏攻擊柳生十兵衛; 柳生十兵衛還有0滴血。
  34. ##    柳生十兵衛有0滴血,掛掉。服部半藏勝利。
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-22 03:44:38 | 显示全部楼层
  1. class Fighter():
  2.     def __init__(self,name,health,damage):
  3.         self.name = name
  4.         self.health = health
  5.         self.damage = damage


  6. def declare_winner(p1_obj, p2_obj, first_act_name):
  7.     p1 = p1_obj
  8.     p2 = p2_obj
  9.     users = [p1,p2]
  10.     for i in range(len(users)):
  11.         if users[i].name == first_act_name:
  12.             users.insert(0,users.pop(i))
  13.     while True:
  14.         users[1].health -= users[0].damage
  15.         if users[1].health <= 0:
  16.             print('{first.name} attacks {second.name}: {second.name} now has {second.health} health and is dead. {first.name} wins.'.format(first=users[0],second=users[1]))
  17.             break
  18.         else:
  19.             print('{first.name} attacks {second.name}; {second.name} now has {second.health} health.'.format(first=users[0],second=users[1]))
  20.             users = users[::-1]

  21. declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew")

  22. #结果
  23. Lew attacks Harry; Harry now has 3 health.
  24. Harry attacks Lew; Lew now has 6 health.
  25. Lew attacks Harry; Harry now has 1 health.
  26. Harry attacks Lew; Lew now has 2 health.
  27. Lew attacks Harry: Harry now has -1 health and is dead. Lew wins.
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-26 11:26:17 | 显示全部楼层
  1. '''
  2. 两名武士决斗,返回出胜利者的名字。
  3. 要求:两名武士轮流攻击对方,先杀死对方的胜者。而死亡取决于其health, health<=0时死亡。
  4. 每个武士为一个Fighter实例。(需要有名字(name),生命值(health),伤害(damage_per_attack))
  5. 主函数为:declare_winner(武士1,武士2,先攻击者)
  6. 返回值为:获胜者。
  7. '''
  8. import random
  9. class Fighter(object):
  10.     def __init__(self,name,health,damage):
  11.         self.name = name
  12.         self.health = health
  13.         self.damage = damage

  14. def attack(f1,f2):
  15.     counter = 1
  16.     while f1.health > 0 and f2.health > 0:
  17.         print('****************第%d回合开始****************' % counter)
  18.         print('%s当前血量为%d,%s当前血量为%d。' %(f1.name,f1.health,f2.name,f2.health))
  19.         print('%s开始攻击' % f1.name,end=' ')
  20.         f2.health -= f1.damage
  21.         print('%s血量降低到%d' %(f2.name,f2.health))
  22.         if f2.health < 0:
  23.             print('%s 战败!' % f2.name)
  24.             winner = f1.name
  25.             break
  26.         print('%s开始攻击' % f2.name,end=' ')
  27.         f1.health -= f2.damage
  28.         print('%s血量降低到%d' %(f1.name,f1.health))
  29.         if f1.health < 0:
  30.             print('%s 战败! ' % f1.name)
  31.             winner = f2.name
  32.             break
  33.         counter += 1
  34.     return winner
  35. def declare_winner(fighter1,fighter2,names):
  36.     if fighter1.name == names:
  37.         return attack(fighter1,fighter2)
  38.     elif fighter2.name == names:
  39.         return attack(fighter2,fighter1)
  40.     else:
  41.         print('names not exists!')
  42.         return 0

  43. bob = Fighter('Bob',1200,99)
  44. stam = Fighter('Stam',1360,88)

  45. declare_winner(bob,stam,'Stam')
  46.             
  47.         
  48.         
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-22 14:38:19 | 显示全部楼层
正在学习类
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-30 09:07:10 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-9-6 09:10:27 | 显示全部楼层
zan
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-9 19:17:20 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-9 19:19:54 | 显示全部楼层
本帖最后由 咕咕鸡鸽鸽 于 2019-1-9 19:28 编辑

我把攻击变成随机的攻击力

  1. from random import *

  2. class Fighter():
  3.     def __init__(self,name):
  4.         self.name = name
  5.         self.health = 10
  6.         self.damage = randint(1,3)
  7.         self.alive = True

  8.     def attack(self):
  9.         self.damage = randint(1,3)
  10.         

  11. def main():
  12.     man_a = Fighter("A")
  13.     man_b = Fighter("B")
  14.     man_list = [man_a,man_b]

  15.     gaming = True
  16.    
  17.     while gaming:
  18.         for each in man_list:
  19.             if each.health <= 0:
  20.                 each.alive = False
  21.                 print("%s is dead." % each.name)
  22.                 man_list.remove(each)
  23.                 print("winner is %s." % man_list[0].name)
  24.                 gaming = False        
  25.                 break
  26.             
  27.             index = man_list.index(each)
  28.             man_list.remove(each)
  29.             each.attack()
  30.             man_list[0].health -= each.damage
  31.             print("%s attacks %s;%s now has %d health" % (each.name,man_list[0].name,man_list[0].name,man_list[0].health))
  32.             man_list.insert(index,each)
  33.         print("---------------------------------")


  34. if __name__ == "__main__":
  35.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-18 16:22:21 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2019-8-18 16:23 编辑
  1. class Fighter:
  2.         def __init__(self,name,health,dpa):self.name=name;self.health=health;self.dpa=dpa
  3.         def fight(self,other):other.health-=self.dpa
  4. declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew")
  5. def declare_winner(F1, F2, F):
  6.         if F!=F1.name:F1,F2=F2,F,1
  7.         while 1:
  8.                 F1.fight(F2)
  9.                 if F2.heath>0:print(f'{F1.name} attacks {F2.name}; {F2.name} now has {F2.health} health.')
  10.                 else:print(f'{F1.name} attacks {F2.name}; {F2.name} now has {F2.health} health and is dead. {F1.name} wins.');winner=F1.name;break
  11.                 F2.fight(F1)
  12.                 if F1.heath>0:print(f'{F2.name} attacks {F1.name}; {F1.name} now has {F1.health} health.')
  13.                 else:print(f'{F2.name} attacks {F1.name}; {F1.name} now has {F1.health} health and is dead. {F2.name} wins.');winner=F2.name;break
  14.         return winner
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 15:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表