Sie sind auf Seite 1von 8

/**************************************************************/

/* Creates BY Charles Oliphant */

/* Period B3 Date 3/15/2017 */

/* This Project was created to for the purpose of making a */

/* Battles ship game. It uses Nested for loops, if and else */

/* Statement and static arrays to create a playing field where */

/* The users can pick and choose a coordinate and receive */

/* Feedback on whether or not it is a hit, miss, or when they */

/* Have sunk A certain ship. */

/**************************************************************/

#include <cstdlib>

#include <iostream>

using namespace std;

int main(int argc, char *argv[])

bool submarine=false; bool carrier=false; bool destroyer=false; bool ship=false; //booleen operaters

cout<<"\n";

cout<<" WELCOME TO BATTLESHIP \n";

cout<<" C = you have hit the carrier\n";

cout<<" D = you have hit the small destroyer \n"; // introduction to game, what the different
symbols mean

cout<<" S = you have hit the submarine \n";

cout<<" A = you have hit the small attack ship \n";

cout<<" M = miss \n";


cout<<" ~ = unchosen \n";

int placeB[10][10]={{1,2,2,0,0,0,0,0,0,0},

{1,0,0,0,0,0,0,0,0,0},

{1,0,0,0,0,0,0,0,0,0},

{1,0,0,0,0,0,0,0,0,0}, // static arrays

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,3,3,3,0,0,0,0},

{0,0,0,0,0,0,0,0,6,0},

{0,0,0,0,0,0,0,0,6,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0}};

int placeA[10][10]={{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,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}};

while(carrier==false|| destroyer==false|| submarine==false||ship==false){ //whille bool is false do


this

cout<<"\n";
cout<<"\n";

cout<<"\n";

cout<<"\n";

cout<<"\n";

cout<<"\n";

cout<<"\n";

cout<<"\n";

int x,y;

int z=0;

for(y=0;y<10;y++)

cout<<"\n";

for(x=0;x<10;x++)

{ // printing out static arrays in for loop also assiging hit miss and unchosen varibales
to input

if(placeA[x][y]==4)

cout<<"M";

else if(placeA[x][y]==8){
cout<<"C";

else if(placeA[x][y]==7){

cout<<"D";

else if(placeA[x][y]==6){

cout<<"S";

else if(placeA[x][y]==5){

cout<<"A";

else if(placeA[x][y]==0)

cout<<" ~";

cout<<"\n";

cout<<"\n";
cout<<"select (Row)X \n";

cin>>x;

cout<<"select (Column)Y \n";

cin>>y;

cout<<" you have selected: "<<"("<<x<<","<<y<<")"<<"\n";

if(x==0&& y==0||x==0&&y==1||x==0&&y==2||x==0&&y==3){ // assigning where the


battleships are also putting in if statement so

// the corrrect output is chosen for each input value

cout<<"\n";

cout<<"you have made a hit \n";

cout<<"\n";

placeA[x][y]=8; }

else if( x==1&&y==0||x==2&&y==0){

cout<<"\n";

cout<<"you have made a hit \n";

cout<<"\n";

placeA[x][y]=7; }

else if (x==3&&y==5||x==4&&y==5||x==5&&y==5){
cout<<"\n";

cout<<"you have made a hit \n";

cout<<"\n";

placeA[x][y]=6; }

else if (x==8&&y==6||x==8&&y==7){

cout<<"\n";

cout<<"you have made a hit \n";

cout<<"\n";

placeA[x][y]=5; }

else{

cout<<"\n";

cout<<" you have missed \n";

cout<<"\n";

placeA[x][y]=4;

if(x==0&&y==0, x==0&&y==1, x==0&&y==2, x==0&&y==3){


cout<<"\n";

cout<<" YOU HAVE SUNK THE CARRIER \n";

carrier=true;

if( x==1&&y==0,x==2&&y==0){

cout<<"\n";

cout<<" YOU HAVE SUNK THE SMALL DESTROYER \n";

destroyer=true;

if(x==3&&y==5,x==4&&y==5,x==5&&y==5){ // if statements to print out which ship


you have sunk

cout<<"\n";

cout<<" YOU HAVE SUNK THE SUBMARINE \n";

submarine=true;

if(x==8&&y==6,x==8&&y==7){

cout<<"\n";

cout<<" YOU HAVE SUNK THE SMALL ATTACK SHIP \n";

ship=true;

}
cout<<" \n YOU HAVE SUNK ALL THE ENEMIES SHIPS CONGRATS \n"; //once it is true do this

system("PAUSE");

return EXIT_SUCCESS;

Das könnte Ihnen auch gefallen