Skip to content

Commit 158da5c

Browse files
committed
Add a safety check to make sure a player is never rewarded before making a move
1 parent 54ce4e3 commit 158da5c

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

ticTacToe.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import EnvironmentUtilities._
2626
import debug.DebugUtilities._
2727

2828
case class InvalidParameter(message: String) extends Exception(message)
29+
case class InvalidCall(message: String) extends Exception(message)
2930

3031
object Parameters {
3132
// Tabular Parameters
@@ -171,7 +172,7 @@ object TicTacToeLearning {
171172
def iterateGameStep(ticTacToeWorld : TicTacToeWorld, epsilon : Double, frame : Option[JFrame], collectingDataFor : String) : Double = { // If you're collecting data, pass in the string "X" or "O" for the player whose data you're interested in. This method returns 1 if that player won this episode, -1 if it lost, 0 if it was a stalemate, and -2 if the episode hasn't ended.
172173
val agent = ticTacToeWorld.currentPlayer
173174
val environment = ticTacToeWorld.environment
174-
environment.applyAction(agent, ticTacToeWorld.firstPlayer, epsilon)
175+
environment.applyAction(agent, epsilon)
175176
var returnValue = -2.0
176177
if (environment.isEndState()) {
177178
if (environment.playerWon(ticTacToeWorld.agent1) == true) {
@@ -222,9 +223,11 @@ class TicTacToeWorld(_tabular : Boolean, agent1Random : Boolean, agent2Random :
222223
agent1.previousState = List.fill(9){""}
223224
agent1.state = List.fill(9){""}
224225
agent1.movedOnce = false
226+
agent1.newlyOccupiedSpace = 0
225227
agent2.previousState = List.fill(9){""}
226228
agent2.state = List.fill(9){""}
227229
agent2.movedOnce = false
230+
agent2.newlyOccupiedSpace = 0
228231
}
229232

230233
}
@@ -338,6 +341,9 @@ class Agent(_name : String, _tabular : Boolean, _random : Boolean) {
338341
/** The environment calls this to reward the agent for its action. */
339342
def reward(reward : Double) {
340343
if (movedOnce == true && random == false) {
344+
if (newlyOccupiedSpace == 0) {
345+
throw new InvalidCall(s"An attempt was made to give reward to ${name} while its previous action is ${newlyOccupiedSpace}. A player must move at least once to be rewarded for it.")
346+
}
341347
debugPrint(s"Give reward ${reward} to ${name} moving from ${previousState} to ${state}")
342348
if (tabular) {
343349
// Make sure they're initialized

0 commit comments

Comments
 (0)