Changing from dead to alive

if ( (shipArray[h][w]->getBelongs() == 2) && (shipArray[h][w]->isAlive()))
{
cout<<"Hit!\n";
}

need help for this, how do I declare my ship isAlive == false once hit? isAlive is true so how do I change it to false once hit? Sorry if my english is bad, but help will be much appreaciated.. Thank you =)
We don't know the type of shipArray (although by looking at the name it's probably ship) and how its defined, therefore, we might have some great difficulties telling you.

-Albatross
#include "Ship.h"

Ship::Ship(void)
{
}

Ship::Ship(bool a, int b)
{
belongs = b;
alive = a;

}

Ship::~Ship(void)
{
}
int Ship::getBelongs ()
{return belongs;
}
bool Ship::isAlive ()
{return alive;
}

Using 2d array, do you need more info? ill post more if needed, thank you for replying =)
If those are all the functions and alive is a private or protected member... then the easiest way without changing your class is to construct a new dead ship with all the same values as the ship you want dead (except for alive) using your second constructor and replacing the ship that you want dead with your new ship.

The right way to do it, however, is to add another function that sets alive to false.

If alive is public, just use shipArray[h][w]->alive = false;.

-Albatross
Last edited on
hmm how do i set alive to false is the question, im still a beginner at this so there might be stuff i know but do not know when to use it
I don't know... try the assignment operator and the false keyword? ;)

-Albatross
here is my project, i dunno wat to do now T.T i juz need to be able to make dead my ships then keep looping the game till one player has no more ships..
http://www.mediafire.com/?wy2mywnqzng
do you know how i can proceed?
in isAlive()

you write this

1
2
3
4
5
6
7
8
bool isAlive(int a)
{
    if(a==1)
      return 1;
    else
      return 0;

}


that mean when you write isAlive(1); the ship is alive ;

if you write isAlive(0); the ship dead;


im getting errors, i put this in my ship.h or ship.cpp?
You need to create the means to make the ship dead in your class design.

Probably something like this:

1
2
3
4
5
6
7
8
9
10
11
12
class Ship
{
private:
    bool alive;

public:
    Ship() : alive(true) {} // alive when created

    bool isAlive() const { return alive; }

    void setAlive(bool a) { alive = a; } // make ship alive or dead
};


Last edited on
Thanks for the help!! =)
Topic archived. No new replies allowed.