【问题标题】:Returning a pointer to a ship in BattleShip game C++在BattleShip游戏C ++中返回指向船的指针
【发布时间】:2014-07-15 18:02:03
【问题描述】:

我正在尝试自学 C++,所以我正在做一个战舰程序。我有一个船舶和董事会课程。

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

我无法在我的 Board 类中实现 Ship *shipAt(int x, int y) 函数来记录这艘船,基本上这个函数返回一个指向那艘船的指针。否则返回空指针。

我有一个战舰板

   a b c d e f g h i j
  +-------------------+
 0|                   |
 1|                   |
 2|                   |
 3|                   |
 4|                   |
 5|                   |
 6|                   |
 7|                   |
 8|                   |
 9|                   |
  +-------------------+

这是我的船舶类中的 bool Ship::includes(int x, int y) 函数,我正在尝试实现该函数以完成我的 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 *shipAt(int x, int y) 函数时遇到问题 对于这个函数,如果一艘船占据了单元格 (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;

}

基本上我是在尝试使用 Ship 类中的 bool Ship::includes(int x, int y)function。我试图这样做,以便如果函数返回 true,那么 atShip 函数也将返回 true,因为包含函数是布尔值。

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

编辑:对于额外的上下文(可能是不必要的,所以不要点击,除非你需要),

这是我的 Ship.cpp:http://pastebin.com/cYDt0f8W

这是我的 Ship 头文件:http://pastebin.com/W6vwKJRz

这是我的 Board 头文件:http://pastebin.com/r36YjHjt

【问题讨论】:

    标签: c++ pointers boolean member


    【解决方案1】:

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

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

    2. Ship::includes() 不是static

    你需要查看你的舰船列表,并且每一个测试它都是“at”(x,y)。一旦你找到了这艘船,你就可以归还它。

    类似(伪代码):

    foreach ship in shipList
        if (ship->includes(x, y))
            return ship
    return NULL;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2010-10-13
      • 2011-02-07
      • 2015-02-04
      相关资源
      最近更新 更多