def Min(depth, maxVal, timer): minVal = 1000000 if (depth == 0 or checkWinner() != 0): return (gameEvaluation()) for i in range(19): for j in range(19): if (time() - timer > 4.85): return(minVal) if (board[i][j] == 0 and hasAdjacent(i, j)): board[i][j] = 2 tmp = Max((depth - 1), minVal, timer) if (tmp >= maxVal and maxVal != -1000000): board[i][j] = 0 return (minVal) if (tmp == minVal): if (random.randint(0, 2) == 1): minVal = tmp elif (tmp < minVal): minVal = tmp board[i][j] = 0 return (minVal)
def iaPlaying(depth): timer = time() maxi = -1 maxj = -1 if (emptyBoard()): board[9][9] = 1 return (9, 9) else: maxVal = -100000 for i in range(19): for j in range(19): if (time() - timer > 4.85): if (maxi == -1): for k in range(19): for l in range(19): if (board[k][l] == 0): board[i][j] = 1 return (k, l) else: board[maxi][maxj] = 1 return (maxi, maxj) if (board[i][j] == 0 and hasAdjacent(i, j)): board[i][j] = 1 tmp = Min(depth - 1, -1000000, timer) if (tmp == maxVal): if (randint(0, 2) == 1): maxVal = tmp maxi = i maxj = j elif (tmp > maxVal): maxVal = tmp maxi = i maxj = j board[i][j] = 0 board[maxi][maxj] = 1 return (maxi, maxj)
def adversEvaluation(): nbTwo = 0 nbGappedThree = 0 nbOpenThree = 0 nbGappedFour = 0 nbWinningPos = 0 for i in range(19): for j in range (19): if (board[i][j] != 0 or hasAdjacent(i, j)): diagPattern1 = [0, 0, 0, 0, 0, 0] diagPattern2 = [0, 0, 0, 0, 0, 0] horizontalPattern = [0, 0, 0, 0, 0, 0] verticalPattern = [0, 0, 0, 0, 0, 0] if (i < 14 and j < 14): diagPattern1 = [board[i][j], board[i + 1][j + 1], board[i + 2][j + 2], board[i + 3][j + 3], board[i + 4][j + 4], board[i + 5][j + 5]] if (i < 14): verticalPattern = [board[i][j], board[i + 1][j], board[i + 2][j], board[i + 3][j], board[i + 4][j], board[i + 5][j]] if (j < 14): horizontalPattern = [board[i][j], board[i][j + 1], board[i][j + 2], board[i][j + 3], board[i][j + 4], board[i][j + 5]] if (i > 4 and j < 14): diagPattern2 = [board[i][j], board[i - 1][j + 1], board[i - 2][j + 2], board[i - 3][j + 3], board[i - 4][j + 4], board[i - 5][j + 5]] if (diagPattern2.count(2) >= 2): if (checkIfWin(diagPattern2)): nbWinningPos += 1 elif (checkIfOpenFour(diagPattern2)): nbWinningPos += 1 elif (checkIfGappedFour(diagPattern2)): nbGappedFour += 1 elif (checkIfOpenThree(diagPattern2)): nbOpenThree += 1 elif (checkIfGappedThree(diagPattern2)): nbGappedThree += 1 elif (checkIfTwo(diagPattern2)): nbTwo += 1 if (diagPattern1.count(2) >= 2): if (checkIfWin(diagPattern1)): nbWinningPos += 1 elif (checkIfOpenFour(diagPattern1)): nbWinningPos += 1 elif (checkIfGappedFour(diagPattern1)): nbGappedFour += 1 elif (checkIfOpenThree(diagPattern1)): nbOpenThree += 1 elif (checkIfGappedThree(diagPattern1)): nbGappedThree += 1 elif (checkIfTwo(diagPattern1)): nbTwo += 1 if (verticalPattern.count(2) >= 2): if (checkIfWin(verticalPattern)): nbWinningPos += 1 elif (checkIfOpenFour(verticalPattern)): nbWinningPos += 1 elif (checkIfGappedFour(verticalPattern)): nbGappedFour += 1 elif (checkIfOpenThree(verticalPattern)): nbOpenThree += 1 elif (checkIfGappedThree(verticalPattern)): nbGappedThree += 1 elif (checkIfTwo(verticalPattern)): nbTwo += 1 if (horizontalPattern.count(2) >= 2): if (checkIfWin(horizontalPattern)): nbWinningPos += 1 elif (checkIfOpenFour(horizontalPattern)): nbWinningPos += 1 elif (checkIfGappedFour(horizontalPattern)): nbGappedFour += 1 elif (checkIfOpenThree(horizontalPattern)): nbOpenThree += 1 elif (checkIfGappedThree(horizontalPattern)): nbGappedThree += 1 elif (checkIfTwo(horizontalPattern)): nbTwo += 1 return (-nbTwo + (-10 * nbGappedThree) + (-100 * nbOpenThree) + (-1000 * nbGappedThree) + (-10000 * nbWinningPos))