PDA

View Full Version : Making Battleship in C, need a little aid (not homework)


pamich
10-16-2006, 02:48 AM
I was bored and decided to make a command line 2 player battleship game. I've been mostly successful except I can't think of a decent way for them to place their ships on a 8x8 grid (two dimensional array). Any suggestions?

Code is below if you want to take a peak at it.

pamich
10-16-2006, 02:49 AM
#include <stdio.h>

int choose(){
int num;
scanf( "%d", &num );

if(num < 1 || num > 8){
printf( "Invalid choice, choose again.\n" );
num = choose();
}

return num;
}

void graph(int grid[][8])
{
int i;
int j;

printf( " 12345678\n" );
for(i = 0; i < 8; i++){
printf( "%d", i );

for(j = 0; j < 8; j++){

if(grid[i][j] == 0)
printf( " " );
else if(grid[i][j] == 1)
printf( "X" );
else if(grid[i][j] == 2)
printf( "O" );
}
printf( "\n" );
}

printf( "\n" );
}

void shoot(int grid[][8], int ships[][8], int y, int x, int *game)
{
if(grid[y][x] == 1 || grid[y][x] == 2){
printf( "This space has already been chosen, choose another space\n" );

printf( "Row:" );
y = choose();

printf( "Column:" );
x = choose();

shoot(grid, ships, y, x, game);
}

else{
if(ships[y][x] == 1){
printf( "HIT!\n" );
grid[y][x] = 1;
ships[y][x] = 0;
}
else if(ships[y][x] == 0){
printf( "Miss.\n" );
grid[y][x] = 2;
}
}

int k;
int l;
int check = 0;

for(k = 0; k < 8; k++){
for(l = 0; l < 8; l++){

if(ships[k][l] == 1)
check = 1;
}
}

if(check == 1)
*game = 0;

}

int main()
{
/* Grid is what the players see, ships is where the ships are */
/* Player 1 ships */
int grid1[8][8] = {{0}, {0}};
int ships1[8][8] = {{0}, {0}};

/* Player 2 ships */
int grid2[8][8] = {{0}, {0}};
int ships2[8][8] = {{0}, {0}};


/* SOME KIND OF SHIP PLACEMENT HERE */


int y;
int x;

int gamestatus = 1;

while(gamestatus == 1){

printf( "\nPlayer 1's turn\n" );

graph(grid2);

printf( "Row:" );
y = choose();

printf( "Column:" );
x = choose();

shoot(grid2, ships2, y - 1, x - 1, &gamestatus);

if(gamestatus == 0){
printf( "PLAYER 1 WINS!\n" );
break;
}

printf( "\nPlayer 2's turn.\n" );

graph(grid1);

printf( "Row:" );
y = choose();

printf( "Column:" );
x = choose();

shoot(grid1, ships1, y - 1, x - 1, &gamestatus);

if(gamestatus == 0)
printf( "PLAYER 2 WINS!\n" );

else
printf( "\nNext round.\n" );
}

return 0;
}

klitzy
10-16-2006, 03:04 AM
Sorry dont know any of that code but...

Note to Mods....We need a section where people can post stuff like this and ask for help or what not or spam their website/sales/etc....And yes, this is the 3rd time I have said this...

pamich
10-16-2006, 03:06 AM
Sorry dont know any of that code but...

Note to Mods....We need a section where people can post stuff like this and ask for help or what not or spam their website/sales/etc....And yes, this is the 3rd time I have said this...

Coding is a bit different than spam, but I agree.

simon
10-16-2006, 12:35 PM
Pamich, how about printing the grid and assigning a number to each square. Then the user can input a number to place the ship in, and update the map to show that a ship can't be placed there again? For example:


void print_map(int array[][8]){
int i,j;
int sq=0;
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(noship(array[i][j]))
printf("%d\t",sq);
else
printf("x\t"); /* shit exists on square already */
sq++;
}
printf("\n");
}
}


We need a section where people can ...spam their website/sales/etc....And yes, this is the 3rd time I have said this...
Yeah, and we also discussed this on the old forums. Take a hint? :rolleyes:

tokenuser
10-16-2006, 12:49 PM
Sorry dont know any of that code but...

