0

So, I have this array array here, and what I want to do is create new array called car and save elements from 'array' that are vertically aligned ([0,4,8,12],[1,5,9,13] ...) and also on diagonal as well ([0,5,10,15],[1,6,11],[2,7],[3],[4,9,14] and so...) how could i do that?

let array = [
  [  0,  1,  2,  3 ],
  [  4,  5,  6,  7 ],
  [  8,  9, 10, 11 ],
  [ 12, 13, 14, 15 ]
]
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • The question should be updated to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. –  Sep 14 '20 at 18:32
  • Are you trying to display the digits right-aligned? If yes, consider padStart() function. – jmrker Sep 14 '20 at 18:35
  • What is "save" supposed to do? Save to disk? Save to a new array `car` in a new sort order? Save to ...? – AxD Sep 14 '20 at 18:44
  • 2
    You are asking two questions in one here. Switching the rows and columns of a 2D array (or matrix if you will) is called [transpose](https://en.wikipedia.org/wiki/Transpose). See: [Transposing a 2D-array in JavaScript](https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript) – 3limin4t0r Sep 14 '20 at 18:51
  • Is diagonal as `[0, 5, 10, 11], [1, 6, 11, 12], [2, 7, 8, 13]` etc. good? What about diagonal the other way `[0, 7, 10, 14]`, etc.? – iAmOren Sep 14 '20 at 20:49

1 Answers1

0

Actually, for your expected solution I wouldn't use an array at all.

To see whether a series of pins on a game board (e.g. Tic-Tac-Toe or Connect Four) is either horizontally, vertically or diagonally connected, just use simple algebra:

In your mind, translate the board in a way that you put each row next to the previous row, e.g.:

| 0 | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 |   =>   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ...
...

You will notice that the board's positions will simply become plain indices.

In this example, index 1 and index 5 are vertically connected, because the board's width is 4. So, the distance between vertically connected pins always is the board's width (i.e. if the board's width is 4, then 1 + 4 = 5).

So, your functions to determine whether pins are vertically, horizontally or diagonally connected could be as simple als this (I used very simple and verbose implementations here for demonstration purposes. If your mathematical knowledge is higher, feel free to optimize*):

const _boardWidth = 4;

function isHorizontallyConnected(indexA, indexB)
{
  const rowA = Math.Floor(indexA / _boardWidth);
  const rowB = Math.Floor(indexB / _boardWidth);

  if (rowA === rowB && Math.Abs(indexB - indexA) === 1) return true;

  return false;
}

function isVerticallyConnected(indexA, indexB)
{
  const rowA = Math.Floor(indexA / _boardWidth);
  const rowB = Math.Floor(indexB / _boardWidth);
  const columnA = indexA % _boardWidth;
  const columnB = indexB % _boardWidth;

  if (columnA === columnB && Math.Abs(rowB - rowA) === 1) return true;

  return false;
}

function isDiagonallyConnected(indexA, indexB)
{
  ...
}

I left the implementation of the isDiagonallyConnected function for you to experiment.


*An optimized version of the isVerticallyConnected function, for instance, could simply be: return Math.Abs(indexB - indexA) === _boardWidth;

AxD
  • 2,714
  • 3
  • 31
  • 53