-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.cpp
executable file
·191 lines (150 loc) · 4.36 KB
/
Puzzle.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <string>
#include <list>
class Position
{
public:
/**
* pre: configin is a string of at least size characters which
* represents a valid configuration
* post: Initialized to that configuration and empty list
* of moves.
*/
Position(string configin);
/*
* Display the pegs and holes configuration
*/
void displayConfiguration();
/*
* Display the sequence of moves needed to get to this
* position from the initial position.
*/
void displayMoves();
/**
* Returns a list of all possible next positions
*/
list<Position> nextPositions();
/**
* Return the number of pegs in the position
*/
int numPegs();
bool Done();
private:
// configuration of holes and pegs
string config;
static const char hole = 'o';
static const char peg = 'p';
// size of the board
int size;
// List of moves to get to this position
// each move is represented by two consecutive integers,
// the index from which a peg moves followed by the
// index to which the peg moves.
list<int> moves;
/**
* pre: A move in this position from from to to is valid
* post: Returns the postion gotten by making that move.
*/
void next(int from, int to);
};
int main(){
string start;
cout <<"Enter a starting position: ";
cin>>start;
Position p(start);
cout<<"Heres the config: ";
p.displayConfiguration();
cout <<"and heres how many of the holes have pegs: "<<p.numPegs()<<endl;
cout << "now the fun..."<<endl;
while (!(p.Done())){
//breadth 1st search
//http://science.kennesaw.edu/~bsetzer/3100sp01/SourceCode/Fogle/Puzzle.cpp
}
return 0;
}
Position::Position(string configin){
config=configin;
size=config.size();
}
void Position::displayConfiguration() {
cout<< config<<endl;
}
void Position::displayMoves() {
list<int>::iterator start, stop;
start=moves.begin();
stop=moves.end();
cout<<"[";
for (int i=0; start!=stop; i++){
if ((i%2)==1)
cout<<"("<<(*start)<<"->";
else
cout<<(*start)<<")";
++start;
}
cout<<"]"<<endl;
}
/*
void Position::displayLONP(){
list<Position> P = nextPositions();
list<Position>::iterator start, stop;
start=P.begin();
stop=P.end();
cout<<"[";
for (int i=0; start!=stop; i++){
Position Q = *start;
Q.displayConfiguration();
++start;
}
cout<<"]"<<endl;
}
*/
list<Position> Position::nextPositions() {
list<Position> posibles;
for (int i=0; i<size; i++){
if ((config[i] == peg)&&(config[i] == config[i+1])&&(config[i+2] == hole)){ //PPO
string copee1(config);
copee1[i]=hole;
copee1[i+1]=hole;
copee1[i+2]=peg;
Position posit (copee1);
posibles.push_back(posit);
}
if ((config[i] == hole)&&(config[i+1] == config[i+2])&&(config[i+1] == peg)){ //OPP
string copee2(config);
copee2[i]=peg;
copee2[i+1]=hole;
copee2[i+2]=hole;
Position posit (copee2);
posibles.push_back(posit);
}
}
return posibles;
}
int Position::numPegs() {
int num = 0; //# of holes with pegs
for (int i = 0; i<size; i++){
if (config[i]==peg)
num++;
}
return num;
}
void Position::next(int from, int to) {
config.replace(to,1,peg);
config.replace(from,1,hole);
if (from<to)
config.replace((to-1),1,hole);
else
config.replace((to+1),1,hole);
}
bool Position::Done(){
bool anser;
if (numPegs()>1){
list <Position> test (nextPositions());
bool testMT = test.empty();
if (testMT)
anser = false;
}
else
anser = true;
return anser;
}