while (HullShipA > 0 && HullShipB > 0 && HullShipC > 0)
 {
 // do whatever i wanna do
 }
 Console.Writeline("End Game");

So i'm kinda new to c# but here is the deal:
In this simulation, ships attack eachother for damage and the way it is, as soon as 1 ship gets destroyed the game ends.
How could I make it so the game only ends with one ship left (therefore becomming the winner)?
Also, I set the whole thing so ship A attacks B that Attacks C that attacks A and so on.
If a wanted to make A attack C after B dies, would I have to add some if's here and there or is there a more simple solution?

Thanks in advance!!

Recommended Answers

All 7 Replies

For the while loop simply put in an integer that holds the number of ships still alive (3,2 or 1). While (shipsAlive >) 1 {} will keep your game looping and you simply decrease the number by one when a ship is destroyed.
For the targets, add variables that indicate which target each ship is targeting then, when B is destroyed, set the A variable to indicate ship C. So, in essence, A isn't attacking ship B then C it is always attacking A_target but that changes during the game. Does that make sense?

just tried it and it worked.... like a professor i had said once after I asked how to do something:
Use logic....

thanks

In this case, replacing the &&s with ||s (i.e. "keep running while at least one of the HullShip variables is greater than 0) would have worked too.

Just a small followup question:
I didn't have much time to think of this but I made it so when a ship's hull gets to 0 or less ShipsAlive = ShipsAlive - 1 to make it so the game ends when only 1 ship is left.
But after the loop ends how do I tell the winning ship in a message?
I can make something like Console.WriteLine("The winning ship is {0}", winningship);
but how do I tell winningship wich one it should be (i know it should be the one with hull > 0 at that given point of time) but i'm unaware of how to do it.
I guess it is pure logic, but my mind is filled with so much details that I still need to implement there before it is finished that I cannot think of a way to do it right now.

Any ideas?
Aaaaaannnnnddddd...... Thanks again.

How are your ships defined? They should be 3 instances of the one class and presumably kept in an arrya or something similar so looping through each ships turn is easy. To determine which ship is the winner loop through the array looking for the ship object that has hull > 0;

ok, I managed to get it working guys..... thanks but a WILD MAJOR APPEARED:

I did a simple version of the program and called it batalha1() (battle in portuguese) and inside it I placed the whole thing for the battle.

AND I also made a batalha() wich is a more complex version of batalha1()
but when I try to ask the user wich one he wants to see (using switch, and I even tried with ifs.....) it gives me 33 errors inside most codes and it keeps telling me that batalha() does not exist.

I tried making batalha and batalha1 both static void() but then i rememebered that void does not return info(Not sure if this is rellevant) and I made a static void Game(string [] args) just like the main, added both batalha and batalha1 inside of it, and added Game(); to static void Main(string[] args).

But it always gives me errors.

When i loaded each one individually, they work.But now they don't

BTW usefull info:
I checked all {'s and }'s to see if the had their pairs and it was ok, even numbered and etc.
I tried playing them without the switch, one after the other like:

static void Main(string[] args)
{
batalha();
console.readkey();
batalha1();
console.readkey();
}

And they worked.

Any Ideas?

ok manage to make it work.....man it was a nightmare.Almost lost everything i did in the past 3 days.
But all is working now and I owe a big thanks to all you guys that helped me in this.
Really....thank you guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.