Answers.Body: <p><code>return bestsofar</code> needs to be outside of the for loop. it was returning after only one piece checked. needs to return after its looped through every move.
  So:</p>
  <pre><code> for i=mvsindx[ply],mvsindx[ply+1]-1 do
    local m=moves[i]
    makemove(m)
    value=minimax(d-1)
    takemove()
    if value&gt;bestsofar then
    bestsofar=value
    end
   end
  return bestsofar --OUTSIDE OF FOR LOOP
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-11-24T21:05:47.223Z
Answers.LastActivityDate: 2024-11-24T21:05:47.223Z
Answers.UserDisplayName: bungee1980
Body: <p><a href="https://i.sstatic.net/A2p6Oq98.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/A2p6Oq98.png" alt="Mate in 1" /></a></p>
  <p>This function is perpetually returning the King moves back and forth, even if I apply a depth.</p>
  <p>The above example is a mate in 1, but the algorithm only returns the King moves back and forth and never analyzes Qb7#. If I take out the depth, it will go forever.</p>
  <p>Is there something in this code that I am missing? Or what concept am I getting wrong?</p>
  <pre><code>function minimax(d)
      if (d&lt;=0) return evaluate()
      genmoves()
      local incheck=attacked(plist[pind(kings[side+1],0)],side^^1)
      if (mvsindx[ply+1]-mvsindx[ply]==0) and incheck then  --checks for mate
          if side==0 then
              return -20000
          else
              return 20000
          end
      end
      if (mvsindx[ply+1]-mvsindx[ply]==0) return 0  --if there are no moves
      local value
  
      if side==0 then
          local bestsofar=-20000
          for i=mvsindx[ply],mvsindx[ply+1]-1 do
              local m=moves[i]
              makemove(m)
              value=minimax(d-1)
              takemove()
              if value&gt;bestsofar then
                  bestsofar=value
              end
              return bestsofar
          end
      else
          local bestsofar=20000
          for i=mvsindx[ply],mvsindx[ply+1]-1 do
              local m=moves[i]
              makemove(m)
              value=minimax(d-1)
              takemove()
              if value&lt;bestsofar then
                  bestsofar=value
              end
              return bestsofar
          end
      end
  end
  </code></pre>
  
Tags: <engines><minimax>
Title: Endless looping in Minimax
ThreadText: # Endless looping in Minimax
  
  (Asked by: username_0 on 2024-11-24)
  
  This function is perpetually returning the King moves back and forth, even if I apply a depth.
  The above example is a mate in 1, but the algorithm only returns the King moves back and forth and never analyzes Qb7#. If I take out the depth, it will go forever.
  Is there something in this code that I am missing? Or what concept am I getting wrong?
  function minimax(d)
      if (d<=0) return evaluate()
      genmoves()
      local incheck=attacked(plist[pind(kings[side+1],0)],side^^1)
      if (mvsindx[ply+1]-mvsindx[ply]==0) and incheck then  --checks for mate
          if side==0 then
              return -20000
          else
              return 20000
          end
      end
      if (mvsindx[ply+1]-mvsindx[ply]==0) return 0  --if there are no moves
      local value
  
      if side==0 then
          local bestsofar=-20000
          for i=mvsindx[ply],mvsindx[ply+1]-1 do
              local m=moves[i]
              makemove(m)
              value=minimax(d-1)
              takemove()
              if value>bestsofar then
                  bestsofar=value
              end
              return bestsofar
          end
      else
          local bestsofar=20000
          for i=mvsindx[ply],mvsindx[ply+1]-1 do
              local m=moves[i]
              makemove(m)
              value=minimax(d-1)
              takemove()
              if value
  
  **Tags:** engines, minimax
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-11-24. Score: 2 | Accepted answer
  
  return bestsofar needs to be outside of the for loop. it was returning after only one piece checked. needs to return after its looped through every move.
  So:
   for i=mvsindx[ply],mvsindx[ply+1]-1 do
    local m=moves[i]
    makemove(m)
    value=minimax(d-1)
    takemove()
    if value>bestsofar then
    bestsofar=value
    end
   end
  return bestsofar --OUTSIDE OF FOR LOOP
  
  
  ---
  
  