I have written an Android app. It is a simple version of the old board game Connect 4. Currently my “Board” object holds the counters which have been places on the board. There are 2 players to this game.
The Board class comprises of 7 ArrayList objects, each one represents a column on the board.(the board is 7 across x 6 upwards). So for example when the red player clicks on the third column on the screen I call
boardObj.addCounter("red", column);
and inside the board object in the addCounter() function it does something like
column3.add(playerColor);
so the ArrayList column3 gets another item added and so forth.
My question is – is there a better data structure I can use as opposed to using 7 ArrayLists? I noticed that the function which checks whether there is a win, which is called after a counter is added to the board, is becoming quite an expensive function in terms of CPU. What structure would allow me to get and set the data and run my checkwin() algorithm with the best performance?
You have a 7×6 board, and you need to test for elements on the same row, column or diagonal.
A 2-dimensional array is the most obvious choice. I would use that.