By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Battleship

P: n/a
Hey, Im kind of new to C++ and I'm working on making a Battleship
program. I need to be able to output a screen with just dots on it for
when people havent tried those spots and then be able to switch it with
the characters in my array after they have been picked. This is the
code I have so far. Right now all all I want to do is make a game where
you have to sink the computers ships in less than 50 turns. Thanks for
any help that I get.

void battleShip::play()
{
char map [8][8];

map[0][0] = '*';
map[0][1] = '*';
map[0][2] = '*';
map[0][3] = 'x';
map[0][4] = 'x';
map[0][5] = 'x';
map[0][6] = 'x';
map[0][7] = 'x';
map[0][8] = 'x';

map[1][0] = 'x';
map[1][1] = 'x';
map[1][2] = 'x';
map[1][3] = 'x';
map[1][4] = 'x';
map[1][5] = 'x';
map[1][6] = 'x';
map[1][7] = 'x';
map[1][8] = 'x';

map[2][0] = 'x';
map[2][1] = 'x';
map[2][2] = 'x';
map[2][3] = 'x';
map[2][4] = 'x';
map[2][5] = 'x';
map[2][6] = 'x';
map[2][7] = 'x';
map[2][8] = '*';

map[3][0] = 'x';
map[3][1] = 'x';
map[3][2] = 'x';
map[3][3] = 'x';
map[3][4] = 'x';
map[3][5] = '*';
map[3][6] = 'x';
map[3][7] = 'x';
map[3][8] = '*';

map[4][0] = 'x';
map[4][1] = 'x';
map[4][2] = 'x';
map[4][3] = 'x';
map[4][4] = 'x';
map[4][5] = '*';
map[4][6] = 'x';
map[4][7] = 'x';
map[4][8] = '*';

map[5][0] = 'x';
map[5][1] = 'x';
map[5][2] = 'x';
map[5][3] = 'x';
map[5][4] = 'x';
map[5][5] = '*';
map[5][6] = 'x';
map[5][7] = 'x';
map[5][8] = 'x';

map[6][0] = 'x';
map[6][1] = '*';
map[6][2] = '*';
map[6][3] = 'x';
map[6][4] = 'x';
map[6][5] = '*';
map[6][6] = 'x';
map[6][7] = 'x';
map[6][8] = 'x';

map[7][0] = 'x';
map[7][1] = 'x';
map[7][2] = 'x';
map[7][3] = 'x';
map[7][4] = 'x';
map[7][5] = 'x';
map[7][6] = 'x';
map[7][7] = 'x';
map[7][8] = 'x';

map[8][0] = 'x';
map[8][1] = 'x';
map[8][2] = '*';
map[8][3] = '*';
map[8][4] = '*';
map[8][5] = '*';
map[8][6] = '*';
map[8][7] = 'x';
map[8][8] = 'x';

system("cls");
cout<< endl;
cout<< " RADAR SCREEN " <<endl;
cout<< " |=====================================| " <<endl;
cout<< " | 1 2 3 4 5 6 7 8 9 | " <<endl;
cout<< " |1 . . . . . . . . . | " <<endl;
cout<< " |2 . . . . . . . . . | " <<endl;
cout<< " |3 . . . . . . . . . | " <<endl;
cout<< " |4 . . . . . . . . . | " <<endl;
cout<< " |5 . . . . . . . . . | " <<endl;
cout<< " |6 . . . . . . . . . | " <<endl;
cout<< " |7 . . . . . . . . . | " <<endl;
cout<< " |8 . . . . . . . . . | " <<endl;
cout<< " |9 . . . . . . . . . | " <<endl;
cout<< " |=====================================| " <<endl<<endl;
for(z=0; z<50; z++)
{

cout<< " Enter the coordinates to fire at: ";
cin>> x;
cin>> y;

cout<< map[x-1][y-1];

}

}

May 1 '06 #1
Share this Question
Share on Google+
24 Replies


