在战舰游戏C+中返回一个指向一艘船的指针+; 我试图自学C++,所以我正在做一个战舰计划。我有船上和船上的课程

在战舰游戏C+中返回一个指向一艘船的指针+; 我试图自学C++,所以我正在做一个战舰计划。我有船上和船上的课程,c++,pointers,boolean,member,C++,Pointers,Boolean,Member,这个版本相当标准。玩家输入一个单元格的坐标,试图击中一艘船。说明船只是否被击中的程序。如果一艘船占用的所有单元格都被命中,程序将打印一条消息,说明该船沉没。每次尝试后,程序都会通过向电路板显示所有成功的尝试(分别标记为“*”或“x”)来打印当前状态 我在我的Board类中实现Ship*shipAt(intx,inty)函数时遇到了问题,该函数将返回指向该船的指针。否则,它将返回空指针 我有一个战舰的董事会 a b c d e f g h i j +-------------------

这个版本相当标准。玩家输入一个单元格的坐标,试图击中一艘船。说明船只是否被击中的程序。如果一艘船占用的所有单元格都被命中,程序将打印一条消息,说明该船沉没。每次尝试后,程序都会通过向电路板显示所有成功的尝试(分别标记为
“*”
“x”
)来打印当前状态

我在我的Board类中实现
Ship*shipAt(intx,inty)
函数时遇到了问题,该函数将返回指向该船的指针。否则,它将返回空指针

我有一个战舰的董事会

   a b c d e f g h i j
  +-------------------+
 0|                   |
 1|                   |
 2|                   |
 3|                   |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|                   |
 9|                   |
  +-------------------+
这是我的Ship类中的
bool Ship::includes(intx,inty)
函数,我正试图实现它来完成我的shipAt函数。为了简洁起见,我把它删掉了:

#include "Ship.h"
#include <iostream>
#include <stdexcept>

using namespace std;

//Would have been more member functions but I cut it down for brevity 

bool Ship::includes(int x, int y)
{
    bool include= false;

    if(x == x1)
    {
        if ((y>= y1) && (y<=y2))
        {
            include = true;
        }
        if ((y>= y2) && (y<=y1))
        {
            include = true;
        }
    }
    else if (y == y1)
    {
        if ((x>= x1) && (x<=x2))
            {
                include = true;
            }
        if ((x>= x2) && (x<=x1))
        {
            include = true;
        }
    }


    return include; 
}





    }
本质上,我是在尝试使用Ship类中的
boolship::includes(intx,inty)
函数。我试图使它成为这样,如果函数返回true,那么atShip函数也会返回true,因为includes函数是布尔函数

但是,该实现不起作用,我收到了一个对非静态成员函数的调用,没有对象参数错误

编辑:了解其他上下文(可能不必要,除非需要,否则不要单击)

这是我的船。cpp:

这是我的船头文件:


这是我的板头文件:

Ship*Ship=Ship::includes(x,y)

  • Ship::includes()
    返回
    bool
    而不是
    Ship*

  • Ship::includes()
    不是静态的

  • 你需要检查你的船只列表,每一次测试都是“在”(x,y)。一旦你找到了一艘船,你可以把它还回去

    类似于(伪代码):

    #include "Board.h"
    #include "Ship.h"
    #include <iostream>
    using namespace std;
    #include <vector>
    #include <string.h>
    #include <stdexcept>
    
    
    //member function definitions
    
    Board::Board(void)
    {
         char score[10][10] = {};
    }
    
    void Board::addShip(char type, int x1, int y1, int x2, int y2) 
    {
        if(shipList.size()<10)
            {
                shipList.push_back(Ship::makeShip(type,x1,y1,x2,y2));
            }
    }
    
    void Board::print(void){
    
     cout<< "   a b c d e f g h i j"<< endl;
        cout <<"  +-------------------+"<< endl;
    
        for (int i = 0; i < 10; i++) {
            // print the first character as part of the opener.
            cout << " " << i << "|" << score[i][0];
            for (int j = 1; j < 10; j++) {
               // only add spaces for subsequent characters.
               cout << " " << score[i][j];
            }
            cout << "          |" << endl;
        }
        cout <<"  +-------------------+"<< endl;
    
    }
    
    void Board::hit(char c, int i){
    
        if (c<'a' || c>'j' || i > 9 || i<0){
            throw invalid_argument("invalid input");
        }
    
        Ship* ship = shipAt(i, c-'a');
    
    
        if (ship) {
            score[i][c-'a']= '*';
        }
        else{
            score[i][c-'a']= 'x';
        }
    }
    
    Ship* Board::shipAt(int x, int y){
        Ship* ship = Ship::includes(x,y);
    
        if (ship){
                return ship;   
            }
        else{
            return NULL;
             }
    }
    
    int Board::level(void)
    {
        int lev = 0;
        std::vector<Ship *>::iterator iter = shipList.begin();
        std::vector<Ship *>::iterator end = shipList.end();
        for ( ; iter != end; ++iter )
        {
           lev += (*iter)->level();
        }
    
        return lev;
    
    }
    
    foreach ship in shipList
        if (ship->includes(x, y))
            return ship
    return NULL;