I'm in need of some assistance regarding c++ and objects\references\pointers. I'm writing a simple Chess program that has several classes. In particular I have a problem with the following two classes Board and Cell:
The board has a vector of vectors of Cells (basically a matrix). Each cell holds a pointer to the current piece that is occupying it.
class Board
{
public:
Board();
void drawBoard();
bool makeMove(int startRow, int startCol, int destRow, int destCol);
private:
vector< vector<Cell> > _board;
void initBoard();
void initPieces();
};
Board::Board()
{
initBoard();
}
void Board::initBoard()
{
for (int i = 0; i < BOARD_SIZE; i++)
{
vector<Cell> row; //create empty row
for (int j = 0; j < BOARD_SIZE; j++)
{
row.push_back((Cell(i, j)));
}
_board.push_back(row);
}
initPieces();
}
void Board::initPieces()
{
//Set Pawns
for (int i = 0; i < BOARD_SIZE; i++)
{
_board[1][i].setOccupying(new Pawn(White));
_board[6][i].setOccupying(new Pawn(Black));
}
}
void Board::drawBoard()
{
drawLettersCoord();
for (int i = BOARD_SIZE-1; i >= 0; i--)
{
std::cout << i + 1 << " ";
for (int j = 0; j < BOARD_SIZE; j++)
{
_board[i][j].draw();
}
std::cout << " " << i + 1 << std::endl;
}
std::cout << "\n";
}
bool Board::makeMove(int startRow, int startCol, int destRow, int destCol)
{
_board[startRow][startCol].getOccupyingPiece()->isLegalMove(
startRow, startCol, destRow, destCol);
return true;
}
here is the Cell class:
class Cell
{
public:
Cell();
Cell(int row, int col);
Cell(int row, int col, ChessPiece * occupying);
void draw();
ChessPiece * getOccupyingPiece();
void setOccupying(ChessPiece *occupying);
private:
int _row;
int _col;
bool _occupied;
ChessPiece * _pieceOccupying;
};
Cell::Cell()
:Cell(0, 0)
{
setColour();
}
Cell::Cell(int row, int col)
: _row(row), _col(col), _occupied(false)
{
setColour();
}
Cell::Cell(int row, int col, ChessPiece * occupying)
: _row(row), _col(col), _pieceOccupying(occupying), _occupied(true)
{
setColour();
}
void Cell::setOccupying(ChessPiece *occupying)
{
_occupied = true;
_pieceOccupying = occupying;
}
void Cell::draw()
{
int foregroundText;
if (!_occupied)
{
foregroundText = 37;
}
else
{
foregroundText = _pieceOccupying->getPlayer();
}
cout << "\33[" << foregroundText << ";" << _colour << "m"
<< (_occupied ? _pieceOccupying->getPieceCode(): " ") << "\33[0m";
}
ChessPiece * Cell::getOccupyingPiece()
{
return _pieceOccupying;
}
When I run the game, I create and draw a new Board by calling
Board _board;
_board.drawBoard();
The drawing seems to work OK without any issues.
However, when I call
_board.makeMove(1,0,2,0);
to check if the pawn can make such a move, I get a memory error. Upon debugging I see that the Cell object which is being called has junk in it and not actual data. When I try to look at the pointer to the occupying piece it shows "Unable To Read Memory", so when I call the occupying piece's isLegalMove function, it crashes.
I can't seem to find out what the problem is. I don't understand why a cell inside the vector would have junk in it, it is defined in the class (without new) so as per my understanding it should be available so long as the current instance of the board is alive.
Can anyone shed some light as to what I am doing wrong?
Not all constructors of Cell initialise _occupied and _pieceOccupying, which means that these can have garbage values in some Cell objects.
Furthermore, your makeMove dereferences getOccupyingPiece() unconditionally - it doesn't check for null.
Finally, you only have pawns on your board, which means (0, 0) does not contain a piece - and because of 1., the Cell object contains a garbage value in its _pieceOccupying, so you access random memory and crash.
Related
I am programming a game for a university project and i am having a little trouble with it. So here is my game board class header file:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <time.h>
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
char RandomPiece();
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[20];
int m_nColumns;
int m_nRows;
};
And here are the important parts of the game board .cpp file:
#include "cmagicalchemistboard.h"
using namespace std;
CMagicAlchemistBoard::CMagicAlchemistBoard(void)
: m_arrBoard(NULL), m_nColumns(6), m_nRows(8)
{
m_arrChars[0] = ' ';
}
CMagicAlchemistBoard::CMagicAlchemistBoard(const CMagicAlchemistBoard& board)
{
// Copy all of the regular data members
m_nColumns = board.m_nColumns; m_nRows = board.m_nRows;
m_arrBoard = NULL;
CreateBoard(); // Create a new game board of the same size
// Copy the contents of the game board
for(int row = 0; row < m_nRows; row++)
for(int col = 0; col < m_nColumns; col++)
m_arrBoard[row][col] = board.m_arrBoard[row][col];
int CMagicAlchemistBoard::GetBoardSpace(int row, int col)
{
if(row<0 || row>=m_nRows || col<0 || col>=m_nColumns) return 0;
return m_arrBoard[row][col];
}
void CMagicAlchemistBoard::CreateBoard(void)
{
// If there is already a board, delete it
if(m_arrBoard != NULL) DeleteBoard();
// Create the array of rows
m_arrBoard = new int*[m_nRows];
// Create each row
for(int row = 0; row < m_nRows; row++){
m_arrBoard[row] = new int[m_nColumns];
// Set each square to be empty
for(int col = 0; col < m_nColumns; col++)
m_arrBoard[row][col] = 0;
}
}
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++)
{
// printf("| %c", m_arrChars[0]);
cout << "| " << m_arrChars[0];
}
cout << "| " << endl;
}
}
So my idea is to have a function that will add one random char between 12 chars to m_arrBoard[0][2] position. Can someone help me with this please?
Create a function that returns a random character out of 12 predefined
characmters. Call it to assign a random character to m_arrBoard[0][2].
// The function to get a random character.
char getRandomChar()
{
char arr[12] = {}; // Initialize the array of characters.
int index = rand()%12;
return arr[index];
}
Somewhere down in some function....
// Assign a random character to m_arrBoard[0][2].
m_arrBoard[0][2] = getRandomChar();
Let's say you want to receive random characters from A to Z. You can try the following code:
std::vector<char> arr;
int n = (90-65 +1);//'Z'-'A'
int movePosition = 65;
for (unsigned int i = 0; i < 12; ++i)
{
int number = rand() % n;//Get a number from 0 to n-1 (where n is 26)
std::cout << "number = " << number << "; movePosition = " << movePosition << std::endl;
// To get a letter add movePosition to it so you are back in the range of 65 to 90
arr.push_back((char)(number + movePosition));
}
To see what characters you have added, you can try the following:
for (int i = 0;i<arr.size();i++)
{
std::cout << "Char=" << arr[i] << std::endl;
}
I have a class TileGrid that holds an std::vector< std::vector<Tile> >. Accessing the Tile objects in the vector works, but I can't change their properties? For the sake of completion, here are all the relevant classes:
tilegrid.h
#include <vector>
#include "tile.h"
class TileGrid {
public:
TileGrid();
TileGrid(unsigned int rows, unsigned int cols);
virtual ~TileGrid();
unsigned int getRows() const { return rows_; };
unsigned int getCols() const { return cols_; };
Tile atIndex(unsigned int row, unsigned int col) const { return tiles_[row].at(col); };
private:
std::vector< std::vector<Tile> > tiles_;
unsigned int rows_;
unsigned int cols_;
};
tilegrid.cpp
#include "tilegrid.h"
TileGrid::TileGrid() : rows_(0), cols_(0) {
}
TileGrid::TileGrid(unsigned int rows, unsigned int cols) : rows_(rows), cols_(cols) {
tiles_.clear();
for (unsigned int y = 0; y < rows_; y++) {
std::vector<Tile> horizontalTiles;
for (unsigned int x = 0; x < cols_; x++) {
horizontalTiles.push_back(Tile());
}
tiles_.push_back(horizontalTiles);
}
}
TileGrid::~TileGrid() {
}
tile.h
class Tile {
public:
Tile();
virtual ~Tile();
bool isActive() const { return isActive_; };
void setActive(bool status) { isActive_ = status; };
private:
bool isActive_;
};
tile.cpp
#include "tile.h"
Tile::Tile() : isActive_(false) {
}
Tile::~Tile() {
}
main.cpp
#include "tilegrid.h"
#include <iostream>
int main() {
TileGrid tg(20, 20);
for (unsigned int i = 0; i < tg.getRows(); i++) {
for (unsigned int j = 0; j < tg.getCols(); j++) {
if (tg.atIndex(i, j).isActive()) {
std::cout << i << "," << j << " is active" << std::endl;
} else {
std::cout << i << "," << j << " is NOT active" << std::endl;
}
}
}
// This is all working. But when I for example use the setActive function, nothing changes:
tg.atIndex(1, 0).setActive(true);
// When I print it again, the values are still the ones that were set in the constructor
for (unsigned int i = 0; i < tg.getRows(); i++) {
for (unsigned int j = 0; j < tg.getCols(); j++) {
if (tg.atIndex(i, j).isActive()) {
std::cout << i << "," << j << " is active" << std::endl;
} else {
std::cout << i << "," << j << " is NOT active" << std::endl;
}
}
}
return 0;
}
I'm really sorry for all this code... I tried to keep it as short as possible, but I thought it'd be better to post it all!
So yeah, my problem is the setActive function. When I just create a Tile and call its setActive function, everything works, but when I call it through the TileGrid object, it won't.
I have tried to solve this on my own for hours and I can't think straight anymore. I'm really desperate here, could you please have a look and maybe help me?
In your method:
Tile atIndex(unsigned int row, unsigned int col) const
you should return a reference to Tile:
Tile& atIndex(unsigned int row, unsigned int col)
now you are returning copy, and that is why modifications does not work. Also it should not be const, otherwise you will get compiler error.
Alright, I'm trying to implement a simple 2D matrix class right now. This is what it looks like so far:
template <typename Type>
class dyMatrix {
private:
Type *mat;
int width, height;
int length;
public:
dyMatrix (int _width, int _height)
: width(_width), height(_height), mat(0)
{
length = width * height;
mat = new Type[length];
};
// ---
int getWidth() {
return width;
};
int getHeight() {
return height;
};
int getLength() {
return length;
}
// ---
Type& operator() (int i, int j) {
return mat[j * width + i];
};
Type& operator() (int i) {
return mat[i];
};
// ---
~dyMatrix() {
delete[] mat;
};
};
To test it, and compare with static multi-dimensional arrays, I wrote the following snippet of code:
#include <iostream>
using namespace std;
/* matrix class goes here */
struct Coord {
int x, y;
Coord()
: x(0), y(0)
{};
Coord (int _x, int _y)
: x(_x), y(_y)
{};
void print() {
cout << x << ", " << y;
};
};
int main() {
dyMatrix<Coord> adabo(5, 7);
Coord inakos[5][7];
int i = 5, j = 0;
adabo(i, j) = *(new Coord(i, j));
inakos[i][j] = *(new Coord(i, j));
inakos[i][j].print();
adabo(i, j).print();
return 0;
}
"Adabo" and "Inakos" being arbitrarily chosen names. Upon execution, inakos prints its contents but the program crashes before adabo can do anything. Another interesting thing is that, if I give i and j values other than 5 and 0, like 5 and 1, respectively, it works fine.
I don't know what exact numbers work and which make the program go haywire, I only know that there's a irregularity here. What am I possibly doing wrong? I'm an amateur at C++, so I may or not have misused something in any of the structures.
If anyone also has the time, I'd very much like to know if there's any other error of notice in my matrix class. Anything that's not possibly related to the problem, but is a fallacy nevertheless.
I've also tested it with the following main(), but it still crashes after inakos prints its contents in [5][1]. Maybe it has to do not with dyMatrix, but a loosely-implemented Coord?
int main() {
dyMatrix<Coord> adabo(5, 7);
Coord inakos[5][7];
for (int i = 0; i < adabo.getHeight(); i++) {
for (int j = 0; j < adabo.getWidth(); j++) {
adabo(i, j) = *(new Coord(i, j));
inakos[i][j] = *(new Coord(i, j));
inakos[i][j].print();
cout << "; ";
}
cout << "\n\n";
}
cout << "\n\n\n";
Coord temp;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 5; j++) {
temp = adabo(i, j);
temp.print();
cout << "; ";
}
cout << "\n\n";
}
return 0;
}
edit: It hasn't to do with Coord. Just tested with a dyMatrix of ints and static matrix of ints, and it crashes after [5][0] nevertheless.
In your first example, you declared inakos[5][7] so the indices range from 0 to 4 and 0 to 6. inakos[5][0] and inakos[5][1] could therefore crash.
In your second example, you again declare inakos[5][7] yet you let the first index loop from 0 to 6. Again inakos[i][j] can crash. One fix is to switch your indices (ie, change to i<adabo.getWidth() and j<adabo.getHeight()).
I am trying to take in an input for the dimensions of a 2D matrix. And then use user input to fill in this matrix. The way I tried doing this is via vectors (vectors of vectors). But I have encountered some errors whenever I try to read in data and append it to the matrix.
//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
for(int j = 0; j<CC; j++)
{
cout<<"Enter the number for Matrix 1";
cin>>matrix[i][j];
}
}
Whenever I try to do this, it gives me a subscript out of range error. Any advice?
You have to initialize the vector of vectors to the appropriate size before accessing any elements. You can do it like this:
// assumes using std::vector for brevity
vector<vector<int>> matrix(RR, vector<int>(CC));
This creates a vector of RR size CC vectors, filled with 0.
As it is, both dimensions of your vector are 0.
Instead, initialize the vector as this:
vector<vector<int> > matrix(RR);
for ( int i = 0 ; i < RR ; i++ )
matrix[i].resize(CC);
This will give you a matrix of dimensions RR * CC with all elements set to 0.
I'm not familiar with c++, but a quick look at the documentation suggests that this should work:
//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
vector<int> myvector;
for(int j = 0; j<CC; j++)
{
int tempVal = 0;
cout<<"Enter the number for Matrix 1";
cin>>tempVal;
myvector.push_back(tempVal);
}
matrix.push_back(myvector);
}
Assume we have the following class:
#include <vector>
class Matrix {
private:
std::vector<std::vector<int>> data;
};
First of all I would like suggest you to implement a default constructor:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
private:
std::vector<std::vector<int>> data;
};
At this time we can create Matrix instance as follows:
Matrix one;
The next strategic step is to implement a Reset method, which takes two integer parameters that specify the new number of rows and columns of the matrix, respectively:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
if (rows == 0 || cols == 0) {
data.assign(0, std::vector<int>(0));
} else {
data.assign(rows, std::vector<int>(cols));
}
}
private:
std::vector<std::vector<int>> data;
};
At this time the Reset method changes the dimensions of the 2D-matrix to the given ones and resets all its elements. Let me show you a bit later why we may need this.
Well, we can create and initialize our matrix:
Matrix two(3, 5);
Lets add info methods for our matrix:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
data.resize(rows);
for (int i = 0; i < rows; ++i) {
data.at(i).resize(cols);
}
}
int GetNumRows() const {
return data.size();
}
int GetNumColumns() const {
if (GetNumRows() > 0) {
return data[0].size();
}
return 0;
}
private:
std::vector<std::vector<int>> data;
};
At this time we can get some trivial matrix debug info:
#include <iostream>
void MatrixInfo(const Matrix& m) {
std::cout << "{ \"rows\": " << m.GetNumRows()
<< ", \"cols\": " << m.GetNumColumns() << " }" << std::endl;
}
int main() {
Matrix three(3, 4);
MatrixInfo(three);
}
The second class method we need at this time is At. A sort of getter for our private data:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
data.resize(rows);
for (int i = 0; i < rows; ++i) {
data.at(i).resize(cols);
}
}
int At(const int &row, const int &col) const {
return data.at(row).at(col);
}
int& At(const int &row, const int &col) {
return data.at(row).at(col);
}
int GetNumRows() const {
return data.size();
}
int GetNumColumns() const {
if (GetNumRows() > 0) {
return data[0].size();
}
return 0;
}
private:
std::vector<std::vector<int>> data;
};
The constant At method takes the row number and column number and returns the value in the corresponding matrix cell:
#include <iostream>
int main() {
Matrix three(3, 4);
std::cout << three.At(1, 2); // 0 at this time
}
The second, non-constant At method with the same parameters returns a reference to the value in the corresponding matrix cell:
#include <iostream>
int main() {
Matrix three(3, 4);
three.At(1, 2) = 8;
std::cout << three.At(1, 2); // 8
}
Finally lets implement >> operator:
#include <iostream>
std::istream& operator>>(std::istream& stream, Matrix &matrix) {
int row = 0, col = 0;
stream >> row >> col;
matrix.Reset(row, col);
for (int r = 0; r < row; ++r) {
for (int c = 0; c < col; ++c) {
stream >> matrix.At(r, c);
}
}
return stream;
}
And test it:
#include <iostream>
int main() {
Matrix four; // An empty matrix
MatrixInfo(four);
// Example output:
//
// { "rows": 0, "cols": 0 }
std::cin >> four;
// Example input
//
// 2 3
// 4 -1 10
// 8 7 13
MatrixInfo(four);
// Example output:
//
// { "rows": 2, "cols": 3 }
}
Feel free to add out of range check. I hope this example helps you :)
try this. m = row, n = col
vector<vector<int>> matrix(m, vector<int>(n));
for(i = 0;i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
cout << endl;
}
cout << "::matrix::" << endl;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
Vector needs to be initialized before using it as cin>>v[i][j]. Even if it was 1D vector, it still needs an initialization, see this link
After initialization there will be no errors, see this link
What you have initialized is a vector of vectors, so you definitely have to include a vector to be inserted("Pushed" in the terminology of vectors) in the original vector you have named matrix in your example.
One more thing, you cannot directly insert values in the vector using the operator "cin". Use a variable which takes input and then insert the same in the vector.
Please try this out :
int num;
for(int i=0; i<RR; i++){
vector<int>inter_mat; //Intermediate matrix to help insert(push) contents of whole row at a time
for(int j=0; j<CC; j++){
cin>>num; //Extra variable in helping push our number to vector
vin.push_back(num); //Inserting numbers in a row, one by one
}
v.push_back(vin); //Inserting the whole row at once to original 2D matrix
}
I did this class for that purpose. it produces a variable size matrix ( expandable) when more items are added
'''
#pragma once
#include<vector>
#include<iostream>
#include<iomanip>
using namespace std;
template <class T>class Matrix
{
public:
Matrix() = default;
bool AddItem(unsigned r, unsigned c, T value)
{
if (r >= Rows_count)
{
Rows.resize(r + 1);
Rows_count = r + 1;
}
else
{
Rows.resize(Rows_count);
}
if (c >= Columns_Count )
{
for (std::vector<T>& row : Rows)
{
row.resize(c + 1);
}
Columns_Count = c + 1;
}
else
{
for (std::vector<T>& row : Rows)
{
row.resize(Columns_Count);
}
}
if (r < Rows.size())
if (c < static_cast<std::vector<T>>(Rows.at(r)).size())
{
(Rows.at(r)).at(c) = value;
}
else
{
cout << Rows.at(r).size() << " greater than " << c << endl;
}
else
cout << "ERROR" << endl;
return true;
}
void Show()
{
std::cout << "*****************"<<std::endl;
for (std::vector<T> r : Rows)
{
for (auto& c : r)
std::cout << " " <<setw(5)<< c;
std::cout << std::endl;
}
std::cout << "*****************" << std::endl;
}
void Show(size_t n)
{
std::cout << "*****************" << std::endl;
for (std::vector<T> r : Rows)
{
for (auto& c : r)
std::cout << " " << setw(n) << c;
std::cout << std::endl;
}
std::cout << "*****************" << std::endl;
}
// ~Matrix();
public:
std::vector<std::vector<T>> Rows;
unsigned Rows_count;
unsigned Columns_Count;
};
'''
I am compiling a program with gcc and when I run a.out I get this error:
Segmentation fault: 11
I think it has to do with Board::display, but I don't see anything wrong with it..
#include <iostream>
using namespace std;
const bool WHITE = 0;
const bool BLACK = 1;
//-----------------------------
class Piece
{
public:
// constructors
Piece();
Piece(bool color);
// functions
char getColor() const {return color; }
bool getSymbol() const {return symbol;}
protected:
bool color;
char symbol;
};
ostream& operator << (ostream& out, const Piece & piece)
{
out << piece.getSymbol();
return out;
}
Piece::Piece() { }
Piece::Piece(bool color)
{
this->color = color;
}
//-----------------------------
class King : public Piece
{
public:
King(bool color);
};
King::King(bool color) : Piece(color)
{
this->symbol = 'K';
}
//-----------------------------
class Tile
{
public:
// constructors/destructors
Tile();
Tile(Piece piece, int row, int col);
// functions
bool getColor() const {return color;}
void display();
private:
Piece piece;
int row, col;
bool color;
};
Tile::Tile() { }
Tile::Tile(Piece piece, int row, int col)
{
this->row = row;
this->col = col;
}
void Tile::display()
{
if (getColor() == BLACK)
{
if (piece.getColor() == BLACK)
cout << "\E[30;47m " << piece << " \E[0m";
else
cout << "\E[37;47m " << piece << " \E[0m";
}
else
{
if (piece.getColor() == BLACK)
cout << "\E[30;41m " << piece << " \E[0m";
else
cout << "\E[37;41m " << piece << " \E[0m";
}
}
//---------------------------
class Board
{
public:
// constructor
Board();
// functions
void display();
private:
Tile *tiles[8][8];
};
Board::Board()
{
for (int r = 0; r < 8; r++)
for(int c = 8; c < 8; c++)
tiles[r][c] = new Tile(King(BLACK), r, c);
}
void Board::display()
{
for (int r = 7; r >= 0; r--)
{
for(int c = 7; c >= 0; c--)
tiles[r][c]->display();
cout << endl;
}
}
//---------------------------
int main()
{
Board board;
board.display();
}
In Board::display(), r++ should be r--, and ditto for c++.
If (like me) you prefer unsigned variables for array indices, you would write the loop like this:
for (std::size_t i = 0; i != N; ++i)
{
array[N - 1 - i] = something();
}
Or, if you find that to cumbersome but you still really don't like <= and prefer iterator-style "not-equals" termination (like me), you can stick with unsigned types and say:
for (std::size_t i = N; i != 0; --i)
{
array[i - 1] = anotherthing();
}
Your next mistake is to store a polymorphic object by value of the base. That can't work! Instead, you might want to save a pointer (or reference). While we're at it, it's time for you to learn constructor initializer lists:
class Tile
{
Piece * piece; // must be a polymorphic handle!
int row;
int col;
public:
Tile(Piece * p, int r, int c) : piece(p), row(r), col(c) { }
Tile() : piece(NULL), row(0), col(0) { }
};
If you store a polymorphic object by value, you end up slicing it. You could have saved yourself the trouble by making Piece abstract.
As you can see, you should also make sure the default constructor does something useful. In fact, I would argue that the default constructor doesn't actually make sense, and that you should just remove it.
Finally, I should add that the Tile class design is a terrible problem, because you don't manage the lifetime of the Piece objects, which currently just leak. If you were to manage them, you'd need to delete the piece in the destructor of Tile, but then you'd need to implement the Rule of Three, and now you realise that Piece needs a virtual destructor...
I'm realising that this is turning into a full-blown chapter of "How to do C++", so I'll stop now. I think we have several good books on our recommended list; please consult those.
In Board::display, you are incrementing your variables in the loops instead of decrementing them.
You also aren't assigning piece to Tile::piece in Tiles constructor. (this->piece = piece)
Surely you mean:
for (int r = 7; r >= 0; r--)
{
for(int c = 7; c >= 0; c--)
to be
for (int r = 6; r > 0; r--)
{
for(int c = 6; c > 0; c--)
Unless it is just a typo the line:
for(int c = 8; c < 8; c++)
in the Board constructor is wrong and will result in none of tiles being allocated with the ultimate result of "bad" things happening when you try to use them.
Change it to:
for(int c = 0; c < 8; c++)
At some point you'll want to delete these although a better option might to be not use pointers at all in this case, just Tile tiles[8][8]; or a vector<vector<Tile> > tiles.