P: n/a
gonzo86 wrote:
Hey, Im kind of new to C++ and I'm working on making a Battleship
program. I need to be able to output a screen with just dots on it for
when people havent tried those spots and then be able to switch it with
the characters in my array after they have been picked. This is the
code I have so far. Right now all all I want to do is make a game where
you have to sink the computers ships in less than 50 turns. Thanks for
any help that I get.


We'd be happy to help you, but what is your question?
Jonathan

May 1 '06 #2

P: n/a
Im just not sure exactly how to print a 2d array so that it would look
like the board and be able to change the different dots on it every
time someone guessed the position.

May 1 '06 #3

P: n/a
gonzo86 wrote:
Im just not sure exactly how to print a 2d array so that it would look
like the board and be able to change the different dots on it every
time someone guessed the position.


Please quote the message you are answering to.

I guess you are looking for a way to specify a coordinate to output a
character; this is impossible in standard C++ (the subject of this
newsgroup). See

http://www.parashift.com/c++-faq-lit...html#faq-15.19
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

for more informations. You'll probably be disappointed by the graphical
possibilities of standard C++, but that's the way it is. One standard
way would be to output the grid one cell after the other, checking
what's in it and displaying the correct symbol. To "clear the screen",
just ouput a few empty lines and output the whole grid again.
Jonathan

May 1 '06 #4

P: n/a
Are there any ways of just using standard C++ to do this that would be
easier?

May 1 '06 #5

P: n/a
gonzo86 wrote:
Are there any ways of just using standard C++ to do this that would be
easier?

Doing what? Please quote as Jonathan asked.

--
Ian Collins.
May 1 '06 #6

P: n/a
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.

Im just not sure exactly how to print a 2d array so that it would look
like a battleship board and be capable of changing the different dots
on it every
time someone guessed the position to whether it was a hit or miss.

May 1 '06 #7

P: n/a
gonzo86 wrote:
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.

Im just not sure exactly how to print a 2d array so that it would look
like a battleship board and be capable of changing the different dots
on it every
time someone guessed the position to whether it was a hit or miss.

Not in a standard way, assuming you want to redraw the screen. There
just isn't a standard way of doing this.

So you can either redraw the grid, scrolling down the screen each time,
or use a terminal manipulation library specific to your platform.

By the way, to quote correctly on google, see
<http://cfaj.freeshell.org/google/>

--
Ian Collins.
May 1 '06 #8

P: n/a

gonzo86 wrote:
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.

Im just not sure exactly how to print a 2d array so that it would look
like a battleship board and be capable of changing the different dots
on it every
time someone guessed the position to whether it was a hit or miss.


http://www.safalra.com/special/googlegroupsreply/

May 1 '06 #9

P: n/a

gonzo86 wrote:
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.

Im just not sure exactly how to print a 2d array so that it would look
like a battleship board and be capable of changing the different dots
on it every
time someone guessed the position to whether it was a hit or miss.


Hard coded numbers aside....:

for( unsigned y = 0;
{
for( unsigned x = 0; x < 9; ++x )
{
cout << map[ x ][ y ];
}
cout << "\n";
}

May 1 '06 #10

P: n/a

gonzo86 wrote:
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.

Im just not sure exactly how to print a 2d array so that it would look
like a battleship board and be capable of changing the different dots
on it every
time someone guessed the position to whether it was a hit or miss.


Hard coded numbers aside....:

for( unsigned y = 0; y < 9; ++y )
{
for( unsigned x = 0; x < 9; ++x )
{
cout << map[ x ][ y ];
}
cout << "\n";
}

May 1 '06 #11

P: n/a
gonzo86 wrote:
Are there any ways of just using standard C++ to do this that would be
easier?


(just to make sure you got the message :), please quote correctly)

No. Standard C++ has no way of manipulating the ouput of the "console"
(whatever that is). Understand that standard C++ strives to be portable
amongst a large spectrum of different platforms, some of them having no
notion of "console" (or "terminal", or even "screen"). The same thing
applies to "keyboard", "thread", "network", "sound", etc.

As I said, you can however use std::cout (which sends character to the
standard output, which is the "screen" on most modern machines, but may
be anything) to output characters, but you cannot specify *where* you
can put them: a character goes just after the last one you put.

So you have several solutions:

1) use a combination of spaces, new lines and graphical characters to
simulate the grid
2) use a library to manipulate the console (some of these libraries may
be portable to some extent, but are by no means "standard")
3) use platform specific features to manipulate the console (non
portable)

If you decide to use 2 or 3, this newsgroup won't be able to help you
as these libraries are not topical here.

Good luck,
Jonathan

May 2 '06 #12

P: n/a

gonzo86 wrote:
Hey, Im kind of new to C++ and I'm working on making a Battleship
program. I need to be able to output a screen with just dots on it for
when people havent tried those spots and then be able to switch it with
the characters in my array after they have been picked. This is the
code I have so far. Right now all all I want to do is make a game where
you have to sink the computers ships in less than 50 turns. Thanks for
any help that I get.

void battleShip::play()
{
char map [8][8];

map[0][0] = '*';
Learn about initializer syntax:

char map[9][9] = {
"***xxxxxxx"
"xxxxxxxxx"
"xxxxxxxx*"
"xxxxx*xx*"
/* ... and so on */
};

map[0][1] = '*';
map[0][2] = '*';
map[0][3] = 'x';
map[0][4] = 'x';
map[0][5] = 'x';
map[0][6] = 'x';
map[0][7] = 'x';
map[0][8] = 'x';
Your array has 8 columns. There are 9 integers in the range [0,8], so
you are trying to initialize 9 columns.

In other words, the array declared as map[8][8] only goes up to
map[7][7].
map[8][8] = 'x';
By the time this line is reached, your program has, many statements
ago, accessed beyond the end of the object, resulting in undefined
behavior.
system("cls");


Nonportable. This line assumes that there is a command interpreter, and
that it accepts a CLS command, and that this CLS command does what you
think it does.

May 2 '06 #13

P: n/a

"gonzo86" <ja*********@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hey, Im kind of new to C++ and I'm working on making a Battleship
program. I need to be able to output a screen with just dots on it for
when people havent tried those spots and then be able to switch it with
the characters in my array after they have been picked. This is the
code I have so far. Right now all all I want to do is make a game where
you have to sink the computers ships in less than 50 turns. Thanks for
any help that I get.

void battleShip::play()
{
char map [8][8];

map[0][0] = '*';
map[0][1] = '*';
map[0][2] = '*';
map[0][3] = 'x';
map[0][4] = 'x';
map[0][5] = 'x';
map[0][6] = 'x';
map[0][7] = 'x';
map[0][8] = 'x';
your 8x8 map goes from [0][0] to [7][7]. [0][8] is out of bounds.

Use initializer a someone showed you, or use a for loop. I would initialize
everything to 'x' first then add the '*' where I wanted them.

for ( int i = 0; i < 8; ++i )
for ( j = 0; j < 8; ++j )
map[i][j] = 'x';

Now your entire 2x2 grid is full of 'x's which I believe you are using for
empty. Now you can fill them up however you want probably using for loops.

<snip rest of assigments>
system("cls");
This may work on your system, but be aware it may not work on all. That is,
if your teacher is going to run this on linux it's not going to work.
cout<< endl;
cout<< " RADAR SCREEN " <<endl;
cout<< " |=====================================| " <<endl;
cout<< " | 1 2 3 4 5 6 7 8 9 | " <<endl;
cout<< " |1 . . . . . . . . . | " <<endl;
cout<< " |2 . . . . . . . . . | " <<endl;
cout<< " |3 . . . . . . . . . | " <<endl;
cout<< " |4 . . . . . . . . . | " <<endl;
cout<< " |5 . . . . . . . . . | " <<endl;
cout<< " |6 . . . . . . . . . | " <<endl;
cout<< " |7 . . . . . . . . . | " <<endl;
cout<< " |8 . . . . . . . . . | " <<endl;
cout<< " |9 . . . . . . . . . | " <<endl;
cout<< " |=====================================| " <<endl<<endl;


Use the for loops again. First print your static stuff.

cout<< " RADAR SCREEN " <<endl;
cout<< " |=====================================| " <<endl;
cout<< " | 1 2 3 4 5 6 7 8 9 | " <<endl;

Now the rest of the lines are going to change. A few ways to do this.

for ( int i = 0; i < 8; ++i )
{
cout << " |" << i << " ";
for ( j = 0; j < 8; ++j )
{
if ( map[i][j] == 'x' )
cout << ". ";
else if ( map[i][j] == '*' )
cout << "* ";
else
cout << "? ";
}
cout << " |" << endl;
}
cout<< " |=====================================| " <<endl<<endl;

There may be bugs in this code (there probably is) but it should give you
the general idea. If you don't understand it, study your text book more.
My intention is not to do your homework for you, but to help you understand
how you can do this. If you don't understand the code, don't use it please.

May 2 '06 #14

P: n/a
gonzo86 wrote:
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.


To get standard quotes, use the method outlined below.


Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
May 2 '06 #15

P: n/a
Default User wrote:
gonzo86 wrote:
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.


To get standard quotes, use the method outlined below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.


This should be in the FAQ by now...
Jonathan

May 2 '06 #16

P: n/a
Jonathan Mcdougall wrote:
Default User wrote:

Please quote enough of the previous message for context. To do so
from Google, click "show options" and use the Reply shown in the
expanded header.


This should be in the FAQ by now...


I've considered approaching Marshall about adding something to the
posting guidelines section.

Brian
May 2 '06 #17

P: n/a
Default User wrote:
Jonathan Mcdougall wrote:
Default User wrote:

Please quote enough of the previous message for context. To do so
from Google, click "show options" and use the Reply shown in the
expanded header.


This should be in the FAQ by now...


I've considered approaching Marshall about adding something to the
posting guidelines section.


.... but? :)
Jonathan

May 2 '06 #18

P: n/a
Jonathan Mcdougall wrote:
Default User wrote:
Jonathan Mcdougall wrote:
Default User wrote:

> Please quote enough of the previous message for context. To do
> so from Google, click "show options" and use the Reply shown in
> the expanded header.

This should be in the FAQ by now...


I've considered approaching Marshall about adding something to the
posting guidelines section.


... but? :)


