Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Python tic-tac-toe for two players with a win checker

A list of nine cells holds the board and a list of eight winning lines is checked after each move. It shows how to model a game with data.

What it shows

A list of nine cells holds the board and a list of eight winning lines is checked after each move. It shows how to model a game with data.

The science

The board is a list of nine items indexed 0 to 8. There are exactly eight ways to win: three rows, three columns and two diagonals, and each is a set of three indexes. After each move the program checks whether all three cells of any line hold the same player’s mark. If the board fills with no winner it is a draw.

Materials

Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • A computer, laptop or phone with a browser (free)
  • Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
  • A sheet of paper with numbers 1 to 9 on a 3 by 3 grid
  • A notebook and a pencil

Safety

This project uses safe, everyday materials. Still, read the rules below with a parent before you start.

Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.

All the safety rules

Step by step

8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.

  1. Create ttt.py and write board = [" "] * 9 to make nine empty cells.

  2. Define def show(): with print(board[0], "|", board[1], "|", board[2]) and the same for the rows 3 to 5 and 6 to 8, with a line of dashes between rows.

  3. Write lines = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6)].

  4. Define def winner(p): returning any(all(board[i] == p for i in line) for line in lines).

  5. Set player = "X" and start for turn in range(9): then show().

  6. Ask pos = int(input(player + " move (1 to 9): ")) - 1 and check it is between 0 and 8 and that board[pos] == " "; otherwise ask again with a while loop.

  7. Set board[pos] = player, then if winner(player): show(), print(player, "wins!") and break.

  8. Switch players with player = "O" if player == "X" else "X" and after the loop print Draw if no one won (use for else).

What you should see

Two players alternate marking squares, the board is printed each turn, and the game announces a winner or a draw.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

1How many winning lines are there?

Eight: three rows, three columns and two diagonals.

2Why subtract 1 from the input?

Players type 1 to 9 but list indexes start at 0.

3What does all() do?

It is True only when every item is True.

4How is a draw detected?

All nine moves are played and nobody has won.

Free ebook: Viva Questions for Science Projects

Take it further

Write a computer player that takes a winning move if it can, or blocks yours, otherwise picks randomly.

Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.

Free ebooks for this project

WhatsApp us