Hello guys!

I’ve implemented my game a minimax algorithm with “DLS” and “transpostion tables” but it is suspiciously **very** slow, When using a 3x3 board.

The goal of the game is to get the grid to an order which is determined at the start of the game.

Square is a class which represent a cell in the grid, and checks for invalid switch attempts.

SwitchGrid is a function which switches 2 cells in the grid.

SwitchStorage is a class which stores 2 cell position. (Used for transposition table)

tTable is the transposition table.

notPossible can be ignored.

Here is my code for the minimax function. Can you catch any errors I did?

```
public void InitZobrist(int[,] grid)
{
System.Random rand = new System.Random();
int cols = Conversion.GetCols(grid);
int rows = Conversion.GetRows(grid);
ZobristTable = new int[cols][][];
for (int i = 0; i < cols; i++)
{
ZobristTable[i] = new int[rows][];
for (int j = 0; j < rows; j++)
{
ZobristTable[i][j] = new int[grid.Length];
for (int k = 0; k < grid.Length; k++)
{
ZobristTable[i][j][k] = rand.Next(0, int.MaxValue);
}
}
}
}
public long GetZobristKey(int[,] grid)
{
if (ZobristTable == null)
InitZobrist(grid);
long hash = 0;
for (int i = 0; i < grid.GetLength(0); i++)
{
for (int j = 0; j < grid.GetLength(1); j++)
{
int index = grid[i, j];
hash ^= ZobristTable[i][j][index];
}
}
return hash;
}
int GetBestMove(int[,] gridA, int[][] notPossible)
{
for (int depth = 0; depth < 15; depth++)
{
if (GetBestMoveDLS(gridA, notPossible, depth) == 10)
{
return depth;
}
}
return -1;
}
int GetBestMoveDLS(int[,] grid, int[][] notPossible, int depth)
{
if (GetCloseToFinish(grid) == 1) //game end
{
return 10;
}
if (depth <= 0)
return 0;
long hash = GetZobristKey(grid);
if (tTable.ContainsKey(hash))
{
int pos1 = tTable[hash].pos1;
int pos2 = tTable[hash].pos2;
int score = GetBestMoveDLS(LevelCreator.SwitchGrid(grid, pos1, pos2), notPossible, depth - 1);
return score;
}
for (int i = 0; i < grid.Length; i++) //from 0 - > end
{
for (int j = (i + 1); j < grid.Length; j++)
{
if (!Square.CanSwitch(i, j, grid, notPossible)) continue; //invalid switch attempt
int score = GetBestMoveDLS(LevelCreator.SwitchGrid(grid, i, j), notPossible, depth - 1);
if (score == 10)
{
tTable.Add(hash, new SwitchStorage(i, j));
return 10;
}
}
}
return 0;
}
```