-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
108 lines (97 loc) · 2.83 KB
/
Board.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// Created by Nikita on 16.06.2021.
//
#include "Board.h"
#include "Figures/Pawn.h"
#include "Figures/Rook.h"
#include "Figures/Knight.h"
#include "Figures/King.h"
#include "Figures/Bishop.h"
#include "Figures/Queen.h"
#include "utils.h"
#include <algorithm>
#include <iostream>
Board *Board::instance = nullptr;
Board *Board::GetInstance() {
if (!instance) {
instance = new Board();
}
return instance;
}
Board::Board() {
}
void Board::Initialize() {
for (int i = 0; i < 16; i++) {
if (i >= 8) {
AddFigure(new Pawn(6, i - 8, BLACK, 'p'));
}else{
AddFigure(new Pawn(1, i, WHITE, 'P'));
}
}
AddFigure(new Rook(0, 0, WHITE, 'R'));
AddFigure(new Rook(0, 7, WHITE, 'R'));
AddFigure(new Rook(7, 0, BLACK, 'r'));
AddFigure(new Rook(7, 7, BLACK, 'r'));
AddFigure(new Knight(0, 1, WHITE, 'H'));
AddFigure(new Knight(0, 6, WHITE, 'H'));
AddFigure(new Knight(7, 1, BLACK, 'h'));
AddFigure(new Knight(7, 6, BLACK, 'h'));
AddFigure(new Bishop(0, 2, WHITE, 'B'));
AddFigure(new Bishop(0, 5, WHITE, 'B'));
AddFigure(new Bishop(7, 2, BLACK, 'b'));
AddFigure(new Bishop(7, 5, BLACK, 'b'));
AddFigure(new Queen(0, 3, WHITE, 'Q'));
AddFigure(new Queen(7, 3, BLACK, 'q'));
AddFigure(new King(0, 4, WHITE, 'K'));
AddFigure(new King(7, 4, BLACK, 'k'));
}
void Board::AddFigure(Figure *obj) {
figures.push_back(obj);
}
void Board::makeBuffer() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
int index = findFigureIndex(i, j, figures);
if (index != -1) {
buffer[i][j] = figures.at(index)->getDisplay();
} else {
buffer[i][j] = '.';
}
}
}
}
void Board::printBuffer() {
for (int i = 0; i < 8; i++) {
std::cout << i + 1 << " | ";
for (int j = 0; j < 8; j++) {
std::cout << buffer[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << " _ _ _ _ _ _ _ _" << std::endl;
std::cout << " A B C D E F G H\n" << std::endl;
}
void Board::show() {
makeBuffer();
printBuffer();
}
void Board::makeMove(int prevRow, int prevCol, int nextRow, int nextCol) {
color player = (movesCounter % 2) ? BLACK : WHITE;
int index = findFigureIndex(prevRow, prevCol, figures);
if (index != -1) {
if (figures.at(index)->getColor() == player) {
std::cout << "made move" << std::endl;
try {
figures.at(index)->move(nextRow, nextCol, figures);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
movesCounter--;
}
}else{
movesCounter--;
std::cout << "it's other player's turn"<<std::endl;
}
movesCounter++;
}
show();
}