2016-04-11 11 views
0

私はいくつかの場所で呼び出すwinningという関数でPythonでtic tac toeゲームを書いています。 winning_human関数から呼び出すと正常に動作しているように見えますが、コンピュータ(AI)部分に使用すると、すべてfalseが返されます。何か案は?入れ子関数が動作しない - Tic Tac Toeゲーム

import pprint 

class Game(object): 

    def __init__(self, player, board): 
     self.player = player   
     self.board = board 

    def print_board(self): 
     print(self.board[1]+ '|' + self.board[2]+ '|' + self.board[3]) 
     print('-----') 
     print(self.board[4]+ '|' + self.board[5]+ '|' + self.board[6]) 
     print('-----') 
     print(self.board[7]+ '|' + self.board[8]+ '|' + self.board[9]) 

    def play(self,board): 
     self.print_board() 
     print('specify your move (1,2,3,4,5,6,7,8,9)') 
     self.move = int(input()) 
     valid_moves = [1,2,3,4,5,6,7,8,9] 
     if self.move not in valid_moves: 
      print('you did not specify a valid move, please try again!') 
      self.play(self.board) 
     if self.board[self.move] != ' ': 
      print ('you can not play that space, it is taken') 
      self.play(self.board)   
     self.board[self.move]='X' 
     self.print_board() 
     self.winning_human('X', self.board) 


    def winning(self, player, board): 
     return ((self.board[1]==self.player and self.board[2]==self.player and self.board[3]==self.player)or   
     (self.board[4]==self.player and self.board[5]==self.player and self.board[6]==self.player)or 
     (self.board[7]==self.player and self.board[8]==self.player and self.board[9]==self.player)or   
     (self.board[1]==self.player and self.board[4]==self.player and self.board[7]==self.player)or  
     (self.board[2]==self.player and self.board[5]==self.player and self.board[8]==self.player)or 
     (self.board[3]==self.player and self.board[6]==self.player and self.board[9]==self.player)or 
     (self.board[1]==self.player and self.board[5]==self.player and self.board[9]==self.player)or 
     (self.board[3]==self.player and self.board[5]==self.player and self.board[7]==self.player)) 

    def winning_human(self, player, board): 
     if self.winning('X', self.board): 
      True 
      print('you won') 
     else: 
      print('the game shall cont (winning human funct).') 
      self.comp_play('O', self.board) 

    def comp_play(self, player, board): 
     for i in range(1,10): 
      if self.board[i] == ' ': 
       self.board[i] = 'O' 
       if self.winning('O',self.board): 
        self.end_game(self) 
       else: 
        self.board[i] = ' ' 

     #check if player can win in next move, block if so 
     #check if middle is free 
     #check if corner is free  


    def end_game(self): 
     self.print_board() 
     print('Thanks for playing, the computer beat you')   

    theBoard = {1:' ', 2:'O', 3:'O', 4: ' ', 5:'X', 6: 'X', 7:' ', 8:' ', 9:' '} 
    g=Game('X', theBoard) 
    g.play(theBoard) 

答えて

0

あなたは(常にXである)winning方法に(XまたはO)をチェックしたが、その後、そのメソッド内だけself.playerを使用するようにプレーヤーを渡します。

+0

ありがとうございます! –