JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 3rd, 2013, 06:44 AM
anson920520 anson920520 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 anson920520 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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('&nbsp;');
//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>

Reply With Quote
  #2  
Old January 3rd, 2013, 07:23 AM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,925 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 14 m 42 sec
Reputation Power: 2571
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?
__________________
[PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

W3Fools - A W3Schools Intervention.

Reply With Quote
  #3  
Old January 3rd, 2013, 07:52 AM
anson920520 anson920520 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 anson920520 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old January 8th, 2013, 12:43 PM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,925 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 14 m 42 sec
Reputation Power: 2571
Sorry about the late reply, I've been away for the last week. Have you made any progress?

Reply With Quote
  #5  
Old January 9th, 2013, 04:10 AM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Research about Randomising , need assistance here.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap