From 47150bfc22dee2f5944e032eeaccdbe902cb42e5 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 27 Apr 2015 14:22:02 +0200 Subject: Start working on charm --- .gitignore | 1 + charm.cpp | 233 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fullcomp.sh | 2 +- fullcompMT.sh | 2 +- ipow.h | 76 +++++++++++++++++++ 5 files changed, 312 insertions(+), 2 deletions(-) create mode 100644 charm.cpp create mode 100644 ipow.h diff --git a/.gitignore b/.gitignore index 619517f..d8d70d0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ gluon neutrino randino photon +charm viewcompetition diff --git a/charm.cpp b/charm.cpp new file mode 100644 index 0000000..1434e8c --- /dev/null +++ b/charm.cpp @@ -0,0 +1,233 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ipow.h" + +#define S (5) + +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}}; + +struct Move{ + int neudir,from,dir; + string str(void){ + stringstream ss; + ss<=S||y2<0||y2>=S||grid[S*y2+x2]!=0)return S*y+x; + x=x2; + y=y2; + } + } + bool move(int at,int dir,int _newatcached=-1){ + int newat=_newatcached==-1?moved(at,dir):_newatcached; + if(newat==at)return false; + grid[newat]=grid[at]; + grid[at]=0; + if(grid[newat]==3)neuidx=newat; + return true; + } + void undomove(int at,int dir){ + int movedidx=moved(at,dir); + movedidx+=S*index_deltas[dir][1]+index_deltas[dir][0]; + grid[at]=grid[movedidx]; + grid[movedidx]=0; + if(grid[at]==3)neuidx=at; + } + 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); + return false; + } + if(!move(mv.from,mv.dir)){ + //undomove(mv.from,mv.dir); + undomove(oldneuidx,mv.neudir); + return false; + } + undomove(mv.from,mv.dir); + 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< &a,const pair &b){ + return a.second>b.second; +} + +Move calculate_move(Board &bd){ + vector> poss; + Move m; + int nd,fr,d; + int newneu; + int score,oldneuidx; + //bd.print(); + for(nd=0;nd<8;nd++){ + newneu=bd.moved(bd.neuidx,nd); + if(newneu==bd.neuidx)continue; + for(fr=0;fr>m.neudir>>m.from>>m.dir; + 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(ostream &s,Move m){ + if(should_flip_board){ + s< 1 + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + }; + + uint64_t result = 1; + + switch (highest_bit_set[exp]) { + case 255: // we use 255 as an overflow marker and return 0 on overflow/underflow + if (base == 1) { + return 1; + } + + if (base == -1) { + return 1 - 2 * (exp & 1); + } + + return 0; + case 6: + if (exp & 1) result *= base; + exp >>= 1; + base *= base; + case 5: + if (exp & 1) result *= base; + exp >>= 1; + base *= base; + case 4: + if (exp & 1) result *= base; + exp >>= 1; + base *= base; + case 3: + if (exp & 1) result *= base; + exp >>= 1; + base *= base; + case 2: + if (exp & 1) result *= base; + exp >>= 1; + base *= base; + case 1: + if (exp & 1) result *= base; + default: + return result; + } +} -- cgit v1.2.3-54-g00ecf