473,748 Members | 7,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Battleship

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::pla y()
{
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
24 9394

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
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

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::pla y()
{
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

"gonzo86" <ja*********@ho tmail.com> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.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::pla y()
{
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
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
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
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
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
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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8093
by: C-man | last post by:
Basically I want to create a battleship game that can be played by having two clients connect to it. I was wonder what the best networking principal would be. Basically I suppose I would have to pass an int value or something representing the x,y coordinates of each shot. Should I use some type of Socket programming or RMI or what else should be used|? Thanks
0
2179
by: Michael Goettsche | last post by:
Hi there, for a project in our computer science lessons at school we decided to write a client/server based battleship like game . I know this game could be written without a server, but the whole project is for educational purposes. Being the initiator of this project, I thought I would write a skeleton for it before we actually start with it. The client thing won't be too hard, what I'm having problems with is the server, maybe...
0
3253
by: webhosting | last post by:
After a failure, we promoted a slave to master. This included renaming the server to the old master's name and ip address, but leaving the server id alone. The new master works fine. We repaired the old server, gave it a different name and ip address but leaving the server id the same. I get the following error on the slave:
5
1158
by: iwdu15 | last post by:
hi, to get the bytes sent by a socket, in VB a simple local variable like below was used Dim recv as Byte() how can i do that in C++...this is what i am trying now Byte * rec = new Bytes; but i get these errors:
1
1533
by: iwdu15 | last post by:
hi, im trying to program the game battleship but i cant figure out how to code how people can place the ships and keep track of it. Ive been trying and researching for a couple days now and am at the end of my rope...if anyone cal help thatd b awsome. i have a 10x10 picturebox grid initialized into a 2d array to hold them. i just need to have a way to have them pick where they want their ships and keep track of it....thanks to anyone...
5
5998
by: muna123 | last post by:
hi, I need help in battleship game here is my code: #include <stdio.h> void draw_game(char board, int maway, int mleft, int score, char prevmove);
19
6732
by: Olivia Jaden | last post by:
Hi, I was about to create a 8x8 battleship program. there should be 8 ships in total with 1x1 size. However, I do not know how to place the ships randomly. Below are some of the codes that I started after doing some research #include <iostream> #include <windows.h> using namespace std;
3
3745
telgroc
by: telgroc | last post by:
I'm trying to write the code for the game Battleship, in order to count how many turns it will take to sink all 5 ships. I've written it without strategy (it just generates random numbers until 17 hits have occurred. However, I can't figure out how to add strategy, so when a hit is scored, the program will adjust to try and specify the spots around the hit and when it finds which direction the rest of the ship lies, it finishes off the ship. Any...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.