C programming newbie; battleships boats

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 911 Views - Last Post: 03 February 2013 - 02:41 PM Rate Topic: -----

#1 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

C programming newbie; battleships boats

Posted 01 February 2013 - 06:36 PM

Greetings, its my first post here;
So i have done only programming in Python before and its my first time in C.
I am making a battleship game, in which the user is to input coordinates on a 10x10 board and, there are hidden boats in the board; the boats are randomized each time. Well the only problem is that i have the programming logic, but i dont know the syntax in C, everytime i try to run something i get like 100 errors..
I have made my board using a while loop;

int main(void){ 


int sea[10][10];
int x;
int y=0;
int m=0;
int n=0;
while(y<10){
		x =0;
	while(x<10){
		sea[x][y]=('.');
		printf("%c",sea[x][y]);
		x=(x+1);
	
	}
	printf("\n");
	
	y=(y+1);
}
	printf("\nPlease enter a coordinate to shoot at!\n");
	scanf("%d%d",&m,&n);



}




This is what i have so far, a double array, and each x and y char '.' making the board..

My question is how would i add the random ships and the hits. I tried using printf to get coords and then scanf to get the numbers, but how do i like, put those numbers to match the array? I know i have to use if statements such as

if m n == 5, 5 .. see thats the thing i know what to do, i dont know how to write it out ? the syntax of C

Any help is appreciated.

This post has been edited by Skydiver: 01 February 2013 - 06:49 PM
Reason for edit:: Fixed code tags. Thanks for the attempt, but look at the edit control more closely for the correct construction.


Is This A Good Question/Topic? 0
  • +

Replies To: C programming newbie; battleships boats

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,608
  • Joined: 05-May 12

Re: C programming newbie; battleships boats

Posted 01 February 2013 - 07:12 PM

You would probably do better with a for loop to make it easier to read the code, but that's just a matter of style rather than substance.

Anyway, you would use the rand() function to get random numbers that you would use to determine how to position the ships.

I highly recommend picking up a C book, or a find a C tutorial. It's usually better to learn the language first before tackling writing a game.
Was This Post Helpful? 0
  • +
  • -

#3 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: C programming newbie; battleships boats

Posted 01 February 2013 - 07:55 PM

I reference off of cpluspplus.com;

anyway i wrote the code in the for loop, and i do know i have to use rand() function but what i dont get is, should my boats be an array? also how would i scan them and like, get them to be the boat?

int sea[10][10];
int x, y;

for(y=0;y<10;y++){
	for (x=0;x<10;x++){
		sea[x][y]=('.');
		printf("%c", sea[x][y]);
	}
	printf("\n");
}


This post has been edited by Skydiver: 01 February 2013 - 08:43 PM
Reason for edit:: Fixed code tags again... *sigh*. Please learn how to read.

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,400
  • Joined: 19-February 09

Re: C programming newbie; battleships boats

Posted 01 February 2013 - 08:40 PM

You can add the boat to the array. To check for a winner you could also keep boat data or just check the whole array. Assume size of boat is 2 squares, get a random position, check the position and adjacent position are free. Different characters can denote different boats and hits and sea etc.


The board could be char if you want.

char sea[10][10];




You can create functions

#define ROWS 10
#define COLS 10

void initialize(int sea[ROWS][COLS])
{
  int x, y;

  for(y=0;y<ROWS;y++){
    for (x=0;x<COLS;x++){
      sea[x][y]='.';
    }
  }
}


int main()
{
  int sea[ROWS][COLS];

  initialize(sea);
}


Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,608
  • Joined: 05-May 12

Re: C programming newbie; battleships boats

Posted 01 February 2013 - 09:01 PM

Although a two dimensional array of chars will work, a 2 dimension array of enums would probably be a more modern and a step towards an object oriented way to do things:

enum Cell
{
    Empty      = 0,
    PatrolBoat = 1,
    Frigate    = 2,
    Submarine  = 3,
    Battleship = 4,
    Carrier    = 5,

    ShotAt     = 0x80,
};

bool IsHit(Cell cell)
{
    return (cell & ShotAt) && (cell ^ ShotAt);
}

bool IsMiss(Cell cell)
{
    return (cell & ShotAt) == ShotAt;
}

Cell sea[HEIGHT][WIDTH];


Was This Post Helpful? 0
  • +
  • -

#6 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 12:17 AM

I appreciate your replies;

but err, when you say:Assume size of boat is 2 squares, get a random position, check the position and adjacent position are free. Different characters can denote different boats and hits and sea etc.

I dont know to 'check' or get a random position about things
Was This Post Helpful? 0
  • +
  • -

#7 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 01:12 AM

If the cell has the value of 0, then it's empty. Wherever you want to put your ships, the idea is to check first, so you never put one ship on top of another one. The Navy hates it when that happens! ;)/>

You have a range of coordinates for your game (row and column position). The rand() function (part of the stdlib.h definitions), can give you a random number, within that range.

First, you want use srand() to seed the random number generator (otherwise you get the same random numbers, as a test).

Then inside a loop:
for(each ship not yet placed) {
   row = rand % maxRows in your game
   column = rand % maxCols in your game.
   check if that cell is empty, as well as the 2nd cell the ship will 
   occupy. If it is empty, assign those two cells the value of the ship
   and increment the number of ships you have placed, in this loop
}




