A downloadable game

I made Knots and Crosses (Tic Tac Toe) in my computing class for python. It doesn't have ai because I'm not smart enough but it works for 2 players, enjoy :)

Source:

import random
# check if a number is an integer
def isInt(x):
    for y in x:
        if ord(y) < 48 or ord(y) > 57:
            return False
    return True
# prints the table
def prtTable():
    print(f"    a   b   c  ")
    print(f"  ┌───┬───┬───┐")
    print(f"1 │ {data[0]} │ {data[1]} │ {data[2]} │")
    print(f"  ├───┼───┼───┤")
    print(f"2 │ {data[3]} │ {data[4]} │ {data[5]} │")
    print(f"  ├───┼───┼───┤")
    print(f"3 │ {data[6]} │ {data[7]} │ {data[8]} │")
    print(f"  └───┴───┴───┘")    
# title ascii art
print("█ █▀ █  █ ▄██▄ █████  ███   ▄▀█     ▄███ ███▄ ▄██▄  ███  ███ ████  ███")
print("██▀  ██▄█ █  █   █   ██▄     █▀ ▄   █▀   █  █ █  █ ██▄  ██▄  █▄▄  ██▄ ")
print("█▀█▄ █ ██ █  █   █    ▀██   █▀██    █▄   ███▄ █  █  ▀██  ▀██ █▀▀   ▀██")
print("█  █ █  █ ▀██▀   █   ███    ▀██ █   ▀███ █  █ ▀██▀ ███  ███  ████ ███")
print("By tomekk06")
# game loop
while True:
    input("Press enter to start")
    data = [" "]*9 # clearing previous game
    turn = random.randint(0,1) # choosing random player
    end = 0
    
    while end != 1:
        # print table
        prtTable()
        if turn == 0:
            print("X's turn")
        else:
            print("O's turn")
        # get position from input
        while True:
            select = input("Enter position: ")
            result = -1
            if select == "":
                select = " "
            # translate position from input to an array
            if select[0] == "a":
                result = 0
            if select[0] == "b":
                result = 1
            if select[0] == "c":
                result = 2
            # input validation
            if result != -1 and isInt(select[1]) == True:
                result += 3*(int(select[1])-1)
                if result >= 0 and result <=8 and data[result] == " ":
                    break
            print("Invalid input")
        # insert correct marking onto array
        if turn == 0:
            data[result] = "X"
        else:
            data[result] = "O"
        turn = 1 - turn
        # array of possible win combinations
        winFlags = [
            [0,3,6],
            [1,4,7],
            [2,5,8],
            [0,1,2],
            [3,4,5],
            [6,7,8],
            [0,4,8],
            [2,4,6],
        ]
        # checks if any of the patterns from the array above apear in the game
        for x in winFlags:
            count = 0
            for y in x:
                if data[y] == "X":
                    count += 1
                else:
                    break
            if count == 3:
                prtTable()
                print("X wins!")
                end = 1
                break
        for x in winFlags:
            count = 0
            for y in x:
                if data[y] == "O":
                    count += 1
                else:
                    break
            if count == 3:
                prtTable()
                print("O wins!")
                end = 1
                break
        # if all boxes are filled without a winner then it declears a draw
        count = 0
        for x in data:
            if x != " ":
                count += 1
            if count == 9:
                end = 1
                prtTable()
                print("Draw!")
                break

Download

Download
knotsAndCrosses.py 3 kB

Install instructions

Download and install python from the python.org or Microsoft Store

Alternatively, visit Online Python to use python in your browser.

Leave a comment

Log in with itch.io to leave a comment.