by DST » Thu Jul 22, 2010 4:53 am
Create an integer array, you can use the variables tab or create one in global script; make it 64 in length.
int board[64];
At any time, you can use a timer or Actor*loop to enter each actor's coordinates into the array. You should make the game so the 0 coordinate (Game Center) is the upper left corner of the board, to make things easier. If your squares are, say , 50x50 pixels each:
int s1=round(x/50); //how many squares from 0
int s2=round(y/50);
int mycell=s1+(s2*8); //xsquare (horizontal) + 8 for every vertical row. This is the core of 1d arrays to make 2d boards.
board[mycell]=1; //show that square is occupied.
A pawn can then easily search the array for (mycell+8) to see if it can move forward, and (mycell+9) & (mycell+7) to see if it can capture.
Eventually you will want to make it a 2d array, (int squares[64][5]) as the 2nd slots would be used to store all the information about a square when you do construct the AI:
0: is this square occupied (can i move here)
1: what color is on it (can i capture here)
2: What can i put in danger by moving here (is this an advantageous square)
3: how many other opposite pieces can capture this (is this a dangerous square)
4: if opposite side piece, is it already in danger (do i need to capture here)
5: will i protect more of my pieces now or if i move to this square (is this an advantageous square)
From this point of view, AI is relatively simple, as you search the array for each piece, ask those 6 questions about every possible move that piece has, and find the piece/square combo with the highest number of positive answers.