aboutsummaryrefslogtreecommitdiff
path: root/higgs.cc
blob: 94da7cd36137322a71125896a1dd8ac0cea33407 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
#include <climits>
#include "higgs.h"

using namespace std;

int getIndex(int x, int y) {
	return x + y*S;
}

int getX(int i) {
	return i % S;
}

int getY(int i) {
	return i / S;
}

void Board::doMove( Move move ) {
	if( move.ndir != -1 ) {
		int new_neutron = pushPiece( neutron, move.ndir );
		square[new_neutron] = -1;
		square[neutron] = 0;
		neutron = new_neutron;
	}
	int new_proton = pushPiece( move.p, move.dir );
	square[new_proton] = square[move.p];
	square[move.p] = 0;
	proton[square[new_proton] - 1] = new_proton;
	move_count++;
}

void Board::undoMove( Move move, int n ) {
	int new_proton = pushPiece( move.p, move.dir, true );
	square[move.p] = square[new_proton];
	square[new_proton] = 0;
	proton[square[move.p]-1] = move.p;
	if( move.ndir != -1 ) {
		square[neutron] = 0;
		square[n] = -1;
		neutron = n;
	}
	move_count--;
}

vector<Move> Board::generateMoves() {
	vector<Move> move_list;
	Move move;
	int n;
	for( move.ndir = 0; move.ndir < 8; move.ndir++ ) {
		n = pushPiece( neutron, move.ndir );
		if( n != neutron ) {
			square[n] = -1;
			square[neutron] = 0;
			for( int i = 0; i < S; i++ ) {
				move.p = proton[i+S*(move_count % 2)];
				for( move.dir = 0; move.dir < 8; move.dir++ ) {
					if( pushPiece( move.p, move.dir ) != move.p )
						move_list.push_back( move );
				}
			}
			square[neutron] = -1;
			square[n] = 0;
		}
	}
	return move_list;
}

int Board::neutronWin() {
	return ( getY(neutron) == 0 ) - ( getY(neutron) == S-1 );
}

int Board::pushPiece( int p, int d, bool e ) {
	int dx = ( d > 0 && d < 4 ) - ( d > 4 && d < 8 );
	int dy = ( d > 2 && d < 6 ) - ( d > 6 || d < 2 );
	int x = getX( p );
	int y = getY( p );
	do {
		x += dx;
		y += dy;
	} while( x >= 0 && x < S && y >= 0 && y < S && square[getIndex(x,y)] == 0 );
	if( !e ) {
		x -= dx;
		y -= dy;
	}
	return getIndex(x,y);
}

void Board::print() {
	for( int i = 0; i < SS; i++ ) {
		if( i % S == 0 )
			cerr << "|";
		if( square[i] == 0 )
			cerr << " .";
		else if( square[i] == -1 )
			cerr << " N";
		else if( square[i] <= S )
			cerr << " +";
		else
			cerr << " -";
		if( i % S == S-1 )
			cerr << " |\n";
	}
}

Board::Board() {
	for( int x = 0; x < S; x++ ) {
		square[proton[x] = getIndex(x,0)] = x + 1;
		square[proton[S+x] = getIndex(x,S-1)] = S + 1 + x;
	}
	for( int i = S; i < (S-1)*S; i++ ) 
		square[i] = 0;
	neutron = getIndex( S / 2, S / 2);
	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;
}