I've just released my first 'bot N
At the risk of repeating myself, welcome to the RoboWiki =) Feel free to post any questions you have, people are generally pretty helpful around here. -- Voidious
Thanks, I can see that you are from the comments I've read around the wiki, because I have a tendency to lurk around a forum or wiki for a few days before joining, to see the general attitude of the people there. I think this may be the only forum / wiki that may hold my interet longer than a month. I tend to get bored very easily with things, especially if I run into any difficulty whatsoever, but it is a habit that I am trying to break :) And I guess I should use the summary field when editing, eh? -- Bal
Welcome to RoboWiki! I'm pretty new myself only been here a little while. Trust me, its hard to get bored here! There are so much you can program its crazy. If you have any questions just ask, I would be happy to help if I know how. --Chase-san
Hey, welcome! Same goes for me, i'll try to answer any questions you have if I know the answer. --wcsv
Question guys, If i did this:
if (Random >= 0.1) {...} else if (Random >= 0.2) {...}
Would that be legal robocode? If not, how would I go about doing else if statements? -- Bal
Yes, that's legal Java / Robocode. (But it's not a loop. =)) -- Voidious
Minor technicalities aside, thanks -- Bal
Oh, would it work like it should if i had one for every decimal? like 0.1, 0.2, 0.3, or would it call a different one due to >=, or do I avoid that by going in order. -- Bal
If you had "Math.random()" in each if statement, it would generate a new random number each time; I read what you posted thinking you meant something like "double Random = Math.random()" before the if statements. (I think that's the issue you're asking about... ?) Also, if you do >= 0.1 first, it will never reach >= 0.2, >= 0.3, etc, because that would've matched the first one. -- Voidious
So the code should be:
double Random = Math.random(); if (Random <= 0.1) {...} else if (Random <= 0.2) {...} ...
So it would be limited to the .X to .A range where .X is the decimal before it, if you understand, heck, if I even understand :P -- Bal
Yeah, I think that would work the way you want it to =) So if you went up the scale to .9 like that (and one last "else"), you'd have 10 branches that each execute 10% of the time. -- Voidious
Cool, Now its time to think of a way to implement that :) -- Bal
If I put the code above in my main run, or onscannedrobot, would it loop? Also, having Random = Math.random(); at the end of the command would refresh the variable for the next run through, correct? -- Bal
It would loop if you put it in your loop. For example:
public void run(){ while (true){ double Random = Math.random(); if (Random <= 0.1) {...} else if (Random <= 0.2) {...} ... //other main loop code } }-- nfwu
The onScannedRobot event is executed each tick, while the run method is executed once at the beginning of the round, if that helps clarify things. If you declare "double Random" in the onScannedRobot event, its scope is only in that method, so doing something to it at the end will have no effect; but if you declare it as an instance variable (in the class itself), that would set it for the next time through the loop. I'd say it seems clearer to just set it to Math.random() before you use it, though. -- Voidious
The answer is 'yes, that works'. I assume you are trying to do random movement. One thing you may find from taking a new random approach every tick is that your movement will tend to average out, either along a path of travel or even crossing back and forth between your point of origin, making you vulnerable to an averaged linear targeting or head-on targeting. Then again it may work splendidly. That's for you to discover. A very minor note: you don't really need <= .. you can do < just as well (and in theory more precisely, though it is trivial) an it is a less costly comparison. -- Martin