In: Computer Science
Creating a list/tuple in python
2. Using a list of lists containing X’s and O’s to represent a tic
tac toe board, write code to check if the board has a
winner.
Program Code Screenshot :
Sample Output :
Program Code to Copy
def hasWinner(board):
for i in range(len(board)):
winner = True
for j in range(1,len(board[i])):
if not (board[i][j] is board[i][0] and board[i][j] in
('X','O')):
winner = False
break
if winner:
return True
for j in range(len(board[0])):
winner = True
for i in range(1,len(board)):
if not (board[i][j] is board[0][j] and board[i][j] in
('X','O')):
winner = False
break
if winner:
return True
winner = True
for i in range(len(board)):
if not (board[i][i] is board[0][0] and board[i][i] in
('X','O')):
winner = False
break
if winner:
return True
winner = True
for i in range(len(board)):
if not (board[i][len(board)-i-1] is board[0][len(board)-i-1] and
board[i][len(board)-i-1] in ('X','O')):
winner = False
break
return winner