But I haven't done so?

I'm not sure how the group at large feels about the situation. On
comp.lang.c, it's obvious, the group is pretty militant about
instructing the Google users. Here it's a bit more hit and miss.

Brian
May 2 '06 #19

P: n/a
Default User wrote:
Jonathan Mcdougall wrote:
Default User wrote:
Jonathan Mcdougall wrote:
> Default User wrote:

> > Please quote enough of the previous message for context. To do
> > so from Google, click "show options" and use the Reply shown in
> > the expanded header.
>
> This should be in the FAQ by now...

I've considered approaching Marshall about adding something to the
posting guidelines section.


... but? :)


But I haven't done so?

I'm not sure how the group at large feels about the situation. On
comp.lang.c, it's obvious, the group is pretty militant about
instructing the Google users. Here it's a bit more hit and miss.


I personally think it would be a good idea, not only we'd stop
repeating the same thing, but it could actually educate some (provided
they read the faq before posting here, which is another discussion).
Jonathan

May 2 '06 #20

P: n/a
Jonathan Mcdougall wrote:
Default User wrote:


[Google Groups posting problems]
I'm not sure how the group at large feels about the situation. On
comp.lang.c, it's obvious, the group is pretty militant about
instructing the Google users. Here it's a bit more hit and miss.


I personally think it would be a good idea, not only we'd stop
repeating the same thing, but it could actually educate some (provided
they read the faq before posting here, which is another discussion).

