aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <hallo@tomsmeding.nl>2015-04-26 21:39:04 +0200
committertomsmeding <hallo@tomsmeding.nl>2015-04-26 21:39:04 +0200
commite8e24f187f451d51551a1db76321e03efe94cc9d (patch)
treec294a79de970a662d3b89e4f5f86f97a92a2c62a
parentf3b7df07e4fd136521186baee86faa822dc68d16 (diff)
Try to fix all kinds of shit
-rw-r--r--.gitignore1
-rw-r--r--Makefile5
-rwxr-xr-xgluon.cc12
-rwxr-xr-xhiggs.cc9
-rwxr-xr-xhiggs.h1
-rwxr-xr-xneutrino.cc2
-rwxr-xr-xphoton.cc86
-rw-r--r--randino.cpp76
8 files changed, 167 insertions, 25 deletions
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> 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> 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 <iostream>
+#include <vector>
+#include <climits>
+#include <cstdlib>
+#include <ctime>
+#include "higgs.h"
+
+using namespace std;
+
+const int MONTE_CARLO_COUNT = 256;
+
+Move random( Board& board ) {
+ vector<Move> 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> 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<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;
}