GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Nov-2006, 12:39
study007 study007 is offline
New Member
 
Join Date: Nov 2006
Posts: 9
study007 is on a distinguished road

help in battleship


hi
I need help in battleship game.

I wrote the prog for battleship but its not working properly

here is my code:

CPP / C++ / C Code:
#include <stdio.h>

#include <string.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[i*26+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';
   printf("\n");
}


the output should be like, suppose if coordinate is: 5T. The X should be like.

X should be place in between 5 and T.

ABCDEFGHIJKLMNOPQRSTUVWXYZ
1|~~~~~~~~~~~~~~~~~~~~~~~~~~|
2|~~~~~~~~~~~~~~~~~~~~~~~~~~|
3|~~~~~~~~~~~~~~~~~~~~~~~~~~|
4|~~~~~~~~~~~~~~~~~~~~~~~~~~|
5|~~~~~~~~~~~~~X~~~~~~~~~~~~|
6|~~~~~~~~~~~~~~~~~~~~~~~~~~|
7|~~~~~~~~~~~~~~~~~~~~~~~~~~|
8|~~~~~~~~~~~~~~~~~~~~~~~~~~|
9|~~~~~~~~~~~~~~~~~~~~~~~~~~|
A|~~~~~~~~~~~~~~~~~~~~~~~~~~|
B|~~~~~~~~~~~~~~~~~~~~~~~~~~|
C|~~~~~~~~~~~~~~~~~~~~~~~~~~|
D|~~~~~~~~~~~~~~~~~~~~~~~~~~|
E|~~~~~~~~~~~~~~~~~~~~~~~~~~|
F|~~~~~~~~~~~~~~~~~~~~~~~~~~|



but i am getting wrong out put. which is:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
1|~~~~X~~~~~~~~~~~~~~~~~~~~~|
2|~~~~~~~~~~~~~~~~~~~~~~~~~~|
3|~~~~~~~~~~~~~~~~~~~~~~~~~~|
4|~~~~~~~~~~~~~~~~~~~~~~~~~~|
5|~~~~~~~~~~~~~~~~~~~~~~~~~~|
6|~~~~~~~~~~~~~~~~~~~~~~~~~~|
7|~~~~~~~~~~~~~~~~~~~~~~~~~~|
8|~~~~~~~~~~~~~~~~~~~~~~~~~~|
9|~~~~~~~~~~~~~~~~~~~~~~~~~~|
A|~~~~~~~~~~~~~~~~~~~~~~~~~~|
B|~~~~~~~~~~~~~~~~~~~~~~~~~~|
C|~~~~~~~~~~~~~~~~~~~~~~~~~~|
D|~~~~~~~~~~~~~~~~~~~~~~~~~~|
E|~~~~~~~~~~~~~~~~~~~~~~~~~~|
F|~~~~~~~~~~~~~~~~~~~~~~~~~~|

please help me with this, don't know what to do

thanx
  #2  
Old 25-Nov-2006, 13:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help in battleship


Quote:
Originally Posted by study007
hi

I wrote the prog
.
.
.
CPP / C++ / C Code:
   printf(" Enter Target Coordinates--> ");
   scanf("%d",&input);
   board[input] ='X';
   printf("\n");
}

What input (a single int, right?) do you give it so that it will place the 'X' at the coordinates that you say you want?

Regards,

Dave
  #3  
Old 25-Nov-2006, 14:15
study007 study007 is offline
New Member
 
Join Date: Nov 2006
Posts: 9
study007 is on a distinguished road

Re: help in battleship


2 input one int and one char. for example Coordinate 4T

i know i wrote one int cause i don't know how to do it, yeh the array will replace with X.
thanks
  #4  
Old 25-Nov-2006, 15:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help in battleship


Quote:
Originally Posted by study007
2 input one int and one char. for example Coordinate 4T

i know i wrote one int cause i don't know how to do it, yeh the array will replace with X.
thanks

You might think of reading two chars. You can do it with scanf("%c%c") or two getchar() calls.

Either way, you have to know a little more about buffered input. Namely, whether you use scanf("%c") or getchar(), here's the story:

When the user starts typing, nothing gets to the C program until the user presses 'Enter' at the end of his/her line. At that time all of the input is in a system buffer, from which a program gets its input data.

