-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveCalc.java
127 lines (112 loc) · 2.77 KB
/
MoveCalc.java
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
127
import java.util.NoSuchElementException;
class MoveCalc {
private Ataxx game;
private AtaxxMove lastAtaxxMove = null;
private AtaxxMove nextAtaxxMove = null;
public boolean hasMoreElements(){
try{
calcNextAtaxxMove(false);
}catch ( NoSuchElementException nsee ){
return false;
}
return nextAtaxxMove != null;
}
public Object nextElement(){
return (Object)nextAtaxxMove();
}
private boolean infoDirty = true;
private int j= 0, i =0;
private int h = -2, v =-2;
private boolean adjacentConsidered = false;
private void calcNextAtaxxMove(boolean makeDirty) throws NoSuchElementException {
if( !infoDirty ){
infoDirty = makeDirty;
return;
}
infoDirty = makeDirty;
lastAtaxxMove = nextAtaxxMove;
if( lastAtaxxMove != null && lastAtaxxMove.isPass )
{
nextAtaxxMove = null;
return;
}
int height = Constants.HEIGHT;
int width = Constants.WIDTH;
for( ; j < height; j++)
{
for( ; i < width; i++, adjacentConsidered = false)
{
if( game.board[i][j] != 0 )
continue;
// skip adjacent AtaxxMove if already considered
if( adjacentConsidered == false ){
adjacentConsidered = true;
// find adjacent AtaxxMove
for( int h = -1; h <= 1; h++)
for( int v = -1; v <= 1; v++){
int col = i + h;
int row = j + v;
if( col < 0 || col >= width ||
row < 0 || row >= height)
continue;
if( game.board[i][j] == game.playerToGo ){
nextAtaxxMove = new AtaxxMove(game.playerToGo,col, row,i, j);
return;
}
}
}
if( null == lastAtaxxMove){
h = v = -3 ;
}
else{
if( lastAtaxxMove.moveDist <3 )
{
h = v = -2;
}
else{
if( lastAtaxxMove.iTo == i &&
lastAtaxxMove.jTo == j )
{
v++;
}
else{
h = v = -3;
}
}
}
// find next jump AtaxxMove
for( ; h <= 3; h++ ) {
for( ; v <= 3; v++ ){
if( Math.max(Math.abs(h),Math.abs(v)) < 3 )
continue;
int col = i + h;
int row = j + v;
if( col < 0 || col >= width ||
row < 0 || row >= height)
continue;
if( game.board[col][row] == game.playerToGo ){
nextAtaxxMove = new AtaxxMove(game.playerToGo,
col, row,
i, j);
return;
}
}
v = -3;
}
}
i =0;
adjacentConsidered = false;
}
if( lastAtaxxMove == null )
nextAtaxxMove = AtaxxMove.getNewPassAtaxxMove(game.playerToGo);
else
nextAtaxxMove = null;
}
public AtaxxMove nextAtaxxMove() throws NoSuchElementException{
calcNextAtaxxMove(true);
return nextAtaxxMove;
}
MoveCalc(Ataxx g){
game = new Ataxx( g );
}
}