Changeset 140

Show
Ignore:
Timestamp:
04/08/04 14:16:00 (5 years ago)
Author:
ug
Message:

Fix abstractBoard.cc

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/devel/abstractBoard.cc

    r123 r140  
    4242  c = m.c; 
    4343  if (m.captures) { 
    44     captures = new vector<pair<char,char>>; 
    45     vector<pair<char,char>>::iterator it; 
     44    captures = new vector<p_cc>; 
     45    vector<p_cc>::iterator it; 
    4646    for(it = m.captures->begin(); it != m.captures->end(); it++)  
    47       captures.push_back(*it); 
     47      captures->push_back(*it); 
    4848  } 
    4949  else captures = 0; 
     
    5555 
    5656 
    57 Move& Move::operator=(const Move& m) { 
     57Move& Move::copy(const Move& m) { 
    5858  if (this != &m) { 
    5959    x = m.x; 
     
    6262    if (captures) delete captures; 
    6363    if (m.captures) { 
    64       captures = new vector<pair<char,char>>; 
    65       vector<pair<char,char>>::iterator it; 
     64      captures = new vector<p_cc>; 
     65      vector<p_cc>::iterator it; 
    6666      for(it = m.captures->begin(); it != m.captures->end(); it++)  
    67         captures.push_back(*it); 
     67        captures->push_back(*it); 
    6868    } 
    6969    else captures = 0; 
     
    9898} 
    9999 
    100 abstractBoard& abstractBoard::operator=(const abstractBoard& ab) { 
     100abstractBoard& abstractBoard::copy(const abstractBoard& ab) { 
    101101  printf("copy assignment operator\n"); 
    102102  if (this != &ab) { 
    103103    delete status; 
    104     boardsize = ab.boardsize()
     104    boardsize = ab.boardsize
    105105    status = new char[boardsize*boardsize+1]; 
    106106    for (int i = 0; i < boardsize*boardsize; i++) 
     
    125125 
    126126int 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; 
    128134} 
    129135 
    130136PyObject* 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; 
    132148} 
    133149 
    134150PyObject* 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); 
    136164} 
    137165 
    138166void abstractBoard::undostack_append_pass() { 
    139   undostack.push_back(Move(20,20,'-')); 
     167  undostack.push(Move(20,20,'-')); 
    140168} 
    141169 
     
    167195  int x = PyInt_AsLong(PyTuple_GetItem(pos,0)); 
    168196  int y = PyInt_AsLong(PyTuple_GetItem(pos,1)); 
    169  
     197  return play(x, y, color); 
     198
     199 
     200 
     201int abstractBoard::play(int x, int y, char* color) throw (BoardError) { 
    170202  if (x<0 || x>=boardsize || y<0 || y>=boardsize) return 0; 
    171203  if (status[boardsize*x+y] != ' ') { 
     
    175207  //  if (color[0]!='B' && color[0]!='W') printf("%c\n", color[0]); 
    176208 
    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); 
    194218    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 
     224vector<p_cc>* abstractBoard::legal(int x, int y, char color) { 
     225  vector<p_cc>* c = new vector<p_cc>; 
    201226  int* nb = neighbors(x,y); 
    202227  for(int i=1; i<=nb[0]; i++) { 
     
    204229    int y1 = nb[i] % boardsize; 
    205230    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; 
    209235    } 
    210236  } 
     
    212238  setStatus(x,y,color); 
    213239 
    214   if (c.size()) { 
    215     vector<pair<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]
    218244      bool contained = false; 
    219       vector<pair<char,char>>::iterator it; 
     245      vector<p_cc>::iterator it; 
    220246      for(it = captures->begin(); it != captures->end(); it++) { 
    221247        if (ctop.first == it->first && ctop.second == it->second) { 
     
    224250        } 
    225251      } 
    226       if (!contained) captures.push_back(ctop); 
    227       c->pop(); 
     252      if (!contained) captures->push_back(ctop); 
     253      c->erase(c->begin()); 
    228254    } 
    229255    delete c; 
     
    231257  } 
    232258  delete c; 
    233   vector<pair<char,char>> d = hasNoLibExcP(x, y); 
     259  vector<p_cc>* d = hasNoLibExcP(x, y); 
    234260  if (d->size()) { 
     261    delete d; 
    235262    status[boardsize*x + y] = ' '; 
    236263    return 0; 
    237264  } 
    238265  else { 
    239     vector<pair<char,char>>* ret = new vector<pair<char,char>>(); 
     266    delete d; 
     267    vector<p_cc>* ret = new vector<p_cc>(); 
    240268    return ret; 
    241269  } 
     
    244272 
    245273 
    246 vector<pair<char,char>> abstractBoard::hasNoLibExcP(char x1, char y1, int exc) { 
    247   vector<pair<char,char>> st()
    248   vector<pair<char,char>> newlyFound()
    249   newlyFound.push_back(pair<char,char>(x1, y1)); 
     274vector<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)); 
    250278 
    251279  int foundNew = 1; 
     
    253281  while (foundNew) { 
    254282    foundNew = 0; 
    255     vector<pair<char,char>> n; 
    256     for(int i=0; i < newlyFound.size(); i++) { 
    257       pair<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]; 
    258286      int x = tup.first; 
    259287      int y = tup.second; 
     
    263291        if (status[yy1] == ' ' && yy1 != exc) { 
    264292          delete [] nbs; 
    265           return vector<pair<char,char>>()
     293          return new vector<p_cc>
    266294        } 
    267295        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]) { 
    273298            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); 
    274315          } 
    275316        } 
     
    278319    } 
    279320     
    280     PyList_SetSlice(st, 0, 0, newlyFound); 
    281     vector<pair<char,char>>::iterator it; 
     321    vector<p_cc>::iterator it; 
    282322    for(it=newlyFound.begin(); it!=newlyFound.end(); it++) 
    283       st.push_back(*it); 
     323      st->push_back(*it); 
    284324    newlyFound = n; 
    285325  } 
     
    294334      undostack.pop(); 
    295335       
    296       char color = Move,color
    297       vector<pair<char,char>> captures = tuple.captures; 
     336      char color = tuple.c
     337      vector<p_cc>* captures = tuple.captures; 
    298338      int x = tuple.x; 
    299339      int y = tuple.y; 
    300340 
    301341      status[x*boardsize+y] = ' '; 
    302       for(int i=0; i < captures.size(); i++) { 
    303         pair<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)); 
    305345      } 
    306346    } 
     
    312352  PyObject* l = PyList_New(0); 
    313353  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]))); 
    315355  status[boardsize*x+y] = ' '; 
    316356} 
  • 06/devel/abstractBoard.h

    r123 r140  
    2424// communication should probably be done via SGF coord. 
    2525// color == 'B' / 'W' 
     26using namespace std; 
    2627 
    2728#include "Python.h" 
     29#include <vector> 
     30#include <utility> 
     31#include <stack> 
    2832 
    2933class BoardError { 
     
    3236}; 
    3337 
     38typedef pair<char,char> p_cc; 
    3439 
    3540class Move { 
     
    3843  Move(const Move& m); 
    3944  ~Move(); 
    40   Move& operator=(const Move& m); 
     45  Move& copy(const Move& m); 
    4146 
    4247  char x; 
    4348  char y; 
    4449  char c; 
    45   vector<pair<char,char>>* captures; 
    46 } 
     50  vector<pair<char,char> >* captures; 
     51}; 
    4752 
    4853 
     
    5661  abstractBoard(const abstractBoard& ab); 
    5762  ~abstractBoard(); 
    58   abstractBoard& operator=(const abstractBoard& ab); 
     63  // abstractBoard& operator=(const abstractBoard& ab); 
    5964  void clear(); 
    6065  int play(PyObject* pos, char* color) throw(BoardError); 
     66  int play(int x, int y, char* color) throw(BoardError); 
    6167  void undo(int n=1); 
    6268  void remove(int x, int y); 
     
    6874  void undostack_append_pass(); 
    6975  int len_undostack(); 
    70   abstractBoard copy(const abstractBoard& ab); 
     76  abstractBoard& copy(const abstractBoard& ab); 
    7177 
    7278 private: 
    7379  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); 
    7682  char invert(char); 
    7783}; 
    78  
    79  
    80  
    81  
    82