-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
70 lines (62 loc) · 2.2 KB
/
Copy pathBoard.cpp
File metadata and controls
70 lines (62 loc) · 2.2 KB
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
#include "Board.h"
const int NUMBER_OF_ROW = 5;
const int NUMBER_OF_COL = 9;
Board::~Board(){
for (auto& row : cells){
for (auto cell : row){
delete cell;
}
}
}
float calculateHeight(sf::Vector2f beginOfBoard , sf::Vector2f endOfBoard){
return (endOfBoard.y - beginOfBoard.y)/NUMBER_OF_ROW;
}
float calculateWeight(sf::Vector2f beginOfBoard , sf::Vector2f endOfBoard){
return (endOfBoard.x - beginOfBoard.x)/NUMBER_OF_COL;
}
Board::Board(sf::Vector2f _beginOfBoard , sf::Vector2f _endOfBoard){
this->beginOfBoard = _beginOfBoard;
this->endOfBoard = _endOfBoard;
float cellHeight = calculateHeight(this->beginOfBoard ,this->endOfBoard);
float cellWeight = calculateWeight(this->beginOfBoard ,this->endOfBoard);
for(int i = 0 ; i < NUMBER_OF_ROW ; i++){
std::vector<Cell*> rowCell;
for(int j = 0 ; j < NUMBER_OF_COL ; j++){
sf::Vector2f beginOfCell;
beginOfCell.x = beginOfBoard.x + j*cellWeight;
beginOfCell.y = beginOfBoard.y + i*cellHeight;
sf::Vector2f endOfCell;
endOfCell.x = beginOfCell.x + cellWeight;
endOfCell.y = beginOfCell.y + cellHeight;
rowCell.push_back(new Cell(beginOfCell , endOfCell));
}
cells.push_back(rowCell);
}
}
sf::Vector2f Board::plantAt(sf::Vector2f mouseAddr ,Plant* plant) {
for(auto rowCell : this->cells){
for(Cell* cell : rowCell){
if(cell->isEmpty() && cell->isContainAddr(mouseAddr)){
cell->getFull();
plant->assignCell(cell);
return cell->setPlant(plant);
}
}
}
}
bool Board::isContain(sf::Vector2f mousePositionFloat){
return ((mousePositionFloat.x > this->beginOfBoard.x) &&
(mousePositionFloat.x < this->endOfBoard.x) &&
(mousePositionFloat.y > this->beginOfBoard.y) &&
(mousePositionFloat.y < this->endOfBoard.y));}
bool Board::isCellEmpty(sf::Vector2f addr){
for(auto cellRow : cells){
for(auto cell : cellRow){
if(cell->isContainAddr(addr)){
if(cell->isEmpty())
return true;
}
}
}
return false;
}