大家星期天快乐!
我试图教自己C++,所以我正在做一个战舰程序。
这个版本相当标准。玩家进入一个细胞的坐标试图击中一艘船。说明船只是否被击中的程序。如果一艘船占用的所有单元格都被击中,程序将打印一条消息,说明该船已沉没。每次尝试之后,程序通过显示板,并分别用"*"或"x"标记所有成功的尝试来打印当前状态。
我所遇到的困难是在我的董事会类中编写等级函数,它概括了船上所有级别的船舶。
所以我为战列舰准备了一个这样的董事会
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头(可能没有必要读取所有内容,只需查看级别函数)
//
// 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下面是我的ship类(可能没有必要读取所有内容,所以我只是将级别函数放在这里)
#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)中,函数应该返回船上所有船舶的水平之和。
本质上,我不知道如何实现int level(void)__in,我的板类,我试图调用每个ship指针的级别,并通过在循环的每一次迭代中将shipList指针指向级别来添加一个和。但我很难做到这一点。
发布于 2014-05-25 21:11:35
我建议通过ShipList建立一个基于范围的for循环(ShipList),以获取每个Ship指针并累加级别:
int Board::level() {
int level = 0;
for(Ship* ship : shipList)
level += ship->level();
return level;
}我认为这看起来比迭代器或基于索引的for循环更干净,尤其是当元素是指针时。
发布于 2014-05-25 21:01:56
迭代Ship的列表并累积每个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;
}https://stackoverflow.com/questions/23859818
复制相似问题