Total Format - Total Entertainment
 
 

Go Back   Total Format Forum > Computing Forums > Coding, Design & Graphics

Coding, Design & Graphics If you are programming a script, designing a web page, building your own graphics or anything related and need to discuss it, get help and tips or general advice, then you should post your thoughts to this section.

Reply
 
LinkBack Thread Tools Display Modes
Old 10-12-2008, 19:25   #1 (permalink)
Name, Title, Location Arcanine
...Sigh...

My room
England
AvatarArcanine's Avatar
Mood
Posts52
Karma Arcanine has not done anything good or bad yet.
Pu742.21
Default

anywho making a battleships in C#i needed to generate 2 random numbers for the computer to attack, these numbers would have to be between 1 and 10 and obviously not have a pattern, so ofcourse i attempted ...

using system;

class battleships
{
public static Random rannum = new Random(10);

~ a few hundred lines of unrelated code later~

static void Computermove()
{
bool check = false;

while(!Gameover())
{
check = false;

do
{
Battleships.x = rannum.Next(10);
if (Battleships.x == 1 || Battleships.x == 2 || Battleships.x == 3 || Battleships.x == 4 || Battleships.x == 5 || Battleships.x == 6 || Battleships.x == 7 || Battleships.x == 8 || Battleships.x == 9 ||Battleships.x == 10){check = true;}
}
while(check == false);

Battleships.x2 = Battleships.x;
check = false;

do
{
Battleships.y = rannum.Next(10);
if (Battleships.y == 1 || Battleships.y == 2 || Battleships.y == 3 || Battleships.y == 4 || Battleships.y == 5 || Battleships.y == 6 || Battleships.y == 7 || Battleships.y == 8 || Battleships.y == 9 ||Battleships.y == 10){check = true;}
}
while(check == false);

Battleships.y2 = Battleships.y;
Battleships.x = (Battleships.x *2) + 1;

Console.SetCursorPosition(Battleships.x,Battleships.y);

Hitmiss();
Gameover();
Gridupdate();
}
}

running my battleships the computer seems to get stuck in the loops endlessly after the 10th move always the 10th move as well

the problem doesnt lie withing hitmiss, gameover or gridupdate as they work fine during a 2 player game.

have i done the whole random number thing wrong which im assuming it is

so i dont really know whats gone wrong help on how to fix/change or just how to do random numbers in the likely case ive done it completely wrong, would be muchly appreciated

Last edited by Arcanine; 10-12-2008 at 19:56..
ToolsArcanine is offline
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-12-2008, 19:42   #2 (permalink)
Name, Title, Location Cintaria
Supervisor

Not where you are :p
England
AvatarCintaria's Avatar
Posts6,682
Karma Cintaria is outstanding.Cintaria is outstanding.Cintaria is outstanding.
Cintaria is outstanding.Cintaria is outstanding.Cintaria is outstanding.Cintaria is outstanding.Cintaria is outstanding.Cintaria is outstanding.Cintaria is outstanding.Cintaria is outstanding.
Pu13,587.97
Critters
Blog
Blog Entries: 7
Awards
TF Top Poster Bronze TF Top Poster - Silver TF Top Poster - Gold 
Total Awards: 3
Little Fishy Four Leaf Clover Champagne Easter Egg Umbrella
Default

Quote:
if (Battleships.y == 1 || Battleships.y == 2 || Battleships.y == 3 || Battleships.y == 4 || Battleships.y == 5 || Battleships.y == 6 || Battleships.y == 7 || Battleships.y == 8 || Battleships.y == 9 ||Battleships.y == 10){check = true;}
}
Tried putting each condition in brackets?
if ((Battleships.y == 1) || (Battleships.y == 2) || etc)
Just a quick solution off the top of my head. Probably isn't that, though. Will Google it shortly
Cintaria's Sig:
Visit Cint City

Seen an inappropriate post? Click the icon to report it!


ToolsCintaria is offline
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 10-12-2008, 19:56   #3 (permalink)
Name, Title, Location Arcanine
...Sigh...

My room
England
AvatarArcanine's Avatar
Mood
Posts52
Karma Arcanine has not done anything good or bad yet.
Pu742.21
Default

Quote:
Originally Posted by Cintaria View Post
Tried putting each condition in brackets?
if ((Battleships.y == 1) || (Battleships.y == 2) || etc)
Just a quick solution off the top of my head. Probably isn't that, though. Will Google it shortly
doesnt compile if i do that, oh and to correct myself earlier it crashes after the 10th move not 9th, im guessing it cant generate a number more than once i.e. once its generated 7 it cant or rather wont generate 7 again which seems silly to me as then how could you constantly generate random numbers between a certain range =/


