Noughts, Crosses, and Questionable Decisions

Noughts, Crosses, and Questionable Decisions

calendar_today agoschedule8 min read

I made this Tic-Tac-Toe game when I was 14 and still a beginner programmer. At the time, I did not know much about using advance functions, so the code is not the most efficient and has some repeated parts. I made it to strengthen my coding ability and practise using loops, lists, user input, and basic game logic. It may be simple, but every great programmer starts somewhere.

The Good Stuff™

  • Interactive two-player Tic-Tac-Toe gameplay
  • Dynamic board updates after every move
  • Comprehensive win detection across rows, columns, and diagonals
  • Turn-based game logic
  • Built using core Python language features

What Beginner Me Missed

  • Reduce code duplication by refactoring repeated logic into reusable functions def
  • Prevent players from selecting board positions that have already been occupied
  • Provide clearer feedback when an invalid move is entered.
  • Improve code organisation and maintainability through a more modular structure
  • Add a replay option to allow multiple games without restarting the program

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

line = "\n------------------"
straight_line = "|"

player_one = "X"
player_two = "O"
current_player = player_one
print(f"{current_player}, it's your turn")

row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "

print(f"\n{row_one}{line}{row_two}{line}{row_three}")
while True:
    play = int(input(f"\n{current_player}, enter your move (1-9): "))
    play -= 1
    if play == 0 :
        board[0] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 1 :
        board[1] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 2 :
        board[2] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 3 :
        board[3] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 4 :
        board[4] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 5 :
        board[5] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 6 :
        board[6] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 7 :
        board[7] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")
    elif play == 8 :
        board[8] = f" {current_player} "
        row_one =  f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two =  f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one , line , row_two , line, row_three)
        if board[0] == board[1] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[3] == board[4] == board[5]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[7] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[3] == board[6]:
            print(f"\n{current_player} wins!")
            break
        if board[1] == board[4] == board[7]:
            print(f"\n{current_player} wins!")
            break
        if board[2] == board[5] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[0] == board[4] == board[8]:
            print(f"\n{current_player} wins!")
            break
        if board[6] == board[4] == board[2]:
            print(f"\n{current_player} wins!")
            break
        if board[0] != " 1 " and board[1] != " 2 " and board[2] != " 3 " and board[3] != " 4 " and board[4] != " 5 " and \
            board[5] != " 6 " and board[6] != " 7 " and board[7] != " 8 " and board[8] != " 9 ":
            print("\nIt's a tie!")
            break
        if current_player == player_one:
            current_player = player_two
            print(f"\n{current_player} , it's your turn")
        else:
            current_player = player_one
            print(f"\n{current_player} , it's your turn")

    else :
        print("\nEnter Valid Number")
        row_one = f" {board[0]} {straight_line}  {board[1]} {straight_line} {board[2]} "
        row_two = f"\n {board[3]} {straight_line}  {board[4]} {straight_line} {board[5]} "
        row_three = f"\n {board[6]} {straight_line}  {board[7]} {straight_line} {board[8]} "
        print(row_one, line, row_two, line, row_three)

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

I Wrote a Script to Fix Audible's Unreadable PDF Filenames

snapsynapseverified - Apr 20

Implementing Cellular Redundancy: Cross-Cloud Failover with AWS Transit Gateway and Azure ExpressRou

Cláudio Raposo - May 5

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

Learn All Seaborn Graphs in One Blog (With Code + Output)

Aaryan Lunis - Jun 24, 2025
chevron_left
126 Points3 Badges
1Posts
0Comments
1Connections

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!