aboutsummaryrefslogtreecommitdiff
path: root/neutrino.cc
blob: 67d8035c2d7b1c525c571cdc1e1dcc65584fd42d (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 "higgs.h"

using namespace std;


int evaluate( const Board& board ) {
	int score = 0;
	for( int i = 0; i < S; ++i ) {
		score += getY( board.proton[i] )*getY( board.proton[i] );
		score -= (S - 1 - getY( board.proton[i+S] ))*(S-1-getY( board.proton[i+S] )); 
	}
	score += S*(getY( board.neutron ) - S/2);
	return (board.move_count % 2 == 0) ? score : -score;
}

int minimax( Board& board, Move& best_move, int depth, int alpha = INT_MIN/2, int beta = INT_MAX/2 ) {
	if( board.neutronWin() != 0	) {
		if( board.neutronWin()*(2*( board.move_count % 2 ) - 1 ) > 0 )
			return INT_MIN/2;
		else
			return INT_MAX/2;
	} else if( depth == 0 )
		return evaluate( board );
	else {
		int global_score = INT_MIN;
		int local_score;
		int prev_neutron;
		Move best_move_recursive;
		vector<Move> move_list = board.generateMoves();
		if( move_list.size() == 0 )
			return INT_MAX/2;
		for( Move& move : move_list ) {
			prev_neutron = board.neutron;
			board.doMove( move );
			local_score = -minimax( board, best_move_recursive, depth-1, -beta, -alpha );
			board.undoMove( move, prev_neutron );
			if( local_score > global_score ) {
				global_score = local_score;
				best_move = move; 
			}
			if( local_score > alpha )
				alpha = local_score;
			if( beta <= alpha )
				break;
		}
		return global_score;
	}
}

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;

	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();
		minimax( board, move, 5 );
		board.doMove( move );
		exportMove( move );
	}
}