EDIT: Something just got even MORE depressing, decided to add ~Console.WriteLine("x = " + Battleships.x);~ after both if statements inside the loops, and then after the 10th move (10th iteration of the computermove method) it again endless looped and printed an ever changing list of numbers between 1 and 10 just like its meant to... so it seems to generate numbers between 1 and 10 but check doesnt get set to true and allow the program to continue after the 10th iteration ? im just really confused now

Last edited by Arcanine; 10-12-2008 at 21:04..
Arcanine's Sig:If the end justifies the means
ToolsArcanine is offline
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 21-12-2008, 15:21   #4 (permalink)
Name, Title, Location Ixpah
Full Member

Nottingham, UK
England
AvatarIxpah's Avatar
Mood
Posts335
Karma Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.Ixpah is a competent TFer.
Pu8,097.60
Default

public static Random rannum = new Random(10);
All this does is seed the generator so that it will repeat the same sequence each time, use new Random() instead.

Battleships.x = rannum.Next(10);
This will return a number between 0 and 9, use rannum.Next(1,11) instead, or just add 1 on, e.g. rannum.Next(10)+1.

So getting something like this:-

using system;

class battleships
{
public static Random rannum = new Random();

~ a few hundred lines of unrelated code later~

static void Computermove()
{
while(!Gameover())
{
Battleships.x = rannum.Next(10)+1;
Battleships.x2 = Battleships.x;
Battleships.y = rannum.Next(10)+1;
Battleships.y2 = Battleships.y;

Battleships.x = (Battleships.x *2) + 1;

Console.SetCursorPosition(Battleships.x,Battleships.y);

Hitmiss();
Gameover();
Gridupdate();
}
}
Ixpah's Sig:Check out my forums @ zints.co.uk or My Blog
ToolsIxpah is offline
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 21-12-2008, 15:51   #5 (permalink)
Name, Title, Location meow
Kissed all the girls

The twighlight zone
Maldives
Avatarmeow's Avatar
Mood
Posts3,207
Karma meow is amazing.
meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.meow is amazing.
Pu6,789.17
Awards
TF Top Poster Bronze 
Total Awards: 1
Cat 3 Gift Box Medal Single Red Rose Flowers Cake French Fries litebeer Subway Apple Heart Love Hearts Cash Love Hearts Tissues

Default

its best to not repeatedlyuse random() or random(seed).

the best thing is to use random, with or without a seed then only call next.
oh, wich is what your doing after re reading it ...

if your still having trouble with it il have a look, but can you post it in a code section so the indentation isnt lost pls .. ive currently got a c# 3d model game editor that runs to about 100k lines of code, so i could tell a tale or two about c#

ive even managed to make use of the MMX/SSE2 SIMD (parallell floating point maths) although i had to write that part using inline assembler in a seperate c++ project and interface to it.

oh after reading al the posts Ixpah gives the answer, you get a number from 0-9 not 1-10 as it seems you were expecting !

its far better to use a switch statement, this may well be faster, not that it probably needs it here, but it allows you to have a more well defined defualt condition wich makes it easier to debug and will trap the condition where n=0 wich isnt something you were expecting and you can either throw an exception wich can give details of exactly what went wrong and where, or you can just simply ignore it and exit the infinite loop.

Last edited by meow; 21-12-2008 at 15:56..
meow's Sig:"Some people see things as they are and ask `Why?'. I dream of things that never were and ask `Why not?'. "
"science without religion is lame, religion without science is blind"
Toolsmeow is offline
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links

Reply

Tags
number, problem, random

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
32Red Casino. £5 free bet on 32 Red on any roulette game ? it's our lucky number (end Detomah Shopping Offers 0 06-11-2008 14:51
Adele's album debuts at chart top Detomah Music & Radio News 0 04-02-2008 09:27
ADHD why is it becoming such a problem? Sash Health, Lifestyle, Hobbies and Sport 6 16-11-2007 17:55
DVD RAM drive writing problem Nemesis General Computer Discussion 0 16-02-2006 17:39
Dark Space - Frequently Asked Questions Detomah Dark Space 0 28-11-2005 13:22

 
 
Archive - RSS Feeds - About Us - Privacy - Terms of Use - Site Map - Advertising - Link To TF - Contact Us - Top
Content Relevant URLs by vBSEO 3.2.0 RC5 Copyright ©2003 - 2000, Total Format. Forums powered by vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Limited.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385