Support Forums

Full Version: [C++] Battleship
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've got this same thread on HF
Hey SupportForums, Simplicity here with my first contribution to this forum.

I'm sure some of you guys have always wondered about game designing with C++. Well To start off you sure do need a good level of knowledge of the Language. This is pretty powerful code if you know what I mean.

I've decided to kind of open this thread as a example and help thread of gaming with C++. I've written a well coded BASIC Battleship game (no graphics). It's a very basic application you can run in cmd.

If you want to learn to Write games and make pimp games with C++ You should be able to write up a code like this in a few hours.

Heres the game source:
http://pastebin.com/V3jWjXtg

Feel free to Edit it anyway you want and play around with it.

You got questions about programming basic games with C++ please post here and or if your serious about programming Games Contact me at

iMiracle@live.com / rl257@yahoo.com / bloddyharry_rsportal@live.com

I've had alot of suggestions to change my code, and alot of people telling me 30k line of code isn't needed.

I agree in that case, here are some of the suggestios I've gotten.


Store ships in arrays of integers like so:
Code:
int p1battleship[5] = { 0, 0, 0, 0, 0 };

When placing the ship, fill the array as needed:
Code:
int startspace = 22;
for (int i = 0; i < 5; i++) {
    p1battleship[i] = startspace;
    startspace++;
}

Now you have the numbers for the spaces the ship is at... When doing checks for hits, your code can be like the following:

Code:
int target = 22;
for (int i = 0; i < 5; i++) {
    if (p1battleship[i] == target) {
  printf("Hit!\n");
  hit++;
  break;
    }
}

Doing this alone will reduce the number of lines by around 18 thousand.

At the end of the function, you have a line "void quitGame();". Remove the "void" from there, otherwise you're just declaring that a function with the name "quitGame()" of type void exists. If you want to call the function, it's just "quitGame();".

2. Next section is where you set 2 spaces to B and A depending on the space targeted. Once again you have a lot of code here where it's not needed.

You can get the row to be used by simple mathematics:
Code:
// Tens digit determines the row
switch (target / 10) {
    case 1:
  // Row 1
    case 2:
  // Row 2
    ...
}

Please give more suggestions and feedback please, thanks.
(08-05-2010, 11:34 PM)BlaiR Wrote: [ -> ]Nice share.

Thumbsup

Though Games arent my thing it has usefull code.

Very useful and interesting, good stuff to know.
That's cool.

Thanks for the share man.
(08-14-2010, 02:16 AM)Sιℓνєя Wrote: [ -> ]That's cool.

Thanks for the share man.

Hey, no problem at all.
Seems alright, I would have thought the code would have been more complex from my experience with C++. But apperently im wrong haha. The next thing you could do is make a grid so when they guess a cordinate, and it misses or hits, you add a symbol on the grid.
Quite interesting mate,thank you.
awesome post dude Thanks
how would you go about adding 3d and crap?
I like it. I should be learning alot from this boardThumbsup
That's pretty cool haha. I would really love to see you release some more fun games like this!