aboutsummaryrefslogtreecommitdiff
path: root/neutrino.cc
diff options
context:
space:
mode:
Diffstat (limited to 'neutrino.cc')
-rwxr-xr-xneutrino.cc86
1 files changed, 86 insertions, 0 deletions
diff --git a/neutrino.cc b/neutrino.cc
new file mode 100755
index 0000000..2d8edac
--- /dev/null
+++ b/neutrino.cc
@@ -0,0 +1,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-1;
+ 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 );
+ }
+} \ No newline at end of file