Tik-Tac-Toe.py

 avatar
unknown
python
6 months ago
3.5 kB
4
Indexable
# Name: Spencer Story
# Date: 11/04/24
# Course: CIS-115-801
# PE1 Final Exam Project
# Tik Tac Toe Game!

index_X = 0
index_O = 0

Input_Check_X_isTrue = True
Input_Check_O_isTrue = True


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


def check_win(player):

    if position[1] == position[2] == position[3] == player:
        return True
    if position[4] == position[5] == position[6] == player:
        return True
    if position[7] == position[8] == position[9] == player:
        return True

    if position[1] == position[4] == position[7] == player:
        return True
    if position[2] == position[5] == position[8] == player:
        return True
    if position[3] == position[6] == position[9] == player:
        return True

    if position[1] == position[5] == position[9] == player:
        return True
    if position[3] == position[5] == position[7] == player:
        return True

    return False


Tik_Tac_Toe_Board_X = "X"
Tik_Tac_Toe_Board_O = "O"


def Tik_Tac_Toe_Board():
    Tik_Tac_Toe_Board_Layout = f"""
 {position[1]} | {position[2]} | {position[3]}
___|___|___
 {position[4]} | {position[5]} | {position[6]}
___|___|___
 {position[7]} | {position[8]} | {position[9]}
"""
    print(Tik_Tac_Toe_Board_Layout)


print("<---Welcome to Tic-Tac-Toe by Spencer Story--->")
Tik_Tac_Toe_Board()

for i in range(1, 10):

    if check_win("X"):
        print('Game Over: Player "X" Wins! :D')
        break
    elif check_win("O"):
        print('Game Over: Player "O" Wins! :D')
        break

    if i == 9:
        print("Game Over: Tie! :(")
        break

    if i % 2 != 0:
        print('Make your first move! Press "CRTL + C" to Quit.' if i ==
              1 else 'Player: [X] Your turn! Press "CRTL + C" to Quit.')
        Input_Check_X_isTrue = True

        while Input_Check_X_isTrue:
            input_X = int(input("Enter X's position from [1...9]: "))
            index_X = input_X
            if index_X <= 9 and index_X >= 1:
                if position[index_X] == "X" or position[index_X] == "O":
                    Tik_Tac_Toe_Board()
                    print(
                        f'Error: **Position already exists!** Number entered: ("{index_X}")')
                    continue
                else:
                    position[index_X] = Tik_Tac_Toe_Board_X
                    Tik_Tac_Toe_Board()
                    Input_Check_X_isTrue = False
            else:
                Tik_Tac_Toe_Board()
                print(
                    f'Error: **Please enter number between!** [1...9] ("{index_X}")')
                continue
    else:
        print('Player: [O] Your turn! Press "CRTL + C" to Quit.')
        Input_Check_O_isTrue = True

        while Input_Check_O_isTrue:
            input_O = int(input("Enter O's position from [1...9]: "))
            index_O = input_O
            if index_O <= 9 and index_O >= 1:
                if position[index_O] == "O" or position[index_O] == "X":
                    Tik_Tac_Toe_Board()
                    print(
                        f'Error: **Position already exists!** Number entered: ("{index_O}")')
                    continue
                else:
                    position[index_O] = Tik_Tac_Toe_Board_O
                    Tik_Tac_Toe_Board()
                    Input_Check_O_isTrue = False
            else:
                Tik_Tac_Toe_Board()
                print(
                    f'Error: **Please enter number between!** [1...9] ("{index_O}")')
                continue
Editor is loading...
Leave a Comment