I don't know how much good it would do as a preventative. Google users
are often brand-new to usenet, and tend not to search out the FAQs.
However, it would be handy to have a FAQ entry to direct them towards.

I have a great deal of sympathy for the Google uses. The company
doesn't make it easy on them. I had to use GG for about 4 months at the
start of 2005, and it wasn't pleasant. Some people have taken to
filtering the entire Google domain, but I think on the whole they are
as prone to be good group members as anyone else, once they know what
to do and how to do it.

Brian

May 2 '06 #21

P: n/a
"Default User" writes:
Jonathan Mcdougall wrote:
Default User wrote:


[Google Groups posting problems]
> I'm not sure how the group at large feels about the situation. On
> comp.lang.c, it's obvious, the group is pretty militant about
> instructing the Google users. Here it's a bit more hit and miss.


I personally think it would be a good idea, not only we'd stop
repeating the same thing, but it could actually educate some (provided
they read the faq before posting here, which is another discussion).

I don't know how much good it would do as a preventative.


Apparently, someone in the distant past thought that if someone provided a
FAQ, someone else would read it. To argue against including something
because it probably won't be read seems defeatist to me. Without doubt, the
problem and solution should be in the FAQ.

Google seems to have decided they will be leaders not followers. I would
certainly endorse a total boycott of all these crappy disjointed posts that
don't meet a certain standard. But it's not going to happen.

May 2 '06 #22

P: n/a
osmium wrote:
"Default User" writes:
Jonathan Mcdougall wrote:
Default User wrote:
[Google Groups posting problems]
I'm not sure how the group at large feels about the situation. On
comp.lang.c, it's obvious, the group is pretty militant about
instructing the Google users. Here it's a bit more hit and miss.

I personally think it would be a good idea, not only we'd stop
repeating the same thing, but it could actually educate some
(provided they read the faq before posting here, which is another
discussion).

I don't know how much good it would do as a preventative.


Apparently, someone in the distant past thought that if someone
provided a FAQ, someone else would read it. To argue against
including something because it probably won't be read seems defeatist
to me. Without doubt, the problem and solution should be in the FAQ.


I guess I wasn't clear. I'm not at all against having it in the FAQ,
and it indeed might clue in some people. I don't don't believe it would
have a large role in preventing the problem. The real solution would be
for Google to change their interface.

My reason for not putting it forward to Marshall is that I'm not sure
what the overall feeling of the newsgroup is. It's seemed to me that
more people are complaining about the quoting of late, but I don't have
any real gauge.
Google seems to have decided they will be leaders not followers. I
would certainly endorse a total boycott of all these crappy
disjointed posts that don't meet a certain standard. But it's not
going to happen.


Agreed, and it's not something I would do. There are good, responsible
people out there using Google to the best of their abilities.

Brian
May 2 '06 #23

P: n/a
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.

ROFL!!
Sorry this is one of the most stupid flame posts i've read in a while.
The answer to the question you "ask" is in the same post you refer
to(the one Jonathan wrote), the one you supposedly "never" saw.

At least you could "try" to make it look as if your "question" had any
other intention than to bash "gonzo86"...
May 2 '06 #24

P: n/a
Mraco G. wrote:
Ian Collins wrote:
Doing what? Please quote as Jonathan asked.
ROFL!!
Sorry this is one of the most stupid flame posts i've read in a while.
The answer to the question you "ask" is in the same post you refer
to(the one Jonathan wrote), the one you supposedly "never" saw.

No, it was a polite request to quote. I didn't make any claims.
At least you could "try" to make it look as if your "question" had any
other intention than to bash "gonzo86"...


It didn't 'bash' anyone and it had the desired effect.

Many new users of google groups don't realise they are using Usenet and
other users may not see what they are replying to without having to look
back though previous messages (which, at least in theory, they may not
have seen).

In most cases, a gentle hint and a pointer to posting guidelines (which
I should have included) puts them straight.

--
Ian Collins.
May 2 '06 #25

This discussion thread is closed

Replies have been disabled for this discussion.