is the general idea.

This post has been edited by Adak: 02 February 2013 - 01:15 AM

Was This Post Helpful? 0
  • +
  • -

#8 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 01:42 AM

how would i check to see if it is empty or not ? :o

I did some coding meanwhile and this is what i have now

[int main(void){
int sea[10][10];
int ship1 = sea[1][2];
int ship2 = sea[1][4];
int x, y;
int m=0;
int n=0;


for(x=0;x<10;x++){
for (y=0;y<10;y++){
sea[x][y]=('.');
printf("%c", sea[x][y]);
}
printf("\n");
}
printf("Enter a co-ordinate to shoot!\n");
scanf("#i #i", &sea[m][n]);
if(sea[m][n]=ship1)
printf("You hit");

}]

a problem is that whenever it asks me for a coordinate and i type either 1, 4 or 2,5 doesnt matter what, if it has a 1 or 2 in it, it will say you hit.. i need it to make it say you hit ONLY when i type 1,2.
Was This Post Helpful? 0
  • +
  • -

#9 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 01:54 AM

oh and btw my ships are not gonna be just one block, i want it to be length of atleast 2 or 3 blocks, but i dont know how to make a 2D array set so it can have a length of that many blocks INSIDE sea[][]..

btw, thanks for all the replies everyone :)
Was This Post Helpful? 0
  • +
  • -

#10 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 02:16 AM

View PostCanis, on 02 February 2013 - 01:54 AM, said:

oh and btw my ships are not gonna be just one block, i want it to be length of atleast 2 or 3 blocks, but i dont know how to make a 2D array set so it can have a length of that many blocks INSIDE sea[][]..

btw, thanks for all the replies everyone :)/>


Normally, each ship would be a struct (how we do object type things in C). Each ship struct would have 1-maybe a Frigate, or a Destroyer, 3-maybe a Cruiser or Submarine, 5-maybe a Battleship, or more blocks (for an aircraft carrier), with these "points". Each point would be a struct, and have int x and int y, being coordinates in the 2D array of the game board.

There are simpler ways of programming this, but you need to have a good grasp of the basics of using a 2D (row and column for dimensions), array, (like your sea[][].

In your code, the scanf() is incorrect:

scanf("#i #i", &sea[m][n]);



To scan two int's you would need:
scanf("%d %d", &sea[m][n],&sea[m][n+1]);




But that's not what you need. you want to scan a point (one row, and one col), and then check if that sea[row][col] is occupied or not. If it is, and it's your ship, then it's a hit, and you'll want report that, etc.

Whenever you post code, be SURE -- SURE, to highlight the code in the forum editor, and then click on the [code] icon in the top middle bar of the editor.

Otherwise, your code will be very hard to read, and generally, gets ignored.
Was This Post Helpful? 0
  • +
  • -

#11 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 10:17 AM

thank you for the tip about the code

Would i use scanf to scan that point? but how would i check to see if that sea[row][col] is occupied or not? i mean how would i know it is my ship is there like another function for this?
Was This Post Helpful? 0
  • +
  • -

#12 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 11:08 AM

alright im stuck...

int main(void){ 
int sea[10][10];
int	ship1 = sea[1][2];
int hits[10][10];
int x, y;
int m=0;
int n=0;

for(x=0;x<10;x++){
	for (y=0;y<10;y++){
		sea[x][y]=('.');
		hits[m][n]=('h');
		
		printf("%c", sea[x][y]);
	}
	printf("\n");
}
		printf("Enter a co-ordinate to shoot!\n");
		scanf("%i %i", &hits[m][n], &hits[m][n+1]);
		if(hits[m][n] == ship1)
				printf("%c", hits[m][n]);
		else 
				printf("You missed");
		
	}



do i make my ships a 2d array? or just an array?, i need to print the board as a hit or a miss, so i decided to make another 10x10 array called hits[10][10]. But i dont know how to print it on to the board if it is a hit or a miss, i knw you guys keep saying to check to see if theres a boat there and what not, but i dont know how to do that :/
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,400
  • Joined: 19-February 09

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 11:26 AM

You are using the character '.' as empty sea.

So to test for that you can use relational operators.

int x, y;

/* assign operator is one equal sign */
x = 2;
y = 2; 

/* equality operator is two equal signs */
if (sea[x][y] == '.') 
{
  printf("sea empty at (%d, %d) \n", x, y);
}


/* not equal operator */
if (sea[x][y] != '.')  
{
  printf("sea is NOT empty at (%d, %d) \n", x, y);
}
else
{
  printf("sea IS empty at (%d, %d) \n", x, y);

}




The tutorial here is C++ but I think all the operators discussed here can be used in C. www.cplusplus.com/doc/tutorial/operators/
Was This Post Helpful? 0
  • +
  • -

#14 Canis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 01-February 13

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 11:30 AM

so am i doing that right? to make the hits array? cuz i need to mark a hit or a miss, not just tell the player
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,400
  • Joined: 19-February 09

Re: C programming newbie; battleships boats

Posted 02 February 2013 - 11:35 AM

You can use a character to represent different things.

empty sea '.'
boat 'b'
frigate 'f'
hit 'x'
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2