Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- var board = document.getElementById("board");
- var matrix = [...Array(12)].map(x=>Array(12).fill(0))
- var playerturn = 0;
- makeboard();
- placeship(dreadnought);
- placeship(submarine);
- placeship(scout);
- placeship(carrier);
- placeship(battleship);
- function assignplayer(){
- }
- function boat(length, id){
- this.id = id;
- this.length = length;
- this.getlength = function(){return this.length; };
- }
- var dreadnought = new boat(5, dn);
- var submarine = new boat(3, sub);
- var scout = new boat(2, sc);
- var carrier = new boat(4, car);
- var battleship = new boat(3,bs);
- function makeboard(){
- for (var i = 0; i< 12; i++) {
- for (var j = 0; j < 12; j++) {
- var square = document.createElement("div");
- board.appendChild(square);
- square.id= 's' + i + j;
- var img = document.createElement("img");
- img.src = "images/standard.png";
- var grid = document.getElementById(square.id);
- grid.appendChild(img);
- }
- }
- };
- function placeship(boat){
- //vertical vs horizontal
- var vert = (Math.round(Math.random()*1000))%2;
- var x = (Math.round(Math.random()*1000));
- var y = (Math.round(Math.random()*1000));
- if(vert ==1){
- for(let i = 0; i<boat.getlength(); i++){
- board[x][(y%(12-boat.getlength())) + i] = 1;
- }
- }else{
- board[x%(12-boat.getlength())+i][y] = 1;
- }
- };
- function isplayerturn(){
- return (playerturn==ws.player);
- }
- function checkmatrix(x,y){
- if(isplayerturn()){
- if(matrix[x][y] == 0){
- document.getElementByID('s'+x+y).className("Miss");
- matrix[x][y]=-1;
- playerturn = (playerturn + 1)%2;
- }if(matrix[x][y]== 1){
- document.getElementByID('s'+x+y).className("Hit");
- matrix[x][y]=-1;
- playerturn = (playerturn + 1)%2;
- }
- }
- };
- /* we define the function */
- function target() {
- var xcoords = this.id.substring(1,2);
- var ycoords = this.id.substring(2,3);
- checkmatrix(x,y);
- var tb = document.getElementById("out");
- tb.value = 'hello world';
- };
- window.onload = function() {
- document.querySelectorAll('[id^="s"]').forEach.onclick = target;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement