# Python Mode : maximum recursion depth

**URL:** <https://discourse.processing.org/t/python-mode-maximum-recursion-depth/24588>\
**Category:** Processing.py\
**Created:** [October 14, 2020, 5:19am UTC](https://discourse.processing.org/t/python-mode-maximum-recursion-depth/24588 "2020-10-14T05:19:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grusat](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@Grusat](https://discourse.processing.org/u/Grusat)\
**Post date:** [October 14, 2020, 5:19am UTC](https://discourse.processing.org/t/python-mode-maximum-recursion-depth/24588/1 "2020-10-14T05:19:24Z")

</div>

Does anyone know how to increase the recursion limit like in python : sys.setrecursionlimit() ?

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [October 14, 2020, 6:14am UTC](https://discourse.processing.org/t/python-mode-maximum-recursion-depth/24588/2 "2020-10-14T06:14:31Z")

</div>

Here’s something I did earlier (not tested in current version).

> **[Second Coming For Processing.py](https://secondcomingforprocessingpy.blogspot.com/search?q=recursion)**

---

<div class="post-metadata">

**Author:** ![Grusat](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@Grusat](https://discourse.processing.org/u/Grusat)\
**Post date:** [October 14, 2020, 7:40pm UTC](https://discourse.processing.org/t/python-mode-maximum-recursion-depth/24588/3 "2020-10-14T19:40:06Z")

</div>

It does work but didn’t answer my problem entirely…  
Thank you

---

<div class="post-metadata">

**Author:** ![Grusat](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@Grusat](https://discourse.processing.org/u/Grusat)\
**Post date:** [October 14, 2020, 7:43pm UTC](https://discourse.processing.org/t/python-mode-maximum-recursion-depth/24588/4 "2020-10-14T19:43:43Z")

</div>

Actually i’m implementing a minimax algorithm with alphabeta pruning for a chess AI but i’m getting a maximum recursive depth reached error in some positions.  
I guess my recursive function minimax isn’t correct but i can’t find the problem.  
Maybe you will ;p

def minimaxRoot(depth, board,isMaximizing, tour):

```
possibleMoves = getPossibleMoves(board, tour)
if len(possibleMoves) == 0:
    return None
bestMove = float('-inf')
bestMoveFinal = None

for m in possibleMoves:
    prev = board[m[3]][m[2]]
    board = move_test(m, board)
    value = max(bestMove, minimax(depth - 1, board,float('-inf'), float('inf'), not isMaximizing, 1-tour))
    #affiche(board)
    board = undo(m, board, prev)
    if( value > bestMove):
        bestMove = value
        bestMoveFinal = m
return bestMoveFinal

```

def minimax(depth, board, a, b, is\_maximizing, tour):

```
global nb
nb+=1
print(depth)
if(depth <1):
    return -evaluate(board, tour)

possibleMoves = getPossibleMoves(board, tour)
if len(possibleMoves) == 0:
    if isInCheck(tour,board):
        if is_maximizing:
            return float('-100000')
        else:
            return float('100000')
    else:
        if is_maximizing:
            return float('20000')
        else:
            return float('-20000')
    
#Meilleur coup pour l'ia
if(is_maximizing):
    bestMove = float('-inf')
    for m in possibleMoves:
        prev = board[m[3]][m[2]]
        board = move_test(m, board)
        bestMove = max(bestMove, minimax(depth - 1, board, a, b, not is_maximizing, 1-tour))
        board = undo(m, board, prev)
        a = max(a, bestMove)
        if b <= a:
            return bestMove
    return bestMove
#Meilleur coup de l'adversaire (= pire coup pour l'ia)
else:
    bestMove = float('inf')
    for m in possibleMoves:
        prev = board[m[3]][m[2]]
        board = move_test(m, board)
        bestMove = min(bestMove, minimax(depth - 1, board, a, b, not is_maximizing, 1-tour))
        board = undo(m, board, prev)
        b = min(b, bestMove)
        if b <= a:
            return bestMove
    return bestMove
```

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [October 14, 2020, 9:54pm UTC](https://discourse.processing.org/t/python-mode-maximum-recursion-depth/24588/5 "2020-10-14T21:54:53Z")

</div>

Hi @Grusat, good luck with your chess engine 🙂

I guess @monsktone example contains this, but I’ll post to make it easier for other people to find it 🙂

To change the recursion limit, you have to import `sys`

```auto
import sys
sys.setrecursionlimit(1000) 
print sys.recursionlimit

```
