fork download
  1. (defparameter *player-health* nil)
  2. (defparameter *player-agility* nil)
  3. (defparameter *player-strength* nil)
  4. (defparameter *monsters* nil)
  5. (defparameter *monster-builders* nil)
  6. (defparameter *monster-num* 12)
  7.  
  8. (defun reload () (load "orc-battle"))
  9. (defun orc-battle (&optional monster-num)
  10. (when (and (integerp monster-num) (< 9 monster-num 21)
  11. (defparameter *monster-num* monster-num)))
  12. (init-monsters)
  13. (init-player)
  14. (game-loop)
  15. (when (player-dead)
  16. (princ "You have been killed. Game Over.")
  17. (decf *monster-num*))
  18. (when (monsters-dead)
  19. (princ "Congratulations! You have vanquished all of your foes.")
  20. (incf *monster-num*))
  21. (when (y-or-n-p "Next battle?")
  22. (orc-battle)))
  23.  
  24. (defun game-loop ()
  25. (unless (or (player-dead) (monsters-dead))
  26. (show-player)
  27. (dotimes (k (1+ (truncate (/ (max 0 *player-agility*) 15))))
  28. (unless (monsters-dead)
  29. (show-monsters)
  30. (player-attack)))
  31. (fresh-line)
  32. (map 'list
  33. (lambda (m) (or (monster-dead m) (monster-attack m))) *monsters*)
  34. (game-loop)))
  35.  
  36. (defun init-player ()
  37. (setf *player-health* 30)
  38. (setf *player-agility* 30)
  39. (setf *player-strength* 30))
  40.  
  41. (defun player-dead ()
  42. (<= *player-health* 0))
  43.  
  44. (defun show-player ()
  45. (fresh-line)
  46. (princ "You are a valiant knight with a health of ")
  47. (princ *player-health*)
  48. (princ ", an agility of ")
  49. (princ *player-agility*)
  50. (princ ", and a strength of ")
  51. (princ *player-strength*))
  52.  
  53. (defun player-attack ()
  54. (fresh-line)
  55. (princ "Attack style: [s]tab [d]ouble swing [r]oundhouse:")
  56. (case (read)
  57. (s (monster-hit (pick-monster)
  58. (+ 2 (randval (ash *player-strength* -1)))))
  59. (d (let ((x (randval (truncate (/ *player-strength* 6)))))
  60. (princ "Your double swing has a strength of ")
  61. (princ x)
  62. (fresh-line)
  63. (monster-hit (pick-monster) x)
  64. (unless (monsters-dead)
  65. (monster-hit (pick-monster) x))))
  66. (otherwise (map 'list
  67. (lambda (m) (unless (monster-dead m )
  68. (monster-hit m 1)))
  69. *monsters*))))
  70.  
  71.  
  72. (defun randval (n)
  73. (1+ (random (max 1 n))))
  74.  
  75. (defun random-monster ()
  76. (let ((m (aref *monsters* (random (length *monsters*)))))
  77. (if (monster-dead m)
  78. (random-monster)
  79. m)))
  80.  
  81. (defun pick-monster ()
  82. (fresh-line)
  83. (princ "Monster #:")
  84. (let ((x (read)))
  85. (if (not (and (integerp x) (>= x 1) (<= x *monster-num*)))
  86. (progn (princ "That is not a valid monster number.")
  87. (pick-monster))
  88. (let ((m (aref *monsters* (1- x))))
  89. (if (monster-dead m)
  90. (progn (princ "That monster is alread dead.")
  91. (pick-monster))
  92. m)))))
  93.  
  94. (defun init-monsters ()
  95. (setf *monsters*
  96. (map 'vector
  97. (lambda (x)
  98. (funcall (nth (random (length *monster-builders*))
  99. *monster-builders*)))
  100. (make-array *monster-num*))))
  101.  
  102. (defun monster-dead (m)
  103. (<= (monster-health m) 0))
  104.  
  105. (defun monsters-dead ()
  106. (every #'monster-dead *monsters*))
  107.  
  108. (defun show-monsters ()
  109. (fresh-line)
  110. (princ "Your foes:")
  111. (let ((x 0))
  112. (map 'list
  113. (lambda (m)
  114. (fresh-line)
  115. (format t "~5d" (incf x))
  116. (princ ". ")
  117. (if (monster-dead m)
  118. (princ "**dead**")
  119. (progn
  120. (format t "(Health=~4a"
  121. (format nil "~a)" (monster-health m)))
  122. (monster-show m))))
  123. *monsters*)))
  124.  
  125. (defstruct monster (health (randval 10)))
  126.  
  127. (defmethod monster-hit (m x)
  128. (decf (monster-health m) x)
  129. (if (monster-dead m)
  130. (progn (princ "You killed the ")
  131. (princ (type-of m))
  132. (princ "! "))
  133. (format t "You hit the ~a, knocking off ~a health point~:p! "
  134. (type-of m) x)))
  135.  
  136. (defmethod monster-show (m)
  137. (princ "A fierce ")
  138. (princ (type-of m)))
  139.  
  140. (defmethod monster-attack (m))
  141.  
  142. (defstruct (orc (:include monster)) (club-level (randval 8)))
  143.  
  144. (push #'make-orc *monster-builders*)
  145.  
  146. (defmethod monster-show ((m orc))
  147. (princ "A wicked orc with a level ")
  148. (princ (orc-club-level m))
  149. (princ " club"))
  150.  
  151. (defmethod monster-attack ((m orc))
  152. (let ((x (randval (orc-club-level m))))
  153. (princ "An orc swings his club at you and knocks off ")
  154. (format t "~a of your health point~:p. " x)
  155. (decf *player-health* x)))
  156.  
  157. (defstruct (hydra (:include monster)))
  158.  
  159. (push #'make-hydra *monster-builders*)
  160.  
  161. (defmethod monster-show ((m hydra))
  162. (format t "A malicious hydra with ~a head~:p" (monster-health m)))
  163.  
  164. (defmethod monster-hit ((m hydra) x)
  165. (decf (monster-health m) x)
  166. (if (monster-dead m)
  167. (progn
  168. (princ "The corpse of the fully decapitated ")
  169. (princ "and decapacitated hydra falls to the floor!"))
  170. (format t "You lop off ~a of the hydra's head~:p! " x)))
  171.  
  172. (defmethod monster-attack ((m hydra))
  173. (let ((x (randval (ash (monster-health m) -1))))
  174. (format t "A hydra attacks you with ~a of its head~:p! " x)
  175. (princ "It also grows back one more head! ")
  176. (incf (monster-health m))
  177. (decf *player-health* x)))
  178.  
  179. (defstruct (slime-mold (:include monster)) (sliminess (randval 5)))
  180.  
  181. (push #'make-slime-mold *monster-builders*)
  182.  
  183. (defmethod monster-show ((m slime-mold))
  184. (princ "A slime mold with a sliminess of ")
  185. (princ (slime-mold-sliminess m)))
  186.  
  187. (defmethod monster-attack ((m slime-mold))
  188. (let ((x (randval (slime-mold-sliminess m))))
  189. (princ "A slime mold wraps around your legs ")
  190. (princ "and decreases your agility by ")
  191. (princ x)
  192. (princ "! ")
  193. (decf *player-agility* x)
  194. (when (zerop (random 2))
  195. (princ "It also squirts in your face, taking away a health point! ")
  196. (decf *player-health*))))
  197.  
  198. (defstruct (brigand (:include monster)))
  199.  
  200. (push #'make-brigand *monster-builders*)
  201.  
  202. (defmethod monster-attack ((m brigand))
  203. (let ((x (max *player-health* *player-agility* *player-strength*)))
  204. (cond ((= x *player-health*)
  205. (princ "A brigand hits you with his slingshot, taking off 2 health points! ")
  206. (decf *player-health* 2))
  207. ((= x *player-agility*)
  208. (princ "A brigand catches your leg with his whip, taking off 2
  209. agility points! ")
  210. (decf *player-agility* 2))
  211. ((= x *player-strength*)
  212. (princ "A brigand cuts your arm with his whip, taking off 2 strength points! ")
  213. (decf *player-strength* 2)))))
  214.  
Success #stdin #stdout 0.02s 9996KB
stdin
Standard input is empty
stdout
Standard output is empty