I have programed a game of chess. However I have used dynamic memory allocation everywhere,when new moves are generated.This has slowed down the speed of my code.
I came up with an idea of making a multi-dimensional array, one stores the depth at which the moves are called and other stores the moves.
For this I have written a X's and O's game.
Here is the snippet.
static int moves[][]=new int[maxDepth][boardSize]
static int n[]=new int[maxDepth];
...
public static void possibleMoves(int board[][], int depth) {
n[depth] = 0; // here it acts as a counter, counts the number of legal moves, this can be used in the loop to play the legalmoves.
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[j] == 0) {
mList[depth][n[depth]++] = size * i + j;
}
}
}
}
I doesnt work though, wanted to know if my basic idea is correct or not.
Basically my AI plays continuously, without me getting a chance to play.
Is this a common problem any sort of help is welcomed.
Thank you.