Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8504637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:05:28+00:00 2026-06-11T02:05:28+00:00

Hi! I’m trying to write a negamax search algorithm for my chess engine, but

  • 0

Hi!

I’m trying to write a negamax search algorithm for my chess engine, but i can’t seem to get it working. I’m using wikipedias pseudocode as an example, but somehow it doesn’t produce the expected results. When i run it with a ply of 2, it changes my board data structure, although it shouldn’t. After the function is finished running with ply 2, all of the white’s (or black’s. depends what player calls the function.) pawns are moved 2 spaces forward from starting position.

My make and unmake move functions are working perfectly , as i tested them with a non-recursive function that searches up to 5-ply. Then, it worked perfectly. There must be something wrong with my negamax implementation.

Thank you very much for your help!

def negaMax(self, board, rules, ply, player):
        """ Implements a minimax algorithm. """
        if ply == 0:
            return self.positionEvaluation()

        self.max_eval = float('-infinity')

        self.move_list = board.generateMoves(rules, player)
        for self.move in self.move_list:
            board.makeMove(self.move, player)
            self.eval = -self.negaMax(board, rules, ply - 1, board.getOtherPlayer(player))
            board.unmakeMove(self.move, player)

            if self.eval > self.max_eval:
                self.max_eval = self.eval

        return self.max_eval
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:05:30+00:00Added an answer on June 11, 2026 at 2:05 am

    The main issue here is I believe the usage of object variables instead of local variable.

    self.move is an object variable, every time you change it – every level of the recursion “sees” the change, which is usually a bad thing for recursive algorithms.

    Recursive algorithms should be self contained, and do minimal if any change on the calling environment – it makes it much easier to walk over the flow of the algorithm.

    The two main issues I see in this code are:

    1. self.move should be a local variable (declare it as move). When you later do: board.unmakeMove(self.move, player) – I suspect that the board is undoing a different move, which was set deeper in the recursion tree, and not the one you intended. Using a local variable will eliminate this undesired behavior.
    2. Each level of the recursive call is setting self.max_eval = float('-infinity') – so you constantly change it, though it is probably not what you want to do.

    The solution should be something like that:

    def negaMax(self, board, rules, ply, player):
            """ Implements a minimax algorithm. """
            if ply == 0:
                return self.positionEvaluation()
    
            max_eval = float('-infinity')
    
            move_list = board.generateMoves(rules, player)
            for move in move_list:
                board.makeMove(move, player)
                currentEval = -self.negaMax(board, rules, ply - 1, board.getOtherPlayer(player))
                board.unmakeMove(move, player)
    
                if currentEval > max_eval:
                    max_eval = currentEval 
            return max_eval
    

    I am not 100% certain it will indeed solve everything in the code (but it will solve some of it) but I am 100% certain avoiding object variables will make your code much easier to understand and debug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
This could be a duplicate question, but I have no idea what search terms
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.