All pastes #270043 Raw Edit

battleship in progre

public cpp v1 · immutable
·published 2006-12-07 00:54 UTC
rendered paste body
#include "stdio.h"#include <iostream>#include <ctime>#include <string>using std::endl;using std::cout;using std::cin;using std::string;   int HorV = 0;   int randRow = 0;   int randCol = 0;   int x = 0;   int y = 0;   int schd = 0;   int schdRow = 0;   int schdCol = 0;void initialize();void setupCompBattle();void setupCompCar();void setupCompBat();void setupCompDes();void setupCompSub();void setupCompPat();void setupPlayerBattle();void setupPlayerCar();void setupPlayerBat();void setupPlayerDes();void setupPlayerSub();void setupPlayerPat();void setupCompColCar();void setupCompColBat();void setupCompColDes();void setupCompColSub();void setupCompColPat();void setupPlayerColCar();void setupPlayerColBat();void setupPlayerColDes();void setupPlayerColSub();void setupPlayerColPat();void displayPlayer();void line();char playerShips[10][10]={0};char playerHits[10][10]={0};char compShips[10][10]={0};void game();void AI();string p1Name = "";string compName = "";int ccount = 1;int bcount = 1;int dcount = 1;int ucount = 1;int pcount = 1;int main(){	srand(int(time(0)));    cout << "enter Player name : " << endl;    cin >> p1Name;    cout << " enter Computer name : " << endl;    cin >> compName;    initialize();    while (ccount != 0 && bcount != 0 && dcount != 0 && ucount != 0 && pcount != 0)    {        game();    }return 0;}void line (){    cout << " ========================================" << endl;}void initialize(){    for(x = 0;x < 10;x = x + 1)    {        for(y = 0;y < 10;y = y + 1)        {            playerShips[x][y] = '*';            playerHits[x][y] = '*';            compShips[x][y] = '*';        }    }setupPlayerBattle();setupCompBattle();displayPlayer();}void displayPlayer(){      cout << "   A B C D E F G H I J" << endl;    for (y = 0;y < 10;y = y + 1)    {        cout << y+1;         for(x = 0;x < 10;x = x + 1)         {             cout << " " << playerShips[y][x];         }         cout << endl;     }    line ();    line ();    cout << "   A B C D E F G H I J" << endl;    for (y = 0;y < 10;y = y + 1)    {         cout << y+1;         for(x = 0;x < 10;x = x + 1)         {             cout << " " << playerHits[y][x];         }         cout << endl;    }}void setupCompBattle(){    HorV = 1 + rand() % 2;    if (HorV == 1)    {        setupCompCar();    }    else    {        setupCompColCar();    }    HorV = 1 + rand() % 2;    if (HorV == 1)    {        setupCompBat();    }    else    {       setupCompColBat();    }    HorV = 1 + rand() % 2;    if (HorV == 1)    {        setupCompDes();    }    else    {        setupCompColDes();    }    HorV = 1 + rand() % 2;    if (HorV == 1)    {        setupCompSub();    }    else    {        setupCompColSub();    }    HorV = 1 + rand() % 2;    if (HorV == 1)    {        setupCompPat();    }    else    {        setupCompColPat();    }}void setupCompCar(){	while (compShips[randRow][randCol] != '*' && compShips[randRow+1][randCol] != '*' && compShips[randRow+2][randCol] != '*' && compShips[randRow+3][randCol] != '*' && compShips[randRow+4][randCol] != '*')	{ // carrier ship        randCol = 1 + rand() % (10 - 1 + 1);        randRow = 1 + rand() % (5 - 1 + 1);	}        // carrier ship        compShips[randRow][randCol]='C';        compShips[randRow+1][randCol]='C';        compShips[randRow+2][randCol]='C';        compShips[randRow+3][randCol]='C';        compShips[randRow+4][randCol]='C';}void setupCompBat(){    while (compShips[randRow][randCol] != '*' && compShips[randRow+1][randCol] != '*' && compShips[randRow+2][randCol] != '*' && compShips[randRow+3][randCol] != '*')    {    // battle ship        randCol = 1 + rand() % (10 - 1 + 1);        randRow = 1 + rand() % (6 - 1 + 1);    }        compShips[randRow][randCol]='B';        compShips[randRow+1][randCol]='B';        compShips[randRow+2][randCol]='B';        compShips[randRow+3][randCol]='B';}void setupCompDes(){    while (compShips[randRow][randCol] != '*' && compShips[randRow+1][randCol] != '*' && compShips[randRow+2][randCol] != '*')    {    // destroyer		randCol = 1 + rand() % (10 - 1 + 1);		randRow = 1 + rand() % (7 - 1 + 1);    }        compShips[randRow][randCol]='D';        compShips[randRow+1][randCol]='D';        compShips[randRow+2][randCol]='D';}void setupCompSub(){    while (compShips[randRow][randCol] != '*' && compShips[randRow+1][randCol] != '*' && compShips[randRow+2][randCol] != '*')    {    // submarine		randCol = 1 + rand() % (10 - 1 + 1);		randRow = 1 + rand() % (7 - 1 + 1);    }        compShips[randRow][randCol]='U';        compShips[randRow+1][randCol]='U';        compShips[randRow+2][randCol]='U';}void setupCompPat(){    while (compShips[randRow][randCol] != '*' && compShips[randRow+1][randCol] != '*')    {    // patroll boat		randCol = 1 + rand() % (10 - 1 + 1);		randRow = 1 + rand() % (8 - 1 + 1);    }        compShips[randRow][randCol]='P';        compShips[randRow+1][randCol]='P';}void setupPlayerBattle(){    HorV = 1 + rand() % 2;    if (HorV == 1)    {		setupPlayerCar();		setupPlayerBat();		setupPlayerDes();		setupPlayerSub();		setupPlayerPat();    }    else    {		setupPlayerColCar();		setupPlayerColBat();		setupPlayerColDes();		setupPlayerColSub();		setupPlayerColPat();    }}void setupPlayerCar(){	while (playerShips[randRow][randCol] != '*' && playerShips[randRow+1][randCol] != '*' && playerShips[randRow+2][randCol] != '*' && playerShips[randRow+3][randCol] != '*' && playerShips[randRow+4][randCol] != '*')    {	    // carrier ship        randCol = 1 + rand() % (10 - 1 + 1);        randRow = 1 + rand() % (5 - 1 + 1);	}        // carrier ship        playerShips[randRow][randCol]='C';        playerShips[randRow+1][randCol]='C';        playerShips[randRow+2][randCol]='C';        playerShips[randRow+3][randCol]='C';        playerShips[randRow+4][randCol]='C';}void setupPlayerBat(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow+1][randCol] != '*' && playerShips[randRow+2][randCol] != '*' && playerShips[randRow+3][randCol] != '*')    {    // battle ship		randCol = 1 + rand() % (10 - 1 + 1);		randRow = 1 + rand() % (6 - 1 + 1);    }        playerShips[randRow][randCol]='B';        playerShips[randRow+1][randCol]='B';        playerShips[randRow+2][randCol]='B';        playerShips[randRow+3][randCol]='B';}void setupPlayerDes(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow+1][randCol] != '*' && playerShips[randRow+2][randCol] != '*')    {    // destroyer		randCol = 1 + rand() % (10 - 1 + 1);		randRow = 1 + rand() % (7 - 1 + 1);    }        playerShips[randRow][randCol]='D';        playerShips[randRow+1][randCol]='D';        playerShips[randRow+2][randCol]='D';}void setupPlayerSub(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow+1][randCol] != '*' && playerShips[randRow+2][randCol] != '*')    {    // submarine		randCol = 1 + rand() % (10 - 1 + 1);		randRow = 1 + rand() % (7 - 1 + 1);    }        playerShips[randRow][randCol]='U';        playerShips[randRow+1][randCol]='U';        playerShips[randRow+2][randCol]='U';}void setupPlayerPat(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow+1][randCol] != '*')    {    // patroll boat		randCol = 1 + rand() % (10 - 1 + 1);		randRow = 1 + rand() % (8 - 1 + 1);    }        playerShips[randRow][randCol]='P';        playerShips[randRow+1][randCol]='P';}//col initializationvoid setupCompColCar(){	while (compShips[randRow][randCol] != '*' && compShips[randRow][randCol+1] != '*' && compShips[randRow][randCol+2] != '*' && compShips[randRow][randCol+3] != '*' && compShips[randRow][randCol+4] != '*')	{		// carrier ship        randCol = 1 + rand() % (5 - 1 + 1);        randRow = 1 + rand() % (10 - 1 + 1);	}        // carrier ship        compShips[randRow][randCol]='C';        compShips[randRow][randCol+1]='C';        compShips[randRow][randCol+2]='C';        compShips[randRow][randCol+3]='C';        compShips[randRow][randCol+4]='C';}void setupCompColBat(){    while (compShips[randRow][randCol] != '*' && compShips[randRow][randCol+1] != '*' && compShips[randRow][randCol+2] != '*' && compShips[randRow][randCol+3] != '*')    {    // battle ship		randCol = 1 + rand() % (6 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        compShips[randRow][randCol]='B';        compShips[randRow][randCol+1]='B';        compShips[randRow][randCol+2]='B';        compShips[randRow][randCol+3]='B';}void setupCompColDes(){    while (compShips[randRow][randCol] != '*' && compShips[randRow][randCol+1] != '*' && compShips[randRow][randCol+2] != '*')    {    // destroyer		randCol = 1 + rand() % (7 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        compShips[randRow][randCol]='D';        compShips[randRow][randCol+1]='D';        compShips[randRow][randCol+2]='D';}void setupCompColSub(){    while (compShips[randRow][randCol] != '*' && compShips[randRow][randCol+1] != '*' && compShips[randRow][randCol+2] != '*')    {    // submarine		randCol = 1 + rand() % (7 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        compShips[randRow][randCol]='U';        compShips[randRow][randCol+1]='U';        compShips[randRow][randCol+2]='U';}void setupCompColPat(){    while (compShips[randRow][randCol] != '*' && compShips[randRow][randCol+1] != '*')    {    // patroll boat		randCol = 1 + rand() % (8 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        compShips[randRow][randCol]='P';        compShips[randRow][randCol+1]='P';}void setupPlayerColCar(){	while (playerShips[randRow][randCol] != '*' && playerShips[randRow][randCol+1] != '*' && playerShips[randRow][randCol+2] != '*' && playerShips[randRow][randCol+3] != '*' && playerShips[randRow][randCol+4] != '*')	{ 		// carrier ship        randCol = 1 + rand() % (5 - 1 + 1);        randRow = 1 + rand() % (10 - 1 + 1);	}        // carrier ship        playerShips[randRow][randCol]='C';        playerShips[randRow+1][randCol]='C';        playerShips[randRow+2][randCol]='C';        playerShips[randRow+3][randCol]='C';        playerShips[randRow+4][randCol]='C';}void setupPlayerColBat(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow][randCol+1] != '*' && playerShips[randRow][randCol+2] != '*' && playerShips[randRow][randCol+3] != '*')    {    // battle ship		randCol = 1 + rand() % (6 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        playerShips[randRow][randCol]='B';        playerShips[randRow][randCol+1]='B';        playerShips[randRow][randCol+2]='B';        playerShips[randRow][randCol+3]='B';}void setupPlayerColDes(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow][randCol+1] != '*' && playerShips[randRow][randCol+2] != '*')    {    // destroyer		randCol = 1 + rand() % (7 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        playerShips[randRow][randCol]='D';        playerShips[randRow][randCol+1]='D';        playerShips[randRow][randCol+2]='D';}void setupPlayerColSub(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow][randCol+1] != '*' && playerShips[randRow][randCol+2] != '*')    {    // submarine		randCol = 1 + rand() % (7 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        playerShips[randRow][randCol]='U';        playerShips[randRow][randCol+1]='U';        playerShips[randRow][randCol+2]='U';}void setupPlayerColPat(){    while (playerShips[randRow][randCol] != '*' && playerShips[randRow][randCol+1] != '*')    {    // patroll boat		randCol = 1 + rand() % (8 - 1 + 1);		randRow = 1 + rand() % (10 - 1 + 1);    }        playerShips[randRow][randCol]='P';        playerShips[randRow][randCol+1]='P';}void game(){    int x = 0;    int y = 0;    cout << "enter row number : ";    cin >> x;    cout << endl;    switch (x)    {    case 1: x = 0;              break;    case 2: x = 1;              break;    case 3: x = 2;              break;    case 4: x = 3;              break;    case 5: x = 4;              break;    case 6: x = 5;              break;    case 7: x = 6;              break;    case 8: x = 7;              break;    case 9: x = 8;              break;    case 10: x = 9;              break;    default:  cout << "1 - 10 ONLY" << endl;              game();              break;    }    line();    cout << "enter collumn letter : ";    cin >> y;    cout << endl;    line();    y = tolower(y);    switch (y)    {    case 'a': y = 0;              break;    case 'b': y = 1;              break;    case 'c': y = 2;              break;    case 'd': y = 3;              break;    case 'e': y = 4;              break;    case 'f': y = 5;              break;    case 'g': y = 6;              break;    case 'h': y = 7;              break;    case 'i': y = 8;              break;    case 'j': y = 9;              break;    default:  cout << "A - J ONLY" << endl;              game();              break;    }    if (x > 10)    {        cout << "1 - 10 ONLY" << endl;        game();    }    int z = 0;    int c = 0;    for (z=0;z<10;z=z+1)    {        for (c=0;c<10;z=z+1)        {            if (compShips[z][c] = 'C')            {                ccount = ccount +1;            }            if (compShips[z][c] = 'B')            {                bcount = bcount +1;            }            if (compShips[z][c] = 'D')            {                dcount = dcount +1;            }            if (compShips[z][c] = 'U')            {                ucount = ucount +1;            }            if (compShips[z][c] = 'P')            {                pcount = pcount +1;            }        }    }    if (compShips[x][y] != '*' && compShips[x][y] != 'S')    {         cout << "HIT!" << endl;         compShips[x][y] = 'S';    }    if (ccount -1 == 0)    {        cout << p1Name << " sunk " << compName << "'s Carrier!" << endl;    }    else if (bcount -1 == 0)    {        cout << p1Name << " sunk " << compName << "'s Battle ship!" << endl;    }    else if (dcount -1 == 0)    {        cout << p1Name << " sunk " << compName << "'s Destroyer!" << endl;    }    else if (ucount -1 == 0)    {        cout << p1Name << " sunk " << compName << "'s U-boat!" << endl;    }    else if (pcount -1 == 0)    {        cout << p1Name << " sunk " << compName << "'s PatrollBoat!" << endl;    }}void AI(){	if (schd != 0)	{		switch(schd)		{		case 1: 				if (playerShips[schdRow+1][schdCol] != '*' && playerShips[schdRow+1][schdCol] != 'S')				{					switch(playerShips[schdRow+1][schdCol])					{					case 'C': cout << compName << " hit " << p1Name << "'s Carrier" << endl;							  playerShips[schdRow+1][schdCol] = 'S';							  schd = schd +1;			                  break;					case 'B': cout << compName << " hit " << p1Name << "'s Battleship" << endl;		                      playerShips[schdRow+1][schdCol] = 'S';			                  break;					case 'D': cout << compName << " hit " << p1Name << "'s Destroyer" << endl;		                      playerShips[schdRow+1][schdCol] = 'S';			                  break;					case 'U': cout << compName << " hit " << p1Name << "'s U-boat" << endl;		                      playerShips[schdRow+1][schdCol] = 'S';			                  break;					case 'P': cout << compName << " hit " << p1Name << "'s Patroll boat" << endl;		                      playerShips[schdRow+1][schdCol] = 'S';		                      break;					default : break;			        }			    }				else				{					cout << compName << "missed to the left" << endl;				}				break;		case 2: 				if (playerShips[schdRow][schdCol+1] != '*' && playerShips[schdRow][schdCol+1] != 'S')				{					switch(playerShips[schdRow][schdCol+1])					{					case 'C': cout << compName << " hit " << p1Name << "'s Carrier" << endl;							  playerShips[schdRow][schdCol+1] = 'S';			                  break;					case 'B': cout << compName << " hit " << p1Name << "'s Battleship" << endl;		                      playerShips[schdRow][schdCol+1] = 'S';			                  break;					case 'D': cout << compName << " hit " << p1Name << "'s Destroyer" << endl;		                      playerShips[schdRow][schdCol+1] = 'S';			                  break;					case 'U': cout << compName << " hit " << p1Name << "'s U-boat" << endl;		                      playerShips[schdRow][schdCol+1] = 'S';			                  break;					case 'P': cout << compName << " hit " << p1Name << "'s Patroll boat" << endl;		                      playerShips[schdRow][schdCol+1] = 'S';		                      break;					default : break;					}				}				else				{					cout << compName << "missed to the left" << endl;				}				break;		case 3: 				if (playerShips[schdRow-1][schdCol] != '*' && playerShips[schdRow-1][schdCol] != 'S')				{					switch(playerShips[schdRow-1][schdCol])					{					case 'C': cout << compName << " hit " << p1Name << "'s Carrier" << endl;							  playerShips[schdRow-1][schdCol] = 'S';			                  break;					case 'B': cout << compName << " hit " << p1Name << "'s Battleship" << endl;		                      playerShips[schdRow-1][schdCol] = 'S';			                  break;					case 'D': cout << compName << " hit " << p1Name << "'s Destroyer" << endl;		                      playerShips[schdRow-1][schdCol] = 'S';			                  break;					case 'U': cout << compName << " hit " << p1Name << "'s U-boat" << endl;		                      playerShips[schdRow-1][schdCol] = 'S';			                  break;					case 'P': cout << compName << " hit " << p1Name << "'s Patroll boat" << endl;		                      playerShips[schdRow-1][schdCol] = 'S';		                      break;					default : break;					}				 }				else				{					cout << compName << "missed to the left" << endl;				}				break;		case 4: 				if (playerShips[schdRow][schdCol-1] != '*' && playerShips[schdRow][schdCol-1] != 'S')				{					switch(playerShips[schdRow][schdCol-1])					{					case 'C': cout << compName << " hit " << p1Name << "'s Carrier" << endl;							  playerShips[schdRow][schdCol-1] = 'S';			                  break;					case 'B': cout << compName << " hit " << p1Name << "'s Battleship" << endl;		                      playerShips[schdRow][schdCol-1] = 'S';			                  break;					case 'D': cout << compName << " hit " << p1Name << "'s Destroyer" << endl;		                      playerShips[schdRow][schdCol-1] = 'S';			                  break;					case 'U': cout << compName << " hit " << p1Name << "'s U-boat" << endl;		                      playerShips[schdRow][schdCol-1] = 'S';			                  break;					case 'P': cout << compName << " hit " << p1Name << "'s Patroll boat" << endl;		                      playerShips[schdRow][schdCol-1] = 'S';		                      break;					default : break;					}				 }				else				{					cout << compName << "missed to the left" << endl;				}		default: break;		}	}	else	{		switch(playerShips[randRow][randCol-1])		{		case 'C': cout << compName << " hit " << p1Name << "'s Carrier" << endl;				  playerShips[randRow][randCol-1] = 'S';			      break;		case 'B': cout << compName << " hit " << p1Name << "'s Battleship" << endl;		          playerShips[randRow][randCol-1] = 'S';			      break;		case 'D': cout << compName << " hit " << p1Name << "'s Destroyer" << endl;		          playerShips[randRow][randCol-1] = 'S';			      break;		case 'U': cout << compName << " hit " << p1Name << "'s U-boat" << endl;		          playerShips[randRow][randCol-1] = 'S';			      break;		case 'P': cout << compName << " hit " << p1Name << "'s Patroll boat" << endl;		          playerShips[randRow][randCol-1] = 'S';		          break;		default : cout << compName << " missed to the left" << endl;			      break;	    }	}}