-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement first test search, just a simple num game and fix all the i…
…ssues that were found on the way
- Loading branch information
Showing
9 changed files
with
68 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package io.dkozak.search.model | ||
|
||
class EngineConfiguration | ||
class EngineConfiguration<NodeType : Node> | ||
( | ||
val initialState: Node, | ||
val searchQueue: SearchQueue, | ||
val expandNode: (Node) -> List<Node>, | ||
val nodeFilter: (Node) -> Boolean, | ||
val isFinalState: (Node) -> Boolean | ||
val initialState: NodeType, | ||
val searchQueue: SearchQueue<NodeType>, | ||
val expandNode: (NodeType) -> List<NodeType>, | ||
val nodeFilter: (NodeType) -> Boolean, | ||
val isFinalState: (NodeType) -> Boolean | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.dkozak.search.model | ||
|
||
import java.util.* | ||
|
||
class QueueBasedSearchQueue<NodeType : Node> : SearchQueue<NodeType> { | ||
private val queue: Deque<NodeType> = LinkedList() | ||
|
||
override fun next(): NodeType = queue.poll() | ||
|
||
override fun addNodes(nodes: List<NodeType>) = nodes.forEach { queue.addLast(it) } | ||
|
||
override fun isEmpty(): Boolean = queue.isEmpty() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package io.dkozak.search.model | ||
|
||
interface SearchQueue { | ||
fun next(): Node | ||
fun addNodes(nodes: List<Node>) | ||
interface SearchQueue<NodeType : Node> { | ||
fun next(): NodeType | ||
fun addNodes(nodes: List<NodeType>) | ||
fun isEmpty(): Boolean | ||
|
||
fun addNode(node: Node) = addNodes(listOf(node)) | ||
fun addNode(node: NodeType) = addNodes(listOf(node)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.dkozak.search.engine | ||
|
||
import io.dkozak.search.model.EngineConfiguration | ||
import io.dkozak.search.model.Node | ||
import io.dkozak.search.model.QueueBasedSearchQueue | ||
import org.junit.jupiter.api.Test | ||
|
||
data class NumNode(val num: Int) : Node | ||
|
||
class EngineTest { | ||
|
||
@Test | ||
fun `simpleNumberGame solution should be found`() { | ||
val configuration = EngineConfiguration( | ||
initialState = NumNode(1), | ||
searchQueue = QueueBasedSearchQueue(), | ||
expandNode = { node -> listOf(1, 2, 3, 4, 5).map { NumNode(it + node.num) } }, | ||
nodeFilter = { node -> true }, | ||
isFinalState = { node -> node.num == 42 } | ||
) | ||
val result = search(configuration, { msg -> println(msg) }) | ||
println("Found result $result") | ||
} | ||
} |