2015-12-10 83 views
0
import random 

class Spinner(object): 

@staticmethod 
def getSpin(): 
    newSpin = random.randint(1,6) 
    return newSpin 

class Player(object): 

def __init__(self,name): 
    self.position = 1 
    self.name = name 

def setName(self,name): 
    self.name = name 

def changePosition(self,number): 
    self.position = self.position + number 

def setPosition(self,pos): 
    self.position = pos 
    return self.position 

def getPosition(self): 
    return self.position 

def getName(self): 
    return self.name 

def spin(self): 
    newSpin = Spinner.getSpin() 
    self.position = self.position + newSpin 
    print(str(self.name) + "'s spin was: " + str(newSpin)) 

class Display(): 
def buildLadders(): 
    ladders = [[0 for x in range(2)] for x in range(2)] 
    ladders[0][0] = 2 
    ladders[0][1] = 7 
    ladders[1][0] = 4 
    ladders[1][1] = 5 
    return ladders 

def buildChutes(): 
    chutes = [[0 for x in range(2)] for x in range(2)] 
    chutes[0][0] = 9 
    chutes[0][1] = 2 
    chutes[1][0] = 6 
    chutes[1][1] = 3 
    return chutes 

class Check(Player): 
def checkLadders(self): 
    ladders = Display.buildLadders() 
    for i in range(0,len(ladders),1): 
     if self.getPosition() == ladders[i][0]: 
      self.position = self.setPosition(ladders[i][1]) 
      print(str(self.name) + " Landed on a Ladder! from " + \ 
        str(ladders[i][0]) +" to " + str(ladders[i][1])) 

def checkChutes(self): 
    chutes = Display.buildChutes() 
    for i in range(0,len(chutes),1): 
     if self.getPosition() == chutes[i][0]: 
      self.position = self.setPosition(chutes[i][1]) 
      print(str(self.name) + " Landed on a Chutes! from " + \ 
        str(chutes[i][0]) + " to " + str(chutes[i][1])) 

def checkQuestions(num): 
    one = random.randint(num,num*10) 
    two = random.randint(num,num*10) 
    listqq = [one,two] 
    return listqq 

class Controller(object): 
def __init__(self,names): 
    self.winner = '' 
    self.players = [] 
    for n in names: 
     p = Player(n) 
     self.players.append(p) 

def move(self): 
    players = self.players 
    winner = self.winner 
    click = '' 
    count = 1 
    while True: 
     for i in range(0,len(players)): 
      if winner == '' and click != 'e': 
       print("----" + str(players[i].getName()) + "'s TURN----") 
       click = input("Press r to roll or e to exit: ") 
       while click != 'r' and click != 'e': 
        click = input("Press r to roll or e to exit: ") 
       if click == 'r' and click != 'e': 
        count = count + 1 
        listqq = Check.checkQuestions(count) 
        answer = input(str(listqq[0]) + ' + ' + str(listqq[1]) +' is:') 
        if answer == str(listqq[0]+listqq[1]): 
         print(str(players[i].getName()) + "'s initial position is " \ 
           + str(players[i].getPosition())) 
         View(players).display() 
         players[i].spin() 
         Check.checkLadders(players[i]) 
         Check.checkChutes(players[i]) 
        else: 
         pass 
        if players[i].getPosition() >= 12: 
         players[i].position = players[i].setPosition(12 - (players[i].getPosition() - 12)) 
        print(str(players[i].getName()) + "'s new position is " \ 
          + str(players[i].getPosition())) 
        View(players).display() 
      if players[i].getPosition() == 12: 
       winner = players[i].getName() 
     if click == 'e' and click != 'r': 
      print('Bye') 
      break 
     if winner != '': 
      print(str(winner) + " is the winner!!!") 
      break 

class View(Player): 
def __init__(self,player): 
    self.players = player 

def display(self): 
    players = self.players 
    listof = [9,10,11,12,8,7,6,5,1,2,3,4] 
    ladders = Display.buildLadders() 
    chutes = Display.buildChutes() 
    board = [[]] * 3 
    for i in range(len(players)): 
     for j in range(len(listof)): 
      if self.players[i].position == listof[j]: 
       listof[j] = 'X' 
    for j in range(len(listof)): 
     for i in range(len(ladders)): 
      if ladders[i][0] == listof[j]: 
       listof[j] = 'L' 
    for j in range(len(listof)): 
     for i in range(len(chutes)): 
      if chutes[i][0] == listof[j]: 
       listof[j] = 'C' 
    for i in range(0,4): 
     board[0] = board[0] + [listof[i]] 
    for i in range(4,8): 
     board[1] = board[1] + [listof[i]] 
    for i in range(8,12): 
     board[2] = board[2] + [listof[i]] 
    for row in board: 
     for num in row: 
      if num == 'X': 
       print(" X", end=" ") 
      if num == 'L': 
       print(" L", end= " ") 
      if num == 'C': 
       print(" C", end= " ") 
      if num != 'X' and num != 'L' and num != 'C': 
       if 10 <= num <= 20: 
        print(num,end=" ") 
      if num != 'X' and num != 'L' and num!= 'C': 
       if 1 <= num <= 9: 
        print(str(num).rjust(2), end= " ") 
     print() 


def main(): 
n = input("Please enter number of players: ") 
names = [] 
for i in range (0,int(n)): 
    name = input("Please input your name: ") 
    names.append(name) 
game = Controller(names) 
game.move() 
while True: 
    ask = input("Do you want to play again? (y/n)") 
    if ask == 'y': 
     game = Controller(names) 
     game.move() 
    if ask == 'n': 
     print("Bye, see you again!") 
     break 
main() 

修正版本:MVC架構

import random 

class Player(object): 

def __init__(self,name): 
    self.position = 1 
    self.name = name 

def setName(self,name): 
    self.name = name 

def changePosition(self,number): 
    self.position = self.position + number 

def setPosition(self,pos): 
    self.position = pos 
    return self.position 

def spin(self): 
    newSpin = self.getSpin() 
    self.position = self.position + newSpin 
    print(str(self.name) + "'s spin was: " + str(newSpin)) 

def checkLadders(self): 
    ladders = self.buildLadders() 
    for i in range(0,len(ladders),1): 
     if self.position == ladders[i][0]: 
      self.position = self.setPosition(ladders[i][1]) 
      print(str(self.name) + " Landed on a Ladder! from " + \ 
        str(ladders[i][0]) +" to " + str(ladders[i][1])) 

def checkChutes(self): 
    chutes = self.buildChutes() 
    for i in range(0,len(chutes),1): 
     if self.position == chutes[i][0]: 
      self.position = self.setPosition(chutes[i][1]) 
      print(str(self.name) + " Landed on a Chutes! from " + \ 
        str(chutes[i][0]) + " to " + str(chutes[i][1])) 
@staticmethod 
def checkQuestions(num): 
    one = random.randint(num,num*10) 
    two = random.randint(num,num*10) 
    listqq = [one,two] 
    return listqq 

@staticmethod 
def getSpin(): 
    newSpin = random.randint(1,6) 
    return newSpin 

@staticmethod 
def buildLadders(): 
    ladders = [[0 for x in range(2)] for x in range(2)] 
    ladders[0][0] = 2 
    ladders[0][1] = 7 
    ladders[1][0] = 4 
    ladders[1][1] = 5 
    return ladders 

@staticmethod 
def buildChutes(): 
    chutes = [[0 for x in range(2)] for x in range(2)] 
    chutes[0][0] = 9 
    chutes[0][1] = 2 
    chutes[1][0] = 6 
    chutes[1][1] = 3 
    return chutes 


class Controller(object): 
def __init__(self,names): 
    self.winner = '' 
    self.players = [] 
    for n in names: 
     p = Player(n) 
     self.players.append(p) 

def move(self): 
    players = self.players 
    winner = self.winner 
    click = '' 
    count = 1 
    while True: 
     for i in range(0,len(players)): 
      if winner == '' and click != 'e': 
       print("----" + str(players[i].name) + "'s TURN----") 
       click = input("Press r to roll or e to exit: ") 
       while click != 'r' and click != 'e': 
        click = input("Press r to roll or e to exit: ") 
       if click == 'r' and click != 'e': 
        count = count + 1 
        listqq = Player.checkQuestions(count) 
        answer = input(str(listqq[0]) + ' + ' + str(listqq[1]) +' is:') 
        if answer == str(listqq[0]+listqq[1]): 
         print(str(players[i].name) + "'s initial position is " \ 
           + str(players[i].position)) 
         View(players).display() 
         players[i].spin() 
         Player.checkLadders(players[i]) 
         Player.checkChutes(players[i]) 
        else: 
         pass 
        if players[i].position >= 12: 
         players[i].position = players[i].setPosition(12 - (players[i].getPosition() - 12)) 
        print(str(players[i].name) + "'s new position is " \ 
          + str(players[i].position)) 
        View(players).display() 
      if players[i].position == 12: 
       winner = players[i].name 
     if click == 'e' and click != 'r': 
      print('Bye') 
      break 
     if winner != '': 
      print(str(winner) + " is the winner!!!") 
      break 

