Link to home
Start Free TrialLog in
Avatar of simmah
simmahFlag for United States of America

asked on

battleship-like C Program

(500 pts)
The first part explains the program and what it should do(this is just the first part). The second part is my sourcecode.It compiles correctly,btw.I know it has something to do with my damn loops! Please help! also, i added fflush(stdin) and I think it's working correctly, please verify.
Instructions:
Part 1.1: Write a function with the following prototype:

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

The function should write a zero value to each element in the passed 2-D array.

Part 1.2: Write a function with the following prototype:

void showBoard (int rows, int columns, int * boardPtr);

The function should display the array. If the value of a board location is 0 to 9, print that value, otherwise print "x". Example:

int board [3][3] = { {0, 0, 1}, {99, -2, 7}, {-1, 6, 0} };
/* ... */
showBoard(3, 3, board);
/* ... */

Should display:

0 0 1
x x 7
x 6 0

Part 1.3: Write a function that reads board coordinates, with the following prototype:

int getCoords (int maxRow, int maxCol, int * row, int * col);

The function should read a character followed by a number, and convert these into a row, column pair that is output through the row, col parameters. The function should return 0 if an invalid entry was made, or a 1 if a valid entry was made. Example:

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

Example output:

Enter coordinates: yy zz
Bad coordinates!
Enter coordinates : 0 0
Bad coordinates!
Enter coordinates : A10
Bad coordinates!
Enter coordinates : A 9
Bad coordinates!
Enter coordinates : B 2
myRow = 1, myCol = 1

The following chart might help illustrate how this coordinate system works:

    1     2     3     4     ...
A (0,0) (0,1) (0,2) (0,3) ...
B (1,0) (1,1) (1,2) (1,3) ...
C (2,0) (2,1) (2,2) (2,3) ...
D (3,0) (3,1) (3,2) (3,3) ...
...

Where the output row and column numbers are given as (row, column).

Part 1.4: Write a function with the following prototype:

void paintSpace (int rows, int cols, int * boardPtr);

The function should read a "Battleship" coordinate using the getCoords() and an integer value from the console. The integer value should be written to the board array corresponding to the input coordinate.

Part 1.5: Write a menu loop (in main(), for now) that asks the following:

************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? _

If they select 0, zero all the values of the board. If they select 1, ask for coordinates and an integer value to write to the board. Then write that value to the board. If they select 2, display the board using the showBoard() function. If they select 3, tell the user that the game isn't ready yet. If they select 4, exit the program. Otherwise, tell the user their input was invalid. Use a 10x10 board

My SourceCode:
#include<stdio.h>
#include<stdlib.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 ");
      }
   }
}

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

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

if(myRownum >= 97)
   {
   myRownum = myRownum - 97;
   }
else if((myRownum >=65) && (myRownum <=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.
   }
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("myRow = %d, myCol = %d\n", myRow, myCol);*/

printf("Enter a value: ");
scanf("%d", &value);
value = *(boardPtr + (myRow*cols) + myCol) ;

}

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
Avatar of simmah
simmah
Flag of United States of America image

ASKER

can someone PLEASE PLEASE HELP ME?????
SOLUTION
Avatar of grg99
grg99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

I just changed it to what you said and it still isnt working. basically, this is what is happening:
1. i select 'clear board' and hit enter
2.i select 2 to 'paint a space' and put in the coordinates. it correctly 'paints a space' in the background(verified it). but, it then displays the menu again(correct) followed by "You choice? _Enter coordinates :Enter a value:" all in one line! that's my issue here. what it should do is bring up the menu and give me the option of choosing 1-4 again.
Please assist.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

so wherever there is "scanf(...%d) add a '\n' by it?
Avatar of simmah

ASKER

can you please be a bit more specific as to where?
thank you.
Avatar of simmah

ASKER

I might be confusing you. I think the program does what it's supposed to, but it's just added extra wording. here is the input/output format(i havent begun working on choice 3 which is the last one, so dont worry about that) If you can, please compile/run it and you'll know exactly what im talking about.

