diff options
Diffstat (limited to 'randino.cpp')
-rw-r--r-- | randino.cpp | 76 |
1 files changed, 56 insertions, 20 deletions
diff --git a/randino.cpp b/randino.cpp index e9b5abb..bb3d917 100644 --- a/randino.cpp +++ b/randino.cpp @@ -8,10 +8,17 @@ using namespace std; +bool should_flip_board=false; + +struct Move; +class Board; + +Move protocol_get_move(void); +void protocol_put_move(ostream &s,Move m); + int index_deltas[8][2]={{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}}; -class Move{ -public: +struct Move{ int neudir,from,dir; string str(void){ stringstream ss; @@ -24,7 +31,7 @@ class Board{ public: int grid[S*S]; int neuidx; - Board(void):neuidx(0){ + Board(void):neuidx(S*(S/2)+S/2){ memset(grid,0,S*S*sizeof(int)); grid[S*(S/2)+S/2]=3; for(int i=0;i<S;i++){ @@ -59,12 +66,15 @@ public: } bool isvalid(Move mv){ int oldneuidx=neuidx; + cerr<<"Called isvalid with move "; + protocol_put_move(cerr,mv); + //print(); if(!move(oldneuidx,mv.neudir)){ - undomove(oldneuidx,mv.neudir); + //undomove(oldneuidx,mv.neudir); return false; } if(!move(mv.from,mv.dir)){ - undomove(mv.from,mv.dir); + //undomove(mv.from,mv.dir); undomove(oldneuidx,mv.neudir); return false; } @@ -72,6 +82,20 @@ public: undomove(oldneuidx,mv.neudir); return true; } + void print(void){ + int x,y; + if(should_flip_board){ + for(y=S-1;y>=0;y--){ + for(x=S-1;x>=0;x--)cerr<<grid[S*y+x]<<' '; + cerr<<endl; + } + } else { + for(y=0;y<S;y++){ + for(x=0;x<S;x++)cerr<<grid[S*y+x]<<' '; + cerr<<endl; + } + } + } }; @@ -79,19 +103,23 @@ Move calculate_move(Board &bd){ vector<Move> poss; Move m; int nd,fr,d; - int newneu,newidx; + int newneu; + //bd.print(); for(nd=0;nd<8;nd++){ newneu=bd.moved(bd.neuidx,nd); if(newneu==bd.neuidx)continue; for(fr=0;fr<S*S;fr++){ if(bd.grid[fr]!=1)continue; + //cerr<<"fr="<<fr<<endl; for(d=0;d<8;d++){ - newidx=bd.moved(fr,d); - if(newidx==fr)continue; m.neudir=nd; m.from=fr; m.dir=d; - poss.push_back(m); + if(bd.isvalid(m)){ + cerr<<"push: "; + protocol_put_move(cerr,m); + poss.push_back(m); + } } } } @@ -99,30 +127,33 @@ Move calculate_move(Board &bd){ } -bool should_flip_board=false; - inline int flip_index(int idx){ return S*(S-1-idx/S)+S-1-idx%S; } inline int flip_dir(int dir){ - return (dir+4)%8; + return dir==-1?-1:(dir+4)%8; } Move protocol_get_move(void){ Move m; - cin>>m.neudir>>m.from>>m.dir; + char c=cin.peek(); + if(c=='q'||c=='Q')exit(0); if(should_flip_board){ + m.neudir=flip_dir(m.neudir); m.from=flip_index(m.from); m.dir=flip_dir(m.dir); } + cerr<<"Got: "; + protocol_put_move(cerr,m); + cin.ignore(1024,'\n'); return m; } -void protocol_put_move(Move m){ +void protocol_put_move(ostream &s,Move m){ if(should_flip_board){ - cout<<m.neudir<<' '<<flip_index(m.from)<<' '<<flip_dir(m.dir)<<endl; + s<<flip_dir(m.neudir)<<' '<<flip_index(m.from)<<' '<<flip_dir(m.dir)<<endl; } else { - cout<<m.neudir<<' '<<m.from<<' '<<m.dir<<endl; + s<<m.neudir<<' '<<m.from<<' '<<m.dir<<endl; } } @@ -133,17 +164,19 @@ int main(void){ struct timeval tv; gettimeofday(&tv,NULL); cerr<<"seed="<<(1000000*tv.tv_sec+tv.tv_usec)<<endl; - srand(1000000*tv.tv_sec+tv.tv_usec); + //srand(1000000*tv.tv_sec+tv.tv_usec); + srand(1430075262226814); getline(cin,line); if(line=="go"){ should_flip_board=false; mv.neudir=-1; mv.from=0; mv.dir=4; - protocol_put_move(mv); + protocol_put_move(cout,mv); } else { if(line!="nogo")cerr<<"no0b "<<line<<" not in (go,nogo)"<<endl; should_flip_board=true; mv=protocol_get_move(); bd.move(mv.from,mv.dir); + bd.print(); mv=calculate_move(bd); if(!bd.isvalid(mv)){ cerr<<"calculate_move gave invalid move "<<mv.str()<<endl; @@ -151,11 +184,14 @@ int main(void){ } bd.move(bd.neuidx,mv.neudir); bd.move(mv.from,mv.dir); - protocol_put_move(mv); + protocol_put_move(cout,mv); } while(true){ + bd.print(); mv=protocol_get_move(); + bd.move(bd.neuidx,mv.neudir); bd.move(mv.from,mv.dir); + bd.print(); mv=calculate_move(bd); if(!bd.isvalid(mv)){ cerr<<"calculate_move gave invalid move "<<mv.str()<<endl; @@ -163,7 +199,7 @@ int main(void){ } bd.move(bd.neuidx,mv.neudir); bd.move(mv.from,mv.dir); - protocol_put_move(mv); + protocol_put_move(cout,mv); } return 0; } |