The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Research about Randomising , need assistance here.
Discuss Research about Randomising , need assistance here. in the JavaScript Development forum on Dev Shed. Research about Randomising , need assistance here. JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 3rd, 2013, 06:44 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 27 m 32 sec
Reputation Power: 0
|
|
|
Research about Randomising , need assistance here.
I'm currently doing some research about random number ,
my plan is to create a table which lay ten mines everytime i refresh it,
i try a lot of different way to fix bugs ,
but it still not able to lay ten mines every times.
I realize i miss "end if" , "end while",
but when we put it in the code the function will stop working.
My current code looks like this:
Code:
<script type="text/javascript">
function addmines (x) {
var n = 10; //number of mines
var mines = []; //an empty array
while (n>0) {
var row = Math.floor(1+Math.random()*10);
var col = Math.floor(1+Math.random()*10);
if (row >= 0 && row < 10){
if (col >= 0 && col < 10) {
var id = "r"+ row + "c" + col;
n++;
var a = document.createElement("img");
a.src = "mine32.gif";
a.height = 30;
a.width = 30;
document.getElementById(id).appendChild(a);
}
}
//n++;
}
return x;
}
</script>
</head>
<body>
<h1>Minesweeper</h1>
<script language="JavaScript" type="text/javascript">
//Creates Table
document.write('<table>');
//for loop that creates the row, increments from 0 to 9 each time it reiterates
for (var row=0; row<9; row++) {
//creates the table row
document.write('<tr>');
//a nested for loop that reiterates 9 times creating the <td>'s and then exits to the parent for loop.
for (var col=0; col<9; col++) {
//references all the <td>'s with a specific id attribute.
document.write('<td id="r', row, 'c', col, '">');
//document.write(' ');
//closes the <td>
document.write('</td>');
}
//closes the <tr> or row in plain English
document.write('</tr>');
}
//closes the <table> tag
document.write('</table>');
addmines();
</script>
|

January 3rd, 2013, 07:23 AM
|
|
|
|
Your for loop does not have enough iterations. With the current logic, it applies the counter after a row has been drawn, so when it comes around on the 10th loop, "row" equals 9 and the loop stops never drawing the 10th row. You need to use "row < 10" or change your logic. The same applies for your columns.
Is it your intention to allow for more than one mine in each cell?
|

January 3rd, 2013, 07:52 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 27 m 32 sec
Reputation Power: 0
|
|
|
you talkin about "row < 10" ,
did u mean my if statement?
i think i'd like to avoid the overlap as well
do my code have any problem about it?
|

January 8th, 2013, 12:43 PM
|
|
|
|
Sorry about the late reply, I've been away for the last week. Have you made any progress?
|

January 9th, 2013, 04:10 AM
|
|
|
Quote: | Originally Posted by Winters Sorry about the late reply, I've been away for the last week. Have you made any progress? |
I would advise to use a for-loop, rather than a while-loop. A for loop usually iterates over a fixed length (in this case, n=10), a while-loop usually iterates until a certain condition is not met anymore.
Besides that: your code will generate overlap, i.e. multiple bombs can end on a similar cell. This is due to this line of code:
Code:
var row = Math.floor(1+Math.random()*10);
var col = Math.floor(1+Math.random()*10);
in each iteration, these values can become the same (since it is random). Another way is to create a list, with the indices of all cells, counting horizontally, so for example if you have a 10x10 grid, then you make a list [1....100]. Then you shuffle that list, pop the first 10 (or n) elements, and then convert these indices back to cell coordinates.
Also, the return 'x' is not needed at the end, in fact the whole x argument is never used.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|