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;