473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with Loading ships in to battle ship

3
I have to make a game with 15 x 26 grid sigular array (array of 390 charecters)
and im supose to load the ships which are charecters horozontally.
only 4 are needed and the function im stuck on is suppose to load them one at a time. the main requirements are that the ships dont overlap eachother and the random placements make sure that the ship fits on the line and dosent go to the next. what i have rite now is

void load_ships(char board[ ], char ship[ ])
{
/*int shiplen;*/
char iship[11] =;

int i, row, col, pos, isoccu=-1;
/*shiplen = strlen(ship);*/
strcpy(iship, ship);

char checkstring[11] = "~~~~~~~~~~" , checkboard[11];


srand(time(NULL)); /* would first be called (but only once, at the top of the
'load_ships' function), and then*/

do{
row = rand( ) % 15; /* could be called to generate a random number between 0 and 14
representing row index 0 to 14 and store it in 'row'.*/
do
{
col = rand( ) % 26; /* stores a random number from 0 to 25 in 'col'*/
} while ( (26 - col - 1) < shiplen );

pos = ( (row * 26) + col);

for(i=0;i<shiplen;i++)
{
checkstring[i] = '~';
checkboard[i] = board[(pos + i)];
}
if (strcmp(checkstring, checkboard) == 0)
isoccu = 1;

} while (isoccu == -1);

if (isoccu == 1)
{
for(i=0;i<shiplen;i++)
{
board[(pos+i)] = ship[i];
}
}
}



i have problems cause it dosent seem to allow me to do call strlen or strcpy before any varuble declaration. if i do so when i compile it it displays an error saying that the stuff below does not exist.

any idea how i should write this.. i changed it arround manytimes so the current version dosent work at all. please help me make this work
Dec 4 '06 #1
5 2041
DeMan
1,806 1GB
I assume, though, that if you move your string functions below the variable declarations we're all good? (otherwise check that you have the line #include <string.h> at the top of your program)

In this case declare all your variables first (you can always change their values later if you need).
Dec 4 '06 #2
basoul
3
but i need those to declare the size of my test strings ><
Dec 4 '06 #3
DeMan
1,806 1GB
You could still set shiplen to 0 first where you declare it,
Or why don't you set the text string size at the point where you use it (so that you can declare everything else first.


BTW I'm not convinced
char iship[11] =;
is such a good idea, I'd leave the = sign out.
Dec 4 '06 #4
Banfa
9,065 Expert Mod 8TB
The problem is you are try to execute code statements before some of the variable declarations, you can't do this in C however you can initialise variables so this would be OK

Expand|Select|Wrap|Line Numbers
  1. void load_ships(char board[ ], char ship[ ])
  2. {
  3. int shiplen = strlen(ship);*/
  4. char iship[11];
  5.  
  6. int i, row, col, pos, isoccu=-1;
  7.  
  8. strcpy(iship, ship);
  9.  
  10.  
  11. ...
  12. }
  13.  
Dec 4 '06 #5
basoul
3
thanx but i did it another way and now my whole code works =) my assignment is done thanx for helping =]
Dec 4 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old...
0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
0
by: Joe Johnson | last post by:
Greetings, I just did a fresh install of Solaris 10 on Sparc. The OS ships with perl_5.8.4. When I run some of my scripts that run fine on Linux, I get this: IO::Seekable::getpos missing at...
6
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18....
6
by: Dr Ann Huxtable | last post by:
Hi All, I am trying to compile an old C code written for the Borland Compiler on the DOS platform. This is a tactical solution and I intend to rip out the GUI side of things and slap on...
7
by: Staale L. Hansen | last post by:
We have an application which uses some mixed-mode code to expose a .NET 1.1 managed API. Only the necessary files are compiled with /clr. We want to be able to load the application without .NET...
4
by: polaris431 | last post by:
I can't seem to find the version of C# that ships with Visual Studio 2005 on Microsoft's web site. What I do find is a document called " C# Language Specification 2.0, March 2005 Draft". But this...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.