⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paperscissorsrock2.py

📁 this is the most basic to learn python
💻 PY
字号:
#: c11:PaperScissorsRock2.py
# Multiple dispatching using a table
from __future__ import generators
import random

class Outcome:
  def __init__(self, value, name): 
    self.value = value
    self.name = name
  def __str__(self): return self.name 
  def __eq__(self, other):
      return self.value == other.value

Outcome.WIN = Outcome(0, "win")
Outcome.LOSE = Outcome(1, "lose")
Outcome.DRAW = Outcome(2, "draw")

class Item(object):
  def compete(self, item):
    # Use a tuple for table lookup:
    return outcome[self.__class__, item.__class__]
  def __str__(self): 
    return self.__class__.__name__ 

class Paper(Item): pass
class Scissors(Item): pass
class Rock(Item): pass

outcome = {
  (Paper, Rock): Outcome.WIN,
  (Paper, Scissors): Outcome.LOSE,
  (Paper, Paper): Outcome.DRAW,
  (Scissors, Paper): Outcome.WIN,
  (Scissors, Rock): Outcome.LOSE,
  (Scissors, Scissors): Outcome.DRAW,
  (Rock, Scissors): Outcome.WIN,
  (Rock, Paper): Outcome.LOSE,
  (Rock, Rock): Outcome.DRAW,
}

def match(item1, item2):
  print "%s <--> %s : %s" % (
    item1, item2, item1.compete(item2))

# Generate the items:
def itemPairGen(n):
  # Create a list of instances of all Items:
  Items = Item.__subclasses__()
  for i in range(n):
    yield (random.choice(Items)(), 
           random.choice(Items)())

for item1, item2 in itemPairGen(20):
  match(item1, item2)
#<hr>
output = '''
Scissors <--> Scissors : draw
Paper <--> Paper : draw
Scissors <--> Rock : lose
Rock <--> Scissors : win
Rock <--> Paper : lose
Paper <--> Scissors : lose
Rock <--> Rock : draw
Paper <--> Scissors : lose
Scissors <--> Scissors : draw
Rock <--> Scissors : win
Rock <--> Scissors : win
Rock <--> Scissors : win
Scissors <--> Paper : win
Scissors <--> Paper : win
Scissors <--> Rock : lose
Rock <--> Paper : lose
Paper <--> Rock : win
Paper <--> Rock : win
Paper <--> Rock : win
Paper <--> Rock : win
'''

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -