Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title></title>
  5. <style>
  6.   input {
  7.     width: 30px;
  8.     height: 30px;
  9.   }
  10.   canvas {
  11.     border: 1px solid black;
  12.   }  
  13. </style>
  14. <script type="text/javascript">
  15. //Variables globales
  16. var tabla0=[];
  17. var canvas;
  18. var ctx;
  19. var libres=100;
  20.    
  21. //Funcion main
  22. function init(){
  23.   //Funcion que inicializara las variables globales
  24.   for(var i=0;i<=9;i++){
  25.     var aux=[];
  26.     for(var j=0;j<=9;j++){
  27.         aux.push(0);
  28.     }
  29.       tabla0.push(aux);
  30.   }
  31.   canvas=document.getElementById("myCanvas");
  32.   ctx=canvas.getContext("2d");
  33.  
  34.   canvas=document.getElementById("myCanvas1");
  35.   ctx1=canvas.getContext("2d");
  36.   pintarTablero();    
  37. }
  38.  
  39. function pintarTablero(){
  40.   //Funcion que dibuja las lineas del tablero
  41.   ctx.lineWidth=0.5;
  42.   ctx.strokeStyle='rgb(0,0,0)';
  43.   ctx.fillStyle='rgb(173,216,230)'; //Color azul por defecto
  44.    
  45.   ctx1.lineWidth=0.5;
  46.   ctx1.strokeStyle='rgb(0,0,0)';
  47.   ctx1.fillStyle='rgb(173,216,230)'; //Color azul por defecto
  48.  
  49.   for(var i=0;i<=9;i++){
  50.     for(var j=0;j<=9;j++){
  51.       ctx.strokeRect(i*30,j*30,30,30);
  52.       ctx1.strokeRect(i*30,j*30,30,30);
  53.       ctx.fillRect(i*30,j*30,30,30);
  54.       ctx1.fillRect(i*30,j*30,30,30);
  55.     }
  56.   }
  57. }
  58.    
  59. function pintarBotones(){
  60.   for(var i=0;i<=9;i++){
  61.       document.write("<br />");
  62.     for(var j=0;j<=9;j++){
  63.         document.write("<input type='button'  value='' onclick='tirar("+i+","+j+")' />");
  64.     }
  65.   }
  66. }
  67.  
  68. function tirar(x,y){
  69.   var inicioX=x*30;
  70.   var inicioY=y*30;
  71.   if (tabla0[y][x]==0 || tabla0[y][x]==1){
  72.     ctx.fillStyle='rgb(30,255,200)';//0,0,60
  73.     ctx.fillRect(inicioY,inicioX,30,30);
  74.     //alert(tabla0[y][x]); ver valor de posición
  75.     libres--;
  76.     //tiradaPC();
  77.   }else if(tabla0[y][x]==40){
  78.     alert("BOOM");
  79.     ctx.fillStyle='rgb(100,0,0)';
  80.     ctx.fillRect(inicioY,inicioX,30,30);
  81.   }
  82. }
  83.  
  84. function tiradaPC(){
  85.   var x=Math.floor(Math.random()*3)+0;
  86.   var y=Math.floor(Math.random()*3)+0;
  87.   var inicioX=x*30;  
  88.   var inicioY=y*30;
  89.  
  90.   if (tabla0[x][y]==0){
  91.      
  92.       ctx1.strokeRect(inicioX,inicioY,30,30);
  93.      
  94.       tabla0[x][y]=1;
  95.       libres--;
  96.   }
  97. }
  98.  
  99. function cargarSubmarinosPC(){
  100.     for(var i=0;i<=3;i++){
  101.         var x=Math.floor(Math.random()*10);
  102.         var y=Math.floor(Math.random()*10);
  103.         if (tabla0[x][y]==0) {
  104.           tabla0[x][y]=40;
  105.           pinta();
  106.          
  107.           if(tabla0[x-1]!=undefined){
  108.             if (tabla0[x-1][y-1]==0) {
  109.                 tabla0[x-1][y-1]=1;
  110.             }
  111.             if (tabla0[x-1][y]==0) {
  112.                 tabla0[x-1][y]=1;
  113.             }
  114.             if (tabla0[x-1][y+1]==0) {
  115.                 tabla0[x-1][y+1]=1;
  116.             }
  117.           }
  118.  
  119.           if (tabla0[x][y-1]!=undefined) {
  120.                 tabla0[x][y-1]=1;
  121.               }
  122.                
  123.           if (tabla0[x+1]!=undefined) {
  124.             if (tabla0[x+1][y+1]==0) {
  125.                 tabla0[x+1][y+1]=1;
  126.             }
  127.             if (tabla0[x+1][y]==0) {
  128.                 tabla0[x+1][y]=1;
  129.             }
  130.             if (tabla0[x+1][y-1]==0) {
  131.                 tabla0[x+1][y-1]=1;
  132.             }
  133.           }
  134.          
  135.           if (tabla0[x][y+1]!=undefined) {
  136.                 tabla0[x][y+1]=1;
  137.               }
  138.  
  139.         }else{
  140.           var ok=false;
  141.           while((tabla0[x][y]==40 || tabla0[x][y]==1) && ok==false){
  142.             x=Math.floor(Math.random()*10);
  143.             y=Math.floor(Math.random()*10);
  144.             if (tabla0[x][y]==0){
  145.                 tabla0[x][y]=40;
  146.                 pinta();
  147.              
  148.               if(tabla0[x-1]!=undefined){
  149.                 if (tabla0[x-1][y-1]==0) {
  150.                     tabla0[x-1][y-1]=1;
  151.                 }
  152.                 if (tabla0[x-1][y]==0) {
  153.                     tabla0[x-1][y]=1;
  154.                 }
  155.                 if (tabla0[x-1][y+1]==0) {
  156.                     tabla0[x-1][y+1]=1;
  157.                 }
  158.                
  159.               }  
  160.                
  161.               if (tabla0[x][y-1]!=undefined) {
  162.                 tabla0[x][y-1]=1;
  163.               }
  164.  
  165.               if (tabla0[x+1]!=undefined) {
  166.                 if (tabla0[x+1][y+1]==0) {
  167.                     tabla0[x+1][y+1]=1;
  168.                 }
  169.                 if (tabla0[x+1][y]==0) {
  170.                     tabla0[x+1][y]=1;
  171.                 }
  172.                 if (tabla0[x+1][y-1]==0) {
  173.                     tabla0[x+1][y-1]=1;
  174.                 }
  175.               }
  176.  
  177.               if (tabla0[x][y+1]!=undefined) {
  178.                 tabla0[x][y+1]=1;
  179.               }
  180.  
  181.               ok=true;
  182.             }
  183.           }
  184.         }    
  185.     }  
  186. }
  187.  
  188. function pinta(){
  189.   for(var i=0;i<=9;i++){
  190.     for(var j=0;j<=9;j++){  
  191.       if(tabla0[j][i]==40 || tabla0[j][i]==30){
  192.           ctx.strokeStyle='rgb(0,0,0)';
  193.           ctx.fillStyle='rgb(0,200,0)'; //Color del barco
  194.           //ctx.strokeRect(j*30,i*30,30,30);
  195.           ctx.fillRect(j*30,i*30,30,30);
  196.       }
  197.     }
  198.   }
  199. }
  200.  
  201. </script>
  202. </head>
  203. <body>
  204. <pre><h1>      tablero PC        tablero Usuario</h1></pre>
  205. <canvas id="myCanvas" width="300px" height="300px"></canvas>
  206. <canvas id="myCanvas1" width="300px" height="300px"></canvas>
  207. <br />
  208. <script type="text/javascript">
  209.   init();
  210.   pintarBotones();
  211.   cargarSubmarinosPC();
  212.   document.write("<table border>");
  213.   document.write("<tr>");
  214.   for (var i = 0; i < tabla0.length; i++) {
  215.     for (var j = 0; j < tabla0.length; j++) {
  216.       document.write("<td>"+tabla0[j][i]+"</td>");
  217.  
  218.     }
  219.     document.write("</tr>");
  220.   }
  221.  
  222.   document.write("</table");
  223. </script>
  224. </body>
  225. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement