Skip to content

Instantly share code, notes, and snippets.

@mebusw
Created June 14, 2017 03:09
Show Gist options
  • Save mebusw/250830b32e94f4eeeaeb5ae0d960cae8 to your computer and use it in GitHub Desktop.
Save mebusw/250830b32e94f4eeeaeb5ae0d960cae8 to your computer and use it in GitHub Desktop.
#!encoding=utf8
"""变形金刚的对战,看具体描述:https://coding.net/u/wanghao/p/The-Transformation-Company/git/blob/master/README.md
变形金刚的对战。
每个变形金刚(Transformer包括Autobot和Decepticon)都有以下的技术参数 参看大黄蜂的例子: http://www.ntfa.net/ntfa/techspecs/index.php?cat=Gen1&group=AutoAN&char=Bumblebee )
力量 (Strength)
智慧 (Intelligence)
速度 (Speed)
耐力 (Endurance)
等级 (Rank)
勇气 (Courage)
火力 (Firepower)
技能 (Skill)
所有参数的指数都是从1到10。 综合参数计= 力量 + 智慧 + 速度 + 耐力 + 火力 每个变形金刚要么是汽车人要么是霸天虎。 你的程序要接受输入一批变形金刚,基于一系列规则输出:
对战(battle)的次数 (一个汽车人与一个霸天虎是一次对战)
获胜的团队 (汽车人或者霸天虎)及还幸存的成员
失败的团队及幸存的成员
基本对战规则:
两个团队要安等级排序相互对战,级别高的和级别高的对战,失败的对手视为死亡,被消灭。
每一次的对战规则
如果有一方的勇气指数比对手少4个点及以上,并且力量也少3个点及以上,则不管综合参数如何,他的对手将自动赢得胜利。(勇气不够的人落荒而逃)
如果以上个情况不存在,则看技能,如果一方技能比对上多3个点及以上,则不管综合参数如何,他将赢得对战胜利。
除以上情况,则综合参数最高者赢。
如果是平局,则双方都死亡。
如果因为两队人数不对等,有些变形金刚排不上,则不需要参加对战,认为他存活。
消灭对方团队人数最多的一队,认为是胜利的团队(而不是剩下人最多的)
特别规则:
老大Optimus Prime或 出马,不过对手是谁,总是赢得对战。(老大嘛)
如果两个老大(Optimus Prime或Predaking)对战,立马游戏结束,所有参与者全部玩完。
这是一个例子, 输入如下(参数按顺序,分别为:姓名,团队,力量, 智慧,速度,耐力,等级,勇气,火力,技能)
Soundwave, D, 8,9,2,6,7,5,6,10
Bluestreak, A, 6,6,7,9,5,2,9,7
Hubcap: A, 4,4,4,4,4,4,4,4
输出
1个对战
胜利团队(Decepticons): Soundwave
失败团队(Autobots): Hubcap
"""
import unittest
from collections import namedtuple
Transformer = namedtuple('Transformer', [
'name', 'team', 'str', 'int', 'spd', 'edr', 'rnk', 'cou', 'fir', 'skl'])
Team = namedtuple('Team', ['killed', 'members', 'remain'])
class Game(object):
def __init__(self, transformers=[]):
self.teamA = Team(0, sorted(filter(lambda t: t.team=='A', transformers), key=lambda x:x.rnk, reverse=True), [])
self.teamD = Team(0, sorted(filter(lambda t: t.team=='D', transformers), key=lambda x:x.rnk, reverse=True), [])
self.battle_times = min(len(self.teamA.members), len(self.teamD.members))
self.pairs = zip(self.teamA.members[:self.battle_times], self.teamD.members[:self.battle_times])
self.remains_A = []
self.remains_D = []
def fight(self, a, d):
if a.name == 'OptimusPrime':
return 'a win'
if d.name == 'Predaking':
return 'd win'
if a.cou - d.cou >= 4 and a.str - d.str >= 3:
return 'a win'
if d.cou - a.cou >= 4 and d.str - a.str >= 3:
return 'd win'
if a.skl - d.skl >= 3:
return 'a win'
if d.skl - a.skl >= 3:
return 'd win'
point_a = a.str+a.int+a.spd+a.edr+a.fir
point_d = d.str+d.int+d.spd+d.edr+d.fir
if point_a == point_d:
return 'deuce'
elif point_a > point_d:
return 'a win'
elif point_a < point_d:
return 'd win'
def start_fight(self):
for pair in self.pairs:
if pair[0].name == 'OptimusPrime' and pair[1].name == 'Predaking':
self.winning_team = 'NA'
return
result = self.fight(pair[0], pair[1])
if result == 'a win':
self.remains_A.append(pair[0])
if result == 'd win':
self.remains_D.append(pair[1])
self.winning_team = 'A' if len(self.remains_A) > len(self.remains_D) else 'D'
self.remains_A += self.teamA.members[self.battle_times:]
self.remains_D += self.teamD.members[self.battle_times:]
class TestDice(unittest.TestCase):
def setUp(self):
self.soundwave = Transformer('Soundwave', 'D', 8, 9, 2, 6, 7, 5, 6, 10)
self.bluestreak = Transformer(
'Bluestreak', 'A', 6, 6, 7, 9, 5, 2, 9, 7)
self.hubcap = Transformer('Hubcap', 'A', 4, 4, 4, 4, 4, 4, 4, 4)
self.OptimusPrime = Transformer(
'OptimusPrime', 'A', 0, 0, 0, 0, 10, 0, 0, 0)
self.Predaking = Transformer('Predaking', 'D', 0, 0, 0, 0, 10, 0, 0, 0)
def test_fight__overwhelming_cou_and_str(self):
a = Transformer(name='AAA', team='A', str=4, int=9,
spd=2, edr=6, rnk=7, cou=1, fir=6, skl=10)
d = Transformer(name='DDD', team='D', str=8, int=9,
spd=2, edr=6, rnk=7, cou=5, fir=6, skl=10)
self.assertEquals('d win', Game().fight(a, d))
def test_fight__more_skill(self):
a = Transformer(name='AAA', team='A', str=7, int=9,
spd=2, edr=6, rnk=7, cou=4, fir=6, skl=10)
d = Transformer(name='DDD', team='D', str=8, int=9,
spd=2, edr=6, rnk=7, cou=5, fir=6, skl=2)
self.assertEquals('a win', Game().fight(a, d))
def test_fight__normal(self):
a = Transformer(name='AAA', team='A', str=9, int=9,
spd=9, edr=9, rnk=9, cou=9, fir=9, skl=9)
d = Transformer(name='DDD', team='D', str=8, int=9,
spd=2, edr=6, rnk=7, cou=5, fir=6, skl=10)
self.assertEquals('a win', Game().fight(a, d))
a = Transformer(name='AAA', team='A', str=8, int=8,
spd=2, edr=6, rnk=7, cou=5, fir=6, skl=8)
d = Transformer(name='DDD', team='D', str=9, int=9,
spd=9, edr=9, rnk=9, cou=9, fir=9, skl=9)
self.assertEquals('d win', Game().fight(a, d))
def test_fight__duece(self):
a = Transformer(name='AAA', team='A', str=9, int=9,
spd=9, edr=9, rnk=9, cou=9, fir=9, skl=9)
d = Transformer(name='DDD', team='D', str=9, int=9,
spd=9, edr=9, rnk=9, cou=9, fir=9, skl=9)
self.assertEquals('deuce', Game().fight(a, d))
def test_fight__boss(self):
a = Transformer(name='AAA', team='A', str=9, int=9,
spd=9, edr=9, rnk=9, cou=9, fir=9, skl=9)
d = Transformer(name='DDD', team='D', str=8, int=9,
spd=2, edr=6, rnk=7, cou=5, fir=6, skl=10)
self.assertEquals('a win', Game().fight(self.OptimusPrime, d))
self.assertEquals('d win', Game().fight(a, self.Predaking))
def test_team_sort_by_rank(self):
game = Game([self.hubcap, self.soundwave, self.bluestreak])
self.assertEquals([(self.bluestreak, self.soundwave)], game.pairs)
self.assertEquals(1, game.battle_times)
def test_teams_fight(self):
game = Game([self.hubcap, self.soundwave, self.bluestreak])
game.start_fight()
self.assertEquals('D', game.winning_team)
self.assertEquals([self.hubcap], game.remains_A)
self.assertEquals([self.soundwave], game.remains_D)
def test_boss_fight(self):
game = Game([self.hubcap, self.soundwave, self.bluestreak, self.OptimusPrime])
game.start_fight()
self.assertEquals('A', game.winning_team)
self.assertEquals([self.OptimusPrime, self.bluestreak, self.hubcap], game.remains_A)
self.assertEquals([], game.remains_D)
def test_boss_fight_each_other(self):
game = Game([self.hubcap, self.soundwave, self.bluestreak, self.OptimusPrime, self.Predaking])
game.start_fight()
self.assertEquals('NA', game.winning_team)
self.assertEquals([], game.remains_A)
self.assertEquals([], game.remains_D)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment