software-challenge / backend

Server, Client und Spiel-Plugins der Software-Challenge Germany
https://www.software-challenge.de
12 stars 9 forks source link

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

Open maxblan opened 1 year ago

maxblan commented 1 year ago

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.

xeruf commented 1 year ago

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.