Changeset 140
- Timestamp:
- 04/08/04 14:16:00 (5 years ago)
- Files:
-
- 06/devel/abstractBoard.cc (modified) (17 diffs)
- 06/devel/abstractBoard.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
06/devel/abstractBoard.cc
r123 r140 42 42 c = m.c; 43 43 if (m.captures) { 44 captures = new vector<p air<char,char>>;45 vector<p air<char,char>>::iterator it;44 captures = new vector<p_cc>; 45 vector<p_cc>::iterator it; 46 46 for(it = m.captures->begin(); it != m.captures->end(); it++) 47 captures .push_back(*it);47 captures->push_back(*it); 48 48 } 49 49 else captures = 0; … … 55 55 56 56 57 Move& Move:: operator=(const Move& m) {57 Move& Move::copy(const Move& m) { 58 58 if (this != &m) { 59 59 x = m.x; … … 62 62 if (captures) delete captures; 63 63 if (m.captures) { 64 captures = new vector<p air<char,char>>;65 vector<p air<char,char>>::iterator it;64 captures = new vector<p_cc>; 65 vector<p_cc>::iterator it; 66 66 for(it = m.captures->begin(); it != m.captures->end(); it++) 67 captures .push_back(*it);67 captures->push_back(*it); 68 68 } 69 69 else captures = 0; … … 98 98 } 99 99 100 abstractBoard& abstractBoard:: operator=(const abstractBoard& ab) {100 abstractBoard& abstractBoard::copy(const abstractBoard& ab) { 101 101 printf("copy assignment operator\n"); 102 102 if (this != &ab) { 103 103 delete status; 104 boardsize = ab.boardsize ();104 boardsize = ab.boardsize; 105 105 status = new char[boardsize*boardsize+1]; 106 106 for (int i = 0; i < boardsize*boardsize; i++) … … 125 125 126 126 int abstractBoard::len_cap_last() { 127 return undostack.top().captures.size(); 127 if (!undostack.size()) { 128 return -1; 129 printf("Error len_cap_last\n"); // FIXME 130 } 131 Move m = undostack.top(); 132 if (m.captures) return m.captures->size(); 133 else return 0; 128 134 } 129 135 130 136 PyObject* abstractBoard::get_cap_last() { 131 // FIXME 137 if (!undostack.size()) { 138 Py_INCREF(Py_None); 139 return Py_None; 140 } 141 Move m = undostack.top(); 142 PyObject* cap = PyList_New(0); 143 if (!m.captures) return cap; 144 vector<p_cc>::iterator it; 145 for(it = m.captures->begin(); it != m.captures->end(); it++) 146 PyList_Append(cap, Py_BuildValue("(ii)", it->first, it->second)); 147 return cap; 132 148 } 133 149 134 150 PyObject* abstractBoard::undostack_pop() { 135 // FIXME 151 if (!undostack.size()) { 152 Py_INCREF(Py_None); 153 return Py_None; 154 } 155 Move m = undostack.top(); 156 undostack.pop(); 157 PyObject* cap = PyList_New(0); 158 if (m.captures) { 159 vector<p_cc>::iterator it; 160 for(it = m.captures->begin(); it != m.captures->end(); it++) 161 PyList_Append(cap, Py_BuildValue("(ii)", it->first, it->second)); 162 } 163 return Py_BuildValue("(ii)cO", m.x, m.y, m.c, cap); 136 164 } 137 165 138 166 void abstractBoard::undostack_append_pass() { 139 undostack.push _back(Move(20,20,'-'));167 undostack.push(Move(20,20,'-')); 140 168 } 141 169 … … 167 195 int x = PyInt_AsLong(PyTuple_GetItem(pos,0)); 168 196 int y = PyInt_AsLong(PyTuple_GetItem(pos,1)); 169 197 return play(x, y, color); 198 } 199 200 201 int abstractBoard::play(int x, int y, char* color) throw (BoardError) { 170 202 if (x<0 || x>=boardsize || y<0 || y>=boardsize) return 0; 171 203 if (status[boardsize*x+y] != ' ') { … … 175 207 // if (color[0]!='B' && color[0]!='W') printf("%c\n", color[0]); 176 208 177 PyObject* l = legal(x, y, color[0]); 178 if (!l) throw BoardError(); 179 if (!PyTuple_Check(l)) throw BoardError(); 180 if (PyTuple_Size(l)) { 181 PyObject* captures = PyTuple_GetItem(l,1); 182 for(int i=0; i<PyList_Size(captures); i++) { 183 PyObject* tup = PyList_GetItem(captures,i); 184 status[boardsize*PyInt_AsLong(PyTuple_GetItem(tup,0))+\ 185 PyInt_AsLong(PyTuple_GetItem(tup,1))] = ' '; // remove captured stones, if any 186 } 187 188 PyObject* undoinfo = PyTuple_New(3); 189 PyTuple_SetItem(undoinfo, 0, Py_BuildValue("(ii)", x, y)); 190 PyTuple_SetItem(undoinfo, 1, Py_BuildValue("c", color[0])); 191 PyTuple_SetItem(undoinfo, 2, captures); 192 193 if (PyList_Append(undostack, undoinfo)) throw BoardError(); // remember move + captured stones for easy undo 209 vector<p_cc>* captures = legal(x, y, color[0]); 210 if (captures) { 211 vector<p_cc>::iterator it; 212 for(it=captures->begin(); it!=captures->end(); it++) 213 status[boardsize*it->first + it->second] = ' '; // remove captured stones, if any 214 Move m(x, y, color[0]); 215 if (captures->size()) m.captures = captures; 216 else delete captures; 217 undostack.push(m); 194 218 return 1; 195 } else return 0; 196 } 197 198 199 vector<pair<char,char>>* abstractBoard::legal(int x, int y, char color) { 200 vector<pair<char,char>>* c = new stack<Move>; 219 } 220 return 0; 221 } 222 223 224 vector<p_cc>* abstractBoard::legal(int x, int y, char color) { 225 vector<p_cc>* c = new vector<p_cc>; 201 226 int* nb = neighbors(x,y); 202 227 for(int i=1; i<=nb[0]; i++) { … … 204 229 int y1 = nb[i] % boardsize; 205 230 if (status[boardsize*x1 + y1] == invert(color)) { 206 vector<pair<char,char>> d = hasNoLibExcP(x1, y1, x*boardsize+y); 207 vector<pair<char,char>>::iterator it; 208 for(it = d.begin(); it != d.end(); d++) c->push_back(*it); 231 vector<p_cc>* d = hasNoLibExcP(x1, y1, x*boardsize+y); 232 vector<p_cc>::iterator it; 233 for(it = d->begin(); it != d->end(); d++) c->push_back(*it); 234 delete d; 209 235 } 210 236 } … … 212 238 setStatus(x,y,color); 213 239 214 if (c .size()) {215 vector<p air<char,char>>* captures = new vector<pair<char,char>>();216 while (c .size()) {217 Move ctop = c->top();240 if (c->size()) { 241 vector<p_cc>* captures = new vector<p_cc>(); 242 while (c->size()) { 243 p_cc ctop = (*c)[0]; 218 244 bool contained = false; 219 vector<p air<char,char>>::iterator it;245 vector<p_cc>::iterator it; 220 246 for(it = captures->begin(); it != captures->end(); it++) { 221 247 if (ctop.first == it->first && ctop.second == it->second) { … … 224 250 } 225 251 } 226 if (!contained) captures .push_back(ctop);227 c-> pop();252 if (!contained) captures->push_back(ctop); 253 c->erase(c->begin()); 228 254 } 229 255 delete c; … … 231 257 } 232 258 delete c; 233 vector<p air<char,char>>d = hasNoLibExcP(x, y);259 vector<p_cc>* d = hasNoLibExcP(x, y); 234 260 if (d->size()) { 261 delete d; 235 262 status[boardsize*x + y] = ' '; 236 263 return 0; 237 264 } 238 265 else { 239 vector<pair<char,char>>* ret = new vector<pair<char,char>>(); 266 delete d; 267 vector<p_cc>* ret = new vector<p_cc>(); 240 268 return ret; 241 269 } … … 244 272 245 273 246 vector<p air<char,char>> abstractBoard::hasNoLibExcP(char x1, chary1, int exc) {247 vector<p air<char,char>> st();248 vector<p air<char,char>> newlyFound();249 newlyFound.push_back(p air<char,char>(x1, y1));274 vector<p_cc>* abstractBoard::hasNoLibExcP(int x1, int y1, int exc) { 275 vector<p_cc>* st = new vector<p_cc>; 276 vector<p_cc> newlyFound; 277 newlyFound.push_back(p_cc(x1, y1)); 250 278 251 279 int foundNew = 1; … … 253 281 while (foundNew) { 254 282 foundNew = 0; 255 vector<p air<char,char>> n;256 for( int i=0; i < newlyFound.size(); i++) {257 p air<char,char>tup = newlyFound[i];283 vector<p_cc> n; 284 for(unsigned int i=0; i < newlyFound.size(); i++) { 285 p_cc tup = newlyFound[i]; 258 286 int x = tup.first; 259 287 int y = tup.second; … … 263 291 if (status[yy1] == ' ' && yy1 != exc) { 264 292 delete [] nbs; 265 return vector<pair<char,char>>();293 return new vector<p_cc>; 266 294 } 267 295 else { 268 pair<char,char> yy(yy1/boardsize, yy1%boardsize); 269 if (status[yy1]==status[x*boardsize+y] 270 && st.find(st.begin(),st.end(),yy)==st.end() 271 && newlyFound.find(newlyFound.begin(),newlyFound.end(),yy)==newlyFound.end()) { 272 n.push_back(yy); 296 p_cc yy(yy1/boardsize, yy1%boardsize); 297 if (status[yy1]==status[x*boardsize+y]) { 273 298 foundNew = 1; 299 vector<p_cc>::iterator it; 300 for(it = st->begin(); it!=st->end(); it++) { 301 if (it->first==yy.first && it->second==yy.second) { 302 foundNew = 0; 303 break; 304 } 305 } 306 if (foundNew) { 307 for(it=newlyFound.begin(); it!=newlyFound.end(); it++) { 308 if (it->first==yy.first && it->second==yy.second) { 309 foundNew = 0; 310 break; 311 } 312 } 313 } 314 if (foundNew) n.push_back(yy); 274 315 } 275 316 } … … 278 319 } 279 320 280 PyList_SetSlice(st, 0, 0, newlyFound); 281 vector<pair<char,char>>::iterator it; 321 vector<p_cc>::iterator it; 282 322 for(it=newlyFound.begin(); it!=newlyFound.end(); it++) 283 st .push_back(*it);323 st->push_back(*it); 284 324 newlyFound = n; 285 325 } … … 294 334 undostack.pop(); 295 335 296 char color = Move,color;297 vector<p air<char,char>>captures = tuple.captures;336 char color = tuple.c; 337 vector<p_cc>* captures = tuple.captures; 298 338 int x = tuple.x; 299 339 int y = tuple.y; 300 340 301 341 status[x*boardsize+y] = ' '; 302 for( int i=0; i < captures.size(); i++) {303 p air<char,char> t = captures[i];304 setStatus(t.first, t.second, invert( *color));342 for(unsigned int i=0; i < captures->size(); i++) { 343 p_cc t = (*captures)[i]; 344 setStatus(t.first, t.second, invert(color)); 305 345 } 306 346 } … … 312 352 PyObject* l = PyList_New(0); 313 353 PyList_Append(l, Py_BuildValue("(ii)", x, y)); 314 undostack.push _back(Move(-1, -1, invert(status[boardsize*x+y])));354 undostack.push(Move(-1, -1, invert(status[boardsize*x+y]))); 315 355 status[boardsize*x+y] = ' '; 316 356 } 06/devel/abstractBoard.h
r123 r140 24 24 // communication should probably be done via SGF coord. 25 25 // color == 'B' / 'W' 26 using namespace std; 26 27 27 28 #include "Python.h" 29 #include <vector> 30 #include <utility> 31 #include <stack> 28 32 29 33 class BoardError { … … 32 36 }; 33 37 38 typedef pair<char,char> p_cc; 34 39 35 40 class Move { … … 38 43 Move(const Move& m); 39 44 ~Move(); 40 Move& operator=(const Move& m);45 Move& copy(const Move& m); 41 46 42 47 char x; 43 48 char y; 44 49 char c; 45 vector<pair<char,char> >* captures;46 } 50 vector<pair<char,char> >* captures; 51 }; 47 52 48 53 … … 56 61 abstractBoard(const abstractBoard& ab); 57 62 ~abstractBoard(); 58 abstractBoard& operator=(const abstractBoard& ab);63 // abstractBoard& operator=(const abstractBoard& ab); 59 64 void clear(); 60 65 int play(PyObject* pos, char* color) throw(BoardError); 66 int play(int x, int y, char* color) throw(BoardError); 61 67 void undo(int n=1); 62 68 void remove(int x, int y); … … 68 74 void undostack_append_pass(); 69 75 int len_undostack(); 70 abstractBoard copy(const abstractBoard& ab);76 abstractBoard& copy(const abstractBoard& ab); 71 77 72 78 private: 73 79 int* neighbors(int x, int y); 74 vector< Move>* legal(int x, int y, char color);75 vector< Move>* hasNoLibExcP(int x1, int y1, int exc=-1);80 vector<pair<char,char> >* legal(int x, int y, char color); 81 vector<pair<char,char> >* hasNoLibExcP(int x1, int y1, int exc=-1); 76 82 char invert(char); 77 83 }; 78 79 80 81 82
