Search code examples
algorithmtic-tac-toe

How to decide whether a Tic-tac-toe game has ended?


The rule of tic-tac-toe game is as follows: when there are three consecutive x's or o's, or all places have been occupied, the game is over.

For example the following image shows this rule:

this picture

This can be achieved by first using for loop to examine column, row, diagonal, anti-diagnal respectively and then checking whether the whole board has been full. But I think this may cause the code to be very long-winded. Is there any better solution?


Solution

  • Winning only happens once one player has taken a turn. There turn will only affect a row and a column and sometimes diagonals. This means that you only need to check some rather of the rows, columns and diagonals instead of all of them.