This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathludii_state_wrapper.h
162 lines (126 loc) · 4.3 KB
/
ludii_state_wrapper.h
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// Author: Dennis Soemers
// - Affiliation: Maastricht University, DKE, Digital Ludeme Project (Ludii
// developer)
// - Github: https://github.com/DennisSoemers/
// - Email: [email protected] (or [email protected])
#pragma once
#include <algorithm>
#include <array>
#include <jni.h>
#include <memory>
#include <string>
#include <vector>
#include "../../core/state.h"
#include "ludii_game_wrapper.h"
namespace Ludii {
// class Action : public ::_Action {
// public:
// Action(int i, int j, int k);
//};
/**
* C++ wrapper around Ludii's "LudiiStateWrapper" class.
*
* This class takes care of calling all the required Java methods from Ludii
* states.
*/
class LudiiStateWrapper : public core::State {
public:
void Initialize();
std::unique_ptr<core::State> clone_() const;
void ApplyAction(const _Action& action);
void DoGoodAction();
public:
/**
* Constructor; calls the LudiiStateWrapper Java constructor
*/
LudiiStateWrapper(int seed, LudiiGameWrapper&& inLudiiGameWrapper);
/**
* Copy constructor; calls the Java copy constructor for LudiiStateWrapper
*
* @param other The LudiiStateWrapper object of which we wish to create a deep
* copy
*/
LudiiStateWrapper(const LudiiStateWrapper& other);
/**
* Destructor
*/
~LudiiStateWrapper();
/**
* @return 2D int array; for every legal move, we have an array of
* length 3 containing [channel, x, y]
*/
std::vector<std::array<int, 3>> LegalMovesTensors() const;
/**
* @return Number of legal moves in current state
*/
int NumLegalMoves() const;
/**
* Applies the n'th legal move in current game state
*/
void ApplyNthMove(const int n) const;
/**
* NOTE: The Java method that we call for this actually first computes
* the array of scores for all players, and then only returns the score
* for the queried player. If we often want to do this inside a loop
* through all players, it'd be more efficient to call a Java method
* that instantly returns the full array once.
*
* @return Score in [-1.0, 1.0] for given player index (starting at 0).
* Will always return 0.0 for non-terminal game states.
*/
double Returns(const int player) const;
/**
* @return True if and only if the current game state is terminal; false
* otherwise.
*/
bool IsTerminal() const;
/**
* @return The current player to move (0 for first, 1 for second, etc.)
*/
int CurrentPlayer() const;
/**
* Calls the Java reset() method on the Java game state object
*/
void Reset() const;
virtual bool isOnePlayerGame() const override;
virtual float getRandomRolloutReward(int player) const override;
LudiiStateWrapper& operator=(LudiiStateWrapper const& other);
private:
void findFeatures();
void findActions();
// We don't want to be accidentally coyping objects of this class
// (without having implemented our own, correct copy constructor or assignment
// operator)
// LudiiStateWrapper& operator=(LudiiStateWrapper const&) = delete;
/** Pointer to our Game wrapper */
std::shared_ptr<LudiiGameWrapper> ludiiGameWrapper;
/** Our object of Java's LudiiStateWrapper type */
jobject ludiiStateWrapperJavaObject;
/** Method ID for the legalMovesTensors() method in Java */
jmethodID legalMovesTensorsMethodID;
/** Method ID for the numLegalMoves() method in Java */
jmethodID numLegalMovesMethodID;
/** Method ID for the applyNthMove() method in Java */
jmethodID applyNthMoveMethodID;
/** Method ID for the returns() method in Java */
jmethodID returnsMethodID;
/** Method ID for the isTerminal() method in Java */
jmethodID isTerminalMethodID;
/** Method ID for the toTensorFlat() method in Java */
jmethodID toTensorFlatMethodID;
/** Method ID for the currentPlayer() method in Java */
jmethodID currentPlayerMethodID;
/** Method ID for the reset() method in Java */
jmethodID resetMethodID;
/** Method ID for the copyFrom() method in Java */
jmethodID copyFromMethodID;
/** Method ID for the getRandomRolloutsReward() method in Java */
jmethodID getRandomRolloutsRewardMethodID;
};
} // namespace Ludii