指针指向一个指针,以便在战舰程序c++中求和级别
Pointer to a pointer in order to sum levels in BattleShip program c++
大家星期天快乐!
我正在尝试自学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;
}
相关文章:
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 是否可以使用非常量指针调用非常量函数,以及当两个unique_ptrs指向同一个对象时程序的行为方式?
- 如果我删除指针,我的C++程序会意外删除系统文件吗?
- 为什么从 constexpr 引用生成的程序集代码与从 constexpr 指针生成的程序集代码不同?
- 在C++中的程序之间共享指针
- 使用唯一指针调用函数会使我的程序崩溃
- 指针和程序性能之间有什么关系吗?
- 使用终止程序的指针在数组中输入字符串
- 如何在程序集函数中将元素数组作为参数传递时转发 ARM 寄存器的地址指针
- 类指针方法崩溃程序
- 找不到如何创建使用指针、字符和 for 函数组合的程序
- LLDB 格式化程序摘要提供程序和模板智能指针的语法子项
- 当程序从该函数调谐器时,指向在函数中声明和定义的某些 C 字符串的指针不再有效.为什么?
- 从 GetProcAddress 获取的函数指针在使用 stdlib 时会使程序崩溃
- C 指针设置为地址,但莫名其妙地指出了程序的不同部分中的不同值
- 如何在没有作弊引擎的情况下从其他应用程序获取指针地址?
- C++简单指针程序
- 字符双指针程序中的访问冲突
- 智能指针——程序终止时的分段错误
- 我如何得到句子反转使用指针程序停止打印出乱码