2

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!

Arad55
  • 25
  • 1
  • 4
  • 1
    Have you asked your teacher to provide the correct formula? – Jontatas Nov 19 '14 at 12:52
  • 2
    Are you actually asking how many diagonals there are in a square...? – Shai Nov 19 '14 at 12:52
  • 4
    Just multiply that with two. Formula is correct for one direction, but there are two directions. – Dialecticus Nov 19 '14 at 12:52
  • Why do you need a formula, by the way? There will always be 2 diagonals on a square – Matias Cicero Nov 19 '14 at 12:54
  • @Shai - I'm not asking how many diagonals there are in a square, my Tic-Tac-Toe game could also be 6x9 so its difficult. – Arad55 Nov 19 '14 at 12:55
  • @Arad55 ok, so what are you looking for then? :-) – Shai Nov 19 '14 at 12:56
  • There are still 2 diagonals in a rectangle. Actually there are 2 diagonals for every M x N matrix – Matias Cicero Nov 19 '14 at 12:57
  • 3
    @MatiCicero first not necessarily a square, second if the diagonal (sequence) is 2 even in a 3x3 square there is 8 possiblities – Aymeric Nov 19 '14 at 12:57
  • Is he talking about diagonals from corner to corner? Because, I repeat, there are 4 corners, there can only be 2 diagonals – Matias Cicero Nov 19 '14 at 12:58
  • @MatiCicero No, simply diagonal lines. – Simon Nov 19 '14 at 12:59
  • A diagonal (not a true diagonal, but a complete slanting tic-tac-toe line) on a rectangle that's x by y will always be the smallest of x and y long (assume this is x. And you can fit one in if y == x, two in if y == x +1. So the number of those is y - x + 1. The diagonals can be in either direction. So the answer is 2*(y-z+1)? – The Archetypal Paul Nov 19 '14 at 12:59
  • Then there are 4 diagonals in a 3x3 square. – Matias Cicero Nov 19 '14 at 13:00
  • @MatiCicero yes but not the point of his question if you take a look to the formula you can guess in two seconds he's not looking literally for diagonals. Anyway community helped him that's what matter ;) – Aymeric Nov 19 '14 at 13:00

1 Answers1

2

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.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103