************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1
Enter coordinates: a0
Bad coordinates!
Enter coordinates: xx yy
Bad coordinates!
Enter coordinates: A -1
Bad coordinates!
Enter coordinates: A1
Enter value to be written at (0, 0): 1
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1
Enter coordinates: j 11
Bad coordinates!
Enter coordinates: j 10
Enter value to be written at (9, 9): 2
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1
Enter coordinates: j9
Enter value to be written at (9, 8): 2
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1
Enter coordinates: F 5
Enter value to be written at (5, 4): 3
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1
Enter coordinates: g6
Enter value to be written at (6, 5): 3
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1
Enter coordinates: H7
Enter value to be written at (7, 6): 3
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 2
1 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 0 0 0
0 0 0 0 3 0 0 0 0 0
0 0 0 0 0 3 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 2 2
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 3
************************************************************
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

I put this:
 if (scanf("%d", &choice) < 1)
   {
        printf("Scanf: Format error");

        exit(0);        
   }
next to my scanf()s and it doesnt run properly. it just stalls and i keep hitting enter after my input.
I also used the other suggestions you gave. Maybe im just not putting it in the right place.
Avatar of simmah

ASKER

it's
correctly letting me type in the value of the coordinate area, but it's re-
looping the entire menu and then some. so, instead of giving me:
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1 (input)
Enter coordinates: F 5 (input)
Enter a value: 3 (input)

IT GIVES ME:
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? 1 (my input)
Enter coordinates: F 5 (my input)
Enter a value:
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
Your choice? _Enter Coordinates :Enter values:3(this is where i put my input(?))
Avatar of simmah

ASKER

It's 10:34pm (central US). I will be going to sleep and will check back again tomorrow. thank you for your assistance.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

This is how my sourcode looks at the bottom. I have put fflush(stdout),fflush(stdin) on before/after all prompts. I set myCol to what was said and it stalled once more. either way, i am still getting the same out as stated in my comment"Date: 08/11/2004 08:23PM PDT".
#include<stdio.h>
#include<stdlib.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 ");
      }
   }
}

int getCoords(int maxRow, int maxCol, int *row, int *col)
{
char myRowchar;
int myCol=0;
int myRownum=0;
 fflush(stdout);
scanf("%c %d", &myRowchar, &myCol);
 fflush(stdin);
myRownum = (int)myRowchar;

if(myRownum >= 97)
   {
   myRownum = myRownum - 97;
   }
else if((myRownum >=65) && (myRownum <=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.
   }
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("myRow = %d, myCol = %d\n", myRow, myCol);*/

printf("Enter a value: ");
 fflush(stdout);
scanf("%d", &value);
 fflush(stdin);
*(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? _");
   fflush(stdout);  
 scanf("%d", &choice);
   fflush(stdin);

   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


Avatar of simmah

ASKER

please try compiling/running it to see exactly what im talking about when i say there is a clutter of statements after i input the coordinates...it'll take the value i assign the coordinates, but the clutter is there first.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

Im using cygwin and have customized it for the gcc compiler.
im not giving any spaces anywhere.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

so i should get rid of all of my fflush(stdin)'s and stdouts and just put fprintf(stderr,"hello"); for all printf statements?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

it doesnt work. I put exactly what you put in. when i entered correct coordinates(a6), it printed out 'bad coordinates!' then asked for the value...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

i did, it shows a '11' before and '11' after and gives 'bad coordinates' error message. so, myrowchar and mycol are throwing out 1's
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

I did, it's still giving me an 11.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simmah

ASKER

it works a bit. below, you can see f6 repeats itself twice.
Enter coordinates:f6 (my input)
                         
Enter a value:
************************************************************
0 - Clear the board.
1 - Paint a space.
2 - Show board.
3 - Play game.
4 - Quit
************************************************************
f 6f 6Enter values:3(this is where i put my input)
Well,now your \n is consumed at the enter value prompt.
Put a ch=getchar() after all your scanf() statements and remove all the fflush(stdin) and stdout.