2017-04-21 2 views
0

私は割り当てのために三目並べゲームを書いています。オブジェクト指向プログラミングを使用する必要があります。パイソン - OOPチックタックトー

私の問題は、ボードがいっぱいであるかどうかをチェックして、同じプレーヤーでゲームを再開する方法を実装することです。 私はすでに、すべてのフィールドがいっぱいしかし、誰成功しているかどうかをチェックする、活躍の場を反復処理しようとしています。

私はまた、勝利した選手を宣伝し、占拠した選手を選んだ場合、前の選手を昇進させることにいくつか問題があります。私は、プレイヤーやターンを追跡するために私の主な機能のforループを変更する必要があると思うが、私はどのように手掛かりがありません。

class Player(object): 
    def __init__(self, name, symbol, initial_score=0): 
     self.name= name 
     self.symbol= symbol 
     self.score= initial_score 

    def won_match(self): 
     self.score+= 100 

    def lost_match(self): 
     self.score-= 50 

    def show_score(self): 
     print('Player {}: {} points'.format(self.name, self.score)) 

class PlayingField(object): 
    def __init__(self): 
     self.field= [ 
        [None, None, None], 
        [None, None, None], 
        [None, None, None] 
        ] 

    def show_field(self): 
     for row in self.field: 
      for player in row: 
       print('_' if player is None else player.symbol,end=' ') 
      print() 

    def set_player(self, x, y, player): 
     if self.field[y][x] is not None: 
      return False 

     self.field[y][x]= player 

     return True 

    def full_board(self): 
    for row in self.field: 
     for col in row: 
      if col is None: 
       return False 
     return True 

    def check_won(self, x, y, player): 
    if self.field[0][x] == player and self.field[1][x] == player and self.field[2][x] == player: 
     return True 
    elif self.field[y][0] == player and self.field[y][1] == player and self.field[y][2] == player: 
     return True 
    elif self.field[0][0] == player and self.field[1][1] == player and self.field[2][2] == player: 
     return True 
    elif self.field[0][2] == player and self.field[1][1] == player and self.field[2][0] == player: 
     return True 
    else: 
     return False 


def main(): 
    name_1= input('Name of Player 1: ') 
    name_2= input('Name of Player 2: ') 

    players= [ 
       Player(name_1, 'X'), 
       Player(name_2, 'O') 
       ] 

    field= PlayingField() 

    while True: 
     for player in players: 
      field.show_field() 

      x= int(input('Player {} choose your column: '.format(player.name))) - 1 

      y= int(input('Player {} choose your row: '.format(player.name))) - 1 

      if not field.set_player(x, y, player): 
       print('That field is already occupied.') 

      elif field.full_board(): 
      field.show_field() 
      print('full board') 
      for player in players: 
       print('{}: {}'.format(player.name, player.score)) 
      field= PlayingField() 

      elif field.check_won(player): 
       field.show_field() 
       print('Player {} won the game.'.format(player.name)) 
       print('Score') 
       for player in players: 
        if field.check_won(player) == True: 
         player.won_match() 
        elif field.check_won(player) == False: 
         player.lost_match() 
        print('{}: {}'.format(player.name, player.score)) 
       field= PlayingField() 

if __name__ == '__main__': 
    main() 
+3

「私はすでに、すべてのフィールドがいっぱいしかし、誰成功しているかどうかをチェックする、活躍の場を反復処理しようとしています。」これは有効なアプローチです*どのように動作しませんでしたか? – timgeb

+0

有効な「完全なボード」をハードコーディングするのではなく、 'check_won'のための再帰的な解決策を考えるべきです[tic-tac-toeのリファレンス](https://inventwithpython.com/chapter10.html) – GiantsLoveDeathMetal

+0

です。あなたのソリューションは、サイズN X Nのボードは、nクイーン問題をチェックするためにスケールしません、それは三目並べボードを解決するための同様のロジックを持っています。 – chukkwagon

答えて

0

ボードがいっぱいになっているかどうかを確認するには、すべての要素を反復して、非None値をチェックするだけです。

あなたは現在

def full_board(self): 
    for row in self.field: 
     for col in self.field: 
      if col == '_': 
       return False 
      else: 
       return True 

がself.fieldはリストのリストである、覚えています。私たちがやりたいことは、3つのリストのすべての要素を調べることです。

def full_board(self): 
    for row in self.field: # all good so far 
     for col in row: # <-- now we're iterating through each list in self.field 
     if col is None: 
      return False # the board is not full if there's at least one empty space 
    return True # we looked at every element and they were all occupied 

あなたの間違いは早すぎます。あなたのバージョンでは、の1つをの要素にチェックし、それに基づいて真偽値を決定します。要素のすべてをチェックするためには、forループの後にTrueを返す必要があります。

選択された場所の検証に関しては、位置が有効になるまで継続的にプロンプ​​トを表示するだけです。あなたのwhileループで

、あなたはここでは、この

x, y = prompt_for_position() 

while not pos_is_valid(x,y): 
    x, y = prompt_for_position() 

# now we can be sure that the chosen position is valid 

のようなものを持っている可能性があり、prompt_for_positionはただのx/yをユーザに求めることができ、そしてそれはだ場合pos_is_validはあなたをxとyに取ると告げることができます有効な位置。 (なし/空の四角)

うまくいけば、これはあなたにとって便利でした!

+0

すべての最初の感謝を修正するためにどのようにそれを得ることはありません。私はコードを編集しました。私はfull_boardメソッドで何が間違っていたのか理解していますが、あなたの説明の下の部分は理解できません。私は私の主な機能でメソッドを実装しようとしました。それは何とか働く。しかし、最初の行がシンボルでいっぱいになるたびに、彼は真を返し、私は理由を説明しません。私は2週間前にPythonでコーディングを始めましたが、コードの半分は私の家庭教師からのものです。 – Hans

+0

あなたのfull_boardメソッドのインデントがオフになりました。 return Trueが両方のループの外側にないことを確認してください。あなたの戻り値Trueがループの1つの内部にある場合、それは1行/ colで繰り返し処理してから復帰することを意味します。 – chatton