Suppose the user presses the '5' key and the 'T' key and 'Enter'. Then there are three chars in the system buffer (the '5', the 'T' and a newline char, '\n'). Two calls to getchar (or a call to scanf with "%c%c" format specifier) will get the char '5' into program memory, the char 'T' into program memory, and it will leave the newline char in the system buffer. Then the next time a user program does its getchar() or scanf("%c.."), it will read the newline char into the user's program memory. That's not what you want (obviously). Also, if the user enters more than two chars, the rest of the line will still be in the system buffer and will screw up future program inputs.

So, here's a way to read exactly two chars from the user and clear out the system buffer so that the next time through the loop it will be waiting for fresh user input.
CPP / C++ / C Code:
    scanf("%c%c", &crow, &ccol); /* read exactly two chars */
    while(getchar() != '\n')  /* an "empty loop" to clear out the input buffer */
        ;


While we are at it, let's also convert user row 0-9,A-F to integer values 0-15 to use as the row index to your array. Also user column A-Z will be converted to integer values 0-25 to be used as the column index to your array. This makes a couple of assumptions about character encoding that will serve for most computers (and all PC implementations that I know about).

If the user enters either or both chars that are not a valid row or column designator for the game, I just set the integer row or column to -1 so that the program can know that it was invalid (and make sure that the program doesn't try to store something outside the bounds of the array).

CPP / C++ / C Code:
.
.
.
    char crow, ccol; /* chars entered by user */
    int row, col;   /* int values corresponding to user input */
.
.
.
    printf(" Enter Target Coordinates --> ");
    /* 
     * Will convert user input to integer values (row, col), where
     * (0, 0) is upper left, (14, 25) is lower right)
     */
    scanf("%c%c", &crow, &ccol); /* two chars, no space between them */

    while(getchar() != '\n')  /* an "empty loop" to clear out the input buffer */
        ;
    printf("crow = %c, ccol = %c\n", crow, ccol);
    if ((crow >= '1') && (crow <= '9')) {
        row = crow - '1';
    }
    else if ((crow >= 'A') && (crow <= 'F')) {
        row = crow - 'A' + 9;
    }
    else if ((crow >= 'a') && (crow <= 'f')) {
        row = crow - 'a' + 9;
    }
    else {
        row = -1;
    }
    printf("row = %d\n", row);
    if ((ccol >= 'A') && (ccol <= 'Z')) {
        col = ccol - 'A';
    }
    else if ((ccol >= 'a') && (ccol <= 'z')) {
        col = ccol - 'a';
    }
    else {
        col = -1;
    }
    printf("col = %d\n", col);
    if ((row < 0) || (col < 0)) {
        printf("Illegal move\n");
    }
    else {
       /* store 'X' in the array position corresponding to this row and column */
    }
    printf("\n");

Regards,

Dave
  #5  
Old 25-Nov-2006, 15:55
study007 study007 is offline
New Member
 
Join Date: Nov 2006
Posts: 9
study007 is on a distinguished road

Re: help in battleship


Hi, Dave

thank u very much. I like the way u teach me. You should be my professor because u teach more better than my professor

thanks

but the prog doesn't compiling getting error:

undeclared identifier crow
undeclared identifier ccol
undeclared identifier row
undeclared identifier col

i did declared that in main here is my code:
CPP / C++ / C Code:
#include <stdio.h>

#include <string.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;
char crow, ccol; /* chars entered by user */
    int row, col;   /* int values corresponding to user input */

   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[i*26+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--> ");
   /*
     * Will convert user input to integer values (row, col), where
     * (0, 0) is upper left, (14, 25) is lower right)
     */
    scanf("%c%c", &crow, &ccol); /* two chars, no space between them */

    while(getchar() != '\n')  /* an "empty loop" to clear out the input buffer */
        ;
    printf("crow = %c, ccol = %c\n", crow, ccol);
    if ((crow >= '1') && (crow <= '9')) {
        row = crow - '1';
    }
    else if ((crow >= 'A') && (crow <= 'F')) {
        row = crow - 'A' + 9;
    }
    else if ((crow >= 'a') && (crow <= 'f')) {
        row = crow - 'a' + 9;
    }
    else {
        row = -1;
    }
    printf("row = %d\n", row);
    if ((ccol >= 'A') && (ccol <= 'Z')) {
        col = ccol - 'A';
    }
    else if ((ccol >= 'a') && (ccol <= 'z')) {
        col = ccol - 'a';
    }
    else {
        col = -1;
    }
    printf("col = %d\n", col);
    if ((row < 0) || (col < 0)) {
        printf("Illegal move\n");
    }
    else {
       /* store 'X' in the array position corresponding to this row and column */
    }
    printf("\n");

}
thanks for ur help buddy
  #6  
Old 25-Nov-2006, 16:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help in battleship


Quote:
Originally Posted by study007
but the prog doesn't compiling getting error:

undeclared identifier crow
undeclared identifier ccol
undeclared identifier row
undeclared identifier col

i did declared that in main here is my code:

The variables should be declared inside the function where they are used. Each function starts its own "address space", and variables declared in other functions are not visible.


Regards,

Dave
  #7  
Old 25-Nov-2006, 16:49
study007 study007 is offline
New Member
 
Join Date: Nov 2006
Posts: 9
study007 is on a distinguished road

Re: help in battleship


Quote:
Originally Posted by davekw7x
The variables should be declared inside the function where they are used. Each function starts its own "address space", and variables declared in other functions are not visible.


Regards,

Dave

hi Dave,

same thing, i am getting same errors as before, don't know why..

now i am getting more error like:

char encountered
int encountered


thanks
  #8  
Old 25-Nov-2006, 17:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help in battleship


Quote:
Originally Posted by study007
errors
1. Post your code.
2. Tell us what compiler and operating system you are using.
3. Post the exact compiler messages. Paste them into your post. Don't edit or delete or paraphrase any part of the messages.

Regards,

Dave
  #9  
Old 25-Nov-2006, 17:26
study007 study007 is offline
New Member
 
Join Date: Nov 2006
Posts: 9
study007 is on a distinguished road

Re: help in battleship


Quote:
Originally Posted by davekw7x
1. Post your code.
2. Tell us what compiler and operating system you are using.
3. Post the exact compiler messages. Paste them into your post. Don't edit or delete or paraphrase any part of the messages.

Regards,

Dave

hi Dave.

I am using c compiler in phobos.
here is my code:
CPP / C++ / C Code:
#include <stdio.h>

#include <string.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[i*26+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--> ");
   /*
     * Will convert user input to integer values (row, col), where
     * (0, 0) is upper left, (14, 25) is lower right)
     */

    char crow,ccol; /* chars entered by user */
    int row, col;   /* int values corresponding to user input */

    scanf("%c%c", &crow,&ccol); /* two chars, no space between them */

    while(getchar() != '\n')  /* an "empty loop" to clear out the input buffer */
        ;

    printf("crow = %c, ccol = %c\n", crow, ccol);
    if ((crow >= '1') && (crow <= '9')) {

        row = crow - '1';
    }
    else if ((crow >= 'A') && (crow <= 'F')) {
        row = crow - 'A' + 9;
    }
    else if ((crow >= 'a') && (crow <= 'f')) {
        row = crow - 'a' + 9;
    }
    else {
        row = -1;
    }
    printf("row = %d\n", row);
    if ((ccol >= 'A') && (ccol <= 'Z')) {
        col = ccol - 'A';
    }
    else if ((ccol >= 'a') && (ccol <= 'z')) {
        col = ccol - 'a';
    }
    else {
        col = -1;
    }
    printf("col = %d\n", col);
    if ((row < 0) || (col < 0)) {
        printf("Illegal move\n");
    }
    else {
       /* store 'X' in the array position corresponding to this row and column */
    }
    printf("\n");

}


here is my error:

"333.c", line 50.5: 1506-275 (S) Unexpected text 'char' encountered.
"333.c", line 50.10: 1506-045 (S) Undeclared identifier crow.
"333.c", line 51.5: 1506-275 (S) Unexpected text 'int' encountered.
"333.c", line 51.9: 1506-045 (S) Undeclared identifier row.
"333.c", line 53.26: 1506-045 (S) Undeclared identifier ccol.
"333.c", line 74.9: 1506-045 (S) Undeclared identifier col.


thanks Dave
  #10  
Old 25-Nov-2006, 17:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: help in battleship


Quote:
Originally Posted by study007

here is my code:


Put all of your variable declarations before any executable code in each function block:

CPP / C++ / C Code:
void draw_game(char board[ ], int maway, int mleft, int score, char prevmove[ ]) {
   int i,input ,j;
   char row_letters[16] = "123456789ABCDEF";
   char crow,ccol; /* chars entered by user */
   int row, col;   /* int values corresponding to user input */

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

Regards,

Dave
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[C] Battleship for NT platforms Alhazred C Programming Language 4 20-Jul-2004 04:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.