diff options
Diffstat (limited to 'week03/Exercise6.cpp')
| -rw-r--r-- | week03/Exercise6.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/week03/Exercise6.cpp b/week03/Exercise6.cpp new file mode 100644 index 0000000..3d3c0c0 --- /dev/null +++ b/week03/Exercise6.cpp @@ -0,0 +1,88 @@ +#include <iostream> + +const int WHITE = 0; +const int BLACK = 1; + +struct ChessPiece { + int color; + char figure; + + ChessPiece(int color, int figure) { + this->color = color; + this->figure = figure; + } +}; + +struct ChessBoard { +private: + ChessPiece*** board; + int N; + + void setColumnPieces(int column, char pieceChar) { + board[column][0] = new ChessPiece(WHITE, pieceChar); + board[column][1] = new ChessPiece(WHITE, 'P'); + for (int i = 2; i < N-2; i++) { + board[column][i] = nullptr; + } + board[column][N-2] = new ChessPiece(BLACK, 'P'); + board[column][N-1] = new ChessPiece(BLACK, pieceChar); + } + + void fillBoardWithPieces() { + for (int column = 0; column < N; column++) { + switch (column % 8) { + case 0: case 7: + setColumnPieces(column, 'R'); break; + case 1: case 6: + setColumnPieces(column, 'K'); break; + case 2: case 5: + setColumnPieces(column, 'B'); break; + case 3: + setColumnPieces(column, 'I'); break; + case 4: + setColumnPieces(column, 'Q'); break; + } + } + } + +public: + ChessBoard(int N) { + if (N < 5) { + throw "Board size cannot be less than 5!"; + } + + board = new ChessPiece**[N]; + for (int i = 0; i < N; i++) { + board[i] = new ChessPiece*[N]; + } + this->N = N; + + fillBoardWithPieces(); + } + ~ChessBoard() { + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + delete board[i][j]; + } + delete[] board[i]; + } + delete[] board; + } + + void Print() { + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (board[j][i] == nullptr) + std::cout << ' '; + else + std::cout << board[j][i]->figure; + } + std::cout << std::endl; + } + } +}; + +int main() { + ChessBoard cb(10); + cb.Print(); +} |
