-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jumps.java
90 lines (78 loc) · 2.67 KB
/
Jumps.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
import java.io.Serializable;
import java.util.Collection;
import java.util.Hashtable;
/*
Board Class needs to implement this jump class just handles counting, insertions and deletions
check the lega squares that are one away(row+1, col+1 ; row+1, col-1...) to see if a jump has been created
with the piece at the new position as the captured piece and for the legal squares two away
check if a jump into the current one was removed by placing a piece there and check the square
itself to see if a jump was created. And then do the same for the old sport but opposite, check
the legal squares 1 away to see if a jump was removed(because the capturing piece is now and
empty location) and check the legal squares two away to see if a jump is now
available(because it's now an empty location).
*/
public class Jumps implements Serializable
{
private static final long serialVersionUID = 1L;
private int whiteJumpCount;
private int blackJumpCount;
private Hashtable<Double, Pair> jumpTable;
//private ArrayList<Move> movesStart;
public Jumps()
{
whiteJumpCount = 0;
blackJumpCount = 0;
jumpTable = new Hashtable<Double, Pair>();
}
public Jumps(Jumps jumps)
{
this.whiteJumpCount = jumps.whiteJumpCount;
this.blackJumpCount = jumps.blackJumpCount;
this.jumpTable = new Hashtable<Double, Pair>();
this.jumpTable.putAll(jumps.jumpTable);
}
public double hashFunction(Move m, Move pM, int color)
{
return ( ( (((m.getRow()*23) + (m.getColumn()*13) * (pM.getRow()*19) + (pM.getColumn()*7))*color)
/ (m.getRow()+m.getColumn()+pM.getRow()+pM.getColumn()) ) );
}
public int getWhiteJumpCount()
{
return whiteJumpCount;
}
public int getBlackJumpCount()
{
return blackJumpCount;
}
public Collection<Pair> getJump()
{
Hashtable<Double,Pair> temp = new Hashtable<Double,Pair>(jumpTable);
return temp.values();
}
public void insertJump(Move m, Move pM, int color)
{
double temp = hashFunction(m,pM,color);
if(temp != 0 && !jumpTable.containsKey(temp))
{
Pair p = new Pair(m,pM);
jumpTable.put(temp, p);
//jumpTable.put(temp, color);
if(color == Board.WHITE || color == Board.WHITEKING)
whiteJumpCount++;
else //if(color == Board.BLACK || color == Board.BLACKKING)
blackJumpCount++;
}
}
public void removeJumpIfExists(Move m, Move pM, int color)
{
double temp = hashFunction(m,pM,color);
if(temp != 0 && jumpTable.containsKey(temp))
{
jumpTable.remove(temp);
if(color == Board.WHITE || color == Board.WHITEKING)
whiteJumpCount--;
else //if(color == Board.BLACK || color == Board.BLACKKING)
blackJumpCount--;
}
}
}