Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var boardLength;
- var numberOfShips; //show on scoreboard
- var board; //show on scoreboard
- var revealedSquares = 0; //show on scoreboard
- var lostOrWon; // show on scoreboard
- var moves;
- var validateInput = function () {
- var textFieldNumberOfShips = parseInt(document.getElementById('textfieldNumberOfShips').value);
- var selector = parseInt(document.getElementById('selectedBoardLength').value);
- if (textFieldNumberOfShips !== parseInt(textFieldNumberOfShips, 10)) {
- alert("Sisetage arvuline väärtus!");
- return false;
- }
- if (textFieldNumberOfShips < 1 || textFieldNumberOfShips >= Math.pow(boardLength, 2)) {
- alert("Pomme peab vähemalt 1 olema ja mitte rohkem kui ruute laual! ");
- return false;
- }
- return true;
- }
- var loadBoard = function (boardDivName) {
- if (validateInput()) {
- console.log(boardDivName);
- document.getElementById(boardDivName).innerHTML = "";
- boardLength = document.getElementById("selectedBoardLength").value;
- numberOfShips = document.getElementById("textfieldNumberOfShips").value;
- board = document.getElementById(boardDivName.value);
- moves = [];
- console.log("Input for board length and number of ships is: " + boardLength, numberOfShips);
- for (let y = 0; y < boardLength; y++) {
- var rowDiv = document.createElement('div');
- for (let x = 0; x < boardLength; x++) {
- var button = document.createElement('button');
- button.className = 'boardSquare';
- var id = y + ':' + x;
- button.id = id;
- button.x = x;
- button.y = y;
- console.log("Button is given the id: " + id);
- button.onclick = function () {
- checkSquare(x, y)
- };
- rowDiv.appendChild(button);
- }
- board.appendChild(rowDiv);
- }
- bombsArray = [];
- for (var x = 0; x < boardLength; x++) {
- bombsArray[x] = [];
- for (var y = 0; y < boardLength; y++) {
- bombsArray[x][y] = 0;
- }
- }
- var n = numberOfShips;
- // while there are bombs left, generate an index for a bomb
- while (n > 0) {
- var x = Math.floor(Math.random() * boardLength);
- var y = Math.floor(Math.random() * boardLength);
- console.log("Bomb is created on: " + x + ", " + y + " coordinate.");
- if (bombsArray[x][y] != 1) {
- bombsArray[x][y] = 1;
- n--;
- }
- }
- }
- }
- loadBoard("computerBoardDiv");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement