我正在制作具有继承性和多态性的蛇梯游戏。该板由可以是普通,蛇形或梯形的瓷砖制成。因此,我有一个具有可变类型Board
(抽象类)的类Tile
,该类型可以是Snake
(子类)或Ladder
(子类)。董事会可以像用户希望的一样大,因此,我正在使用矢量指针Tile
。
我想用板子的大小给定的固定大小来初始化这样的向量,但是我看到一个错误消息“ function Board::getBoardSize() is not a type name
”,但我发现互联网上的每个人甚至我的教授都像这样初始化向量。
[注:Board
是在另一个类(MyGame
)中构造的,特别是在称为start()
的函数中,而在主体中,我只是创建了一个对象类型MyGame
来调用start函数,因此一切都会发生。
Board.h:
#ifndef BOARD_H
#define BOARD_H
#include <vector>
#include "Tile.h"
class Board
{
public:
Board(int, int);
~Board();
void setBoardSize(int);
void setNumSnakes(int);
void setNumLadders(int);
int getBoardSize();
int getNumSnakes();
int getNumLadders();
public:
int boardSize;
int numSnakes;
int numLadders;
std::vector<Tile*> place (getBoardSize()); -<------ I get the error here.
};
#endif
Board.cpp:
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include "Board.h"
#include "Snake.h"
#include "Ladder.h"
Board::Board(int rwrd, int pnlty)
{
//initialize random seed to randomize snakes and ladders.
srand(time(NULL));
int index = rand() % getBoardSize() + 1;
// All tiles are set to 'N' when the array of Tiles is created.
//Creates (numSnakes value) Snake objets inside the vector Tile pointer in random indexes.
for(int i = 0; i < getNumSnakes(); i++)
{
/*
Makes sure it only replaces tiles when:
- with type = 'N'
- there is no 'S' tiles before tile 4.
- there is no 'S' (penlaty value) tiles before the end.
*/
while (index < pnlty || index > (getBoardSize() - pnlty) || place[index]->getType() != 'N')
index = rand() % boardSize + 1;
place[index] = new Snake(pnlty);
}
index = rand() % getBoardSize() + 1;
//Creates (numLadders value) Ladder objets inside the vector Tile pointer in random indexes.
for(int i = 0; i < getNumLadders(); i++)
{
/*
Makes sure it only replaces tiles when:
- with type = 'N'.
- there is no 'L' tiles before tile 4.
- there is no 'L' (reward value) tiles before the end.
*/
while (index < rwrd || index > (getBoardSize() - rwrd) || place[index]->getType() != 'N')
index = rand() % boardSize + 1;
place[index] = new Ladder(rwrd);
}
}
Board::~Board()
{
}
void Board::setBoardSize(int SIZE)
{
if(SIZE > 0)
boardSize = SIZE;
else
{
std::cout << "Board size has to be longer than zero" << std::endl;
boardSize = 0;
}
}
void Board::setNumSnakes(int numS)
{
if(numS >= 0)
numSnakes = numS;
else
{
std::cout << "There can't be a negative number of snake tiles in the board." << std::endl;
numSnakes = -1;
}
}
void Board::setNumLadders(int numL)
{
if(numL >= 0)
numLadders = numL;
{
std::cout << "There can't be a negative number of ladder tiles in the board." << std::endl;
numLadders = -1;
}
}
int Board::getBoardSize()
{
return boardSize;
}
int Board::getNumSnakes()
{
return numSnakes;
}
int Board::getNumLadders()
{
return numSnakes;
}
MyGame.h:
#ifndef MYGAME_H
#define MYGAME_H
class MyGame
{
protected:
int numPlayers;
int maxTurns;
public:
MyGame();
~MyGame();
void start(); <---------- Function where everything happens
void setNumPlayers(int);
void setMaxTurns(int);
int setReward();
int setPenalty();
int getNumPlayers();
int getMaxTurns();
};
#endif
MyGame.cpp:
#include <iostream>
#include "MyGame.h"
#include "Board.h"
MyGame::MyGame()
{
}
MyGame::~MyGame()
{
}
void MyGame::start()
{
/*
std::cout << "Number of player: ";
std::cin >> numPlayers;
std::cout << std::endl;
std::cout << "Maximum number of turns: ";
std::cin >> maxTurns;
std::cout << std::endl;
int r = setReward(); //I'm debugging so I'm just making sure the board is constructed right
int p = setPenalty();
*/
numPlayers = 2;
maxTurns = 20;
Board brd(3,3);
for(int i = 0; i < brd.getBoardSize(); i++)
std::cout << i + 1 << brd.place[i]->getType() << std::endl;
}
void MyGame::setNumPlayers(int numP)
{
if(numP > 1)
numPlayers = numP;
else
{
std::cout << "The number of players isn't enough to start." << std::endl;
numPlayers = 0;
}
}
void MyGame::setMaxTurns(int max)
{
if(max > 0)
maxTurns = max;
else
{
std::cout << "The Limit of turns has to be higger than 0" << std::endl;
maxTurns = 0;
}
}
int MyGame::setReward()
{
int rew;
while(true)
{
std::cout << "Reward (Ladder's consequence): ";
std::cin >> rew;
if(rew > 0)
return rew;
else if(rew < 0)
return (rew * -1);
else
std::cout << "The Rewrd value can't be zero." << std::endl << std::endl;
}
}
int MyGame::setPenalty()
{
int pen;
while(true)
{
std::cout << "Penalty (Snake's consequence): ";
std::cin >> pen;
if(pen > 0)
return (pen * -1);
else if(pen < 0)
return pen;
else
std::cout << "The penalty value can't be zero." << std::endl << std::endl;
}
}
int MyGame::getNumPlayers()
{
return numPlayers;
}
int MyGame::getMaxTurns()
{
return maxTurns;
}
main.cpp:
#include <iostream>
#include "MyGame.h"
using namespace std;
int main()
{
MyGame snakes;
snakes.start();
cin.get();
}
所以似乎有两个错误,第一个是技术错误。编写要编写的代码的方法是在构造函数初始化列表中创建板,像这样]
Board::Board(int rwrd, int pnlty) : place (getBoardSize()) { ... }
但是第二个问题似乎更严重。在您的代码中没有看到任何表明您知道创建Board对象时的木板尺寸的信息。在您发布的代码中,仅在调用setBoardSize方法后才知道板子的大小,据我所知这从未发生。
您可能应该使电路板的尺寸成为构造函数的参数。