Noughts, Crosses, and Questionable Decisions

Noughts, Crosses, and Questionable Decisions

●2 ●10
calendar_today • schedule8 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)

Want more #python & #beginner deep dives?

Join 6,157+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Stop trusting verdicts your policy no longer supports

VEACESLAV - Dec 1

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

When One Python Process Fails, You Need to See All of Them

Tom Smithverified - Nov 23

Stop invoking the whole graph to test one node: unit tests and checkpoints for my LangGraph agent

VEACESLAV - Nov 10

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

snapsynapseverified - Apr 20
chevron_left
213 Points • 12 Badges
4Posts
0Comments
1Connections

Commenters (This Week)

6 comments
5 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!