BattleShip Game

Hello guys!
Hope everyone is safe and sound!
Im a student of Computer Science and engineering and this is my first year but i was more focused on the maths and physics and left programing behind.
I have a battle ship game that needs to have a 10x10 board. The rows are in between 1 and 10 and the collums are between A and J but what is giving hard times doing is that i need to have the header file and the source file working together and until this subject i've always worked only in the source file im not sure what to do next.
Hope someone is smarther than me and could help me starting this project.
:)

This is the code that the teachers gave to the students.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

class NavelPoint
{
private:
int x; //1 -- 10
char y; //A -- J
public:
NavelPoint();
NavelPoint(int m_x, char m_y);
NavelPoint(int m_x, int m_y);
virtual ~NavelPoint();
bool Set_x(int m_x);
bool Set_y(char m_y);
bool Set_y(int m_y);
void Set_PN(int m_x, char m_y);
void Set_PN(NavelPoint m_PN);
bool Ask2Set_PN(void);
int Get_x(void) const {return x;} 
char Get_y(void) const {return y;}
int Get_int_y(void) const;
bool IsValid(void);
void ShowNavelPoint(void);
bool operator == (const NavelPoint point) const;
bool operator != (const NavelPoint point) const;
void operator = (NavelPoint ponto);
void Save(ofstream &os);
void Read(ifstream &is);
};
In general, you put that class declaration in a header file, like NavalPoint.h. Then you create a NavalPoint.cpp file and at the top you put:
#include "NavalPoint.h"
The #include effectively pastes the file into that location in the source.

I have to say that sadly, this is another case of professor teaching bad habits. This class is full of sound and fury, signifying nothing. Each method is a one-liner. The class doesn't really need to exist. Worse, it encourages you to store the coordinates using NavalPoint. It's much better to store the coordinates using "computer friendly" numbers 0-9 for both vertical and horizontal. Then you convert to human-friendly 1-10 and A-J just after input from the user, and just before output to the user.
Hello Samarrao,

First would be to mention what IDE and compiler you are using.

Second would be to include the source file(s) that you are using.

The class forward declares functions, but where are they defined?

Not really important, but I like doing this with a class. It makes it easier to read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class NavelPoint
{
    private:
        int x; //1 -- 10
        char y; //A -- J

    public:
        NavelPoint();
        NavelPoint(int m_x, char m_y);
        NavelPoint(int m_x, int m_y);
        virtual ~NavelPoint();

        bool Set_x(int m_x);
        bool Set_y(char m_y);
        bool Set_y(int m_y);
        void Set_PN(int m_x, char m_y);
        void Set_PN(NavelPoint m_PN);

        int Get_x(void) const { return x; }
        char Get_y(void) const { return y; }
        int Get_int_y(void) const;
        
        bool Ask2Set_PN(void);
        bool IsValid(void);
        void ShowNavelPoint(void);
        bool operator == (const NavelPoint point) const;
        bool operator != (const NavelPoint point) const;
        void operator = (NavelPoint ponto);
        void Save(ofstream &os);
        void Read(ifstream &is);
};

If you are not aware a class is private by default, so starting the class with "private" is redundant, but OK if you leave it.

int Get_int_y(void) const;. The "void" here is old and no longer necessary. Not sure if that was a change in C++ from the start of if it came from 1 of the standards in C++11 or earlier.

Andy
Hello there thanks for the quick response dhayden and Andy.
Im using Visual Studio because it's the IDE that the professors told us to use.

"The class forward declares functions, but where are they defined?"

This code came in a pdf file not really in a program so im not sure where the professor took it from.

Handy Andy wrote:
The "void" here is old and no longer necessary. Not sure if that was a change in C++ from the start of if it came from 1 of the standards in C++11 or earlier.


The void in a function with no parameters has never been a requirement in C++. I think it is really just a hangover from those who have done C programming, and are not aware of the difference with C++, because compilers ignore it. I also think it is not a requirement in C++ because it doesn't really help humans either :+)

I guess the aim of the assignment is to write all the functions and put it all together in main ?
Topic archived. No new replies allowed.