Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
void printArray( const int z[][ 10 ] );
void ShowMessage()
{
cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
cout<<"<< Welcome to my BattleShip Game <<"<<endl;
cout<<"<< Student ID: <<"<<endl;
cout<<"<< Student : <<"<<endl;
cout<<"<< C++ programming <<"<<endl;
cout<<"<< First ,i need to ........ c programming <<"<<endl;
cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
cout<<endl;
}
void printArray( const int z[][ 10 ] )
{
int i; /* row counter */
int j; /* column counter */
/* loop through rows */
for ( i = 0; i <= 9; i++ ) {
if( i < 4)
{
printf(" %d ", i);
}
if( i > 4 )
{
printf(" %d ", i);
}
if(i == 4)
{
printf(" 4 ");
}
/* output column values */
for ( j = 0; j <= 9; j++ )
{
printf( "%d ", z[ i ][ j ] );
}
/* end inner for */
printf( "\n" ); /* start new line of output */
if( i == 3 )
{
printf(" A");
}
} /* end outer for */
} /* end function printArray */
void Player()/*This is Player turn */
{
int Ptable[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
int Testtable[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
int ship=1;
for(ship=1;ship<10;ship++)/*5 time loop for 5 ship*/
{
int i=rand() % 10;
int j=rand() % 10;
Testtable[i][j]=1;
}
int playagain=1;
//playagain:
while(playagain)
{
printf("\n------------Player Turn------------\n\n");
printf("\t B\n\n");
printf(" 0 1 2 3 4 5 6 7 8 9\n\n");
printArray(Ptable);
printf("\nPlease Enter the Number of the Coordinates: \n\n");
int lnput1,lnput2;
cout<<" A is:";
cin>>lnput1;/*input the first value*/
cout<<" B is:";
cin>>lnput2;/*input the second value*/
if(lnput1<10,lnput2<10)
{
if(Testtable[lnput1][lnput2]==1)
{
Ptable[lnput1][lnput2]=Testtable[lnput1][lnput2];
cout<<" you just hit ("<<lnput1<<","<<lnput2<<")="<<Testtable[lnput1][lnput2]<<endl;
cout<<" ====================="<<endl;
cout<<" ==Very Good, Hit!!!=="<<endl;
cout<<" ====================="<<endl;
/*this show me where are the ship..!!*/
/*
for (int time = 0; time < 10; time++)
{
for (int time2 = 0; time2 < 10; time2++)
cout <<Testtable[time][time2];
cout << '\n';
}
*/
//goto playagain;
}
else
{
cout<<" you just putted ("<<lnput1<<","<<lnput2<<")="<<Ptable[lnput1][lnput2]<<endl;
cout<<" ======================"<<endl;
cout<<" ==Sorry, u Missed!!!=="<<endl;
cout<<" ======================"<<endl;
/*
for (int time = 0; time < 10; time++)
{
for (int time2 = 0; time2 < 10; time2++)
cout <<Testtable[time][time2];
cout << '\n';
}
*/
playagain=0;
//when i didnt hit the right ship,goto computer turn;
}
}
else
{
cout<<"This is wrong Coordinates"<<endl;
playagain=0;
//goto playagain;
}
}
}
/*
this is a comment
*/
void Computer()
{
int Ctable[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
int Testtable2[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
printf("\n\n------------Computer Turn------------\n\n");
printf("\t B\n\n");
printf(" 0 1 2 3 4 5 6 7 8 9\n\n");
printArray(Ctable);
printf("\nComputer is playing with you: \n\n");
int comship;
for(comship=1;comship<6;comship++)/*5 time loop for 5 ship*/
{
int p=rand() % 10;
int k=rand() % 10;
Testtable2[p][k]=1;
}
int p2=rand() % 10;
int k2=rand() % 10;
if(Ctable[p2][k2]=1)
{
cout<<"Computer choose : ("<<p2<<","<<k2<<")"<<endl;
cout<<"========Sorry, Com hit your ship!!!========"<<endl;
}
else
{
cout<<"========Good, Computer missed!!!==============="<<endl;
}
cout<<endl;
}
int main()
{
{
ShowMessage();
}
int Ptable[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
int Ctable[ 10 ][ 10 ] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
/*Player-put a random ship to the table*/
{
Player();
}
/*Computer*/
int p=rand() % 10;
int k=rand() % 10;
Ctable[p][k]=1;
/*結果*/
{
Computer();
}
return 0;
}