play(ply, first)
    {
        var bestMove = null;
        var assessment;
        
        if (ply == 0)
        {
            assessment = this.assessment();
        }
        else if (this.outcome != ONGOING)
        {
            assessment = this.outcome;

            if (assessment == WHITE_WIN)
                assessment += ply;
                
            else if (assessment == BLACK_WIN)
                assessment -= ply;
        }
        else //game ongoing and ply >= 1
        {
            let legalMoves = this.getLegalMoves();
            assessment = (this.#whiteMoves) ? (BLACK_WIN - ply) : (WHITE_WIN + ply);

            for (const move of legalMoves)
            {               
                let tempBoard = this.clone();
                tempBoard.move(move);
                const advisedPlay = tempBoard.play(ply - 1, false);
                                
                if (this.#whiteMoves)
                {
                    if (advisedPlay.bestAssessment > assessment)
                    {
                        bestMove = move;
                        assessment = advisedPlay.bestAssessment;
                    }
                    // if (assessment >= WHITE_WIN) //save time as optimal move already found
                    // break;
                }
                else // black
                {
                    if (advisedPlay.bestAssessment < assessment)
                    {
                        bestMove = move;
                        assessment = advisedPlay.bestAssessment;
                    }
                    // if (assessment <= BLACK_WIN) //save time as optimal move already found
                    //     break;
                }

                

                // let ret = this.takeback();
                // console.assert(ret);
            }

            // no best move found - pick one at random
            if (bestMove === null)
            {
                bestMove = legalMoves[Math.floor(Math.random() * legalMoves.length)];
            }
        }
        
        return {advisedMove: bestMove, bestAssessment: assessment};
    } //play

/**
     * Returns what it thinks is the best move for the currently playing side using ply number of recursive assessments, as well as the assessment of this position.
     * If the game is already over, returns null as the best move.
     * 
     * @param {*} ply 0 will return an assessment of the current sitaution without looking ahead
     * @param {*} first pass true if calling to this function externally (as opposed to recursive calls) - for diagnostics only
     */
Returns what it thinks is the best move for the currently playing side using ply number of recursive assessments, as well as the assessment of this position.
If the game is already over, returns null as the best move.

@param {*} ply 0 will return an assessment of the current sitaution without looking ahead
@param {*} first pass true if calling to this function externally (as opposed to recursive calls) - for diagnostics only
Returns what it thinks is the best move for the currently playing side using ply number of recursive assessments as well as the assessment of this position . If the game is already over returns null as the best move . @param { * } ply 0 will return an assessment of the current sitaution without looking ahead @param { * } first pass true if calling to this function externally ( as opposed to recursive calls ) - for diagnostics only
play(ply, first)
    {
        var bestMove = null;
        var assessment;
        if (ply == 0)
        {
            assessment = this.assessment();
        }
        else if (this.outcome != ONGOING)
        {
            assessment = this.outcome;
            if (assessment == WHITE_WIN)
                assessment += ply;
            else if (assessment == BLACK_WIN)
                assessment -= ply;
        }
        else 
        {
            let legalMoves = this.getLegalMoves();
            assessment = (this.#whiteMoves) ? (BLACK_WIN - ply) : (WHITE_WIN + ply);
            for (const move of legalMoves)
            {               
                let tempBoard = this.clone();
                tempBoard.move(move);
                const advisedPlay = tempBoard.play(ply - 1, false);
                if (this.#whiteMoves)
                {
                    if (advisedPlay.bestAssessment > assessment)
                    {
                        bestMove = move;
                        assessment = advisedPlay.bestAssessment;
                    }
                }
                else 
                {
                    if (advisedPlay.bestAssessment < assessment)
                    {
                        bestMove = move;
                        assessment = advisedPlay.bestAssessment;
                    }
                }
            }
            if (bestMove === null)
            {
                bestMove = legalMoves[Math.floor(Math.random() * legalMoves.length)];
            }
        }
        return {advisedMove: bestMove, bestAssessment: assessment};
    } 
play ( ply , first ) { var bestMove = null ; var assessment ; if ( ply == 0 ) { assessment = this . assessment ( ) ; } else if ( this . outcome != ONGOING ) { assessment = this . outcome ; if ( assessment == WHITE_WIN ) assessment += ply ; else if ( assessment == BLACK_WIN ) assessment -= ply ; } else { let legalMoves = this . getLegalMoves ( ) ; assessment = ( this . #whiteMoves ) ? ( BLACK_WIN - ply ) : ( WHITE_WIN + ply ) ; for ( const move of legalMoves ) { let tempBoard = this . clone ( ) ; tempBoard . move ( move ) ; const advisedPlay = tempBoard . play ( ply - 1 , false ) ; if ( this . #whiteMoves ) { if ( advisedPlay . bestAssessment > assessment ) { bestMove = move ; assessment = advisedPlay . bestAssessment ; } } else { if ( advisedPlay . bestAssessment < assessment ) { bestMove = move ; assessment = advisedPlay . bestAssessment ; } } } if ( bestMove === null ) { bestMove = legalMoves [ Math . floor ( Math . random ( ) * legalMoves . length ) ] ; } } return { advisedMove : bestMove , bestAssessment : assessment } ; }
Returns what it thinks is the best move for the currently playing side using ply number of recursive assessments, as well as the assessment of this position.
Returns what it thinks is the best move for the currently playing side using ply number of recursive assessments as well as the assessment of this position .
//game ongoing and ply >= 1
 // if (assessment >= WHITE_WIN) //save time as optimal move already found
 // break;
 // black
 // if (assessment <= BLACK_WIN) //save time as optimal move already found
 //     break;
 // let ret = this.takeback();
 // console.assert(ret);
 // no best move found - pick one at random
 //play
