Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawn hints in neighborhood of open fields #477

Merged
merged 3 commits into from
Nov 11, 2023
Merged

Spawn hints in neighborhood of open fields #477

merged 3 commits into from
Nov 11, 2023

Conversation

gboehl
Copy link
Contributor

@gboehl gboehl commented Nov 10, 2023

This is a proposal to address issue #305. As stated there, hints appear at any random field that is not open and has no marker. Hints out in the void which are not in the neighborhood of any open field are never useful. I thus propose to only show hints that, if possible, are adjacent to a disclosed field.

Thanks for creating this nice app!

@lucasnlm
Copy link
Owner

lucasnlm commented Nov 11, 2023

Nice! Thanks for your contribution!

Comment on lines 43 to 51
fun hasUncoveredNeighbor( index: Int ): Boolean {
val numOfUncoveredNeighbors = field.getOrNull(index)?.run {
neighborsIds
.map { field[it] }
.filter { !it.isCovered }
.count()
}
return numOfUncoveredNeighbors!! > 0
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do this, instead:

    private fun hasUncoveredNeighbor(area: Area): Boolean {
        return area.neighborsIds.map { field[it] }.count { !it.isCovered } > 0
    }

Comment on lines 63 to 71
val unrevealedMinesWithUncoveredNeighbors = unrevealedMines.filter {
hasUncoveredNeighbor(it.id)
}
val potentialTargets =
if (unrevealedMinesWithUncoveredNeighbors.count() > 0) {
unrevealedMinesWithUncoveredNeighbors
} else {
unrevealedMines
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that suggestion, you can call here:

        val unrevealedMinesWithUncoveredNeighbors = unrevealedMines.filter(::hasUncoveredNeighbor)
        val potentialTargets = unrevealedMinesWithUncoveredNeighbors.ifEmpty {
            unrevealedMines
        }

val nearestTarget =
if (lastX != null && lastY != null) {
unrevealedMines.filter {
potentialTargets.filter {
(lastX - it.posX).absoluteValue < NEAR_MINE_THRESHOLD &&
(lastY - it.posY).absoluteValue < NEAR_MINE_THRESHOLD
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, I would like to prioritize the mines that are visible in the screen.

@lucasnlm
Copy link
Owner

@gboehl Your suggestion looks functional. Please, let me know if you will implement the suggestions. I can finish for you if you are ok.
Don't bother with Check Code Quality BTW.

@gboehl
Copy link
Contributor Author

gboehl commented Nov 11, 2023

Thanks for the suggestions! I actually have zero previous experience with kotlin, so this is much appreciated. I added your suggestions and the PR should now be good to go.

@gboehl
Copy link
Contributor Author

gboehl commented Nov 11, 2023

This should close #305

@lucasnlm lucasnlm merged commit cc9451d into lucasnlm:main Nov 11, 2023
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants