井字棋(Python实现)
井字棋
http://www.nowcoder.com/questionTerminal/0375c1d206ef48a3894f72aa07b2fdde
这里我们需要用矩阵中的关键值进行比较,这里取矩阵正对角线上的三个值,比较方式如图:
关键值为A[1][1]
关键值为A[0][0]
关键值为A[2][2]
具体代码如下:
Qipan = [list(input().strip().split()) for i in range(3)] Caipan1,Caipan2,Caipan3 = False,False,False if Qipan[1][1] != 'O' : if (Qipan[0][0] == Qipan[1][1] == Qipan[2][2]) or (Qipan[0][2] == Qipan[1][1] == Qipan[2][0]) : Caipan1 = True elif (Qipan[0][1] == Qipan[1][1] == Qipan[2][1]) or (Qipan[1][0] == Qipan[1][1] == Qipan[1][2]) : Caipan1 = True if Caipan1 : if Qipan[1][1] == 'K' : print('KiKi wins!') if Qipan[1][1] == 'B' : print('BoBo wins!') if Qipan[0][0] != 'O' : if (Qipan[1][0] == Qipan[0][0] == Qipan[2][0]) or (Qipan[0][1] == Qipan[0][0] == Qipan[0][2]) : Caipan2 = True if Caipan2 : if Qipan[0][0] == 'K' : print('KiKi wins!') if Qipan[0][0] == 'B' : print('BoBo wins!') if Qipan[2][2] != 'O' : if (Qipan[2][0] == Qipan[2][2] == Qipan[2][1]) or (Qipan[0][2] == Qipan[2][2] == Qipan[1][2]) : Caipan3 = True if Caipan3 : if Qipan[2][2] == 'K' : print('KiKi wins!') if Qipan[2][2] == 'B' : print('BoBo wins!') if Caipan1 == Caipan2 == Caipan3 == False : print('No winner!')