ticTackToe

 avatar
user_7676782
python
2 years ago
7.2 kB
10
Indexable
Never
import turtle


def checkWinner(p11, p12, p13, p21, p22, p23, p31, p32, p33):
    if p11 == p12 == p13:
        turtle.goto(-250, 200)
        turtle.color("red")
        turtle.pendown()
        turtle.forward(500)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True
    if p21 == p22 == p23:
        turtle.goto(-250, 0)
        turtle.color("red")
        turtle.pendown()
        turtle.forward(500)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True
    if p31 == p32 == p33:
        turtle.goto(-250, -200)
        turtle.color("red")
        turtle.pendown()
        turtle.forward(500)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True
    if p11 == p21 == p31:
        turtle.goto(-200, 250)
        turtle.color("red")
        turtle.right(90)
        turtle.pendown()
        turtle.forward(500)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True
    if p12 == p22 == p32:
        turtle.goto(0, 250)
        turtle.color("red")
        turtle.right(90)
        turtle.pendown()
        turtle.forward(500)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True
    if p13 == p23 == p33:
        turtle.goto(200, 250)
        turtle.color("red")
        turtle.right(90)
        turtle.pendown()
        turtle.forward(500)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True
    if p11 == p22 == p33:
        turtle.goto(-250, 250)
        turtle.color("red")
        turtle.right(45)
        turtle.pendown()
        turtle.forward(700)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True
    if p13 == p22 == p31:
        turtle.goto(250, 250)
        turtle.color("red")
        turtle.right(135)
        turtle.pendown()
        turtle.forward(700)
        if p11 == 'X':
            print("Player 1 win")
        else:
            print("player 2 win")
        return True


def drawCircle(x, y):
    turtle.goto(x, y)
    turtle.pendown()
    turtle.circle(50)
    turtle.penup()


def drawX(x1, x2, y):
    turtle.goto(x1, y)
    turtle.right(45)
    turtle.pendown()
    turtle.forward(150)
    turtle.penup()
    turtle.goto(x2, y)
    turtle.right(90)
    turtle.pendown()
    turtle.forward(150)
    turtle.left(135)
    turtle.penup()


def drawBoard():
    turtle.setup(600, 600)
    turtle.hideturtle()
    turtle.speed(9)
    turtle.penup()
    turtle.goto(-100, 300)
    turtle.pendown()
    turtle.right(90)
    turtle.forward(800)
    turtle.penup()
    turtle.goto(100, 300)
    turtle.pendown()
    turtle.forward(800)
    turtle.penup()
    turtle.left(90)
    turtle.goto(-300, 100)
    turtle.pendown()
    turtle.forward(800)
    turtle.penup()
    turtle.goto(-300, -100)
    turtle.pendown()
    turtle.forward(800)
    turtle.penup()


def main():
    drawBoard()
    #  we initialize the variables to avoid false winner
    e11 = 1
    e12 = 2
    e13 = 1
    e21 = 2
    e22 = 0
    e23 = 2
    e31 = 1
    e32 = 2
    e33 = 1
    isWinner = False
    i = 1
    #  We get an input form the player where to set X or a circle 
    #  and draw the shape according to the player's request
    #  we also initialize variable with X or circle("O")
    while i <= 9:
        playerTurn = (input("please mark the requested slot \n"
                            "for example '13' mean 1 row and 3 col - "))

        if playerTurn == '11':
            if i % 2 == 0:
                e11 = "O"
                x = -200
                y = 150
                drawCircle(x, y)
            else:
                e11 = "X"
                x1 = -250
                x2 = -150
                y = 250
                drawX(x1, x2, y)

        if playerTurn == '12':
            if i % 2 == 0:
                e12 = "O"
                x = 0
                y = 150
                drawCircle(x, y)
            else:
                e12 = "X"
                x1 = -50
                x2 = 50
                y = 250
                drawX(x1, x2, y)
        if playerTurn == '13':
            if i % 2 == 0:
                e13 = "O"
                x = 200
                y = 150
                drawCircle(x, y)
            else:
                e13 = "X"
                x1 = 150
                x2 = 250
                y = 250
                drawX(x1, x2, y)
        if playerTurn == '21':
            if i % 2 == 0:
                e21 = "O"
                x = -200
                y = -50
                drawCircle(x, y)
            else:
                e21 = "X"
                x1 = -250
                x2 = -150
                y = 50
                drawX(x1, x2, y)
        if playerTurn == '22':
            if i % 2 == 0:
                e22 = "O"
                x = 0
                y = -50
                drawCircle(x, y)
            else:
                e22 = "X"
                x1 = -50
                x2 = 50
                y = 50
                drawX(x1, x2, y)
        if playerTurn == '23':
            if i % 2 == 0:
                e23 = "O"
                x = 200
                y = -50
                drawCircle(x, y)
            else:
                e23 = "X"
                x1 = 150
                x2 = 250
                y = 50
                drawX(x1, x2, y)
        if playerTurn == '31':
            if i % 2 == 0:
                e31 = "O"
                x = -200
                y = -250
                drawCircle(x, y)
            else:
                e31 = "X"
                # x1 = 150
                # x2 = 250
                # y = 250
                x1 = -250
                x2 = -150
                y = -150

                drawX(x1, x2, y)
        if playerTurn == '32':
            if i % 2 == 0:
                e32 = "O"
                x = 0
                y = -250
                drawCircle(x, y)
            else:
                e32 = "X"

                x1 = -50
                x2 = 50
                y = -150
                drawX(x1, x2, y)
        if playerTurn == '33':
            if i % 2 == 0:
                e33 = "O"
                x = 200
                y = -250
                drawCircle(x, y)
            else:
                e33 = "X"
                x1 = 150
                x2 = 250
                y = -150
                drawX(x1, x2, y)
        if i > 4:
            isWinner = checkWinner(e11, e12, e13, e21, e22, e23, e31, e32, e33)
            if isWinner:
                break
        i += 1

    if not isWinner:
        print("NO winner")
    

main()