Noughts and crosses

Noughts and crosses

A game of Tic-Tac-Toe.

Noughts and crosses, also known as Tic-Tac-Toe is a game for two players. It is played on a 3x3 grid of squares. Player one plays with counters marked "X". Player two plays with counters marked "O". The aim of the game is to win by having placed three of your own counters adjacent to each other either horizontally, vertically or diagonally. On a players turn they place just one of their counters onto an empty square.

Make


Write the program to play the game.

Use this boilerplate code as a starting point:

Success Criteria

Remember to add a comment before a subprogram or selection statement to explain its purpose.

  1. In the main program, initialise the game board as a 3x3 array of -1 values.

-1 indicates an empty square, 0 indicates an "O" and 10 indicates an "X".

Numbers will make it easier to calculate if there is a winner.

Complete the subprogram called `get_move` that:

  1. Prompts the user to enter the xy position of where they want to place their counter. E.g. 00 would be the top left corner, 22 would be the bottom right corner.
  2. Checks that a counter is only being placed on an empty square.
  3. Places a counter on a valid square.

Complete the subprogram called `draw_board` that:

  1. Outputs the board for the players using the format shown in the typical inputs and outputs below.

Complete the subprogram called `won` that:

  1. Checks if a player has won, returning True if they have or False if they have not.

Complete the subprogram called `play_game` that:

  1. Plays the game until either a player has won or the game is a draw. The game is a draw when a player cannot place a counter on the board.
  2. Outputs the result of the game.

Complete the `main program` so that:

  1. The `play_game` subprogram is called.

Typical inputs and outputs from the program would be:

0 1 2

0 | |

---------

1 | |

---------

2 | |

Enter xy position: 11

0 1 2

0 | |

---------

1 |X|

---------

2 | |

Enter xy position: 21

0 1 2

0 | |

---------

1 |X|O

---------

2 | |

Enter xy position: 20

0 1 2

0 | |X

---------

1 |X|O

---------

2 | |

Enter xy position: 02

0 1 2

0 | |X

---------

1 |X|O

---------

2 O| |

Enter xy position: 00

0 1 2

0 X| |X

---------

1 |X|O

---------

2 O| |

Enter xy position: 10

0 1 2

0 X|O|X

---------

1 |X|O

---------

2 O| |

Enter xy position: 22

0 1 2

0 X|O|X

---------

1 |X|O

---------

2 O| |X

X wins.

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Create the unit tests below to check that your program has met the success criteria.

  1. The game is a draw
  2. Invalid move
  3. O wins diagonal
  4. O wins horizontal
  5. O wins vertical
  6. X wins diagonal
  7. X wins horizontal
  8. X wins vertical

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Craig'n'Dave logo

Craig 'n' Dave

Bett Awards 2024 Finalist