Advanced Data Types
Tags: javascript
Published: 2021-01-12 16:42:41

## Matrices, a matrice is a two-dimensional arrays represented using rows and columns </a>
### This data type is commonly used when representing a grid, using statistics, 3D rendering, etc... </a>
### Using this data type i will create a one player Battleship </a>
### A matrix is created using "board", using null will create an empty square and "s" for a ship </a>
``` Java
let board = [
[null, null, "s"]
["s", null, null]
[null, "s", null]
];
```
### How to access a row </a>
### The number that is assigned to the rows begins at 0 and increases one per row. You must assign a variable to the row in order to log it to the console: </a>
``` Java
let firstRow = board[0]
let secondRow = board[1]
let thirdRow = board[2]
console.log(`The Board contains ${firstRow }, ${secondRow } and ${thirdRow`};
```
### How to access a column </a>
### After a row is assigned a variable, you can access a single element inside it. Using a similar notation we can pull out one element from one array: </a>
``` Java
console.log(`first ship is board[1][2]`)
// using the second brackets is how you access the columns in the row you identify

// "firstRow1" is accesing the first element in the first array
```
### Matrix Iteration </a>
### Each elements in the matrix is an array, so we have to iterate over each row using a loop: </a>
``` Java 
for (let i = 0, i < board.length; i++){
// a single row
let row = board[i];
// loop over each element
for( let = j, j < row.length; j++){
let column = row[j];
if (column === "s"){
console.log(`Ship is located at ${row} row and ${column} column`);}
}
// i used "i" and "j" to assign coordinates when the ship is located
```
### Turns </a>
### To create turns i will create a loop that iterates 10 times </a>
``` Java
for(let i = 0; i < 10; i++){}
```
### Randomizing </a>
#### Randon Index, in order to choose a random square we can use the `Math.random` function: </a>
``` Java
for(let i = 0; i < 10; i++){}

function getRandomNum(){
return Math.floor(Math.random() * board.length);
// using "board.length creates the max number the generator can use
}
// Math.random will return a random number between 0 and 1 so we use" * 5 " to make it a whole number we can use

console.log(getRandomNum());
console.log(getRandomNum());
console.log(getRandomNum());
console.log(getRandomNum());
```
### Selecting a Random Square </a>
### Using the same `Math.random` function, i will create code that displays a certain square's coordinates: </a>
``` Java
for(let i = 0; i < 10; i++(){
let row = getRandomNum();
let column = getRandomNum();
console.log(`The box can be found at ${} row and ${} column`);
let randomSquare = board[row][column];
console.log(randomSquare);}
function getRandomNum(){
return Math.floor(Math.random() * 5)
}
```
### Updating the board </a>
### If you land a hit, that square will become null or an empty square </a>
``` Java
for (var i = 0; i < 10; i++){
 var row = getRandomNum();
 var column = getRandomNum();

 var randomSquare = board[row][column];

 if (randomSquare === "S"){
   console.log(`Hit on ${row} row and ${column} column);
   board[row][column] = null;
 }
}

function getRandomNum(){
 return Math.floor(Math.random() * 5);
}
```
### Conclusion </a>
### We now know how to create a matrix, access either a row or column and both at once. The Battleship game is a quick and simple demnstratin of a matrix </a>

### Sources </a>
#### Iron Hack Document "Advanced Data Types" </a>