2016-10-18 4 views
0

私は関数の引数にリストを入力する練習問題に取り組んでいます。これはチックタックトウボードを表し、ボードの結果を返します。つまり、Xが勝ち、Oが勝ち、Draw、またはNone(ヌル文字列)です。If文をループに変換する

私はそれを解決しましたが、アルゴリズムをループに操作する方法があるかどうかは疑問でした。ループを使用してメイン対角線の各要素をすべて 行と列を交差し、2つの対角線をチェックします。私はPythonを初めて使っているので、私の解決策はもう少し長くなるはずです。 tic tac toe boardの結果をチェックするためにループをどのように実装することができますか?

def gameState (List): 
    xcounter=0 
    ocounter=0 
    if List[0][0]==List[0][1] and List[0][0]==List[0][2]: 
     return List[0][0] 
    elif List[0][0]==List[1][0] and List[0][0]==List[2][0]: 
     return List[0][0] 
    elif List[0][0]==List[1][1] and List[0][0]==List[2][2]: 
     return List[0][0] 
    elif List[1][1]==List[1][2] and List[1][1]==List[1][0] : 
     return List[1][1] 
    elif List[1][1]==List[0][1] and List[1][1]==List[2][1]: 
     return List[1][1] 
    elif List[1][1]==List[0][0] and List[1][1]==List[2][2]: 
     return List[1][1] 
    elif List[2][2]==List[2][0] and List[2][2]==List[2][1]: 
     return List[2][2] 
    elif List[2][2]==List[1][2] and List[2][2]==List[0][2]: 
     return List[2][2] 
    elif List[2][2]==List[1][1] and List[2][2]==List[0][0]: 
     return List[2][2] 
    for listt in List: 
     for elm in listt: 
      if elm=="X" or elm=="x": 
       xcounter+=1 
      elif elm=="O" or elm=="o": 
       ocounter+=1 
    if xcounter==5 or ocounter==5: 
     return "D" 
    else: 
     return '' 
+3

ループを試しましたか?どのような問題がありましたか?また、 'List'は、Pythonの組み込み関数であり、ある時点で、それを組み込んだものを使用すると、問題を引き起こすので、不正な変数名です。 – enderland

+1

これは「動作中の」コードですか?もしそうなら、[Code Review](http://codereview.stackexchange.com/) –

+0

でそれを尋ねてください。入れ子にされた 'for'ステートメントの使用ですでにループが実装されているようです。 –

答えて

5

まずアップ、三目並べで勝つための唯一の方法があります。あなたは9つの比較と返信文を持っていますので、余計です。実際、さらに検討すると、00, 11, 223つの回(ケース3,6,9)、合計でが不一致 02, 11, 20の場合をチェックします。次のように

ループとチェックの観点では、対角線から行/列チェックを分割することができ:これは空のセルの行が直ちにとしてピックアップされていない保証すること

# Check all three rows and columns. 

for idx in range(3): 
    if List[0][idx] != ' ': 
     if List[0][idx] == List[1][idx] and List[0][idx] == List[2][idx]: 
      return List[0][idx] 
    if List[idx][0] != ' ': 
     if List[idx][0] == List[idx][1] and List[idx][0] == List[idx][2]: 
      return List[idx][0] 

# Check two diagonals. 

if List[1][1] != ' ': 
    if List[1][1] == List[0][0] and List[1][1] == List[2][2]: 
     return List[1][1] 
    if List[1][1] == List[0][2] and List[1][1] == List[2][0]: 
     return List[1][1] 

# No winner yet. 

return ' ' 

注誰も勝ちません。あなたは "本当の"プレイヤーによって勝ちをチェックする必要があります。これにより、最初の行の3つの空のセルを検出し、2番目の行にという実際のという勝者がある場合は、それに基づいて表示を戻したくないということです。


もちろん、このようなコードをリファクタリングして読みやすく理解する方法は数多くあります。一つの方法は、単一ラインをチェックするためのロジックを分離し、各ラインのためにそれを呼び出すことです:

# Detect a winning line. First cell must be filled in 
# and other cells must be equal to first. 

def isWinLine(brd, x1, y1, x2, y2, x3, y3): 
    if brd[x1][y1] == ' ': return False 
    return brd[x1][y1] == brd[x2][y2] and brd[x1][y1] == brd[x3][y3] 

# Get winner of game by checking each possible line for a winner, 
# return contents of one of the cells if so. Otherwise return 
# empty value. 

def getWinner(brd): 
    # Rows and columns first. 

    for idx in range(3): 
     if isWinLine(brd, idx, 0, idx, 1, idx, 2): return brd[idx][0] 
     if isWinLine(brd, 0, idx, 1, idx, 2, idx): return brd[0][idx] 

    # Then diagonals. 

    if isWinLine(brd, 0, 0, 1, 1, 2, 2): return brd[1][1] 
    if isWinLine(brd, 2, 0, 1, 1, 0, 2): return brd[1][1] 

    # No winner yet. 

    return ' ' 

次に、あなただけ使用することができます:あなたのコードとあなた'LLで

winner = getWinner(List) 

を勝者を返すか、もし存在しなければ空の指示を返す。