2017-02-24 80 views
0

我设法让2个玩家轮流玩,但在第二个玩家输入她的答案后停止。我想做While循环,但是我很认真地跑出了想法。我把它放在脑海里,就像我希望两个玩家一直玩,直到0或3的位置被填满,但是把它放在代码中,我真的需要帮助。如何使用while循环满足许多条件?#

下面是我的病情,但我希望它继续询问玩家,直到这些条件都满足

if list1[0,1,2] == 'x': 
print ('Congrats! x won!') 
elif (list1 [0] , list2 [0] , list3[0]) == 'x': 
print ('Congrats! x won!') 
elif (list1 [0] , list2 [1], list3[2]) == 'x': 
print ('Congrats! x won!') 
elif (list2 [0] , list2 [1], list2[2])== 'x': 
print ('Congrats! x won!') 
elif (list3 [0] , list3 [0], list3[0]) == 'x': 
print ('Congrats! x won!') 
elif (list1 [1] , list2 [1], list3[1]) == 'x': 
print ('Congrats! x won!') 
elif (list1 [2] , list2 [2], list3[2]) == 'x': 
print ('Congrats! x won!') 
elif (list1 [0] , list2 [1], list3[0]) == 'x': 
print ('Congrats! x won!') 
elif (list1 [1] , list2 [1], list3[1]) == 'x': 
print ('Congrats! x won!') 
elif (list1 [2] , list2 [2], list3[2]) == 'x': 
print ('Congrats! x won!') 
elif (list1 [0] , list2 [0], list3[0]) == 'x': 
print ('Congrats! x won!') 
elif (list1[0,1,2] == 'o':) 
print ('Congrats! o won!') 
elif (list1 [0] , list2 [0] ,list3[0]) == 'o': 
print ('Congrats! o won!') 
elif (list1 [0] , list2 [1], list3[2]) == 'o': 
print ('Congrats! o won!') 
elif (list2 [0] , list2 [1], list2[2]) == 'o': 
print ('Congrats! o won!') 
elif (list3 [0] , list3 [0], list3[0]) == 'o': 
print ('Congrats! o won!') 
elif (list1 [1] , list2 [1], list3[1]) == 'o': 
print ('Congrats! o won!') 
elif (list1 [2] , list2 [2], list3[2]) == 'o': 
print ('Congrats! o won!') 
elif (list1 [0] , list2 [1], list3[0]) == 'o': 
print ('Congrats! o won!') 
elif (list1 [1] , list2 [1], list3[1]) == 'o': 
print ('Congrats! o won!') 
elif (list1 [2] , list2 [2], list3[2]) == 'o': 
print ('Congrats! o won!') 
elif (list1 [0] , list2 [0], list3[0]) == 'o': 
print ('Congrats! o won!') 
else 
+0

等待,是一个井字棋有3列,而你检查是否有人赢了? –

+0

你的任何条件都不会是真的,因为你正在比较不同的对象。我建议你看看[python教程](https://docs.python.org/2/tutorial/) –

+0

是的,我使用了3个列表。 list1,list2和list3 – sarah

回答

2

它会更容易让你的板为3只列出一个清单(每个3元)。

随着allany,就可以避免许多不必要的重复:

board = [['x', None, 'o'], 
     ['o', 'x', None], 
     ['o', None, 'x'] 
     ] 


def three_in_row(board, player): 
    return any(all(board[j][i] == player for i in range(3)) for j in range(3)) 


def three_in_column(board, player): 
    return any(all(board[i][j] == player for i in range(3)) for j in range(3)) 


def three_in_diagonal(board, player): 
    return all(board[i][i] == player for i in range(3)) or\ 
     all(board[i][2 - i] == player for i in range(3)) 


print three_in_row(board, 'x') 
# False 
print three_in_column(board, 'x') 
# False 
print three_in_diagonal(board, 'x') 
# True 
0

试试这个:

def check_line(line): 
    if line[0]==line[1] and line[1]==line[2]: 
     return line[0] 
    return ' ' 

def check_win(board): 
    #check horizontals 
    for row in board: 
     res=check_line(row) 
     if res != ' ': 
      return res 

    #check verticals 
    x=0 
    for col in board[0]: 
     res=check_line([board[y][x] for y in range(0, 3)]) 
     x+=1 
     if res != ' ': 
      return res 

    #check diagonals 
    for diagonal in ([board[0][0], board[1][1], board[2][2]], [board[0][2], board[1][1], board[2][0]]): 
     res = check_line(diagonal) 
     if res != ' ': 
      return res 

    return ' ' 

def show_winner(list1, list2, list3): 
    winner=check_win([list1, list2, list3]) 
    if winner!=" ": 
     print("Congrats! {0} won!".format(winner)) 

list1=['o', 'x', 'x'] 
list2=['o', 'x', ' '] 
list3=['x', 'o', ' '] 
show_winner(list1, list2, list3) 
相关问题