Note to Mods....We need a section where people can post stuff like this and ask for help or what not or spam their website/sales/etc....And yes, this is the 3rd time I have said this...The powers that be have stated that there will be no new forums created at this stage. So, until new forums ARE created (it wasn't ruled out, just not happening just yet) you need to use some discretion where to post. This topic would fall under a tehnology forum if an appropriate one existed, but since there isn't leaving it under General Discussion is fine.

pamich
10-16-2006, 08:18 PM
Pamich, how about printing the grid and assigning a number to each square. Then the user can input a number to place the ship in, and update the map to show that a ship can't be placed there again? For example:


void print_map(int array[][8]){
int i,j;
int sq=0;
for(i=0;i<8;i++){
for(j=0;j<8;j++){
if(noship(array[i][j]))
printf("%d\t",sq);
else
printf("x\t"); /* shit exists on square already */
sq++;
}
printf("\n");
}
}



That would work if each ship was one square, but I was going for differrent lengths.

I put one together that works moderately well. Says what length is being placed, the user chooses a point and vertical/horizontal and it sets all the spaces from that point in that dirrection for however big the ship is to 1 (1 representing a ship). Checks if it intersects another ship of goes off the grid. Kind of poor, but usable.

void placement2(int map[][8], int ships[][8], int num);

void placement(int ships[][8])
{
int map[8][8] = {0};

printf( "Place your 5 space ship\n" );
placement2(map, ships, 5);

printf( "Place your 4 space ship\n" );
placement2(map, ships, 4);

printf( "Place your 3 space ship\n" );
placement2(map, ships, 3);

printf( "Place your second 3 space ship\n" );
placement2(map, ships, 3);

printf( "Place your 2 space ship\n" );
placement2(map, ships, 2);
}

void placement2(int map[][8], int ships[][8], int num)
{
int y;
int x;
char voh;

int i;
int check = 0;

graph(map);

printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );

printf( "Row:" );
y = choose();

printf( "Column:" );
x = choose();

printf( "Vertical or Horizontal? (V/H):" );
scanf( " %c", &voh ); /* BREAKS DOWN HERE */

if(voh == 'V' || voh == 'v'){
if(y + num >= 8){
printf( "Goes off the grid. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = y; i < y + num; i++){
if(ships[i][x] == 1)
check = 1;
}
if(check == 1){
printf( "Intersects another ship. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = y; i < y + num; i++){
map[i][x] = 2;
ships[i][x] = 1;
}
}
}
}

else if(voh == 'H' || voh == 'h'){
if(num + x >= 8){
printf( "Goes off the grid. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = x; i < x + num; i++){
if(ships[y][i] == 1)
check = 1;
}
if(check == 1){
printf( "Intersects another ship. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = x; i < x + num; i++){
map[y][i] = 2;
ships[y][i] = 1;
}
}
}
}

else{
printf( "Invalid. Retry.\n" );
placement2(map, ships, num);
}
}

masherscf
10-17-2006, 12:01 AM
I can;t think of anything more useless than posting code to a programer. How about explaining the algorithm you used and then letting the guy code it himself. This sounds a lot more fun too me.

Here's my idea. Create an 8x8 array of integers. As each ship is placed. Put a number of the ship in the correct cells. Store 2 adjadent for the sub, 3 for the destoyer and so on. As you hit, you can add 10 or something.

say

2=sub
3=destroyer
4=cruiser
5=battleship.

Create another array to record hits.

You can even use the array to display the grid as you make hits.

Just an idea.

simon
10-17-2006, 01:03 AM
That would work if each ship was one square, but I was going for differrent lengths.

I put one together that works moderately well. Says what length is being placed, the user chooses a point and vertical/horizontal and it sets all the spaces from that point in that dirrection for however big the ship is to 1 (1 representing a ship). Checks if it intersects another ship of goes off the grid. Kind of poor, but usable.


Hey, as long as it works for now you can always make revisions to it. I think there may be a problem with the graph function though. It prints the array from 0 to 7, but shoot() accepts a value between 1 and 8.

masherscf, its a simple nested for loop I was using as a means of conveying what I was trying to explain, and I'm pretty sure Pamich understood. If you are a programmer, have you never learnt by reading someone else's code? Also, if you'd read his original post, you would note that he is already using two multi-dimensional arrays, one to record the position of the ships and one to display shots.

pamich
10-17-2006, 01:09 AM
Hey, as long as it works for now you can always make revisions to it. I think there may be a problem with the graph function though. It prints the array from 0 to 7, but shoot() accepts a value between 1 and 8.

Yeah, I noticed that earlier today when testing. Happened when I changed some parts but not others. I modified choose() so it returns the user's input - 1 to x and y so it always works with the arrays.

Also, kind of took a concept from mash and gave the ship spots a value of 2 for the 2 space ship, 4 for the 4, etc. instead of just 1 all around. Added a check in shoot() to see if, after a hit, any spaces with that value are left. If not, it prints that the ship has been sunk.