Guest User

Untitled

a guest
Mar 8th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. (define bestMove '(0 0))
  2. (define maxValue 0)
  3. (define minValue 0)
  4.  
  5. (define (board-copy b)
  6. (let* ((size (vector-length b))
  7. (newb (make-vector size)))
  8. ;; build the new board with all zeros
  9. (do ((r 0 (+ r 1)))
  10. ((= r size) )
  11. (vector-set! newb r (make-vector size 0)))
  12.  
  13. ;; copy in the values from the old board
  14. (do ((r 0 (+ r 1)))
  15. ((= r size) newb)
  16.  
  17. (do ((c 0 (+ c 1)))
  18. ((= c size))
  19. (set-board-val! newb r c (board-ref b r c))))))
  20.  
  21.  
  22. (define (getEnemy p)
  23. (if (= p 1) 2 1))
  24.  
  25. (define (mini b p cutoff node)
  26. (if (= node cutoff)
  27. (begin(- (board-count-vals b p) (board-count-vals b (getEnemy p))) (display "testMini\n"))
  28. (let ((moves (gen-legal-moves b p)) (bigOne 100000) )
  29. (display "test!!!\n")
  30. (for-each
  31. (lambda (x)
  32. (let ((row (car x)) (col (cadr x)) (boardCopy (board-copy b)))
  33. (let ((boardMove (make-move boardCopy p row col)))
  34. (draw-board boardMove)
  35. (set! maxValue (maxi boardMove p cutoff (+ node 1)))
  36. (map display (list "min move: " x "bigone: "maxValue "\n"))
  37. (if (< maxValue bigOne)
  38. (set! bigOne maxValue)))))moves)))maxValue)
  39.  
  40.  
  41. (define (maxi b p cutoff node)
  42. (if (= node cutoff)
  43. (begin(- (board-count-vals b p) (board-count-vals b (getEnemy p))) (display "testMaxi\n"))
  44. (let ((moves (gen-legal-moves b p)) (smallOne -100000) )
  45. (for-each
  46. (lambda (x)
  47. (let ((row (car x)) (col (cadr x)) (boardCopy (board-copy b)))
  48. (let ((boardMove (make-move boardCopy p row col)))
  49. (draw-board boardMove)
  50. (set! minValue (mini boardCopy (getEnemy p) cutoff (+ node 1)))
  51. (map display (list "max move: " x "smallone: " minValue "\n"))
  52. (if (> minValue smallOne)
  53. (set! smallOne minValue)
  54. (set! bestMove x)))))moves)))minValue)
  55.  
  56.  
  57. (define (minimax b p)
  58. (letrec ((maxiLoop (lambda (i)
  59. (if (eqv? #f (testcounter 9)) bestMove
  60. (begin
  61. (maxi b p i 0)
  62. (maxiLoop (+ i 1)))))))))
  63.  
  64.  
  65.  
  66. (define (testcounter timelen)
  67. (let ((counter 0))
  68. (letrec ((countproc
  69. (lambda ()
  70. (set! counter (+ 1 counter))
  71. (countproc))))
  72. (let ((t (thread countproc)))
  73. (sync/timeout timelen 9)
  74. (kill-thread t)
  75. (map display (list "Time: " timelen " Count: " counter "\n"))
  76. #f))))
  77.  
  78.  
  79. (define counter 0)
  80.  
  81. (define (countproc)
  82. (set! counter (+ 1 counter))
  83. (countproc))
Add Comment
Please, Sign In to add comment