在战列舰游戏c++中返回一个指向舰船的指针
Returning a pointer to a ship in BattleShip game C++
我正在尝试自学c++,所以我正在做一个战舰程序。我有一个Ship and Board班
这个版本是相当标准的。玩家输入一个单元格的坐标,试图击中一艘船。如果船被击中,程序就会显示。如果一艘船所占用的所有单元格都被击中,程序就打印一条消息,说明该船已沉没。每次尝试后,程序通过显示电路板的当前状态,所有成功的尝试分别标记为"*"
或"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;
}
}
这是我的Board Class。我有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;
}
基本上我试图使用bool Ship::includes(int x, int y)
函数从船类。我试图使它,如果函数返回真,那么atShip函数也会返回真,因为包含函数是一个布尔值。
然而,该实现不工作,我正在接收一个调用非静态成员函数没有对象参数错误。
EDIT:对于额外的上下文(可能没有必要,所以除非需要,否则不要点击),
这是我的Ship.cpp: http://pastebin.com/cYDt0f8W
这是我的船头文件:http://pastebin.com/W6vwKJRz
这是我的Board头文件:http://pastebin.com/r36YjHjt
Ship* ship = Ship::includes(x,y);
-
Ship::includes()
返回bool
而不是Ship*
-
Ship::includes()
不是static
您需要浏览您的船舶列表,并测试每一艘船是否为"at"(x,y)。一旦你找到了这样的船,你就可以把它退回去。
类似于(伪代码):
foreach ship in shipList
if (ship->includes(x, y))
return ship
return NULL;
- 修改函数中的指针(将另一个指针作为参数传递)
- 将指针分配给另一个指针时会发生什么情况?
- C++将一个指针分配给另一个指针时执行的类型检查
- 更改一个指针后更改乘法对象
- 从另一个指针指向函数的指针
- 有没有办法在同名类 (c++) 中为对象分配一个指针变量
- 为什么通过定义另一个指针单元格,整个代码停止工作?
- C:试图将指针值复制到另一个指针中,得到可修改的左值错误
- C++ typedef 函数指针,并在一个语句中声明一个指针
- 一个对象的两个指针.删除了一个指针,对象仍然存在
- 如何获得GUID编码器,如果我在IMFTransform上有一个指针?
- 我不明白我的错误需要返回一个指针数组
- 使用 SFINAE 测试是否可以将一个指针类型static_cast到另一个指针类型
- 将指针分配给另一个指针时会发生什么情况?
- 如何保存指针地址,以便另一个指针可以继续处理它
- 为什么 std::span 是一个指针 + 大小而不是两个迭代器
- 类的成员应该是一个指针
- 隐式指针变量,在自动定义中具有另一个指针变量
- 利用双重链接列表中的下一个指针
- 你能把一个指针投射到数组中吗?