Note that there are some explanatory texts on larger screens.

plurals
  1. POTraversing a Grid of N Cells without declaring 2 variables (rows and columns)
    primarykey
    data
    text
    <p>I have been developing a game for canvas in JavaScript.</p> <p>I have chosen to create the game without limitations to the world size grid. So if they wish to play with a grid size of N, the logic will generate it.</p> <p>I have been working with bounds detection and have begun to program a function that will change the cell location on the grid if the user decides to travel outside the screen.</p> <p>For simplicity sake, I have shrunk the grid size to N = 9 so you can visualize this. The grid looks like </p> <pre><code> [0][1][2] [3][4][5] [6][7][8] </code></pre> <p>If the user is to travels off a -x value Cell[0], they will appear at the rightmost x position of cell [2]. this is represented as SQRT(N). So I know that the rightmost corner is SQRT(N). I also know that N is the bottom right hand cell [8]. </p> <p>With that said, here is a sample of the cells and thier equation IDs.</p> <pre><code> [0][1][SQRT(N)] [3][4][5] [N - SQRT(N)][7][N] </code></pre> <p>The grid above shows the x axis formulas for the 4 corners, but it is essential that I at least try to answer the inner cell equations before using static magic number switch-case conditionals.</p> <p>here is some javscript code showing what I am explaining:</p> <pre><code>if (player.X &lt; 0 - 10) { player.X = SCREEN_WIDTH; if (gameQuadrent == 0) { gameQuadrent = Math.floor(Math.sqrt(WORLD_SIZE)); } else if (gameQuadrent == (WORLD_SIZE - Math.floor(Math.sqrt(WORLD_SIZE)))) { gameQuadrent = WORLD_SIZE; } else { gameQuadrent--; } } </code></pre> <p>Any more efficient ways of doing this? I am trying to solve this problem without having to move over to a fixed world size.</p> <p>Much Appreciated. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload