Untitled

 avatar
unknown
python
2 years ago
1.2 kB
5
Indexable
import random

map = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


def win():
    for i in range(3):
        if map[i][0] == map[i][1] == map[i][2]:
            return True
        if map[0][i] == map[1][i] == map[2][i]:
            return True

    if map[0][0] == map[1][1] == map[2][2]:
        return True

    if map[0][2] == map[1][1] == map[2][0]:
        return True

    return False


print("You are O, and computer is X")

while (True):
    for i in range(3):
        for j in range(3):
            print(map[i][j], end=' ')
        print("\n")

    # O(player) move
    choose = int(input("Please enter number to choose step : "))
    choose -= 1
    map[choose // 3][choose % 3] = 'O'

    if win():
        print("O is the winner!")
        break

    # X(computer) move
    while(True):
        choose=random.randint(1,9)
        choose-=1
        
        if map[choose // 3][choose % 3]!='O' and map[choose // 3][choose % 3]!='X':
            break
    
    map[choose // 3][choose % 3]='X'
    
    if win():
        print("X is the winner!")
        break

for i in range(3):
    for j in range(3):
        print(map[i][j], end=' ')
    print("\n")
Editor is loading...