简体   繁体   中英

Best way to search 2d Array board

I have a 2D array for a game of a 9x9 board for Gomoku. I want to scan the board as quickly and efficiently as possible to find pieces. I need to check if their is a row/col/diagonal of X amount of whites/blacks.

Is the best way just to use a for-loop or is there a better way?

Thanks

This usually depends on the game, how many pieces there are vs cells on the board. If your game had a 1000x1000 board and only 10 pieces it would be terrible to use a nested loop to find them (sparse array). It would be better to track the board pieces using a dedicated structure like a list or hashmap. But in your case a nested loop + 2d array is a good solution. The 2d array also has the benefit of quick random access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM