c++ 2d array maze navigation - c++

I'm new to c++ and wondering if I'm doing this the best way. I need to read in a line from a text file and build an array from it and then navigate it. Let me know if I'm doing something wrong. Can I access matrix the way I am?
header:
#ifndef MAZE_HPP_
#define MAZE_HPP_
#include <fstream>
#include <vector>
#include <string>
class Maze
{
public:
Maze(int size);
~Maze() {}
enum Direction { DOWN, RIGHT, UP, LEFT };
// Implement the following functions:
// read maze from file, find starting location
void readFromFile(std::ifstream &f);
// make a single step advancing toward the exit
void step();
// return true if the maze exit has been reached, false otherwise
bool atExit();
// set row and col to current position of 'x'
void getCurrentPosition(int &row, int &col);
// You can add more functions if you like
private:
// Private data and methods
int size;
static string matrix[30][30];
};
#endif /* MAZE_HPP_ */
c++ file:
#include <iostream>
#include "maze.hpp"
#include "utils.hpp"
#include <vector>
#include <string>
Maze::Maze(int size) {
size = size;
}
Maze::void readFromeFile(std::ifstream &f) {
std::string line;
int i, j;
while(std::getline(f, line)) {
for(i = 0; i < line.length(); i++) {
for(j = 0; j < line.length(); j++) {
matrix[i][j] = line.at(j);
}
}
}
}
Maze::void step() {
}
Maze::bool atExit() {
}
Maze::void getCurrentPosition(int &row, int &col) {
}

Maze::void readFromeFile {}
Maze::void step() {}
Maze::bool atExit() {}
Maze::void getCurrentPosition(int &row, int &col) {}
these should be
void Maze::readFromeFile{}
void Maze::step() {}
bool Maze::atExit(){}
void Maze::getCurrentPosition(int &row, int &col){}

Related

Vector member always zero size on call to member function that uses push_back to add to the vector member

What I am trying to do is add cards to a vector that is to be a Blackjack hand, but each time I enter the function that uses push_back to add a card to the vector, the vector starts out empty. The problem is in my Hand class in the addCardToHand function, and the same issue is present in the same class in my showHand function. I have whittled the code down as much as I could but still present a full working version with the issue. I cannot figure out why my Hand class treats the hand as brand new every time I call a function in the class. Please advise.
// Card.h
#ifndef CARD_H
#define CARD_H
#include <string>
#include <array>
class Card {
private:
int rank;
int suit;
int hardValue;
int softValue;
static const std::array<std::string,14> ranks;
static const std::array<std::string,5> suits;
public:
Card();
Card(int rank, int suit);
int getHardValue() const;
int getSoftValue() const;
std::string toString() const;
};
#endif
// Card.cpp
#include "Card.h"
const std::array<std::string,14> Card::ranks {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
const std::array<std::string,5> Card::suits {"","C","D","H","S"};
Card::Card() : rank(0), suit(0) {
hardValue = 0;
softValue = 0;
}
Card::Card(int rank, int suit) : rank(rank), suit(suit) {
if (rank == 1) {
hardValue = 1;
softValue = 11;
}
else if (rank <= 10) {
hardValue = rank;
softValue = rank;
}
else {
hardValue = 10;
softValue = 10;
}
}
int Card::getHardValue() const {
return hardValue;
}
int Card::getSoftValue() const {
return softValue;
}
std::string Card::toString() const {
return ranks[rank] + suits[suit];
}
// Shoe.h
#ifndef SHOE_H
#define SHOE_H
#include <vector>
#include <random>
#include "Card.h"
class Shoe {
private:
int numDecksInShoe;
std::vector<Card> shoe;
static int currentCard;
static int maxDealCard;
static const unsigned long int seed;
static std::mt19937 randEng;
void renewShoe();
public:
Shoe();
explicit Shoe(int numDecksInShoe);
std::vector<Card> getShoe() const;
int getCurrentCard() const;
int getMaxDealCard() const;
void shuffle();
Card dealCard();
};
#endif
// Shoe.cpp
#include <ctime>
#include "Shoe.h"
const unsigned long int Shoe::seed = static_cast<unsigned long int>(std::time(nullptr));
std::mt19937 Shoe::randEng(seed);
int Shoe::currentCard = 0;
int Shoe::maxDealCard = 51;
Shoe::Shoe() {
}
Shoe::Shoe(int decksInShoe) {
numDecksInShoe = decksInShoe;
int count = 0;
for (int i = 0; i < numDecksInShoe; i++) {
for (int suit = 4; suit >= 1; suit--) {
for (int rank = 1; rank <= 13; rank++) {
Card card(rank,suit);
shoe.push_back(card);
count += 1;
}
}
}
currentCard = 0;
maxDealCard = count - 1;
}
std::vector<Card> Shoe::getShoe() const {
return shoe;
}
int Shoe::getCurrentCard() const {
return currentCard;
}
int Shoe::getMaxDealCard() const {
return maxDealCard;
}
void Shoe::shuffle() {
Card temp;
std::uniform_int_distribution<int> deckDist(0,numDecksInShoe*52-1);
int index;
for (int i = 0; i < numDecksInShoe*52; i++) {
do {
index = deckDist(randEng);
} while (index == i);
temp = shoe[index];
shoe[index] = shoe[i];
shoe[i] = temp;
}
std::uniform_int_distribution<int> maxDeal(10,41);
int tempMax = (numDecksInShoe-1)*52 - 1;
maxDealCard = tempMax + 51 - maxDeal(randEng);
}
Card Shoe::dealCard() {
if (currentCard == maxDealCard) {
renewShoe();
}
Card dealCard = shoe[currentCard];
currentCard += 1;
return dealCard;
}
void Shoe::renewShoe() {
Shoe newShoe(numDecksInShoe);
shoe = newShoe.getShoe();
shuffle();
}
here is my Hand class
// Hand.h
#ifndef HAND_H
#define HAND_H
#include <vector>
#include <string>
#include "Card.h"
class Hand {
private:
std::vector<Card> hand;
public:
Hand();
std::vector<Card> getHand() const;
void addCardToHand(Card card);
void clearHand();
Card revealBottomCard();
std::string showHand() const;
std::string peakHand() const;
};
#endif
// Hand.cpp
#include <iostream>
#include "Hand.h"
Hand::Hand() {
}
std::vector<Card> Hand::getHand() const {
return hand;
}
void Hand::addCardToHand(Card card) {
std::cout << card.toString() << std::endl;
hand.push_back(card);
}
void Hand::clearHand() {
hand.clear();
}
std::string Hand::showHand() const {
std::string returnString = "";
for (int i = hand.size()-1; i >= 1; i--) {
returnString += hand[i].toString() + "\n";
}
returnString += "XX\n";
return returnString;
}
std::string Hand::peakHand() const {
std::string returnString = "";
for (int i = hand.size()-1; i >= 0; i--) {
returnString += hand[i].toString() + "\n";
}
return returnString;
}
here is the Table class that has the code that calls the Hand class functions
// Table.h
#ifndef TABLE_H
#define TABLE_H
#include "Shoe.h"
#include "Player.h"
class Table {
private:
Shoe shoe;
public:
explicit Table(Shoe shoe);
Shoe getShoe() const;
void clearHand(std::vector<Player> players);
void dealHand(std::vector<Player> players);
};
#endif
// Table.cpp
#include <iostream>
#include "Table.h"
Table::Table(Shoe shoe) : shoe(shoe) {
}
Shoe Table::getShoe() const {
return shoe;
}
void Table::clearHand(std::vector<Player> players) {
for (Player &player : players) {
player.getHand().clearHand();
}
}
void Table::dealHand(std::vector<Player> players) {
for (int i = 0; i <= 1; i++) {
for (Player &player : players) {
player.getHand().addCardToHand(shoe.dealCard());
}
}
}
// Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "Hand.h"
class Player {
private:
std::string name;
Hand hand;
double money;
public:
explicit Player(std::string name, double money = 1000.0);
std::string getName() const;
Hand getHand() const;
double getMoney() const;
};
#endif
// Player.cpp
#include "Player.h"
Player::Player(std::string name, double money) : name(name), money(money) {
}
std::string Player::getName() const {
return name;
}
Hand Player::getHand() const {
return hand;
}
double Player::getMoney() const {
return money;
}
and finally here is a short driver that runs and displays the issue
// blackjack testing
#include <iostream>
#include "Player.h"
#include "Table.h"
int main() {
std::vector<Player> players {Player("Tom",1500.0),Player("Sue"),Player("Dave")};
Shoe shoe1(6);
Table table1(shoe1);
table1.dealHand(players);
for (Player player : players) {
std::cout << player.getName() << "'s hand\n";
std::cout << player.getHand().showHand() << "\n\n";
}
}
The output is below. I printed out the cards that were added to the hand vector (and then 'forgotten') and below that above the XX's, if everything were to work correctly you should see the 4, 5 and 6 of spades since I did not shuffle the deck. The XX's are to simulate face down bottom card.
AS
2S
3S
4S
5S
6S
Tom's hand
XX
Sue's hand
XX
Dave's hand
XX
Sorry for dumping all this code in here, but I wanted to provide a full working solution and this is as small as I could get it. Thanks for any help.
player.getHand().addCardToHand(...);
player.getHand() returns a temporary Hand object that's a copy of player.hand. Then you add a card to that temporary object. Then that temporary object dies, added card and all. player.hand remains unchanged. This line of code is an elaborate no-op.
To add on #Igor's answer, the best way to fix this should probably return a reference instead:
const Hand& Player::getHand() const {
return hand;
}
I made this a const because returning a non-const could (potentially) allow you to break constness of a constant Player object. That is just inviting potential bugs.
Because of this, you may want to add a non-const version too:
Hand& Player::getHand() {
return hand;
}
Now you can't modify a const Player object, yet modify it properly when you need to.

Error invalid new-expression of abstract class type FancyPlayer cpp

i have this code at main.cpp :
#include <iostream>
#include <limits>
#include <conio.h>
#include "Ghost/SmartGhost/SmartGhost.h"
#include "Player/Player.h"
#include "PlayerInfo/PlayerInfo.h"
#include "GameBoard/GameBoard.h"
#include "ManualPlayer/ManualPlayer.h"
#include <vector>
using namespace std;
class Position {
public:
int x;
int y;
Position();
Position(int xo,int yo) {x=xo; y=yo;} };
class FancyPlayer : public Player
{
private: vector<Position> visited; //vector of visited nodes int size[2]; //width and height of maze Position start; vector <Position> path; //last visited node(backtrack) public:
void init(int width,int height,int x,int y) {
size[0]=width;
size[1]=height;
start.x=x; //starting position
start.y=y; visited.push_back(Position(start.x,start.y)); path.push_back(Position(start.x,start.y)); }
bool isValid(char ** ViewAround,int x,int y) {
return (ViewAround[x][y]!='#' && ViewAround[x][y]!='*'); }
bool isvisited(int x,int y){
bool f=false;
for(int i=0;i<visited.size();i++) {
if (visited[i].x==x && visited[i].y==y)
f=true;
} return f; }
int getMove(char** ViewAround) {
if (isValid(ViewAround,1,2) && !isvisited(start.x-1,start.y))
{
visited.push_back(Position(start.x-1,start.y));
path.push_back(Position(start.x-1,start.y));
start.x--;
return 0; }
else if (isValid(ViewAround,3,2) && !isvisited(start.x+1,start.y))
{
visited.push_back(Position(start.x+1,start.y));
path.push_back(Position(start.x+1,start.y));
start.x++;
return 1; } else if (isValid(ViewAround,2,3) && !isvisited(start.x,start.y+1))
{
visited.push_back(Position(start.x,start.y+1));
path.push_back(Position(start.x,start.y+1));
start.y++;
return 2; } else if (isValid(ViewAround,2,1) && !isvisited(start.x,start.y-1))
{
visited.push_back(Position(start.x,start.y-1));
path.push_back(Position(start.x,start.y-1));
start.y--;
return 3; } else
{
if (path[path.size()-1].x<start.x){
path.pop_back();
start.x++;
return 1;
}
if (path[path.size()-1].x>start.x){
path.pop_back();
start.x--;
return 0;
}
if (path[path.size()-1].y<start.y){
path.pop_back();
start.y++;
return 2; }
if (path[path.size()-1].y>start.y){
path.pop_back();
start.y--;
return 3;
}
}
}
std::string getName() { return std::string("mplampla"); }
std::string getId() { return std::string("cs141065"); }
};
int main() {
std::vector<ObjectInfo *> PlayersVector; //vector with players engaged
PlayersVector.push_back(new PlayerInfo(*new FancyPlayer(), 'A')); //Erase the comments to play with keyboard
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'B')); //Fool Standby player
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'C')); //Fool Standby player
PlayersVector.push_back(new PlayerInfo(*new StupidPlayer(), 'D')); //Fool Standby player
GameBoard *MainGameObject; //Main Game Object
InfoBoard *ptrToInfoBoard = new InfoBoard();
for (int j = 0; j < PlayersVector.size(); ++j) {
ptrToInfoBoard->addPlayer(*static_cast<PlayerInfo *>(PlayersVector[j]));
}
std::ostringstream he;
std::vector<std::string>*ptr = GameBoard::getMapFileNames(DEVCPP_PATH);
for (unsigned int i = 0; i < ptr->size(); ++i) {
he<<DEVCPP_PATH<<ptr->operator[](i);
for (int j = 0; j < EATCH_MAP_PLAY_TIMES; ++j) {
MainGameObject=new GameBoard(true,he.str().c_str(),PlayersVector,EXETASI_MODE,ptrToInfoBoard);
MainGameObject->StartGame();
delete(MainGameObject);
getchar();
}
he.str("");
}
while(1); }
Player.h code :
#ifndef GHOST_PLAYER_H
#define GHOST_PLAYER_H
#include <iostream>
#include <windows.h>
class Player{
private:
public:
Player(){}
virtual std::string getName()=0 ;
virtual std::string getId()=0 ;
virtual int getMove(const char **ViewAround)=0;
virtual void init(int width,int height,int CurrentX,int CurrentY )=0;
};
#endif //GHOST_PLAYER_H
When i am putting at main the
*new FancyPlayer(), 'A')) i am getting an error that telling "[Error] invalid new-expression of abstract class type 'FancyPlayer'" , if i put this line in comment the code works properly...
the stupidplayer code :
StupidPlayer.h :
#ifndef GHOST_STUPIDPLAYER_H
#define GHOST_STUPIDPLAYER_H
#include "../Player/Player.h"
#include "../Move.h"
class StupidPlayer: public Player {
int getMove(const char **ViewAround);
void init(int width,int height,int x,int y);
std::string getName();
std::string getId();
};
#endif //GHOST_STUPIDPLAYER_H
StupidPlayer.cpp :
#include <cstdlib>
#include "StupidPlayer.h"
#include <fstream>
int StupidPlayer::getMove(const char **ViewAround) {
std::ofstream he("inner.txt");
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
he<<ViewAround[i][j];
}
he<<'\n';
}
return STAND;
}
void StupidPlayer::init(int width, int height, int x, int y) {
}
std::string StupidPlayer::getName() {
return std::string("StupidPlayer");
}
std::string StupidPlayer::getId() {
return std::string("cs161119");
}
i cant see any difference between stupidplayer and fancy... what can i do... in order to make fancy works properly... it may be a problem that have 2 classes in use for fancy?

