Hello i am trying to make a chess game for a uni project i am still on the preliminary stages, but i am already having problems with my print_board function. My board object has as private data a vector consisting of piece objects. However when i try to access these objects and print their type (defined a function in the piece class to do that) the program terminates and my guess is that the vector that was supposed to store the pieces is empty. Why is that the case? The problem arises at the void board::print_board() function, the std::cout<<"Message"; is carried out while std::cout<<"Does not print"; does not. Thank you in advance for the help.
#include<iostream>
#include<string>
#include<vector>
class piece{
protected:
std::string colour;
std::string loc;
std::string type;
bool captured;
public:
//Default constructor
piece() :colour{}, loc{}, type{},captured{0} {};
//Parametrised
piece(std::string piece_colour, std::string piece_loc,std::string piece_type,bool piece_captured):
colour{piece_colour}, loc{piece_loc}, type{piece_type},
captured{piece_captured}
{};
~piece(){} //Destructor
std::string get_piece_colour() const {return colour;}
std::string get_piece_loc() const {return loc;}
std::string get_piece_type() const {return type;}
};
class board{
private:
std::vector<piece> Board;
public:
//Default constructor
board() :Board{} {};
//Parametrised constructor
board(std::vector<piece> &Board);
~board(){} //Destructor
void print_board();
};
board::board(std::vector<piece> &Board_data)
{
int counter =0;
std::cout<<"Parameterized constructor called"<<std::endl;
std::vector<piece> Board(64);
std::vector<piece>::iterator Board_begin{Board.begin()};
std::vector<piece>::iterator Board_end{Board.end()};
std::vector<piece>::iterator Board_iterator;
for(Board_iterator=Board_begin;
Board_iterator<Board_end;++Board_iterator){
*Board_iterator = Board_data[counter];
counter++;
}
std::cout<<Board[0].get_piece_type()<<std::endl;
std::cout<<Board[1].get_piece_type()<<std::endl;
}
void board::print_board()
{
std::cout<<"Message";
std::cout<<Board[1].get_piece_type();
std::cout<<"Does not print";
std::vector<piece>::iterator Board_begin{Board.begin()};
std::vector<piece>::iterator Board_end{Board.end()};
std::vector<piece>::iterator Board_iterator;
for(int i{}; i<8; i++){
if (i==0){
std::cout<<" a b c d e f g h"<<std::endl;
std::cout<<8-i;
for(Board_iterator=Board_begin;
Board_iterator<Board_end;++Board_iterator){
std::cout<< Board_iterator -> get_piece_type()<<" ";
}
}else if (i==7){
std::cout<<8-i<<" "<<8-i<<std::endl;
std::cout<<" a b c d e f g h"<<std::endl;
}else{
std::cout<<8-i<<" "<<8-i<<std::endl;
}
}
}
int main()
{
piece knight_white_1{"White","b1","N",0};
std::vector<piece> Board(64);
for(int i{}; i<64; i++){
Board[i] = knight_white_1;
//std::cout<<Board[i].get_piece_type()<<std::endl;
}
board game{Board};
game.print_board();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire