public void spawnChess(int index) { if (countdown >= dropTime && BoardUtility.canInsert(gameGrid, index)) { countdown = 0; int chessKey = 0; //get the position to insert Vector2 pos = BoardUtility.insertingPosition(gameGrid, index); Chess chs; if (redTurn) { chs = Instantiate(redChess) as Chess; chs.gameObject.transform.position = spawnPosition[index].position; chessKey = 1; } else { chs = Instantiate(yellowChess) as Chess; chs.gameObject.transform.position = spawnPosition[index].position; chessKey = 2; } chs.x = (int)pos.x; chs.y = (int)pos.y; all_chess.Add(chs); redTurn = !redTurn; //put it into the gamegrid gameGrid = BoardUtility.insertToGrid(gameGrid, pos, chessKey); checkWinner(); } }
//minimax algorithm that will expend its search and return the heuristic value for different result //with remove option public static Node minimaxWithRemove(int[] gameGrid, int depth, int alpha, int beta, bool maximizingPlayer, int playerKey, int index, int h) { if (depth == 0) { Node n = new Node(); n.value = heristic(gameGrid, playerKey, h); n.index = index; return(n); } else { if (maximizingPlayer) { Node n = new Node(); n.value = -9999999; int j = -1; for (int i = 0; i < 14; i++) { if (i < 7 && !BoardUtility.canInsert(gameGrid, i)) { break; } else if (i >= 7 && !BoardUtility.canRemove(gameGrid, i - 7, playerKey)) { break; } int[] newGrid = BoardUtility.copyGameGrid(gameGrid); if (i < 7) { BoardUtility.insertToGrid(newGrid, BoardUtility.insertingPosition(newGrid, i), playerKey); } else { BoardUtility.removeFromGrid(newGrid, i - 7, 5); } Node n1 = minimax(newGrid, depth - 1, alpha, beta, false, playerKey, i, h); if (n1.value >= n.value) { n = n1; j = i; } else if (n1.value == n.value) { if (Random.Range(0, 2) == 1) { n = n1; j = i; } } if (alphaBeta && n1.value >= alpha) { alpha = n1.value; } if (alphaBeta && beta <= alpha) { break; } } n.index = j; return(n); } else { Node n = new Node(); n.value = 99999999; int key = (playerKey + 1) % 2; int j = -1; for (int i = 0; i < 7; i++) { if (i < 7 && !BoardUtility.canInsert(gameGrid, i)) { break; } else if (i >= 7 && !BoardUtility.canRemove(gameGrid, i - 7, key)) { break; } int[] newGrid = BoardUtility.copyGameGrid(gameGrid); if (i < 7) { BoardUtility.insertToGrid(newGrid, BoardUtility.insertingPosition(newGrid, i), key); } else { BoardUtility.removeFromGrid(newGrid, i - 7, 5); } Node n1 = minimax(newGrid, depth - 1, alpha, beta, true, playerKey, i, h); if (n1.value <= n.value) { n = n1; j = i; } else if (n1.value == n.value) { if (Random.Range(0, 2) == 1) { n = n1; j = i; } } if (alphaBeta && beta >= n1.value) { beta = n1.value; } if (alphaBeta && beta <= alpha) { break; } } n.index = j; return(n); } } }