After a few days of my question being closed, and my edits that would answer all of the questions in the comments not being approved, I have decided to re-post with a few changes.
I am new to c++, but I am an experienced programmer. I am trying to create a chess engine, as I have done it in python but I want it faster. I am storing my chess board as a 2d array of Piece pointers.
chessBoard.h
#include <iostream>
#include "pieces.h"
class Chess
{
private:
Piece* board[8][8] = {};
public:
Chess();
void print_board();
};
pieces.h
enum PieceType {
pawn, knight, bishop, rook, queen, king
};
enum Colour {
white, black
};
class Piece {
protected:
PieceType pieceType;
Colour colour;
public:
char symbol;
Piece(Colour colour);
};
class Pawn : public Piece {
public:
Pawn(Colour colour);
};
// The rest of the pieces are defined in the same way
pieces.cpp
#include "pieces.h"
Piece::Piece(Colour colour) {
this->colour = colour;
}
Pawn::Pawn(Colour colour) : Piece(colour) {
this->pieceType = pawn;
this->symbol = (colour == white) ? 'P' : 'p';
}
// The rest of the pieces constructors are the same
chessBoard.cpp
Chess::Chess()
{
// The back row of black pieces
Rook bq_r(black);
Knight bq_n(black);
Bishop bq_b(black);
Queen bq_q(black);
King bk_k(black);
Bishop bk_b(black);
Knight bk_n(black);
Rook bk_r(black);
board[0][0] = &bq_r;
board[0][1] = &bq_n;
board[0][2] = &bq_b;
board[0][3] = &bq_q;
board[0][4] = &bk_k;
board[0][5] = &bk_b;
board[0][6] = &bk_n;
board[0][7] = &bk_r;
}
void Chess::print_board() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j]) {
std::cout << board[i][j]->symbol;
std::cout << " ";
} else {
std::cout << ". ";
}
}
std::cout << std::endl;
}
}
main.cpp
#include "chessBoard.h"
int main()
{
Chess chess;
chess.print_board();
return 1;
}
I understand this is a lot of code, and I wish I could post only the relevant information, but unfortunately I have no clue what the problem was, and most of the comments on my last post asked for more info. If needed the complete code, it can be found here: https://github.com/kiran-isaac/chess-engine/tree/test.
The Expected Outcome
r n b q k b m r
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
The Actual Outcome
r � � � � r
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
The first rook is always correct. Everything after that changes with each running of the program.
The cause seems to be that the everything in the board array is corrupted. It is all fine until it hits the "printBoard" function. As soon as it encounters the first cout, everything in the array becomes corrupted. I can work around this by having it store the output in a string and outputting it afterwords, but this still causes the array to be corrupted, albeit after I have sucessfully printed it.
I hope that someone can help, as I am enjoying development with c++, and I don't want to give up on this project.
This is a screenshot of the value of the board array from line 1 of the printBoard() function. This is how it should be.
This is a screenshot from after the first iteration, after encountering the std::cout on line 4 of the printBoard() function.
I have been stuck on this problem for 2 days, researching and debugging, and I've found nothing. I'm sure it's something simple I'm doing wrong, I just can't find it. Please comment if you need any more info.
Thank you

