a13a6a80dd59 entry-03-1

moar.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 08 Jul 2012 17:19:34 -0400
parents 94179c6aec42
children aacbf1ac74fc
branches/tags entry-03-1
files src/caves/core.clj src/caves/world.clj test/caves/core_test.clj

Changes

--- a/src/caves/core.clj	Sun Jul 08 09:09:32 2012 -0400
+++ b/src/caves/core.clj	Sun Jul 08 17:19:34 2012 -0400
@@ -1,16 +1,20 @@
 (ns caves.core
+  (:use [caves.world :only [random-world]])
   (:require [lanterna.screen :as s]))
 
 
+; Constants -------------------------------------------------------------------
+(def screen-size [80 24])
+
 ; Data Structures -------------------------------------------------------------
 (defrecord UI [kind])
-(defrecord World [])
 (defrecord Game [world uis input])
 
 ; Utility Functions -----------------------------------------------------------
 (defn clear-screen [screen]
-  (let [blank (apply str (repeat 80 \space))]
-    (doseq [row (range 24)]
+  (let [[cols rows] screen-size
+        blank (apply str (repeat cols \space))]
+    (doseq [row (range rows)]
       (s/put-string screen 0 row blank))))
 
 
@@ -21,7 +25,10 @@
 
 (defmethod draw-ui :start [ui game screen]
   (s/put-string screen 0 0 "Welcome to the Caves of Clojure!")
-  (s/put-string screen 0 1 "Press enter to win, anything else to lose."))
+  (s/put-string screen 0 1 "Press any key to continue.")
+  (s/put-string screen 0 2 "")
+  (s/put-string screen 0 3 "Once in the game, you can use enter to win,")
+  (s/put-string screen 0 4 "and backspace to lose."))
 
 (defmethod draw-ui :win [ui game screen]
   (s/put-string screen 0 0 "Congratulations, you win!")
@@ -31,6 +38,22 @@
   (s/put-string screen 0 0 "Sorry, better luck next time.")
   (s/put-string screen 0 1 "Press escape to exit, anything else to restart."))
 
+(defmethod draw-ui :play [ui {{:keys [tiles]} :world :as game} screen]
+  (let [[cols rows] screen-size
+        vcols cols
+        vrows (dec rows)
+        start-x 0
+        start-y 0
+        end-x (+ start-x vcols)
+        end-y (+ start-y vrows)]
+    (doseq [[vrow-idx mrow-idx] (map vector
+                                     (range 0 vrows)
+                                     (range start-y end-y))
+            :let [row-tiles (subvec (tiles mrow-idx) start-x end-x)]]
+      (doseq [vcol-idx (range vcols)
+              :let [{:keys [glyph color]} (row-tiles vcol-idx)]]
+        (s/put-string screen vcol-idx vrow-idx glyph {:fg color})))))
+
 (defn draw-game [game screen]
   (clear-screen screen)
   (doseq [ui (:uis game)]
@@ -44,9 +67,15 @@
     (:kind (last (:uis game)))))
 
 (defmethod process-input :start [game input]
-  (if (= input :enter)
-    (assoc game :uis [(new UI :win)])
-    (assoc game :uis [(new UI :lose)])))
+  (-> game
+    (assoc :world (random-world))
+    (assoc :uis [(new UI :play)])))
+
+(defmethod process-input :play [game input]
+  (case input
+    :enter     (assoc game :uis [(new UI :win)])
+    :backspace (assoc game :uis [(new UI :lose)])
+    game))
 
 (defmethod process-input :win [game input]
   (if (= input :escape)
@@ -72,10 +101,7 @@
         (recur (process-input (dissoc game :input) input))))))
 
 (defn new-game []
-  (new Game
-       (new World)
-       [(new UI :start)]
-       nil))
+  (new Game nil [(new UI :start)] nil))
 
 (defn main
   ([screen-type] (main screen-type false))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/caves/world.clj	Sun Jul 08 17:19:34 2012 -0400
@@ -0,0 +1,26 @@
+(ns caves.world)
+
+
+(def world-size [160 50])
+
+(defrecord World [tiles])
+(defrecord Tile [kind glyph color])
+
+(def tiles
+  {:floor (new Tile :floor "." :white)
+   :wall  (new Tile :wall  "#" :white)
+   :bound (new Tile :bound "X" :black)})
+
+(defn get-tile [tiles x y]
+  (get-in tiles [y x] (:bound tiles)))
+
+(defn random-tiles []
+  (let [[cols rows] world-size]
+    (letfn [(random-tile []
+              (tiles (rand-nth [:floor :wall])))
+            (random-row []
+              (vec (repeatedly cols random-tile)))]
+      (vec (repeatedly rows random-row)))))
+
+(defn random-world []
+  (new World (random-tiles)))
--- a/test/caves/core_test.clj	Sun Jul 08 09:09:32 2012 -0400
+++ b/test/caves/core_test.clj	Sun Jul 08 17:19:34 2012 -0400
@@ -9,13 +9,9 @@
 
 (deftest test-start
   (let [game (new Game nil [(new UI :start)] nil)]
-
-    (testing "Enter wins at the starting screen."
-      (let [result (process-input game :enter)]
-        (is (= (current-ui result) :win))))
-
-    (testing "Other keys lose at the starting screen."
+    (testing "Any key sends you to the play screen and generates a world."
       (let [results (map (partial process-input game)
-                         [\space \a \A :escape :up :backspace])]
+                         [:enter \space \a \A :escape :up :backspace])]
         (doseq [result results]
-          (is (= (current-ui result) :lose)))))))
+          (is (= (current-ui result) :play))
+          (is (not= nil (:world result))))))))