Undefined Reference Error/ Dynamically Calling Functions

Hi I am working on a program that involves a Main.cpp, Connect4.cpp, and Connect4.h file. When I compile my program I am getting an error in the Main file saying that my playGame function is an undefined reference. I am compiling both files together(main first) I believe something is wrong in the way I am trying to dynamically call the function playGame. Any input would be much appreciated!
Main.cpp
#include <iostream>
#include <array>
#include "Connect4.h"
void playGame();
using namespace std;
int main()
{
Connect4 *ptr;
ptr=new Connect4;
ptr-> playGame();
delete ptr;
}
Connect4.cpp
#include <iostream>
#include <array>
#include "Connect4.h"
char gameBoard[9][7];
int rows;
int columns;
using namespace std;
void playGame()
{
void display();
int selectColumn(bool);
int tokenPlacement(char token, int columns);
bool winOrLose();
cout<<"Welcome to Connect Four.";
for(int i=0; i<rows;++i)
{
for(int j=0; j<columns; ++j)
{
gameBoard[i][j]=' ';
}
}
bool player1Turn=true;
char winner='n';
int column =0;
while(true){
display();
column=selectColumn(player1Turn);
if(player1Turn==true)
{
tokenPlacement('x',column);
player1Turn=false;
}
else
{
tokenPlacement('o', column);
player1Turn=true;
winner= winOrLose();
if(winner!='n')
{
break;
}
}
cout<<"Winner is:"<<winner;
}
Connect4.h
#ifndef CONNECT4_H_
#define CONNECT4_H_
#include
using namespace std;
class Connect4 {
public:
static void playGame();
private:
void display();
int selectColumn(bool);
int tokenPlacement(char, int);
bool winOrLose();
char gameBoard[9][7];
};
#endif /* CONNECT4_H_ */

Incomplete type error when using std::vector with structs

I'm working with c++ STL vectors, and have a vector of structures called projectileList. I'm trying to iterate through the vector, getting and setting values in the struts as I iterate, but my code refuses to compile, with the error 'Incomplete type is not allowed.'
Can anyone please point out what I'm doing wrong:
Code:
ProjectHandeler.h:
#include "stdafx.h"
#include "DataTypes.h"
#include <vector>
class ProjectileHandeler {
private:
int activeObjects;
std::vector<projectile> projectileList;
void projectileUpdater();
public:
ProjectileHandeler(projectile* input[], int projectileCount);
~ProjectileHandeler();
};
#endif
projectileHandeler.cpp
#include "stdafx.h"
#include "DataTypes.h"
#include "ProjectHandeler.h"
#include <vector>
ProjectileHandeler::ProjectileHandeler(projectile* input[], int projectileCount)
{
for (int i = 0; i < projectileCount; i++)
{
projectileList.push_back(*input[i]);
activeObjects += 1;
}
//NO extra slots. Not that expensive.
projectileList.resize(projectileList.size());
}
void ProjectileHandeler::projectileUpdater()
{
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true)
break;
}
}
}
This compiles fine (tested it here: http://codepad.org/cWn6MPJq):
#include <vector>
struct projectile {
bool isEditing;
};
class ProjectileHandeler {
private:
std::vector<projectile> projectileList;
void projectileUpdater()
{
//This bit loops to infinity and beyond! ...or at least untill the handeler is destroyed.
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true) //Throws Incomplete type error
break;
}
}
}
};
int main()
{
}
Notice the removal of *, correct type of loop variable and removal of extra class specifier.

Getting the "undefined reference to class::function" error on my C++ game.

I am programming this game for a university work and i believe that my code is right, but i keep getting this error and it is keeping me from finishing my work in time. So here's the game board class header:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
class CMagicAlchemistBoard
{
public:
CMagicAlchemistBoard(void); // Default Constructor
CMagicAlchemistBoard(const CMagicAlchemistBoard& board); // Copy Constructor
~CMagicAlchemistBoard(void ); // Destructor
void SetupBoard(void); // Function to setup the board
int GetBoardSpace(int row, int col); // Get the color at row,col
// Accessor functions to get/set board size information
int GetColumns(void) const { return m_nColumns; }
void SetColumns(int nColumns) { m_nColumns = (nColumns >= 6) ? nColumns : 6; }
int GetRows(void) const { return m_nRows; }
void SetRows(int nRows) { m_nRows = (nRows >= 8) ? nRows : 8; }
void DeleteBoard(void); // Function to delete the board and free memory
void ExecuteMove(int row, int col);
bool IsGameOver(void) const; // Is the game over?
void DrawBoard(void);
bool ValidMove(int row, int col); // Function to see if a move is valid
private:
void CreateBoard(void); //Function to create the board and allocate memory
// Class Data
int** m_arrBoard; // 2D array pointer
// Board size information
char m_arrChars[10];
int m_nColumns;
int m_nRows;
};
And here is the .cpp file with only the implementation of the DrawBoard() function:
#include "cmagicalchemistboard.h"
using namespace std;
void CMagicAlchemistBoard::DrawBoard(void)
{
cout << "MAGIC ALCHEMIST" << endl;
cout << " ";
for(int col = 0; col < m_nColumns; col++){ printf(" ---",col); }
cout << endl;
for(int row = 0; row < m_nRows; row++)
{
for(int col = 0; col < m_nColumns; col++)
{
cout << "| " << m_arrChars[GetBoardSpace(row, col)];
}
cout << "| " << endl;
}
}
I pretend to use this function on another class. Here's the header of that class:
#include "cmagicalchemistboard.h"
#include <iostream>
#include <cstdlib>
#include <conio.h> //Contains the function getch(), which reads the input from the keyboard
#define LEFT_ARROW 75
#define RIGHT_ARROW 77
#define UP_ARROW 72
#define DOWN_ARROW 80
#define ESC 27
class CMagicAlchemist
{
public:
CMagicAlchemist();
~CMagicAlchemist();
// Functions for accessing the game board
char GetBoardSpace(int row, int col) { return m_board->GetBoardSpace(row, col); }
void SetupBoard(void) { m_board->SetupBoard(); }
int GetColumns(void) { return m_board->GetColumns(); }
void SetColumns(int nColumns) { m_board->SetColumns(nColumns); }
int GetRows(void) { return m_board->GetRows(); }
void SetRows(int nRows) { m_board->SetRows(nRows); }
void DeleteBoard(void) { m_board->DeleteBoard(); }
bool IsGameOver() { return m_board->IsGameOver(); }
void InputGameParameters();
void GetMove(int &row, int &col);
void DrawBoard();
void NewGame();
void Game();
private:
CMagicAlchemistBoard* m_board; // Instance of the game board
int m_nmoves;
};
And finally, the .cpp file of this last class with only the implementation of the function that calls the DrawBoard() function:
#include "cmagicalchemist.h"
void CMagicAlchemist::Game()
{
int x,y;
InputGameParameters();
NewGame();
DrawBoard();
}
So, my problem is: when i compile this program im getting this error: "undefined reference to CMagicAlchemist::DrawBoard()". This is stupid because the DrawBoard() function doesnt even belong to CMagicAlchemist class but instead, it belong to CMagicAlchemistBoard class. Can somebody help me?
You have DrawBoard declared in your CMagicAlchemist class.
You call DrawBoard from a CMagicAlchemist member function.
That tries to call Drawboard for the CMagicAlchemist class.