All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.k0zka.finder4j.backtrack.examples.lab.LabStepFactory.kt Maven / Gradle / Ivy

The newest version!
package com.github.k0zka.finder4j.backtrack.examples.lab

import com.github.k0zka.finder4j.backtrack.StepFactory
import java.util.ArrayList

class LabStepFactory : StepFactory {

	override fun produce(state: LabState): List {
		val steps = ArrayList()
		// produce up, down, left and right steps in case that position is not
		val route = state.route
		val position = route[route.size - 1]
		if (position.x > 0) {
			addIfNotpresent(route, steps, Position(position.x - 1,
												   position.y), state)
		}
		if (position.x < state.lab.size - 1) {
			addIfNotpresent(route, steps, Position(position.x + 1,
												   position.y), state)
		}
		if (position.y > 0) {
			addIfNotpresent(route, steps, Position(position.x,
												   position.y - 1), state)
		}
		if (position.y < state.lab[0].size - 1) {
			addIfNotpresent(route, steps, Position(position.x,
												   position.y + 1), state)
		}
		return steps
	}

	fun addIfNotpresent(route: List, steps: MutableList,
						position: Position, state: LabState) {
		val targetObj = state.lab[position.x][position.y]
		if (!route.contains(position) && LabObject.Wall != targetObj) {
			steps.add(LabStep(position))
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy