-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoredState.java
More file actions
151 lines (137 loc) · 3.63 KB
/
Copy pathStoredState.java
File metadata and controls
151 lines (137 loc) · 3.63 KB
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
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.ggp.base.util.statemachine.MachineState;
import org.ggp.base.util.statemachine.Move;
import org.ggp.base.util.statemachine.Role;
import org.ggp.base.util.statemachine.StateMachine;
import org.ggp.base.util.statemachine.exceptions.GoalDefinitionException;
import org.ggp.base.util.statemachine.exceptions.MoveDefinitionException;
import org.ggp.base.util.statemachine.exceptions.TransitionDefinitionException;
public class StoredState {
public StoredState(MachineState state, StateMachine machine, Role role, EnemyMove parent)
throws MoveDefinitionException
{
mGameState = state;
mChildren = null;
mMyRole = role;
mMachine = machine;
mParent = parent;
mVisitCount = 0;
mTotalScore = 0;
}
public MachineState getState()
{
return mGameState;
}
public List<MyMove> getMyLegalMoves() throws MoveDefinitionException
{
if (mChildren == null)
{
calculatedLegalMove();
}
return mChildren;
}
public Role getRole()
{
return mMyRole;
}
public StateMachine getMachine()
{
return mMachine;
}
public void deleteParent()
{
mParent = null;
}
public void UpdateScore(int score)
{
++mVisitCount;
mTotalScore += score;
}
public int getScore()
{
if (mVisitCount == 0)
{
return 50;
}
return mTotalScore / mVisitCount;
}
public int sendProbe(int[] depth) throws GoalDefinitionException, TransitionDefinitionException,
MoveDefinitionException
{
if (mMachine.isTerminal(mGameState))
{
if (mVisitCount == 0)
{
++mVisitCount;
mTotalScore = mMachine.getGoal(mGameState, mMyRole);
}
return mTotalScore;
// System.out.println("Probe found terminal state with score = " +
// score);
} else
{
++mVisitCount;
List<Integer> choices = null;
int choosenMove = 0;
// Des mouvements n'ont jamais été explorés
if (mChildren == null)
{
calculatedLegalMove();
}
if (mVisitCount < mChildren.size())
{
choices = new ArrayList<>(mChildren.size() - mVisitCount);
for (int i = 0; i < mChildren.size(); ++i)
{
if (mChildren.get(i).getVisitCount() == 0)
{
choices.add(i);
}
}
choosenMove = choices.get(new Random().nextInt(choices.size()));
}
// Tous les mouvements ont été explorés au moins une fois
else
{
choosenMove = new Random().nextInt(mChildren.size());
}
int score = mChildren.get(choosenMove).sendProbe(depth);
mTotalScore += score;
return score;
}
}
public int getVisitCount()
{
return mVisitCount;
}
private void calculatedLegalMove() throws MoveDefinitionException
{
List<Move> legalMoves = mMachine.getLegalMoves(mGameState, mMyRole);
if (legalMoves.size() == 0)
{
System.out.println("No legal move found :'(");
System.out.println("No legal move found :'(");
System.out.println("No legal move found :'(");
System.out.println("No legal move found :'(");
System.out.println("No legal move found :'(");
System.out.println("No legal move found :'(");
System.out.println("No legal move found :'(");
System.out.println("No legal move found :'(");
throw new MoveDefinitionException(mGameState, mMyRole);
}
mChildren = new ArrayList<MyMove>(legalMoves.size());
for (Move move : legalMoves)
{
mChildren.add(new MyMove(move, this));
}
}
private int mVisitCount;
private int mTotalScore;
private MachineState mGameState;
private List<MyMove> mChildren;
private StateMachine mMachine;
private Role mMyRole;
private EnemyMove mParent;
}