struct Board
{
int length; //col
int height; //rows
int pos_x;
int pos_y;
};
class BoardGame
{
public:
BoardGame::BoardGame(int rows, int columms);
void DrawBoard();
void AddBoard(const Board& board);
private:
int _rows;
int _columns;
int _rowoffset;
int _columnoffset;
std::vector<std::vector<char> > _lines;
std::vector<Board> _game_boards;
void fill(int row_offset, int col_offset, int start, const Board& board);
int row_offset(int offset, int start); //down
int col_offset(int offset, int start); //left
void PositionBoard(int x, int y); // top left corner of board
};
BoardGame::BoardGame(int rows, int columns) : _rows(rows), _columns(columns), _lines(_rows, std::vector<char> (_columns) )
{}
void BoardGame::DrawBoard()
{
if (_rows > 0 && _columns > 0)
{
std::vector<Board>::iterator board_iter = _game_boards.begin();
_lines[1][1] = 't';
_lines[2][1] = 's';
std::vector<std::vector<char> >::iterator first_iter = _lines.begin();
for (; first_iter != _lines.end(); first_iter++)
{
std::vector<char>::iterator second_iter = first_iter->begin();
for (; second_iter != first_iter->end(); second_iter++)
std::cout << *second_iter;
}
for (; board_iter != _game_boards.end(); board_iter++)
{
//Set starting offset
PositionBoard(board_iter->pos_x, board_iter->pos_y);
fill(_rowoffset, _columnoffset, 0, *board_iter);
if ((_rowoffset + board_iter->height) > _rows) //height of board and board offset should not exceed screen matrix height (ie rows)
std::cout << "Board is too high, please resize board" << std::endl;
if ((_columnoffset + board_iter->length) > _columns) // length of board and col offset should not exceed cols of screen matrix ( ie cols)
std::cout << "Board is too long, please resize board" << std::endl;
for(int i = _rowoffset; i <= ((_rowoffset + board_iter->height) - 1); i++) //Rows
{
for (int j = _columnoffset; j <= ((_columnoffset + board_iter->length) - 1); j++)//Columns
{
if ((i <= _rowoffset) || (i == ((_rowoffset + board_iter->height) - 1))) {// Only first and last line
if ((i == ((_rowoffset + board_iter->height) - 1)))
_lines[i][j] = '\n';
_lines[i][j] = '-';
}
else if ((j <= _columnoffset) || (j == ((_columnoffset + board_iter->length) - 1)))
{
if ((i % 5) == 0)
_lines[i][j] = '-';
else
_lines[i][j] = '|';
}
else
{
_lines[i][j] = ' ';
}
}
}
/// draw matrix to screen
/* for (int i = 0; i <= (_rows - 1); i++)
{
for (int j = 0; j <= (_columns - 1); j++)
std::cout << _lines[i][j];
}*/
}
}
else
std::cout << "No rows or columns to draw gameboard from!";
}
void BoardGame::PositionBoard(int x, int y)
{
_rowoffset = x;
_columnoffset = y;
}
void BoardGame::AddBoard(const Board& board)
{
_game_boards.push_back(board);
}
void BoardGame::fill(int rowoffset, int coloffset, int start, const Board& board)
{
int rows = row_offset(rowoffset, start);
int cols = col_offset(coloffset, start);
for (int r = 0; r <= ((rows + board.height) -1); r++)
{
for (int c = 0; c <= (cols -1); c++)
{
if (c == cols-1) // end of the line
_lines[r][c] = '\n';
_lines[r][c] = ' ';
}
}
}
//...