Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Chris Kopcow
Chris Kopcow
2,852 Points

I'm nearly finished with my Battleship program, but I can't figure out how to get a Ship method to function.

I'm finishing up the two-player section of my Battleship game, as suggested in the Teacher's Notes on the C# Objects "Wrap Up" video. I'm nearly finished, but as you can see from my snapshot (https://w.trhou.se/j3s2a9vdof), on Game.cs, I'm getting stuck with a "'ship' does not exist in this context" error during gameplay.

I want to call the method FireOnShip to decrease a ship's health, and I pass a specific ship from an array as I call it, but it still says it can't do it because I don't mention ships within the if statement or while loop, so it won't call a method from Ship.cs. I define six ships (three per array) prior to the while loop, so I thought that was enough, but apparently it isn't.

Does anyone have any idea of how to rearrange my code, so I can get it to compile? I really can't figure it out.

2 Answers

Steven Parker
Steven Parker
231,110 Points

It's been a while since I took this course, so what I'm about to suggest might not be the same as what is presented in the lesson. But that said, just looking at the code I notice that the "FireOnShip" method doesn't seem to use the parameter at all, so I would re-define it without one on line 29 of Ship.cs:

Ship.cs
    public void FireOnShip()

Then, on line 111 of Game.cs (and 5 other places between lines 115 and 142) I would modify the method calls to apply them to the ship instead of passing the ship as an argument:

Game.cs
                shipsPlayerTwo[0].FireOnShip(); 

After those changes it should compile and run.

Chris Kopcow
Chris Kopcow
2,852 Points

Ah, thanks so much! That did it! It was so simple too.

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

I don't see where you created the ship variable at all in the Game.cs, it just sort of appears in an if-statement. Make sure that you instantiated it. I don't have time to go through the whole code right now. but I will return later after looking through it a bit better.

Chris Kopcow
Chris Kopcow
2,852 Points

Thanks! I created six ship objects prior to the while-loop and if-statements where I call FireOnShip, and I thought that would be enough, but I guess not. The condition for the if-statement only includes the coordinates of the ship (the ship's BoardLocation), but doesn't refer to a ship itself. Is that the problem? How do I pass a ship from an array into the if-statement in this scenario instead?

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

You would pass a ship by referring to that specific ship's index in your array of ships:

in your example for 3 ship array:

Ship [] ships = new Ship[] {Ship1, Ship2, Ship3};

Ship ship1 = ships[0];    // returns Ship1
Ship ship2 = ships[1];    // returns Ship2
Ship ship3 = ships[2];   // returns Ship3