The new state when applying a given action

The returned state can be:

  • a completely new state,
  • an existing state,
  • a new state but equals to an existing state in this case, ensure that the == and hash method are correctly implemented.

Remember, this method should not modify its parameters.

REQUIRE: actions(state).has(action)

Property definitions

ai $ SearchProblem :: apply_action
	# The new state when applying a given action
	#
	# The returned state can be:
	# * a completely new state,
	# * an existing state,
	# * a new state but equals to an existing state
	#   in this case, ensure that the `==` and `hash` method
	#   are correctly implemented.
	#
	# Remember, this method should not modify its parameters.
	#
	# REQUIRE: `actions(state).has(action)`
	fun apply_action(state:S, action:A): S is abstract
lib/ai/search.nit:71,2--83,51

ai $ PuzzleProblem :: apply_action
	# Return the state where the tile at hole+action has moved
	redef fun apply_action(state, action)
	do
		# Copy the state
		var res = new ArrayCmp[Tile].with_capacity(state.length)
		res.add_all(state)

		# Get the hole and the tile next to it
		var h = get_hole(res)
		var t = h + action

		# Move (by swapping the tile and the hole)
		res[h] = res[t]
		res[t] = hole

		return res
	end
lib/ai/examples/puzzle.nit:111,2--127,4