From e8e24f187f451d51551a1db76321e03efe94cc9d Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 26 Apr 2015 21:39:04 +0200 Subject: Try to fix all kinds of shit --- .gitignore | 1 + Makefile | 5 +++- gluon.cc | 12 ++++++--- higgs.cc | 9 +++++++ higgs.h | 1 + neutrino.cc | 2 +- photon.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ randino.cpp | 76 ++++++++++++++++++++++++++++++++++++++++-------------- 8 files changed, 167 insertions(+), 25 deletions(-) create mode 100755 photon.cc diff --git a/.gitignore b/.gitignore index 2800b41..ccdaa2a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ playerlogs/ gluon neutrino randino +photon *.o diff --git a/Makefile b/Makefile index 4476c41..76abc5f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: all -all: neutrino randino gluon +all: neutrino randino gluon photon neutrino: neutrino.cc higgs.o g++ -Wall -std=c++11 -O2 -o neutrino neutrino.cc higgs.o @@ -8,6 +8,9 @@ neutrino: neutrino.cc higgs.o gluon: gluon.cc higgs.o g++ -Wall -std=c++11 -O2 -o gluon gluon.cc higgs.o +photon: photon.cc higgs.o + g++ -Wall -std=c++11 -O2 -o photon photon.cc higgs.o + randino: randino.cpp g++ -Wall -std=c++11 -O2 -o randino randino.cpp diff --git a/gluon.cc b/gluon.cc index 86b78bd..363fe4b 100755 --- a/gluon.cc +++ b/gluon.cc @@ -7,6 +7,13 @@ using namespace std; +Move random( Board& board ) { + vector move_list = board.generateMoves(); + if( move_list.size() == 0 ) + return {-1,-1,-1}; + return move_list[ rand() % move_list.size() ]; +} + Move importMove() { Move move; cin >> move.ndir >> move.p >> move.dir; @@ -27,7 +34,7 @@ int main() { if( input == "go" ) { //board.print(); move.ndir = -1; - move.p = S-1; + move.p = S-2; move.dir = 4; board.doMove( move ); exportMove( move ); @@ -37,8 +44,7 @@ int main() { board.doMove( importMove() ); //board.print(); - vector move_list = board.generateMoves(); - move = move_list[ rand() % move_list.size() ]; + move = random( board ); board.doMove( move ); exportMove( move ); } diff --git a/higgs.cc b/higgs.cc index 237346a..94da7cd 100755 --- a/higgs.cc +++ b/higgs.cc @@ -115,3 +115,12 @@ Board::Board() { square[neutron] = -1; move_count = 0; } + +Board::Board( Board& board ) { + for( int i = 0; i < SS; i++ ) + square[i] = board.square[i]; + for( int i = 0; i < 2*S; i++ ) + proton[i] = board.proton[i]; + neutron = board.neutron; + move_count = board.move_count; +} diff --git a/higgs.h b/higgs.h index c201955..504eae6 100755 --- a/higgs.h +++ b/higgs.h @@ -28,4 +28,5 @@ public: int pushPiece( int p, int d, bool e = false ); void print(); Board(); + Board( Board& ); }; diff --git a/neutrino.cc b/neutrino.cc index 2d8edac..67d8035 100755 --- a/neutrino.cc +++ b/neutrino.cc @@ -69,7 +69,7 @@ int main() { if( input == "go" ) { //board.print(); move.ndir = -1; - move.p = S-1; + move.p = S-2; move.dir = 4; board.doMove( move ); exportMove( move ); diff --git a/photon.cc b/photon.cc new file mode 100755 index 0000000..977a50b --- /dev/null +++ b/photon.cc @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include "higgs.h" + +using namespace std; + +const int MONTE_CARLO_COUNT = 256; + +Move random( Board& board ) { + vector move_list = board.generateMoves(); + if( move_list.size() == 0 ) + return {-1,-1,-1}; + return move_list[ rand() % move_list.size() ]; +} + +Move monteCarlo( Board& board ) { + vector move_list; + int win_max = INT_MIN; + int win_count; + int neutron = board.neutron; + Move best_move; + Move random_move; + for( Move move: move_list ) { + win_count = 0; + board.doMove( move ); + for( int i = 0; i < MONTE_CARLO_COUNT; i++ ) { + Board playground( board ); + while( !playground.neutronWin() ) { + random_move = random( playground ); + if( random_move.ndir == -1 ) + break; + playground.doMove( random_move ); + } + if( playground.neutronWin() != 0 ) + win_count += (1-2*(board.move_count%2))*playground.neutronWin() > 0; + else if( random_move.ndir == -1 ) + win_count += ( playground.move_count%2 == 0 ); + } + board.undoMove( move, neutron ); + cerr << move.ndir << " " << win_count << endl;; + if( win_count > win_max ) { + win_max = win_count; + best_move = move; + } + } + return best_move; +} + +Move importMove() { + Move move; + cin >> move.ndir >> move.p >> move.dir; + return move; +} + +void exportMove( Move move ) { + cout << move.ndir << " " << move.p << " " << move.dir << endl; +} + +int main() { + Board board; + Move move; + string input; + srand( time( NULL ) ); + + cin >> input; + if( input == "go" ) { + board.print(); + move.ndir = -1; + move.p = S-2; + move.dir = 4; + board.doMove( move ); + exportMove( move ); + } + while( 1 ) { + board.print(); + board.doMove( importMove() ); + + board.print(); + move = monteCarlo( board ); + board.doMove( move ); + exportMove( move ); + } +} \ No newline at end of file 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=0;y--){ + for(x=S-1;x>=0;x--)cerr< 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>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<