-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.cpp
77 lines (60 loc) · 1.73 KB
/
State.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
//
// Created by Rosalie Razonable on 5/6/2021.
//
#include "State.h"
vector<int> State::getState() {
return this->state;
}
void State::setState(const vector<int>& s8) {
this->state = s8;
}
void State::setDimension(unsigned int dmension) {
this->dimension = dmension;
}
void State::createBoard() {
for(unsigned int i = 1; i <= this->dimension; i++) {
for(unsigned int j = (i*this->dimension) - (this->dimension-1); j <= (this->dimension*i); j++) {
if(state.at(j-1) == 0) {
if(this->dimension> 3) {
cout << " | __";
} else
cout << " | _";
} else if(state.at(j-1) >= 10) {
cout << " | " << state.at(j - 1);
} else {
if( this->dimension > 3)
cout << " | 0" << state.at(j - 1);
else
cout << " | " << state.at(j - 1);
}
}
cout << " | " << endl;
}
cout << endl;
}
unsigned int State::locateEmptyTile() {
for(int i = 0; i < this->getState().size() ; i++) {
if(this->getState().at(i) == 0)
return i;
}
return -1; //no empty tile, bizarre
}
State State::swapTiles(int idxToSwap) {
unsigned int emptyIdx = this->locateEmptyTile();
int newValue;
vector<int> stateConfig;
stateConfig = this->getState();
newValue = stateConfig.at(idxToSwap);
stateConfig.at(emptyIdx) = newValue;
stateConfig.at(idxToSwap) = 0;
State s(this->dimension, stateConfig);
return s;
}
bool State::isEqual(State &s8) {
if(this->getState() == s8.getState())
return true;
return false;
}
int State::getDimension() const {
return (int) this->dimension;
}