Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:45:47+00:00 2026-06-09T14:45:47+00:00

I am trying to fill a game board (8 by 8) with random game

  • 0

I am trying to fill a game board (8 by 8) with random game pieces (images), but not the entire board is filled. Some will be empty. To do so, I am randomly generating x,y coordinates on the board and assigning a random number to it. Since fillBoard is private function in a module, I am copy it over to copyBoard, which is public function.

The idea is to randomly generate an x,y coordinate and put it in an array[x], array[y]. I am having trouble copying the array array though, since not all of them is defined. I was wondering how do you do so?

Here’s what I got so far. Its displaying an error because splice() cannot work for an undefined variable.

function fillBoard(){
    var x, y;
    monsters=[]; //empty array

    x = Math.floor(Math.random()*cols);
    y = Math.floor(Math.random()*rows);

    monsters[x] = []; /* making x variable an array */
    monsters[x][y] = Math.floor(Math.random() * numMonsterTypes);
}

function copyBoard() {
    var copy = [],
        x;

    for (x = 0; x < cols; x++) {
        if(monsters[x]){
            copy[x] = monsters[x].slice(0); //slice(array) -> returns the selected elements in an array
        };
    };
    return copy;
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:45:48+00:00Added an answer on June 9, 2026 at 2:45 pm

    Here’s the solution:

    function fillBoard(){
        var x, y;
        monsters=[]; //empty array
    
        x = Math.floor(Math.random()*cols);
        y = Math.floor(Math.random()*rows);
    
        monsters[x] = []; /* making x variable an array */
        monsters[x][y] = Math.floor(Math.random() * numMonsterTypes);
    }
    
    function copyBoard() {
        var copy = [],
            x;
    
        for (x = 0; x<cols; x++){
            copy[x] = [];
            if(monsters[x]){
                copy[x] = monsters[x].slice(0);;
            };
        };
    
        return copy;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just trying to fill my ListView but I get a NullPointerException Here
I am trying to fill object[] with List<string> but I cannot figure out how
Trying to write a game such that most of the screen gets filled with
I've got weird problem. I'm trying to write simple game in C++, but I
I have some classes and I'm trying to fill the objects of this class.
Very simple problem...but weird results. Im just trying to fill a drop down list
I m trying to make some kind of game with c#, its loto game
I'm trying to create a chess game board with 8x8 buttons. I'm using nested
I'm trying to make a basic (Mario style) game but my sprite(plumber) doesn't appear,
Currently I'm trying to fill a 3x3 square with random x's and o's to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.