1

I'm very new with programming (C#) and I have a school assignment that is to program a simplified version of the classic game Battleships.

I have all the ships in an array, and a class (Ship) that looks like this:

 public Ship(Texture2D shipsheet, Rectangle hitbox, Rectangle sourceRectangle, bool rotated)

I feel like the problem is that the for-loop isn't recognising that the ships need different bools, like it just reads that line once? However, it shouldn't, right? Because they have different hitboxes and sourceRectangles? So shouldn't they have different bools as well?

ships = new Ship[5];
Rectangle shipHitbox;


        for (int i = 0; i < ships.GetLength(0); i++)
        {
            int x = Rand(0, 9) * 60;
            int y = Rand(0, 9) * 60;

            shipHitbox = new Rectangle(x, y, 60 + (60 * i), 60);
            shipSourceRectangle = new Rectangle(0, (60 * i), 60 + (60 * i), 60);
            rotated = random.Next(2) == 0 ? false : true;


            ships[i] = new Ship(shipsheet, shipHitbox, shipSourceRectangle, rotated);
        }

It's my first time trying to make random bool work, and I just do not understand why ALL the ships get that the bool rotated is false or rotated is true. I feel like I've tried everything, any help?

I don't know if it's clear or not, but all ships rotate once, or they don't. THAT's random, but not different bools assigned to different ships. (If I make any sense?)

The Rand method has nothing to do with this. It was a Method my professor wanted us to use (to get to know methods). I have this instead now: (which has nothing to do with my question)

                int x = random.Next(0, 9) * 60;
                int y = random.Next(0, 9) * 60;
Elma
  • 11
  • 2
  • when you debug it - you should see where its going wrong – BugFinder Oct 17 '17 at 15:15
  • what does Rand() do? Doesn't seem to be a native C# function. – ADyson Oct 17 '17 at 15:16
  • I guess you initialze the `Random` instance too "late". Store it as a field in your class. – Tim Schmelter Oct 17 '17 at 15:16
  • 1
    So, in your `Rand()` method I bet you are creating a `Random` object each time it's called. Don't do that - create it once. When you call the `Random()` constructor without parameters, it initialised it with a seed read from the real time clock, which only updates every few milliseconds. Call it too frequently, and you'll get the same seed each time. – Matthew Watson Oct 17 '17 at 15:17
  • Why you need the `Rand` method at all if you already have a `Random` instance there? – Tim Schmelter Oct 17 '17 at 15:18
  • 1
    Possible duplicate of [Random number generator only generating one random number](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – Equalsk Oct 17 '17 at 15:22
  • log the result of the random.Next(2). This will give you clues about what is happening – pm100 Oct 17 '17 at 15:39
  • Also note that it would be simpler to write `rotated = random.Next(2) == 0 ? false : true;` as `rotated = random.Next(2) != 0;` – Matthew Watson Oct 17 '17 at 15:41

1 Answers1

0

You can use the "random.next" method empty (to get any number) and then use the mod operator (finds the remainder after division). You don't need the ternary operator.

var random = new System.Random();
for (int i = 0; i < 100; i++){
 var number = random.Next();
 bool rotated = number % 2 == 0;
 System.Console.WriteLine("number: " + number + " boolean:" + rotated);
}
ERai
  • 133
  • 8