forked from oyachai/HearthSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardStateFactory.java
211 lines (185 loc) · 7.57 KB
/
BoardStateFactory.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.hearthsim.util;
import java.util.ArrayList;
import java.util.HashMap;
import com.hearthsim.card.Card;
import com.hearthsim.card.Deck;
import com.hearthsim.card.minion.Minion;
import com.hearthsim.exception.HSException;
import com.hearthsim.util.BoardState;
import com.hearthsim.util.HearthTreeNode;
public class BoardStateFactory {
HashMap<BoardState, Integer> boardMap;
final Deck deck_;
boolean lethal_;
public final long maxTime_;
long startTime_;
long curTime_;
double curScore_;
/**
* Constructor
*
* maxThinkTime defaults to 20000 milliseconds (20 seconds)
*/
public BoardStateFactory(Deck deck) {
this(deck, 20000);
}
/**
* Constructor
*
* @param maxThinkTime The maximum amount of time in milliseconds the factory is allowed to spend on generating the simulation tree.
*/
public BoardStateFactory(Deck deck, long maxThinkTime) {
deck_ = deck;
boardMap = new HashMap<BoardState, Integer>(1000000);
lethal_ = false;
startTime_ = System.currentTimeMillis();
curScore_ = -1.e200;
maxTime_ = maxThinkTime;
}
/**
* Recursively generate all possible moves
*
* This function recursively generates all possible moves that can be done starting from a given BoardState.
* The results are stored in a tree structure and returned as a tree of BoardState class.
*
* @param boardStateNode The initial BoardState wrapped in a HearthTreeNode.
* @param deck The deck that the player is playing with.
*
* @return boardStateNode manipulated such that all subsequent actions are children of the original boardStateNode input.
*/
public HearthTreeNode<BoardState> doMoves(HearthTreeNode<BoardState> boardStateNode) throws HSException {
if (System.currentTimeMillis() - startTime_ > maxTime_) {
return null;
}
if (lethal_) {
//if it's lethal, we don't have to do anything ever. Just play the lethal.
return null;
}
if (boardStateNode.numChildren() > 0) {
//If this node already has children, just call doMoves on each of its children.
//This situation can happen, for example, atfer a battle cry
for (HearthTreeNode<BoardState> child : boardStateNode.getChildren()) {
this.doMoves(child);
}
return boardStateNode;
}
if (boardMap.containsKey(boardStateNode.data_)) {
//no need to continue down this path
return null;
} else {
//add it to the list of known states
boardMap.put(boardStateNode.data_, 1);
}
if ((!boardStateNode.data_.isAlive_p0()) || (!boardStateNode.data_.isAlive_p1())) {
//one of the players is dead, no reason to keep playing
if (!boardStateNode.data_.isAlive_p1())
lethal_ = true;
return null;
}
//-----------------------------------------------------------------------------------------
// Use the Hero ability
//-----------------------------------------------------------------------------------------
{
//Case0: Decided not to use the hero ability
BoardState newState = (BoardState)boardStateNode.data_.deepCopy();
newState.getHero_p0().hasBeenUsed(true);
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
{
//Case1: Decided to use the hero ability -- Use it on everthing!
for(int i = 0; i <= boardStateNode.data_.getNumMinions_p0() + 1; ++i) {
HearthTreeNode<BoardState> newState = new HearthTreeNode<BoardState>((BoardState)boardStateNode.data_.deepCopy());
newState = newState.data_.getHero_p0().useHeroAbility(0, 0, i, newState, deck_);
if (newState != null) {
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
}
for(int i = 0; i <= boardStateNode.data_.getNumMinions_p1() + 1; ++i) {
HearthTreeNode<BoardState> newState = new HearthTreeNode<BoardState>((BoardState)boardStateNode.data_.deepCopy());
newState = newState.data_.getHero_p0().useHeroAbility(0, 1, i, newState, deck_);
if (newState != null) {
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
}
}
//-----------------------------------------------------------------------------------------
// Use the cards in the hand
//-----------------------------------------------------------------------------------------
//check to see if all the cards have been used already
boolean allUsed = true;
for (final Card card : boardStateNode.data_.getCards_hand_p0()) {
allUsed = allUsed && card.hasBeenUsed();
}
//the case where I chose not to use any more cards
if (!allUsed) {
BoardState newState = (BoardState)boardStateNode.data_.deepCopy();
for (Card card : newState.getCards_hand_p0()) {
card.hasBeenUsed(true);
}
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
int mana = boardStateNode.data_.getMana_p0();
for (int ic = 0; ic < boardStateNode.data_.getNumCards_hand(); ++ic) {
if (boardStateNode.data_.getCard_hand_p0(ic).getMana() <= mana && !boardStateNode.data_.getCard_hand_p0(ic).hasBeenUsed()) {
//we can use this card! Let's try using it on everything
for(int i = 0; i <= boardStateNode.data_.getNumMinions_p0() + 1; ++i) {
HearthTreeNode<BoardState> newState = new HearthTreeNode<BoardState>((BoardState)boardStateNode.data_.deepCopy());
Card card = newState.data_.getCard_hand_p0(ic);
newState = card.useOn(ic, 0, i, newState, deck_);
if (newState != null) {
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
}
for(int i = 0; i <= boardStateNode.data_.getNumMinions_p1() + 1; ++i) {
HearthTreeNode<BoardState> newState = new HearthTreeNode<BoardState>((BoardState)boardStateNode.data_.deepCopy());
Card card = newState.data_.getCard_hand_p0(ic);
newState = card.useOn(ic, 1, i, newState, deck_);
if (newState != null) {
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
}
}
}
//-----------------------------------------------------------------------------------------
// Attack with the minions on the board
//-----------------------------------------------------------------------------------------
//Use the minions that we have out on the board
//the case where I choose to not use any more minions
boolean allAttacked = true;
for ( final Minion minion : boardStateNode.data_.getMinions_p0()) {
allAttacked = allAttacked && minion.hasAttacked();
}
if (!allAttacked) {
BoardState newState = (BoardState)boardStateNode.data_.deepCopy();
for (Minion minion : newState.getMinions_p0()) {
minion.hasAttacked(true);
}
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
for (int ic = 1; ic < boardStateNode.data_.getNumMinions_p0() + 1; ++ic) {
final Minion minion = boardStateNode.data_.getMinion_p0(ic-1);
if (minion.hasAttacked()) {
continue;
}
ArrayList<Integer> attackable = boardStateNode.data_.getAttackableMinions_p1();
for(final Integer integer : attackable) {
int i = integer.intValue();
HearthTreeNode<BoardState> tempBoard = new HearthTreeNode<BoardState>((BoardState)boardStateNode.data_.deepCopy());
Minion tempMinion = tempBoard.data_.getMinion_p0(ic-1);
HearthTreeNode<BoardState> newState = tempMinion.attack(ic, 1, i, tempBoard, deck_);
if (newState != null) {
HearthTreeNode<BoardState> newNode = boardStateNode.addChild(newState);
newNode = this.doMoves(newNode);
}
}
}
return boardStateNode;
}
}