gp icon indicating copy to clipboard operation
gp copied to clipboard

implement O(1) map for game

Open HKhademian opened this issue 3 years ago • 1 comments

Hi

Recently I watch YouTube series about this project, and have some idea to optimize it.

I saw how the game stores map data in separated arrays , which cause multiple source of truth issue. also it's not efficient to lookup agents in the map.

To rapid solve this issue , I introduce env_map which holds single source of truth about games map. For now each tile is an integer , 0 means EMPTY , 1 means WALL , 2 means FOOD , and other keeps agents index (starting from 3).

by this approach each query about map (for any query type) is an array access and takes just O(1) .

Note: I didn't remove Agent.pos for now but they can easily be removed.

HKhademian avatar Feb 02 '22 18:02 HKhademian

Better solution is to create a Token-like type which hold env_type and operand. for Agent type the operand means array index and for other types it discards. this type takes double amount of memory , but make whole solution more readable and expandable for new types of environment.

HKhademian avatar Feb 02 '22 18:02 HKhademian