Anda di halaman 1dari 3

function countRows() {

// Counts the number of rows in the grid.

return GRID.length;

function countColumns() {

// Counts the number of columns in the grid.

return GRID[0].length;

function gridSize() {

// Returns grid size as (width x height).

return (countColumns() + " x " + countRows());

function totalCells() {

// Computes the total number of cells in the grid.

return (countColumns() * countRows());

function convertColumn(coordinate) {

// Returns the column index from the coordinate.

var index = 0;

return (coordinate.toUpperCase().charCodeAt(index) - 65);

function lightCell(coordinate) {

// Returns the content of a specified cell.

convertRow = parseInt(coordinate.charAt(1)) - 1;

if ((parseInt(coordinate.charAt(1)) - 1) < 9 && convertColumn(coordinate) < 9) {


return GRID[convertRow][convertColumn(coordinate)];

else {

return false;

function isRock(coordinate) {

// Returns true if a rock is present in a given cell.

if (lightCell(coordinate) == "^")

{return true;}

else {return false;}

function isCurrent(coordinate) {

// Returns true if a current is present in a given cell.

if (lightCell(coordinate) == "~")

{return true;}

else {false;}

function isShip(coordinate) {

// Returns true if a ship is present in a given cell.

if (lightCell(coordinate) == "v") {

return true;}

else {

return false;

}
}

function lightRow(row) {

// Takes in the number of the row and returns its contents in an array.

return GRID[row-1];

function lightColumn(coordinate){

// Takes in the letter of the column and returns an array with the contents of the column.

var col = [];

for (row = 0; row < countRows(); row++) {

col.push(GRID[row][convertColumn(coordinate)]);

return col;

function indexToCoordinate(rowIndex, colIndex) {

rowNum = rowIndex + 1;

var letter = '';

colLetter = String.fromCharCode(colIndex +65);

return (colLetter + rowNum);

Anda mungkin juga menyukai