Randomize Pacman maps, Javascript

102 Views Asked by At

I have to randomize the map (a different map each time it is opened) of pacman every time someone starts playing, I don't understand how I could do it. please help. I have a variable (world) that contains the static map, when the drawWorld function is executed it always creates that same map, but I need to create a different one every time the page is opened.

<script type="text/javascript">
  //mapa
  var world = [
    [1,1,1,1,1],
    [1,0,2,2,1],
    [1,2,1,2,1],
    [1,3,1,2,1],
    [1,2,2,3,1],
    [1,2,1,2,1],
    [1,2,2,2,1],
    [1,1,1,1,1],
  ];
  var worldDict = {
    0: "blank",
    1: "wall",
    2: "sushi",
    3: "onigiri",
  }
//creacion de mapa
  function drawWorld(){
    output = "";
    for(var row = 0; row < world.length; row++){
      output += "<div class = 'row'>"
      for(var x = 0; x < world[row].length; x++){
        output += "<div class = '"+ worldDict[world[row][x]] +"'></div>"
      }
      output += "</div>"
    }
  
    document.getElementById("world").innerHTML = output;
  }
  drawWorld();
var ninjaman = {
  x:1,
  y:1
}
  var count = 0;
  var countsushi = 10;
  function drawNinjaman(){
    document.getElementById("ninjaman").style.top = ninjaman.y * 40 + "px"
    document.getElementById("ninjaman").style.left = ninjaman.x * 40 + "px"
  }
  drawNinjaman()

  document.onkeydown = function(e){
    if(e.keyCode == 37){
      if(world[ninjaman.y][ninjaman.x - 1] !=1){
        ninjaman.x--;
        if(world[ninjaman.y][ninjaman.x] == 2){
          count = count + countsushi;
        }
    }
    }
    if(e.keyCode == 39){
      if(world[ninjaman.y][ninjaman.x + 1] !=1){
        ninjaman.x++;
        if(world[ninjaman.y][ninjaman.x] == 2){
          count = count + countsushi;
        }
      }
    }
    if(e.keyCode == 40){
      if(world[ninjaman.y + 1][ninjaman.x] !=1){
        ninjaman.y++;
        if(world[ninjaman.y][ninjaman.x] == 2){
          count = count + countsushi;
        }
      }
    }
    if(e.keyCode == 38){
      if(world[ninjaman.y - 1][ninjaman.x] !=1){
        ninjaman.y--;
        if(world[ninjaman.y][ninjaman.x] == 2){
          count = count + countsushi;
        }
      }
    }
    world[ninjaman.y][ninjaman.x] = 0;
    drawNinjaman()
    drawWorld()
  }
  function totalcount(){
      document.write("!Genial¡, obtuviste " + count + " puntos");
    }
</script>
0

There are 0 best solutions below