Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Battleship

https://w.trhou.se/i8o2dicpou

Doing the 2 or 3 projects proposed at the end of C# Objects. This one is battleship, pretty simple, I don't think I have to explain the rules.

Welcome on anything is welcome, but I'm mostly looking for ways to improve my code. I'm also struggling with the concept of static methods, if someone could explain it to me it would be appreciated.

1 Answer

Steven Parker
Steven Parker
220,378 Points

Game improvement suggestions:

  • allow "quit" anytime during game
  • store shot history and warn against shooting in the same place twice
  • display a summary status (hits/misses, enemy hits) on each turn
  • display status as a grid showing hits and misses
  • expand grid and have ships of different sizes like the traditional paper game

I'm not sure what issue you're having with static methods, but here's a significant distinction: Normal instance methods are applied to an instance of the class and may use and/or modify the instance it is called from, but static methods are applied to the class itself and can only use or affect arguments passed in.

-I could do that but I don't really see the point. I already know how to do it and it would just make the code messier and less dry.

-Working on that, as well as the AI.

-Again, don't really see the point. I could do it easily for sure, but the console display would look a lot heavier, and it's not that hard to remember.

-How do you make a grid in console applications? And more importantly, how do you change its size using the _height and _width variables?

-You can expand the grid using the _height and _width constant variables. Might add other ships if I see a use to it.

Also, big thanks for telling me the difference between instance and static methods.

Steven Parker
Steven Parker
220,378 Points
Here's an example of a console (text) grid:
+---+---+---+---+
|   | X |   |   |
+---+---+---+---+
|   |HIT| X |   |
+---+---+---+---+
|   |   |   |   |
+---+---+---+---+
|   |   |   |   |
+---+---+---+---+
If you generated it using loops, the size would be controlled by your dimension variables.

Sounds cool. I'll try to that tomoz