gapophustu / 4D-Minesweeper

A 4D Minesweeper game written in godot
Mozilla Public License 2.0
9 stars 5 forks source link

Performance #4

Open bobderbobs opened 1 year ago

bobderbobs commented 1 year ago

First of all I want to say that this game is great at imaging a 4 dimensional game of minesweeper especially how yoou show to the player wich fields are nearby the mouse.

But performance is a huge issue (more than 7^4 is not really possible and 7^4 already takes extremely long to start). I am not that good at writing code. I rather read the code and search for optimisations.

I was able to find the following: 1: I think a great portion of why the game takes so long to start is the amount of calculations to get the numbers for the fields. This can be improved by making smarter calculations (in the sense of use what has already been calculated tto reduce the total amount of calculations). You are adding all the neighbours with mines up for !EVERY! field. That are 80n^4 calculations (not considering the borders) you can do better by first going after the first dimension: for field i use the calculation of field i-1 substract 1 if at i-2 is a mine, add 1 if i+1 is a mine. You got numbers for groups of 3 in 2n^4 calculations now onto groups of 9: basically the same procedure as before but with the next dimension and subtract/add the numbers of the groups of 3. This takes 2n^3 calculations. the next step for groups of 27 takes 2n^2 calculations and the last step takes 2*n calculations. This is again not considering borders but with small n the numbers of calculations are not a problem and with big n the affect of the borders is extremly low.

2: I also noticed that there are a lot of calculations when uncovering a lot of zeros. but i dont really understand how the game handles that. Maybe consider an update queue?