Changeset 31921

Show
Ignore:
Timestamp:
04/05/09 12:18:06 (4 years ago)
Author:
nishio
Message:

/lang/python/spaghetti_monster separate tests

Location:
lang/python/spaghetti_monster
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/python/spaghetti_monster/main.py

    r31918 r31921  
    44 
    55""" 
     6 
     7class Inputter(object): 
     8    def __init__(self, is_human=False, buf=[]): 
     9        self.is_human = is_human 
     10        self.buf = buf 
     11    def get(self, msg="?>", typ=str): 
     12        if self.is_human: 
     13            s = raw_input(msg) 
     14            return typ(s) 
     15        else: 
     16            return self.buf.pop(0) 
     17 
     18inputter = Inputter(is_human=True) 
     19 
    620 
    721class Card(object): 
     
    2438        print self.description % dict((k, k) for k in self.variables) 
    2539        print self.variables 
    26         pos = raw_input("?>") 
     40        pos = inputter.get() 
    2741        self.variables[pos] += value 
    2842        self.modifiers.append("%s%+d" % (pos, value)) 
     
    7993        for i in range(self.variables["x"]): 
    8094            c = game.select_card() 
    81             c.change_value(self.variables["y"] * direction) 
     95            c.change_value(self.variables["y"] * self.direction) 
    8296             
    8397 
     
    162176        cards = [game.get_card(-1), game.get_card(1)] 
    163177        print cards 
    164         pos = input("?>") 
     178        pos = inputter.get("?>", int) 
    165179        cards[pos].removed = True 
    166180 
     
    263277        for p in enumerate(self.cards): 
    264278            print "%2d: %s" % p 
    265         return self.cards[input("?>")] 
     279        return self.cards[inputter.get("?>", int)] 
    266280 
    267281    def select_player(self): 
     
    269283        for p in enumerate(self.players): 
    270284            print "%2d: %s" % p 
    271         return self.players[input("?>")] 
     285        return self.players[inputter.get("?>", int)] 
    272286 
    273287    def select_bit(self): 
    274         c = game.select_card() 
     288        c = self.select_card() 
    275289        print ", ".join("%d:%s" % (i, b.mark) for i, b in enumerate(c.bits)) 
    276         pos = input("?>") 
     290        pos = inputter.get("?>", int) 
    277291        return (c, pos) 
    278292     
    279293    def eval_card(self): 
    280294        c = self.cards[self.pc] 
    281         if not c.bits: return False 
     295        if not c.bits: 
     296            self.progress_pc() 
     297            return self.eval_card() 
    282298        print "eval: ", c 
    283299        self.cur_card = c 
    284300        for b in c.bits: 
    285301            self.cur_player = b 
    286             if input("activate %s?" % c): 
     302            if inputter.get("activate %s?" % c, int): 
    287303                c.then_action(self) 
    288304            else: 
     
    297313        for i in range(len(self.cards)): 
    298314            c = self.get_card(i) 
    299             pos = new_card.find(c) 
    300             if pos >= 0: 
    301                 self.pc = pos 
    302                 break 
     315            try: 
     316                pos = new_card.index(c) 
     317            except: 
     318                continue 
     319            self.pc = pos 
     320            break 
    303321        # bit個数判定 
    304322        # ライフ判定 
     
    309327        # new_bits を追加する 
    310328        for c in self.cards: 
    311             c.bits.extend(c.new_bits) 
    312         pass 
    313  
     329            new_bits = [b for b in c.bits if b != NULL_BIT] 
     330            new_bits.extend(c.new_bits) 
     331            c.bits = new_bits 
     332 
     333    def progress_pc(self): 
     334        self.pc += self.direction 
     335        self.pc %= len(self.cards) 
     336         
    314337    def main_loop(self): 
    315338        while 1: 
    316339            to_reprint = self.eval_card() 
    317340            if to_reprint: self.pretty_print() 
    318             self.pc += self.direction 
    319             self.pc %= len(self.cards) 
    320  
    321  
    322 def test_worm(): 
    323     cards = [Worm()] 
    324     players = [Player("nishio", "(N)")] 
    325     cards[0].bits.append(players[0]) 
    326     g = GameManager(cards, players) 
    327     g.pretty_print() 
    328     g.eval_card() 
    329     g.pretty_print() 
    330  
    331 def test_heal(): 
    332     cards = [Heal()] 
    333     players = [Player("nishio", "(N)")] 
    334     cards[0].bits.append(players[0]) 
    335     g = GameManager(cards, players) 
    336     g.pretty_print() 
    337     g.eval_card() 
    338     g.pretty_print() 
    339  
    340 def test_inc(): 
    341     cards = [Inc(), Heal()] 
    342     players = [Player("nishio", "(N)")] 
    343     cards[0].bits.append(players[0]) 
    344     cards[1].bits.append(players[0]) 
    345     g = GameManager(cards, players) 
    346     g.pretty_print() 
    347     g.main_loop() 
    348  
    349 def test_mimic(): 
    350     cards = [Mimic(), Inc(), Inc()] 
    351     players = [Player("nishio", "(N)")] 
    352     cards[0].bits.append(players[0]) 
    353     g = GameManager(cards, players) 
    354     g.pretty_print() 
    355     g.eval_card() 
    356     g.pretty_print() 
    357      
    358  
    359 def test_rm_bit(): 
    360     cards = [RemoveBit(), Inc(), Inc()] 
    361     players = [Player("nishio", "(N)")] 
    362     cards[0].bits.append(players[0]) 
    363     g = GameManager(cards, players) 
    364     g.pretty_print() 
    365     g.eval_card() 
    366     g.pretty_print() 
    367  
    368      
    369 def test_float(): 
    370     cards = [Test("A"), Test("B"), Float(), Test("C"), Test("D")] 
    371     players = [Player("nishio", "(N)")] 
    372     cards[2].bits.append(players[0]) 
    373     g = GameManager(cards, players) 
    374     g.pretty_print() 
    375     g.main_loop() 
    376  
    377  
     341            self.progress_pc() 
     342 
     343 
     344