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

feat(Board): generate random fish distribution on game board #420

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

maxblan
Copy link
Contributor

@maxblan maxblan commented May 5, 2023

The whole thing is just to serve as an approach to build on. Accordingly, this is not tested, just an idea.

This commit refactors the generateFields() method in the companion object to produce a better distribution of fish on the game board.

Previously, the method randomly generated the fish placement with a maximum of 5 holes per side, and a minimum of 5 1-fish fields per side. The remainingFish variable was used to keep track of the number of fish left to place, and maxholes was used to ensure the maximum number of holes was not exceeded.

The new implementation replaces the previous algorithm with a weighted probability distribution. The method now uses a list of Field objects and their corresponding probabilities to determine the number of fish that should be placed on each field.

To ensure that the sum of the probabilities is 1, an IllegalArgumentException is thrown if the weighted sum does not equal 1. The method then determines the range of possible fish values that can be placed on the field, based on the number of fields remaining to be filled and the sum of fish already placed.

The method then generates a random float between 0 and 1 and selects the corresponding fish value based on the probability distribution. The selected value is then assigned to the current field.

Finally, the method shuffles the board to ensure that the placement of fish is random, and returns the resulting game board.

Overall, this refactor results in a more evenly distributed placement of fish on the board, and eliminates the likelihood of having less than 8 1-fish fields. It also adds a constant distribution of (BoardSizeBoardSize2) so 128 Fish on the Board.

This commit refactors the generateFields() method in the companion object to produce a better distribution of fish on the game board.

Previously, the method randomly generated the fish placement with a maximum of 5 holes per side, and a minimum of 5 1-fish fields per side. The remainingFish variable was used to keep track of the number of fish left to place, and maxholes was used to ensure the maximum number of holes was not exceeded.

The new implementation replaces the previous algorithm with a weighted probability distribution. The method now uses a list of Field objects and their corresponding probabilities to determine the number of fish that should be placed on each field.

To ensure that the sum of the probabilities is 1, an IllegalArgumentException is thrown if the weighted sum does not equal 1. The method then determines the range of possible fish values that can be placed on the field, based on the number of fields remaining to be filled and the sum of fish already placed.

The method then generates a random float between 0 and 1 and selects the corresponding fish value based on the probability distribution. The selected value is then assigned to the current field.

Finally, the method shuffles the board to ensure that the placement of fish is random, and returns the resulting game board.

Overall, this refactor results in a more evenly distributed placement of fish on the board, and eliminates the likelihood of having less than 8 1-fish fields. It also adds a constant distribution of (BoardSize*BoardSize*2) so 128 Fish on the Board.
@maxblan maxblan requested a review from xeruf as a code owner May 5, 2023 20:22
@xeruf
Copy link
Member

xeruf commented Jul 4, 2023

Yeah that was the original concept, but I never got around to it.
Code looks quite complicated though.
Maybe we can merge this for the next iteration?

Also, do not apply unrelated reformattings.
The codestyle is unfortunately only codified for Intellij IDEA so far, feel free to add a .editorconfig or the like.

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.

3 participants