Code:
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
void shoot( int grid[][ 12 ], int ships[][ 12 ], int y, int x, int *game );
void placement2( int map[][ 8 ], int ships[][ 8 ], int num );
int choose()
{
int num;
int ships;
int y;
int x;
scanf( "%d", &num );
if ( num < 1 || num > 12 )
{
printf( "Invalid choice, choose again.\n" );
}
return num;
}
void graph( int grid[][ 12 ] )
{
int i;
int j;
printf( " 123456789101112\n" );
for ( i = 0; i < 12; i++ )
{
printf( "%d", i );
for ( j = 0; j < 12; 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[][ 12 ], int ships[][ 12 ], 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:" );
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[ 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}};
void placement2( int map[][ 12 ], int ships[][ 12 ], int num );
void placement( int ships[][ 12 ] );
{
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 >= 12 )
{
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 >= 12 )
{
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;
}
}