Yikes. *multiple deep breaths* So building a game of tic-tac-toe is supposed to be a pretty straight forward programming task, but… this is going to be a bit of a challenge for me.

Current status: I’ve reached exercise 37 of 52 in Learn Python the Hard Way. I don’t know if I’ve got enough down now to do this. Probably not? But okay — let’s try anyway. Attempting to write that “cheese cake recipe“ too early was kinda fun, so let’s do the same with this.

Humble beginnings 🌱

Boiled down to the absolute bare minimum, my script needs to:

  • know there are 9 positions
  • know there are 2 players
    • and which player’s turn it is
    • and who holds which position
  • be able to recognize three-in-a-row
    • know about vertical, horizontal or diagonal
    • determine a win or a draw
  • ask the players for input
  • print a result

(And then next the script should know if a position is occupied, so players can’t make impossible moves. But I can actually think of that as an enhancement, it’s not critical.)

But where to start? 😱

print("Welcome! Let’s play a game of tic-tac-toe.")

playerA = input("X: ")
print(f"Player X: {playerA}")

playerB = input("0: ")
print(f"Player 0: {playerB}")

I’m already wondering if I got the concept of positions wrong, if that is more complicated than it needs to be, since this is neither chess nor othello. Will it be even simpler if the script at first just needs to know about the players taking 9 turns? 🤔

print("Welcome! Let’s play a game of tic-tac-toe.")

playerA1 = input("X: ")
print(f"Player X: {playerA1}")

playerB2 = input("0: ")
print(f"Player 0: {playerB2}")

playerA3 = input("X: ")
print(f"Player X: {playerA3}")

playerB4 = input("0: ")
print(f"Player 0: {playerB4}")

playerA5 = input("X: ")
print(f"Player X: {playerA5}")

playerB6 = input("0: ")
print(f"Player 0: {playerB6}")

playerA7 = input("X: ")
print(f"Player X: {playerA7}")

playerB8 = input("0: ")
print(f"Player 0: {playerB8}")

playerA9 = input("0: ")
print(f"Player X: {playerA9}")

print("Done! Sry, I have no idea yet if anyone won.")

That looks like it could benefit from getting a list.

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

moves[0] = input("X: ")
print(f"Player X: {moves[0]}")

moves[1] = input("0: ")
print(f"Player 0: {moves[1]}")

# and so on!

print("Still no idea if anyone won.")

…and then a loop next?!

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

for playermove in moves:
    playermove = input(">>> ")
    print(f"Player X or 0 said: {playermove}")

print("Yeah sry not getting any smarter here.")

Omg. It works. Haha! It’s the most basic script ever that hasn’t really got a clue about anything, but it’s some sort of start. Next tiny step…? Maybe storing input from the players?!


I peaked ahead in LPTHW and the next exercise 38 is called “Doing Things to Lists”. Yeah, that sounds like it could be somewhat useful to learn before I proceed… 😆

(Yes. Yes, it was… Very useful.)

moves = [ ]

for playermove in range(0,9):
    playermove = input(">>> ")
    print(f"Player X or 0 said: {playermove}")
    moves.append(playermove)

# check that the list contains actual player input
print(f"{moves}") # yay! now it does

I’ve now got a tiny super simple script that can…
…promt for input in the console 9 times and store that in a list.

To do next ✏️

  • The script needs to differentiate between the two players.
  • And start learning some rules of the game.