Solved

battleship C again

Posted on 2004-08-12
12
2,839 Views
Last Modified: 2011-09-20
(view http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21090791.html)
I have this program, and it does not put it in the correct row. please compile/run using gcc compiler(i dont know if that makes a difference). i have a feeling the program resides in getcoordinates().
the output should be:
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1 (input)
Enter coordinates: H7 (input)
Enter value to be written at (7, 6): 3 (input)
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 2 (input)
0 0 0 0 0 0 x 0 0 0  (but instead, the 3 is not showing there. it is showing on the part where I put the x)
0 0 0 0 0 0 0 0 0 0   also, this statement: printf("\nmyRow = %d, myCol = %d\n", myRow, myCol);
0 0 0 0 0 0 0 0 0 0   shows as myRow = 0 myCol = 8 (help!)
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 3 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

void clearBoard(int rows, int columns, int * boardPtr);
void showBoard(int rows, int columns, int * boardPtr);
int getCoords(int maxRow, int maxCol, int *row, int *col);
void paintSpace(int rows, int cols, int * boardPtr);

void clearBoard(int rows, int columns, int * boardPtr)
{
int i;

for(i=0; i < rows * columns; i++)
   {
      *(boardPtr+i) = 0; //initialize value to 0
   }
} // End of clearBoard function.


void showBoard(int rows, int columns, int * boardPtr)
{
int i;
for(i=0; i < rows * columns; i++)
   {
   // Print newline character is row is finished.
   // If i modulus the columns is 0, then you've reached
   // the end of a row and need to print a newline.
   if(i % columns == 0)
      {
      printf("\n");
      }
   
   if ((*(boardPtr+i) >= 0) && (*(boardPtr+i) <= 9))
      {
      printf("%d ", *(boardPtr+i));
      }
   else
      {
      printf("x");
      }
   }
}

void eatWhitespace (void)
{
  char ch;
  while(isspace(ch = getchar()))
    {
      /* Do nothing. */
    }
  ungetc(ch, stdin);
}

void eatToNewline (void)
{
  while (getchar() != '\n')
    {
      /* Do nothing. */
    }
}


int getCoords(int maxRow, int maxCol, int *row, int *col)
{
char myRowchar;
 
int myCol=0;
int myRownum=0;
myRownum = (int)myRowchar;
 
eatWhitespace();

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
 
  if(myRownum >= 97) /*if the row is greater than or equal to a*/  
   {
    myRownum = myRownum - 97;
     
   }
  else if((myRownum >=65) && (myRownum <=90))
    /*if row is greater than A AND less than 90*/
   {
    myRownum = myRownum - 65;
   
   }  

  if (myRownum > maxRow)
   {
    *row = 0;
    *col = 0;
    return 0;
   }
  else if (myCol > maxCol)
   {
    *row = 0;
    *col = 0;
    return 0;
   }

  else
   {
    *row = myRownum;
    *col = myCol - 1; // Need to compensate because array starts at 0.
   }
 
}
 else
   {
     eatToNewline();
   }

 return 1;
}

void paintSpace(int rows, int cols, int * boardPtr)
{
int myRow = 0, myCol = 0, value = 0;

printf("Enter coordinates :");

 
  while ( 0 == getCoords(rows,cols,&myRow,&myCol))
{
   printf("Bad coordinates!\nEnter coordinates: ");
}
  printf("\nmyRow = %d, myCol = %d\n", myRow, myCol);

printf("Enter a value: ");
scanf("%d", &value);

*(boardPtr + (myRow*cols) + myCol)  = value;


}










