2016-08-19 87 views
-3

問題是,它沒有檢測到平局,只是掛起,如果沒有人勝利。我明白,在這裏粘貼整個劇本並尋求幫助並不是一個好習慣,但我完全沒有想法。我將這個TicTacToe腳本從github上取下來,並試圖在兩個AI玩家之間實現隨機移動(都只是隨機移動)。雖然循環沒有正確地打破,腳本掛起

import random 
    import time 


class Tic(object): 
winning_combos = (
    [0, 1, 2], [3, 4, 5], [6, 7, 8], 
    [0, 3, 6], [1, 4, 7], [2, 5, 8], 
    [0, 4, 8], [2, 4, 6]) 

    winners = ('X-win', 'Draw', 'O-win') 

    def __init__(self, squares=[]): 
     if len(squares) == 0: 
      self.squares = [" " for i in range(9)] 
     else: 
      self.squares = squares 

def show(self): 
    for element in [self.squares[i:i + 3] for i in range(0, len(self.squares), 3)]: 
     print(element) 

def available_moves(self): 
    """what spots are left empty?""" 
    return [k for k, v in enumerate(self.squares) if v is " "] 

def available_combos(self, player): 
    """what combos are available?""" 
    return self.available_moves() + self.get_squares(player) 

def complete(self): 
    """is the game over?""" 
    if " " not in [v for v in self.squares]: 
     return True 
    if self.winner() != " ": 
     return True 
    return False 

def X_won(self): 
    return self.winner() == 'X' 

def O_won(self): 
    return self.winner() == 'O' 

def tied(self): 
    return self.complete() is True and self.winner() is " " 

def winner(self): 
    for player in ('X', 'O'): 
     positions = self.get_squares(player) 
     for combo in self.winning_combos: 
      win = True 
      for pos in combo: 
       if pos not in positions: 
        win = False 
      if win: 
       return player 

    return " " 

def get_squares(self, player): 
    """squares that belong to a player""" 
    return [k for k, v in enumerate(self.squares) if v == player] 

def make_move(self, position, player): 
    """place on square on the board""" 
    self.squares[position] = player 


def determine(board, player): 
    a = -2 
    choices = [] 
    if len(board.available_moves()) == 9: 
     return 4 
    for move in board.available_moves(): 
     board.make_move(move, player) 
     board.make_move(move, " ") 
     if val > a: 
      a = val 
      choices = [move] 
     elif val == a: 
      choices.append(move) 
    return random.choice(choices) 


def get_enemy(player): 
    if player == 'O': 
     return 'X' 
    return 'O' 


board = Tic() 

count = 0 
player = 'X' 

while not board.complete(): 
     if board.complete(): 
      break 
     while count == 0: 
      player_move = int(random.randint(1, 9)) 
      if player_move not in board.available_moves(): 
       continue 
      board.make_move(player_move, player) 

      player = get_enemy(player) 
      count += 1 

     while count == 1: 
      computer_move = int(random.randint(1, 9)) 
      if computer_move not in board.available_moves(): 
       continue 
      board.make_move(computer_move, player) 

      count -= 1 

     if board.complete(): 
      break 


if board.complete(): 
     print("winner is", board.winner()) 
     final_win = "winner is " + board.winner() 
     log = open("log_for_orig.txt", "a") 
     log.write(final_win + "\n" + "\n") 
+1

嘗試削減你的代碼拿出[MCVE。這將幫助你縮小問題範圍,並幫助我們給你建議。 – Praveen

回答

0

播放塊

while count == 0: 
     player_move = int(random.randint(1, 9)) 
     if player_move not in board.available_moves(): 
      continue 

計算機塊

while count == 1: 
     computer_move = int(random.randint(1, 9)) 
     if computer_move not in board.available_moves(): 
      continue 

問題上面的代碼的兩個塊在於英寸

在第一個塊中,當count爲0,並且player_move不在可用的移動中時,則通過繼續跳過,因此count仍然爲0,這將使其進入相同的第一個塊,並且該模式繼續除非玩家改變他的舉動。你應該基本上提示用戶改變他的舉動而不是繼續。

同樣適用於第二塊代碼。當電腦在玩時,你應該以更好的方式處理它。

+0

玩家移動不是來自用戶,而是來自'random.randint(1,9)'。看起來這個想法是,它應該繼續採取行動,直到找到可用的。然後它會通過這個並執行改變'count'的代碼。 – Barmar

+0

@Barmar然後我覺得你應該讓玩家移動部分去分離函數,然後繼續調用它,而不是讓它穿過整個while循環。更好的方法是從board.available_moves()中隨機選擇,而不是從'int(random.randint(1,9))' –

+0

我不需要做任何事情,這不是我的代碼。 – Barmar

1

這是選擇一個可用的移動非常好辦法:

while count == 0: 
    player_move = int(random.randint(1, 9)) 
    if player_move not in board.available_moves(): 
     continue 

時有很多可用的舉動它將工作確定。但是當可用的移動很少時,random.randint()可能需要很長時間才能選擇其中一個,因此您的程序可能會掛起。

random模塊提供了直接從列表中選擇元素的功能。

if count == 0: 
    player_move = random.choice(board.available_moves()) 

How to randomly select an item from a list?