Skip to content

Commit

Permalink
Chapter 9: Initial progress with Battle Orc Game example
Browse files Browse the repository at this point in the history
I need understand better some functions and write the rest of the example too.
But seems a pretty straightforward game.
  • Loading branch information
ryukinix committed Feb 19, 2017
1 parent 11a1e12 commit 4196e99
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
A personal repository for annotation about learning lisp patterns.
The actual content are answers and code covering the book [Land of Lisp](http://www.landoflisp.com) and the insights at the MIT 6.001 Course about Structures and Interpretations of Computer Programs.

# Land of Lisp book (reading) [172/482]
![progress](http://progressed.io/bar/35)
# Land of Lisp book (reading) [176/482]
![progress](http://progressed.io/bar/36)

- [x] Chapter 1 (intro)
- [x] Chapter 2 (guess my numbers)
Expand Down
107 changes: 107 additions & 0 deletions land-of-lisp/cap9-orc-battle-game.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
;; Common Lisp Script
;; Manoel Vilela


#|
-- ORC BATTLE GAME --
In the Orc Battle game, you are a knight surrounded by 12 monsters, engaged
in a fight to the death. With your superior wits and your repertoire
of sword-fighting maneuvers, you must carefully make strategies in your battle with orcs,
hydras, and other nasty enemies.
Using DEFMETHOD and DEFDESTRUCT, let's dispatch some whoop ass on these vermin!
|#



;; global variables for player status

(defparameter *player-health* nil)
(defparameter *player-agility* nil)
(defparameter *player-strength* nil)


;; we'll store our monsters in an array called *monsters*
;; we'll also define a list of functions for building monsters that
;; we'll store in the variable *monster-builders* (AQUI É MONSTRO, PORRA)


(defparameter *monsters* nil)
(defparameter *monster-builders* nil)
(defparameter *monster-num* 12) ;; high => more difficult

;; undefined functions yet:
;; +MONSTER-HIT
;; +MONSTERS-DEAD
;; +PICK-MONSTER
;; +RANDVAL
;; I need define the structure of MONSTER as well yet.

(defun init-player()
"Set the initial tributes of our knight"
(setf *player-health* 30)
(setf *player-agility* 30)
(setf *player-strength* 30))

(defun player-dead ()
"Check if the player is alive"
(<= *player-health* 0))

(defun show-player ()
"If the player is alive, show in REPL your info at each action"
(fresh-line)
(format t "You are a valiant knight with a health of ~a,
an agility of ~a and a strength of ~a"
*player-health*
*player-agility*
*player-health*))

(defun player-attack ()
"The player-attack function lets us manage a player's attack"
(fresh-line)
(princ "Attack style: [s]tab [d]ouble swing [r]oundhouse: ")
(case (read)
(s (monster-hit (pick-monster)
(+ 2 (randval (ash *player-strength* -1)))))
(d (let ((x (randval (truncate (/ *player-strength* 6)))))
(format t "Your double swing has a strength of ~a" x)
(fresh-line)
(monster-hit (pick-monster) x)
(unless (monsters-dead)
(monster-hit (pick-monster) x))))
(otherwise (dotimes (x (1+ (randval (truncate (/ *player-strength* 3)))))
(unless (monsters-dead)
(monster-hit (random-monster 1)))))))

(defun game-loop ()
"The game-loop function handles the repeated cycles of monster
and player attacks."
(unless (or (player-dead)
(monsters-dead))
(show-player)
(dotimes (k (1+ (truncate (/ (max 0 *player-agility*) 15))))
(unless (monsters-dead)
(show-monsters)
(player-attack)))
(fresh-line)
(map 'list
(lambda (m)
(or (monster-dead m)
(moster-attack m)))
*monsters*)
(game-loop)))

;; the big picture function
(defun orc-battle ()
"Main function of the game"
(init-monsters)
(init-player)
(game-loop)
(when (player-dead)
(princ "You have been killed. Game Over."))
(when (monsters-dead)
(princ "Congratulations! You have vanquished all of your foes.")))

0 comments on commit 4196e99

Please sign in to comment.