Ravan's dream is to win Tic-Tac-Toe championship.to accomplish this he practices alone at home to learn the strategies of the game . your task is to identify the cell where Ravan should place his third piece so that he wins the game. Assume that each cell in the tic-tac-toe board is marked with a number as shown in the below
tic tac toe
0 1 2
3 4 5
6 7 8
input:
The first line of input contain two space-seperated integers representing the cells where Ravan has placed his first two pieces
output:
output should be a single line integer representing the cell where ravan should place his final piece
explanation
sample Output 1 :
ravan's first two moves are 0 and 1 so he not 2 to win the game .because it will complete the first row so the out put is 2
sample Output 2 :
ravan's first two moves are 0 and 3 so he not 6 to win the game .because it will complete the first row so the out put is 6
# inp is the input data, 2 numbers separated by a space
inp = input("ravan's first two moves")
# ind defines the separator of these numbers in the string, by condition it is a space
ind = inp.index(' ')
# a and b are the created variables in which the input numbers will be written
# this is done for a convenient solution to the task
a, b = int(inp[:ind]), int(inp[ind+1:])
# I created a board for the convenience of checking by winning data and displaying the missing cell to win
board = (a, b)
# win_coord this is a tuple with winning options
win_coord = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
# using this loop, the input data with the data specified in the win_coord is checked,
# I also decided to take into account that the user can enter not only 0 1, but 1 0,
# and many other options for winning positions, so I swapped the indices through the elif for verification
for each in win_coord:
if board[0] == each[0] and board[1] == each[1]:
print(each[2])
elif board[0] == each[1] and board[1] == each[0]:
print(each[2])
elif board[0] == each[1] and board[1] == each[2]:
print(each[0])
elif board[0] == each[2] and board[1] == each[1]:
print(each[0])
elif board[0] == each[0] and board[1] == each[2]:
print(each[1])
elif board[0] == each[2] and board[1] == each[0]:
print(each[1])
Comments
Leave a comment