Solved

battleship C again

Posted on 2004-08-12
12
2,855 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using unmanaged DLL from managed application 14 297
Problem to show menu 5 98
Computer slow / BSOD 10 70
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 75
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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

730 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