Search code examples
c#mathformulatic-tac-toe

Is there a mathematical formula to determine how many diagonal sequences are there in a Tic-Tac-Toe game? C#


I'm building a Tic-Tac-Toe game project with C#. My teacher asked me to create an array while its size is the max number of diagonal sequences that are possible in the specific board size (Not only 3x3). Problem now, is that my teacher gave me a wrong formula and it won't calculate it right. For example, in a 3x3 board, there should be 2 possible diagonal sequences, but the formula calculates only 1.

This is the formula: (rows-sequence+1)*(cols-sequence+1).

Which means: (3-3+1)*(3-3+1) = 1

If someone knows the correct formula, I'll be grateful!


Solution

  • Think about it like this. How many ways are there to place seq * seq square within row * col rectangle. Teacher gave you that answer. There are two diagonals in any square, so number of what you call diagonals in row * col matrix are just 2 times the value that you get with teachers function.