class View(): 
def __init__(self,player): 
    self.players = player 

def display(self): 
    players = self.players 
    listof = [9,10,11,12,8,7,6,5,1,2,3,4] 
    ladders = Player.buildLadders() 
    chutes = Player.buildChutes() 
    board = [[]] * 3 
    for i in range(len(players)): 
     for j in range(len(listof)): 
      if self.players[i].position == listof[j]: 
       listof[j] = 'X' 
    for j in range(len(listof)): 
     for i in range(len(ladders)): 
      if ladders[i][0] == listof[j]: 
       listof[j] = 'L' 
    for j in range(len(listof)): 
     for i in range(len(chutes)): 
      if chutes[i][0] == listof[j]: 
       listof[j] = 'C' 
    for i in range(0,4): 
     board[0] = board[0] + [listof[i]] 
    for i in range(4,8): 
     board[1] = board[1] + [listof[i]] 
    for i in range(8,12): 
     board[2] = board[2] + [listof[i]] 
    for row in board: 
     for num in row: 
      if num == 'X': 
       print(" X", end=" ") 
      if num == 'L': 
       print(" L", end= " ") 
      if num == 'C': 
       print(" C", end= " ") 
      if num != 'X' and num != 'L' and num != 'C': 
       if 10 <= num <= 20: 
        print(num,end=" ") 
      if num != 'X' and num != 'L' and num!= 'C': 
       if 1 <= num <= 9: 
        print(str(num).rjust(2), end= " ") 
     print() 


def main(): 
n = input("Please enter number of players: ") 
names = [] 
for i in range (0,int(n)): 
    name = input("Please input your name: ") 
    names.append(name) 
game = Controller(names) 
game.move() 
while True: 
    ask = input("Do you want to play again? (y/n)") 
    if ask == 'y': 
     game = Controller(names) 
     game.move() 
    if ask == 'n': 
     print("Bye, see you again!") 
     break 

的main()

這是繼MVC架構?如何知道它是否適合MVC結構?謝謝。我們是否必須將每個模型,視圖和控制器放在不同的模塊中,以便將其描述爲遵循MVC結構?

+1

在問是否是MVC之前,還有其他一些奇怪的事情要解決(除了縮進之外)。只有靜態方法的類在語義上沒有類,只是函數塞進類定義中。普通的getter和setter是「unpythonic」 - 只是訪問屬性。 'Check'和'View'不應該從'Player'繼承。繼承是一個*是一個* relatationship,既不'檢查'也不''觀看'*是一個'播放器'。如何將類分佈到多個模塊中並不影響MVC模式。 – BlackJack

回答

0

首先,程序並不是真正面向對象的。每當遊戲狀態只需要顯示以調用唯一的方法時,就會立即觸發每個遊戲狀態View。只有__init__()和另一種方法的類是一種代碼異味,因爲在這種情況下,它僅僅是一個沒有充分理由而擠入類的函數。同樣可以說關於Control - 也只是__init__()和一個額外的方法,即在實例化對象之後立即調用一次,直接在調用之後對象不再使用。

考慮到這些「假」類,並考慮靜態方法的數量,看起來你在印象類別下會自動導致面向對象的程序 - 它們不會。使用函數沒有什麼錯,而不是人爲地將不必要的複雜性引入僅僅是語法類的類。函數也是Python中的對象,所以當你有一個顯示函數時,可以將它視爲MVC模式的「視圖」部分。這是一個可調用的對象。

這給我們帶來了MVC問題:假設Player是模型,那麼它不應該通過打印的東西與用戶交互。顯示和輸入是爲了處理視圖。所以Control不是一個控制器,因爲僞裝的功能也通過print()input()與用戶嚴重交互。