Function: gomoku-strongest-square

gomoku-strongest-square is a byte-compiled function defined in gomoku.el.gz.

Signature

(gomoku-strongest-square)

Documentation

Compute index of free square with highest score, or nil if none.

Source Code

;; Defined in /usr/src/emacs/lisp/play/gomoku.el.gz
(defun gomoku-strongest-square ()
  "Compute index of free square with highest score, or nil if none."
  ;; We just have to loop other all squares.  However there are two problems:
  ;; 1/ The SCORE-TABLE only gives correct scores to free squares.  To speed
  ;;	up future searches, we set the score of padding or occupied squares
  ;;	to -1 whenever we meet them.
  ;; 2/ We want to choose randomly between equally good moves.
  (let ((score-max 0)
	(count	   0)			; Number of equally good moves
	(square	   (gomoku-xy-to-index 1 1)) ; First square
	(end	   (gomoku-xy-to-index gomoku-board-width gomoku-board-height))
	best-square score)
    (while (<= square end)
      (cond
       ;; If score is lower (i.e. most of the time), skip to next:
       ((< (aref gomoku-score-table square) score-max))
       ;; If score is better, beware of non free squares:
       ((> (setq score (aref gomoku-score-table square)) score-max)
	(if (zerop (aref gomoku-board square)) ; is it free ?
	    (setq count 1		       ; yes: take it !
		  best-square square
		  score-max   score)
	    (aset gomoku-score-table square -1))) ; no: kill it !
       ;; If score is equally good, choose randomly.  But first check freedom:
       ((not (zerop (aref gomoku-board square)))
	(aset gomoku-score-table square -1))
       ((zerop (random (setq count (1+ count))))
	(setq best-square square
	      score-max	  score)))
      (setq square (1+ square)))	; try next square
    best-square))