help with battleship program
im doing a battleship program for my c++ class and i keep gettin these errors when i compile:
line
84 `grid' undeclared (first use this function)
84 `y' undeclared (first use this function)
84 `x' undeclared (first use this function)
93 `game' undeclared (first use this function)
93 `shoot' undeclared (first use this function)
In function `int main()':
151 `ships' undeclared (first use this function)
167 a function-definition is not allowed here before '{' token
167 expected `,' or `;' before '{' token
301 `shoot' undeclared (first use this function)
`void placement2(int (*)[12], int (*)[12], int)':
my source code is
Code:
#include <stdio.h>
#include <iostream>
using namespace std;
int choose(){
int num;
void placement(int ships[][12]);
void shoot(int grid[][12], int ships[][12], int y, int x, int *game);
scanf( "%d", &num );
if(num < 0 || num > 12){
printf( "Invalid choice, choose again.\n" );
num = choose();
}
return num;
}
void graph(int grid[][12])
{
int i;
int j;
cout << " A B C D E F G H I J K L\n";
cout << " ----------------------------\n";
for(i = 0; i < 12; i++){
printf( "%d", i );
for(j = 0; j < 12; j++){
if(grid[i][j] == 0)
cout << " " ;
else if(grid[i][j] == 1)
cout<< "X" ;
else if(grid[i][j] == 2)
cout<< "-" ;
}
cout<<"\n";
}
cout<<"\n" ;
}
void placement2(int map[][12], int ships[][12], int num)
{
if(grid[y][x] == 1 || grid[y][x] == 2){
cout<<"This space has already been chosen, choose another space\n";
cout<< "Row:";
y = choose();
cout<< "Column:" ;
x = choose();
shoot(grid, ships, y, x, game);
}
else{
if(ships[y][x] == 1){
cout<< "HIT!\n" ;
grid[y][x] = 1;
ships[y][x] = 0;
}
else if(ships[y][x] == 0){
cout<< "Miss.\n" ;
grid[y][x] = 2;
}
}
int k;
int l;
int check = 0;
for(k = 0; k < 12; k++){
for(l = 0; l < 12; l++){
if(ships[k][l] == 1)
check = 1;
}
}
if(check == 1)
*game = 0;
}
int main()
{
/* Grid is what the players see, ships is where the ships are */
/* Player 1 ships */
int grid1[12][12] = {{0}, {0}};
int ships1[12][12] = {{0}, {0}};
/* Player 2 ships */
int grid2[12][12] = {{0}, {0}};
int ships2[12][12] = {{0}, {0}};
{
int map[12][12] = {0};
printf( "Place your 5 space ship\n" );
placement2(map, ships, 5);
printf( "Place your 4 space ship\n" );
placement2(map, ships, 4);
printf( "Place your 3 space ship\n" );
placement2(map, ships, 3);
printf( "Place your second 3 space ship\n" );
placement2(map, ships, 3);
printf( "Place your 2 space ship\n" );
placement2(map, ships, 2);
}
void placement2(int map[][12], int ships[][12], int num)
{
int y;
int x;
char voh;
int i;
int check = 0;
graph(map);
printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
printf( "Row:" );
y = choose();
printf( "Column:" );
x = choose();
printf( "Vertical or Horizontal? (V/H):" );
scanf( " %c", &voh ); /* BREAKS DOWN HERE */
if(voh == 'V' || voh == 'v'){
if(y + num >= 8){
printf( "Goes off the grid. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = y; i < y + num; i++){
if(ships[i][x] == 1)
check = 1;
}
if(check == 1){
printf( "Intersects another ship. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = y; i < y + num; i++){
map[i][x] = 2;
ships[i][x] = 1;
}
}
}
}
else if(voh == 'H' || voh == 'h'){
if(num + x >= 8){
printf( "Goes off the grid. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = x; i < x + num; i++){
if(ships[y][i] == 1)
check = 1;
}
if(check == 1){
printf( "Intersects another ship. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = x; i < x + num; i++){
map[y][i] = 2;
ships[y][i] = 1;
}
}
}
}
else{
printf( "Invalid. Retry.\n" );
placement2(map, ships, num);
}
}
int y;
int x;
int gamestatus = 1;
while(gamestatus == 1){
cout<< "Player 1's turn\n" ;
graph(grid2);
cout<< "Row:" ;
y = choose();
cout<<"Column:" ;
x = choose();
shoot(grid2, ships2, y - 1, x - 1, &gamestatus);
if(gamestatus == 0){
cout<< "PLAYER 1 WINS!\n" ;
break;
}
cout<< "Player 2's turn.\n";
graph(grid1);
cout<< "Row:" ;
y = choose();
cout<<"Column:" ;
x = choose();
shoot(grid1, ships1, y - 1, x - 1, &gamestatus);
if(gamestatus == 0)
cout<< "PLAYER 2 WINS!\n" ;
else
cout<<"Next round.\n" ;
}
return 0;
}
i fixed that part but im still having errors
i fixed that but im still gettin erros. heres my code
Code:
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int choose;
int num;
int ships;
int y;
int x;
scanf( "%d", &num );
if(num < 1 || num > 8){
printf( "Invalid choice, choose again.\n" );
num = choose;
}
return num;
}
void graph(int grid[][8])
{
int i;
int j;
printf( " 12345678\n" );
for(i = 0; i < 8; i++){
printf( "%d", i );
for(j = 0; j < 8; j++){
if(grid[i][j] == 0)
printf( " " );
else if(grid[i][j] == 1)
printf( "X" );
else if(grid[i][j] == 2)
printf( "O" );
}
printf( "\n" );
}
printf( "\n" );
}
void shoot(int grid[][8], int ships[][8], int y, int x, int *game)
{
if(grid[y][x] == 1 || grid[y][x] == 2){
printf( "This space has already been chosen, choose another space\n" );
printf( "Row:" );
y = choose;
printf( "Column:" );
x = choose();
shoot(grid, ships, y, x, game);
}
else{
if(ships[y][x] == 1){
printf( "HIT!\n" );
grid[y][x] = 1;
ships[y][x] = 0;
}
else if(ships[y][x] == 0){
printf( "Miss.\n" );
grid[y][x] = 2;
}
}
int k;
int l;
int check = 0;
for(k = 0; k < 8; k++){
for(l = 0; l < 8; l++){
if(ships[k][l] == 1)
check = 1;
}
}
if(check == 1)
*game = 0;
}
int main()
{
/* Grid is what the players see, ships is where the ships are */
/* Player 1 ships */
int grid1[8][8] = {{0}, {0}};
int ships1[8][8] = {{0}, {0}};
/* Player 2 ships */
int grid2[8][8] = {{0}, {0}};
int ships2[8][8] = {{0}, {0}};
void placement2(int map[][8], int ships[][8], int num);
void placement(int ships[][8]);
{
int map[8][8] = {0};
printf( "Place your 5 space ship\n" );
placement2(map, ships, 5);
printf( "Place your 4 space ship\n" );
placement2(map, ships, 4);
printf( "Place your 3 space ship\n" );
placement2(map, ships, 3);
printf( "Place your second 3 space ship\n" );
placement2(map, ships, 3);
printf( "Place your 2 space ship\n" );
placement2(map, ships, 2);
}
void placement2(int map[][8], int ships[][8], int num)
{
int y;
int x;
char voh;
int i;
int check = 0;
graph(map);
printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
printf( "Row:" );
y = choose();
printf( "Column:" );
x = choose();
printf( "Vertical or Horizontal? (V/H):" );
scanf( " %c", &voh ); /* BREAKS DOWN HERE */
if(voh == 'V' || voh == 'v'){
if(y + num >= 8){
printf( "Goes off the grid. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = y; i < y + num; i++){
if(ships[i][x] == 1)
check = 1;
}
if(check == 1){
printf( "Intersects another ship. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = y; i < y + num; i++){
map[i][x] = 2;
ships[i][x] = 1;
}
}
}
}
else if(voh == 'H' || voh == 'h'){
if(num + x >= 8){
printf( "Goes off the grid. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = x; i < x + num; i++){
if(ships[y][i] == 1)
check = 1;
}
if(check == 1){
printf( "Intersects another ship. Retry.\n" );
placement2(map, ships, num);
}
else{
for(i = x; i < x + num; i++){
map[y][i] = 2;
ships[y][i] = 1;
}
}
}
}
else{
printf( "Invalid. Retry.\n" );
placement2(map, ships, num);
}
}
/* SOME KIND OF SHIP PLACEMENT HERE */
int y;
int x;
int gamestatus = 1;
while(gamestatus == 1){
printf( "\nPlayer 1's turn\n" );
graph(grid2);
printf( "Row:" );
y = choose();
printf( "Column:" );
x = choose();
shoot(grid2, ships2, y - 1, x - 1, &gamestatus);
if(gamestatus == 0){
printf( "PLAYER 1 WINS!\n" );
break;
}
printf( "\nPlayer 2's turn.\n" );
graph(grid1);
printf( "Row:" );
y = choose();
printf( "Column:" );
x = choose();
shoot(grid1, ships1, y - 1, x - 1, &gamestatus);
if(gamestatus == 0)
printf( "PLAYER 2 WINS!\n" );
else
printf( "\nNext round.\n" );
}
return 0;
}
i indented it the proper way.
im sorry for all of that. my compiler didnt have a format option. but i tried to compiled it and i declared choose in the beginning and its giving me these other errors :
In function `void shoot(int (*)[8], int (*)[8], int, int, int*)':
line 52 `choose' undeclared (first use this function)
line 139 a function-definition is not allowed here before '{' token
Code:
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choose;
int num;
int ships;
int y;
int x;
scanf( "%d", &num );
if ( num < 1 || num > 8 )
{
printf( "Invalid choice, choose again.\n" );
num = choose;
}
return num;
}
void graph( int grid[][ 8 ] )
{
int i;
int j;
printf( " 12345678\n" );
for ( i = 0; i < 8; i++ )
{
printf( "%d", i );
for ( j = 0; j < 8; j++ )
{
if ( grid[ i ][ j ] == 0 )
printf( " " );
else if ( grid[ i ][ j ] == 1 )
printf( "X" );
else if ( grid[ i ][ j ] == 2 )
printf( "O" );
}
printf( "\n" );
}
printf( "\n" );
}
void shoot( int grid[][ 8 ], int ships[][ 8 ], int y, int x, int *game )
{
if ( grid[ y ][ x ] == 1 || grid[ y ][ x ] == 2 )
{
printf( "This space has already been chosen, choose another space\n" );
printf( "Row:" );
y = choose;
printf( "Column:" );
x = choose();
shoot( grid, ships, y, x, game );
}
else
{
if ( ships[ y ][ x ] == 1 )
{
printf( "HIT!\n" );
grid[ y ][ x ] = 1;
ships[ y ][ x ] = 0;
}
else if ( ships[ y ][ x ] == 0 )
{
printf( "Miss.\n" );
grid[ y ][ x ] = 2;
}
}
int k;
int l;
int check = 0;
for ( k = 0; k < 8; k++ )
{
for ( l = 0; l < 8; l++ )
{
if ( ships[ k ][ l ] == 1 )
check = 1;
}
}
if ( check == 1 )
* game = 0;
{
/* Grid is what the players see, ships is where the ships are */
/* Player 1 ships */
int grid1[ 8 ][ 8 ] = {{0}, {0}};
int ships1[ 8 ][ 8 ] = {{0}, {0}};
/* Player 2 ships */
int grid2[ 8 ][ 8 ] = {{0}, {0}};
int ships2[ 8 ][ 8 ] = {{0}, {0}};
void placement2( int map[][ 8 ], int ships[][ 8 ], int num );
void placement( int ships[][ 8 ] );
{
int map[ 8 ][ 8 ] = {0};
printf( "Place your 5 space ship\n" );
placement2( map, ships, 5 );
printf( "Place your 4 space ship\n" );
placement2( map, ships, 4 );
printf( "Place your 3 space ship\n" );
placement2( map, ships, 3 );
printf( "Place your second 3 space ship\n" );
placement2( map, ships, 3 );
printf( "Place your 2 space ship\n" );
placement2( map, ships, 2 );
}
void placement2( int map[][ 8 ], int ships[][ 8 ], int num )
{
int y;
int x;
char voh;
int i;
int check = 0;
graph( map );
printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
printf( "Row:" );
y = choose();
printf( "Column:" );
x = choose();
printf( "Vertical or Horizontal? (V/H):" );
scanf( " %c", &voh ); /* BREAKS DOWN HERE */
if ( voh == 'V' || voh == 'v' )
{
if ( y + num >= 8 )
{
printf( "Goes off the grid. Retry.\n" );
placement2( map, ships, num );
}
else
{
for ( i = y; i < y + num; i++ )
{
if ( ships[ i ][ x ] == 1 )
check = 1;
}
if ( check == 1 )
{
printf( "Intersects another ship. Retry.\n" );
placement2( map, ships, num );
}
else
{
for ( i = y; i < y + num; i++ )
{
map[ i ][ x ] = 2;
ships[ i ][ x ] = 1;
}
}
}
}
else if ( voh == 'H' || voh == 'h' )
{
if ( num + x >= 8 )
{
printf( "Goes off the grid. Retry.\n" );
placement2( map, ships, num );
}
else
{
for ( i = x; i < x + num; i++ )
{
if ( ships[ y ][ i ] == 1 )
check = 1;
}
if ( check == 1 )
{
printf( "Intersects another ship. Retry.\n" );
placement2( map, ships, num );
}
else
{
for ( i = x; i < x + num; i++ )
{
map[ y ][ i ] = 2;
ships[ y ][ i ] = 1;
}
}
}
}
else
{
printf( "Invalid. Retry.\n" );
placement2( map, ships, num );
}
}
/* SOME KIND OF SHIP PLACEMENT HERE */
int y;
int x;
int gamestatus = 1;
while ( gamestatus == 1 )
{
printf( "\nPlayer 1's turn\n" );
graph( grid2 );
printf( "Row:" );
y = choose();
printf( "Column:" );
x = choose();
shoot( grid2, ships2, y - 1, x - 1, &gamestatus );
if ( gamestatus == 0 )
{
printf( "PLAYER 1 WINS!\n" );
break;
}
printf( "\nPlayer 2's turn.\n" );
graph( grid1 );
printf( "Row:" );
y = choose();
printf( "Column:" );
x = choose();
shoot( grid1, ships1, y - 1, x - 1, &gamestatus );
if ( gamestatus == 0 )
printf( "PLAYER 2 WINS!\n" );
else
printf( "\nNext round.\n" );
}
return 0;
}