aboutsummaryrefslogtreecommitdiff
path: root/photon.cc
blob: 977a50bedde2cd9d00d9807730d2ef3966521839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 );
	}
}