Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define bestMove '(0 0))
- (define maxValue 0)
- (define minValue 0)
- (define (board-copy b)
- (let* ((size (vector-length b))
- (newb (make-vector size)))
- ;; build the new board with all zeros
- (do ((r 0 (+ r 1)))
- ((= r size) )
- (vector-set! newb r (make-vector size 0)))
- ;; copy in the values from the old board
- (do ((r 0 (+ r 1)))
- ((= r size) newb)
- (do ((c 0 (+ c 1)))
- ((= c size))
- (set-board-val! newb r c (board-ref b r c))))))
- (define (getEnemy p)
- (if (= p 1) 2 1))
- (define (mini b p cutoff node)
- (if (= node cutoff)
- (begin(- (board-count-vals b p) (board-count-vals b (getEnemy p))) (display "testMini\n"))
- (let ((moves (gen-legal-moves b p)) (bigOne 100000) )
- (display "test!!!\n")
- (for-each
- (lambda (x)
- (let ((row (car x)) (col (cadr x)) (boardCopy (board-copy b)))
- (let ((boardMove (make-move boardCopy p row col)))
- (draw-board boardMove)
- (set! maxValue (maxi boardMove p cutoff (+ node 1)))
- (map display (list "min move: " x "bigone: "maxValue "\n"))
- (if (< maxValue bigOne)
- (set! bigOne maxValue)))))moves)))maxValue)
- (define (maxi b p cutoff node)
- (if (= node cutoff)
- (begin(- (board-count-vals b p) (board-count-vals b (getEnemy p))) (display "testMaxi\n"))
- (let ((moves (gen-legal-moves b p)) (smallOne -100000) )
- (for-each
- (lambda (x)
- (let ((row (car x)) (col (cadr x)) (boardCopy (board-copy b)))
- (let ((boardMove (make-move boardCopy p row col)))
- (draw-board boardMove)
- (set! minValue (mini boardCopy (getEnemy p) cutoff (+ node 1)))
- (map display (list "max move: " x "smallone: " minValue "\n"))
- (if (> minValue smallOne)
- (set! smallOne minValue)
- (set! bestMove x)))))moves)))minValue)
- (define (minimax b p)
- (letrec ((maxiLoop (lambda (i)
- (if (eqv? #f (testcounter 9)) bestMove
- (begin
- (maxi b p i 0)
- (maxiLoop (+ i 1)))))))))
- (define (testcounter timelen)
- (let ((counter 0))
- (letrec ((countproc
- (lambda ()
- (set! counter (+ 1 counter))
- (countproc))))
- (let ((t (thread countproc)))
- (sync/timeout timelen 9)
- (kill-thread t)
- (map display (list "Time: " timelen " Count: " counter "\n"))
- #f))))
- (define counter 0)
- (define (countproc)
- (set! counter (+ 1 counter))
- (countproc))
Add Comment
Please, Sign In to add comment