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

array help in battleship game.

P: 5
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[ ]);

int main( ) {
char board[391], prevmove[41];
int i, missles = 50, mleft = 0, score = 0;

strcpy(prevmove, ""); /* an empty string */

for(i=0; i<15*26; i++){
board[i] = '~';
}
board[i] = '\0';

draw_game(board, missles, mleft, score, prevmove);

draw_game(board, missles, mleft, score, prevmove);
return 0;
}


void draw_game(char board[ ], int maway, int mleft, int score, char prevmove[ ]) {
int i,input ,j;
char row_letters[16] = "123456789ABCDEF";

printf(" C Battleship...\n");
printf(" ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");

for(i=0; i<15; i++) { /* rows */
printf("%c|", row_letters[i]);

for(j=0; j<26; j++) { /* columns */
printf("%c", board[j]);
}
printf("|\n");
}

printf(" Missiles Away:%.2d Missiles Left:%.2d\n", maway, mleft);
printf(" Current Score:%.3d Last Move: %s\n", score, prevmove);
printf(" Enter Target Coordinates--> ");
scanf("%d",&input);
board[input] ='X';
}

__________________________________________________ ________________

the output it should be like this:

C Battleship...
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1|~~~~~~~~~~~~~~~~~~~~~~~~~~|
2|~~~~~~~~~~~~~~~~~~~~~~~~~~|
3|~~~~~~~~~~~~~~~~~~~~~~~~~~|
4|~~~~~~~~~~~~~~~~~~~~~~~~~~|
5|~~~~~~~~~~~~~~~~~~~~~~~~~~|
6|~~~~~~~~~~~~~~~~~~~~~~~~~~|
7|~~~~~~~~~~~~~~~~~~~~~~~~~~|
8|~~~~~~~~~~~~~~~~~~~~~~~~~~|
9|~~~~~~~~~~~~~~~~~~~~~~~~~~|
A|~~~~~~~~~~~~~~~~~~~~~~~~~~|
B|~~~~~~~~~~~~~~~~~~~~~~~~~~|
C|~~~~~~~~~~~~~~~~~~~~~~~~~~|
D|~~~~~~~~~~~~~~~~~~~~~~~~~~|
E|~~~~~~~~~~~~~~~~~~~~~~~~~~|
F|~~~~~~~~~~~~~~~~~~~~~~~~~~|
Missiles Away:00 Missiles Left:50
Current Score:000 Last Move:
Enter Target Coordinates--> 5T

C Battleship...
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1|~~~~~~~~~~~~~~~~~~~~~~~~~~|
2|~~~~~~~~~~~~~~~~~~~~~~~~~~|
3|~~~~~~~~~~~~~~~~~~~~~~~~~~|
4|~~~~~~~~~~~~~~~~~~~~~~~~~~|
5|~~~~~~~~~~~~~~~~~~~X~~~~~~|
6|~~~~~~~~~~~~~~~~~~~~~~~~~~|
7|~~~~~~~~~~~~~~~~~~~~~~~~~~|
8|~~~~~~~~~~~~~~~~~~~~~~~~~~|
9|~~~~~~~~~~~~~~~~~~~~~~~~~~|
A|~~~~~~~~~~~~~~~~~~~~~~~~~~|
B|~~~~~~~~~~~~~~~~~~~~~~~~~~|
C|~~~~~~~~~~~~~~~~~~~~~~~~~~|
D|~~~~~~~~~~~~~~~~~~~~~~~~~~|
E|~~~~~~~~~~~~~~~~~~~~~~~~~~|
F|~~~~~~~~~~~~~~~~~~~~~~~~~~|
Missiles Away:01 Missiles Left:49
Current Score:000 Last Move: MISS on 5T
Enter Target Coordinates-->
__________________________________________________ ____

I have troubling with coordinates, i don't know how to do it. suppose if users enter 5T, the result should be X in row 5 and col T.

need help
thanks
Nov 25 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
May I ask why you have implemented the board as a single dimentioned array. For this program a 2 dimensioned array would seem to be easier

board[15][26];
Nov 25 '06 #2

P: 5
May I ask why you have implemented the board as a single dimentioned array. For this program a 2 dimensioned array would seem to be easier

board[15][26];

i know but i am troubling with the results plz help me out


thanks
Nov 25 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Well the reason for my last question is that you have accessed the board array incorrectly in the nested loop

Expand|Select|Wrap|Line Numbers
  1.  for(i=0; i<15; i++) { /* rows */
  2.     printf("%c|", row_letters[i]);
  3.  
  4.     for(j=0; j<26; j++) { /* columns */
  5.         printf("%c", board[j]);
  6.     }
  7.  
  8.     printf("|\n");
  9. }
  10.  
printf("%c", board[j]);

should be

printf("%c", board[i*26+j]);
Nov 25 '06 #4

P: 5
Well the reason for my last question is that you have accessed the board array incorrectly in the nested loop

Expand|Select|Wrap|Line Numbers
  1.  for(i=0; i<15; i++) { /* rows */
  2.     printf("%c|", row_letters[i]);
  3.  
  4.     for(j=0; j<26; j++) { /* columns */
  5.         printf("%c", board[j]);
  6.     }
  7.  
  8.     printf("|\n");
  9. }
  10.  
printf("%c", board[j]);

should be

printf("%c", board[i*26+j]);
hi,

it works but it only count col. suppose if u enter 4T. it will only count ```x. it doesn't count char.

plz help me out
thanks
Nov 25 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
That is because you have used

scanf("%d",&input);

You are requesting a number where as what you actually want as input is a string, a 1 char row id and a 1 char column id.

Use fgets to read in a line of data, then verify the first character is a valid row and the second character is a valid column and that the 3rd character is the termintor.
Nov 26 '06 #6

Post your reply

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