I am writing a bot that will play the simplest game. At the beginning of each turn, the playing field is read from the file, which is an 8x8 matrix. There are 4 players in the game, their chips, respectively, are marked with numbers 1, 2, 3, 4 and are placed on the playing field. It is necessary to find all the chips of each player and somehow store the location (coordinates) of each chip. How can this be done? I tried to memorize coordinates in a regular array and in a vector , but this is not very convenient.
- What exactly do you want to achieve? How less space, faster operation (what?), Something else? - Harry
- When all the player's pieces are found, the random one is selected and walks randomly. It is necessary that you can easily get access to the coordinates of the selected chips. - Yakimov Herman
2 answers
There are two storage options - storing the state of the game (board), and chips on it (for example, an array
int board[8][8]; in which empty fields are marked with zeros, and non-empty fields - the number of chips standing there.
But based on "It is necessary that you can access the coordinates of the selected chip as simply as possible , " you need the second option - namely, an array of chips with their coordinates (x, y), which can be implemented as a standard pair<int,int> But you can use your type structure
struct Point { int x,y; }; But, as I understand it, you need both, and more - so that, knowing the chip, you can immediately get possible moves - for example, is a cage empty?
I would just make a class like Game , which would keep both the board and four chips, and with each move I would update both the coordinates of the chips and the state of the board. The interface of this class would contain functions for
- return coordinates chips number N
- return the chip number on the field (x, y) (0 if empty)
- move the N chips to the new field.
No matter how hard you try, you will not be able to make this class slow - with 64 fields. What kind of internal representation is needed - see for yourself what is familiar to you.
I tried to memorize coordinates in a regular array and in a vector, but this is not very convenient.
And what's uncomfortable? If you make an array, you need to make a converter in a visual form. And everything will be very convenient.
But I would take an example from chess. Where one axis is indicated by letters, the other is numerals. Coordinates are recorded as A6, E3 ... etc. This is a very fast option and very visual.