指针指向一个指针,以便在战舰程序c++中求和级别

Pointer to a pointer in order to sum levels in BattleShip program c++

本文关键字:指针 程序 c++ 求和 一个      更新时间:2023-10-16

大家星期天快乐!

我正在尝试自学c++,所以我正在做一个战舰程序。

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

我遇到的麻烦是在我的Board类中编写级别函数,该函数总结了板上船舶的所有级别。

我有一个像这样的战列舰板

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

这是我的板头文件的上下文:

#ifndef BOARD_H
#define BOARD_H
#include "Ship.h"
#include <vector>
class Board
{
  public:
    Board(void);
    void addShip(char type, int x1, int y1, int x2, int y2);
    void print(void);
    void hit(char c, int i);
    int level(void);
  private:
    std::vector<Ship *> shipList;
    char score[10][10];
    Ship *shipAt(int x, int y);
};
#endif

我还有一个ship类来存储船的坐标

这是我的船头(可能没有必要阅读所有内容,只是看看水平函数):

//
// Ship.h
//
#ifndef SHIP_H
#define SHIP_H
class Ship
{
  public:
    virtual ~Ship(void) {}
    virtual const char *name(void) const = 0;
    virtual int size(void) const = 0;
    int getX(int i) const;
    int getY(int i) const;
    void print(void) const;
    bool includes(int x, int y);
    int level(void) const;
    void decreaseLevel(void);
    static Ship *makeShip(char ch, int x1, int y1, int x2, int y2);
  protected:
    void setPos(int a1, int b1, int a2, int b2);
    int lev;
  private:
    bool checkConfig(int x1, int y1, int x2, int y2);
    int x1,y1,x2,y2;
};
class AircraftCarrier : public Ship
{
  public:
    AircraftCarrier(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};
class BattleShip: public Ship
{
  public:
    BattleShip(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};
class Cruiser: public Ship
{
  public:
    Cruiser(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};
class Destroyer: public Ship
{
  public:
    Destroyer(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};
#endif

这是我的船类(它可能不需要读取所有内容,所以我只是把水平函数)

#include "Ship.h"
#include <iostream>
#include <stdexcept>
using namespace std;
//LOTS OF CODE I OMITTED FOR BREVITY

int Ship::level (void) const
{
    return lev;
}


    }

在我的船类

int level(void)

函数返回受保护变量lev的当前值

这是我到目前为止为我的Board.cpp所做的(对不起,代码很长,但有必要为这个问题提供上下文):

#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] = "                                                                                                    ";  
    char score[10][10] = {{' '}};
}
void Board::addShip(char type, int x1, int y1, int x2, int y2) 
{
    if(shipList.size()<=9)
        {
            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++) {
        cout<<" "<< i <<"|" ;
        for (int j = 0; j < 10; j++) {
           cout << score[i][j];
        }
            if(i == 0){
                cout << "                  |";
            }
            else{
                cout << "                   |";
                }
        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 = shipAt(x, y);
    if (ship){
            return ship;   }
    else{
        return NULL;
         }

}
int Board::level(void)
{
}
int level(void)

该函数应返回所有船舶的等级之和。

基本上,我不知道如何在我的board类中实现int level(void),我试图调用每个船舶指针的级别,并通过将shipList指针指向循环的每次迭代中的级别来添加总和。但是我很难执行

我建议通过ShipList进行基于范围的for循环(c++ 11),以获得每个Ship指针并累积级别:

int Board::level() {
  int level = 0;
  for(Ship* ship : shipList)
    level += ship->level();
  return level;
}

我认为这看起来比迭代器或基于索引的for循环更干净,特别是当元素是指针时。

遍历Ship s列表并累加每个Ship的级别。返回累计值

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;
}