-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
122 lines (101 loc) · 3.05 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
109
110
111
112
113
114
115
116
117
118
119
120
//
// Created by hars on 23/07/22.
//
#include <stdexcept>
#include <iostream>
#include "Board.h"
Board::Board() {
//Assign and init cell for each entry of boardCells
for (int i = 0; i <9 ; ++i) {
for (int j = 0; j < 9; ++j) {
boardCells[i][j] = Cell{Coord{i, j}};
}
}
cleanupStreaks();
}
void Board::cleanupStreaks(){
//cleanup :
// Goes through each cell horizontally, if there is a streak, then change the 3rd candy to be "disjoint" from the surrounding candies.
// Repeat vertically.
for (int i = 0; i < 2; ++i) {
for (int y = 0; y <9 ; ++y){
int previousType = 0;
int counter = 0;
for (int x = 0; x < 9; ++x) {
Coord target = Coord{x, y};
//if horizontal cleanup = done
if(i) {
//swap coords to scan vertically
target.x = y;
target.y = x;
}
if (previousType == getCandyType(target)) {
if (++counter == 2) {
generateDisjointCandy(target);
}
}
else{
counter = 0;
}
previousType = getCandyType(target);
}
}
}
}
Cell& Board::getCell(Coord target) {
return boardCells[target.x][target.y];
}
Candy& Board::getCandy(Coord target) {
return getCell(target).candy;
}
int Board::getCandyType(Coord target) {
return getCandy(target).candyType;
}
void Board::setCandyType(Coord target, int candyType) {
getCandy(target).setCandyType(candyType);
}
void Board::swapCandyType(Coord c1, Coord c2) {
int c1type = getCandyType(c1);
int c2type = getCandyType(c2);
setCandyType(c1,c2type);
setCandyType(c2,c1type);
}
std::vector<Coord> Board::cellsOnPath(int range, Coord target, int xDir, int yDir) {
//Goes through a path given a range from target with xDir/yDir and returns all cellCoords on path.
std::vector<Coord> output;
int x = target.x + xDir;
int y = target.y + yDir;
for (int i = 0; i < range; ++i) {
while (y>=0 && y<=9 && x>=0 && x<=9){
output.emplace_back(x,y);
y+=yDir;
x+=xDir;
}
}
return output;
}
void Board::generateDisjointCandy(Coord c1) {
//move if statements inside fxns.
std::vector <int> neighborTypes;
//if cell is not on edge, then proceed to pushback
if(c1.x!=0) {
neighborTypes.push_back(getCandyType(cellsOnPath(1, c1, -1, 0)[0]));
}
if(c1.x!=8) {
neighborTypes.push_back(getCandyType(cellsOnPath(1, c1, 1, 0)[0]));
}
if(c1.y!=0){
neighborTypes.push_back(getCandyType(cellsOnPath(1, c1, 0, -1)[0]));
}
if(c1.y!=8){
neighborTypes.push_back(getCandyType(cellsOnPath(1, c1, 0, 1)[0]));
}
setCandyType(c1, Candy::drawType(neighborTypes));
}
void Board::draw() {
for (int i = 0; i <9 ; ++i) {
for (int j = 0; j < 9; ++j) {
boardCells[i][j].draw();
}
}
}