aboutsummaryrefslogtreecommitdiff
path: root/week03/Exercise6.cpp
blob: 3d3c0c02cfb943d8eb6e5b5189e232b03bdbb7df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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();
}