Changeset 31921
- Timestamp:
- 04/05/09 12:18:06 (4 years ago)
- Location:
- lang/python/spaghetti_monster
- Files:
-
- 1 added
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/python/spaghetti_monster/main.py
r31918 r31921 4 4 5 5 """ 6 7 class 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 18 inputter = Inputter(is_human=True) 19 6 20 7 21 class Card(object): … … 24 38 print self.description % dict((k, k) for k in self.variables) 25 39 print self.variables 26 pos = raw_input("?>")40 pos = inputter.get() 27 41 self.variables[pos] += value 28 42 self.modifiers.append("%s%+d" % (pos, value)) … … 79 93 for i in range(self.variables["x"]): 80 94 c = game.select_card() 81 c.change_value(self.variables["y"] * direction)95 c.change_value(self.variables["y"] * self.direction) 82 96 83 97 … … 162 176 cards = [game.get_card(-1), game.get_card(1)] 163 177 print cards 164 pos = input ("?>")178 pos = inputter.get("?>", int) 165 179 cards[pos].removed = True 166 180 … … 263 277 for p in enumerate(self.cards): 264 278 print "%2d: %s" % p 265 return self.cards[input ("?>")]279 return self.cards[inputter.get("?>", int)] 266 280 267 281 def select_player(self): … … 269 283 for p in enumerate(self.players): 270 284 print "%2d: %s" % p 271 return self.players[input ("?>")]285 return self.players[inputter.get("?>", int)] 272 286 273 287 def select_bit(self): 274 c = game.select_card()288 c = self.select_card() 275 289 print ", ".join("%d:%s" % (i, b.mark) for i, b in enumerate(c.bits)) 276 pos = input ("?>")290 pos = inputter.get("?>", int) 277 291 return (c, pos) 278 292 279 293 def eval_card(self): 280 294 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() 282 298 print "eval: ", c 283 299 self.cur_card = c 284 300 for b in c.bits: 285 301 self.cur_player = b 286 if input ("activate %s?" % c):302 if inputter.get("activate %s?" % c, int): 287 303 c.then_action(self) 288 304 else: … … 297 313 for i in range(len(self.cards)): 298 314 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 303 321 # bit個数判定 304 322 # ライフ判定 … … 309 327 # new_bits を追加する 310 328 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 314 337 def main_loop(self): 315 338 while 1: 316 339 to_reprint = self.eval_card() 317 340 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
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)