gpt4 book ai didi

c++ - 指向指针的指针,以便在 BattleShip 程序 c++ 中求和级别

转载 作者:行者123 更新时间:2023-11-28 00:28:35 25 4
gpt4 key购买 nike

大家星期天快乐!

我正在尝试自学 C++,所以我正在做一个 Battleship 程序。

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

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

所以我有一个这样的战舰板

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

这是我的 board header 上下文文件:

#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类来存储船的坐标

这是我的 scapy (可能没有必要阅读所有内容,只看关卡函数):

//
// 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)

函数返回 protected 变量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)该函数应返回棋盘上所有船只的等级总和。

基本上我不知道如何在我的棋盘类中实现 int level(void)在循环的每次迭代中指向 level 的 shipList 指针。但我正在努力执行它。

最佳答案

我建议通过 ShipList 使用基于范围的 for 循环 (C++11) 来获取每个 Ship 指针并累积级别:

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

我认为这看起来比迭代器或基于索引的 for 循环更清晰,尤其是当元素是指针时。

关于c++ - 指向指针的指针,以便在 BattleShip 程序 c++ 中求和级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com