int main()
{
int choice;
int rows = 10;
int columns = 10;
int boardPtr[rows][columns];

while(1)
{
   printf("\n*************************************************\n");
   printf("0 - Clear the board.\n");
   printf("1 - Paint a space.\n");
   printf("2 - Show board.\n");
   printf("3 - Play game.\n");
   printf("4 - Quit.\n");
   printf("*************************************************\n");
   printf("Your choice? _");
   scanf("%d", &choice);
   
   switch(choice)
      {
      case 0:
         clearBoard(rows, columns, (int *)boardPtr);
         break;

      case 1:
       fflush(stdin);
 paintSpace(rows, columns, (int *)boardPtr);
         break;

      case 2:
         showBoard(rows, columns, (int *)boardPtr);
         break;

      case 3:
         printf("The game isn't ready yet.\n");
         break;

      case 4:
         exit(0);

      default:
         printf("Your input was invalid.\n");
         break;
      } //End of Switch statement
   } //End of while loop
 return 0;
} //End of Program
0
Comment
Question by:simmah
  • 5
  • 4
  • 3
12 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11790165
in int getCoords(int maxRow, int maxCol, int *row, int *col)

---
you declare  myRowchar,

and then initialize myRownum by casting myRowchar to an int.

the scanf reads the value of myRowchar and myCol

but after that you check for values of "myRownum "  which is initialised only with the junk value thatwas present when you declare
myRowchar,.

/abhijit/
0
 

Author Comment

by:simmah
ID: 11790210
what do you suggest I do?
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 100 total points
ID: 11790234
That would depend on what your intention is

the I pointed out is that

in funtion getCoords

you declare myRowchar --char myRowchar;

now after this line the value of "myRowchar" is not defined. It can have any junk value.
now what you do next is
myRownum = (int)myRowchar;

i.e you transfer the junk value of myRowchar to myRownum after casting it to an int.

and then later you check
if(myRownum >= 97

so basically you are comparing a junk value againts 97 , the result of which is not defnite.

-----
now what you should do , depends very much on what is your aim. I have no idea bout that. Only thing i know is you are using uninitailised value .which is wrong.









0
 

Author Comment

by:simmah
ID: 11790254
I made it an (int) because myrowchar first starts off as a character type. then I want to convert it into an int and do the calculations through it. the rows are supposed to be from A-J(1-10). therefore, 97 is decimal value for 'a' and 65 is decimal value for 'A'. if you go to the link mentioned on the initial posting, it'll show you the requirements and also the output and what it should be.
0
 

Author Comment

by:simmah
ID: 11790331
It is almost 11pm. I will check back again 8am(central US), if you need anything else. Thank you and I hope we get somewhere with this.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 400 total points
ID: 11790338
Hi,
>myRownum = (int)myRowchar;
Put this statement after the scanf in getcoords()
It should be:

int getCoords(int maxRow, int maxCol, int *row, int *col)
{
char myRowchar;

int myCol=0;
int myRownum=0;

eatWhitespace();

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
 myRownum = (int)myRowchar;
 
 if(myRownum >= 97) /*if the row is greater than or equal to a*/  
  {
   myRownum = myRownum - 97;
     
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 11790358
when scanf() is not successful,it means bad coordinates,so make the user enter again

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
 .
 .
 .
//Your code here
}
else
  {
    eatToNewline();
    return 0;
  }

Also,Right now,it accepts 0 and -ve values for myCol,put in a check so that if mycol<=0 then print bad coords and enter again.
0
 
LVL 11

Expert Comment

by:avizit
ID: 11790359
That is fine

what is wrong is this

char myRowchar;

after this line the value of myRowchar is undefined as you havent initialized it . So it contains a junk value.

and now you use this junk value to populate myRownum  -> myRownum = (int)myRowchar;

and then you use myRownum for comparisons which actually contains junk value .....

also read up http://cplus.about.com/library/weekly/aa101902k.htm

0
 
LVL 11

Expert Comment

by:avizit
ID: 11790364
ankuratvb is fast ( as usual :)
my response is to simmah
just mentioned lest it looks like i am responding to ankur :)

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11790368
Hi avizit,

if (2 == scanf("%c %d", &myRowchar, &myCol))
{
myRownum = (int)myRowchar;

This solves the problem,as myrowchar is scanf'd and if it is successful,then only,myrownum is set and the calculations done.
0
 
LVL 11

Expert Comment

by:avizit
ID: 11790389
It does solves :) sorry I dint mean to say your solution was wrong . Your response slipped in between ..i was responding to the author.
I typed my response and was googling for reference and guess then you responded.



0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11790410
NP :~))
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now