Sunday, March 20, 2016

The Big Short

A short (no pun intended) offering today, as I'm currently intellectually and emotionally exhausted from worrying about the future of our country and the world in general...

Last night, Tanya and Marco and Tanya and I (yes, two separate Tanya's) watched The Big Short, the movie about the guys that made a ton of money off of betting against the American economy from 2005 through 2008 or so.  Well, that's not really what the movie (or book) is about.  The story actually follows and exposes the absolutely ridiculous series of unfortunate events that got us to the point of the housing market bubble bursting.  The fact that those people that bet against the market made a lot of money in the meantime is almost a footnote, which I thought was really interesting.

The movie was poignant and entertaining, which is also a pretty rare combination.  Every performance was memorable and well executed, with the exception of Brad Pitt -- sorry, he's really hit-or-miss with me and felt very average in this role.  There was a greater than usual amount of breaking the fourth wall, and it was always compelling and appropriate.

In light of our current political climate, I encourage everyone that isn't familiar with the story here to go watch the movie and/or read the book.  Think about how the candidate you favor relates to (or is actually culpable in) the events portrayed there.  Consider how they might behave on a going-forward basis with regard to Wall Street (and fiscal responsibility overall).

Now bear in mind that this is only *one* of the really important issues that probably don't have enough substantive discourse in the debates from either party.  It is my sincere hope that everyone that can *will* participate in this year's political process and will do so thoughtfully.

Pick your top three issues.  Really think them through from as many perspectives as you can so that you can have a fair view of them.  Find the candidate that best aligns with those top three, and vett that they do so as much as you possibly can.  Don't just listen to what the candidate is saying -- look at their public/behavioral record.  This is relatively easy to do with current/former government officials, less so with private citizens.  Do your best in this effort so that you can support and vote with a clear conscience.

Insofar as the opportunities present themselves, participate in meaningful, thoughtful, and respectful discussion with people that disagree with the importance of your issues or your stances.  Be open to learning something, understanding someone else's point of view, and potentially changing your attitude.

Above all, be kind to yourself and to everyone else.  There's too much hate and misery in the world as it is -- don't contribute to vitriol or negative behavior/attitude.  I read this commentary yesterday and found that it resonated with my own mental state pretty well.  It was written more eloquently than I could have done, so please give it a read.

Alright, that's it for politics and sadness for me.  I promise the next post will be back to the normal book reviews, writing, exercise, technology, etc.  If you need a diversion until then, check out Tim's Snake Lake blog post and play the game.  Yum!!

Saturday, March 12, 2016

Book review: "Mistborn" (original trilogy) by Brandon Sanderson

This is actually a general review of the entire Mistborn original trilogy, as I've read them over the past several months with the boys and on my own.  We started reading them together in the car via Audible, but Garrett couldn't wait for Gabriel and I and finished listening to the entire series on his own before we'd even finished the first book as a group.  Eventually, Gabriel said he was going to pause listening to them, leaving me to finish them on my own.

I also have the paperback collection, shown here.  I finished the series by both reading and listening to them on Audible.

This review contains minor spoilers at the very least.  Read on at your own peril!

Encapsulate the plot of this book in one sentence.

A group of thieves plot to overthrow the rule of an immortal emperor/god, deal with the repercussions of all actions to that end, and discover the full history, responsibilities, and sources of the mystical powers that both hold the world together and rip it asunder.

When and where did you get this book?

I originally got the first Mistborn book in November, 2014.  I subsequently bought the paperback trilogy set from Barnes and Noble in the summer of 2015.

What year or edition?

First editions, from audible.com and in paperback.

Did you finish it?

Yes, although it took me longer than expected... :-)

What's your verdict?

Fantastic series all around.  Brandon Sanderson is a really interesting world builder, and his magic systems are really well thought ought.  His characters are relatable, and this series features a really strong female lead.

The paperback versions have held up well under normal usage, although the paper and cover are a bit thin.  Considering the books are ~700 pages each, it doesn't seem like there's a lot that can be done about that.

The Audible series is narrated by Michael Kramer, who has become one of my favorite readers.  He found unique voices for the characters, and there are lots of them.  The only knock on him was that his voice for Elend (one of the main male characters) seemed to change between books 2 and 3.  There's quite a bit of character development between those two books, so it's certainly possible.  However, reading the words on the pages, I didn't make that change in my head.

Overall, both content and performance, 4.5 out of 5 stars!

What surprises did it hold, if any?

Without giving too much away, the first book had a really interesting and somewhat unexpected plot twist near the end.  That kind of thing continues to happen throughout the series, although never as dramatically as in the first book.

The other surprise has to do with the magic system.  You figure everything out about the races, the settings, etc, but a surprise about the magic system is revealed near the end of book 3 and is never resolved.  Apparently, Brandon Sanderson decided to play that one REALLY close to the vest... ;-)

Which scenes will stay with you?

There are lots of pretty memorable scenes.  The first time we see a mistwraith, the first ball, seeing the Lord Ruler (book 1), the assault on Cett and the attack on Luthadel (book 2), the rush to the end (book 3).  Describing these in any more detail would give more away than I want to.

Which characters will stay with you?

Vin, Vin, Vin, and Vin.  She's astounding.  Powerful, tough, vulnerable, delicate, complex, blunt -- like I said earlier, a fantastic lead.

Elend and Sazed to lesser extents, and Kelsier (of course).  Spook and Breeze as well.  And I suspect I'll think of TenSoon every time I see a big black wolf-ish canine for the near future at least.

What genre would you say it is?

Fantasy all the way.

Have you read anything else by this author?

Yes!  The first two of The Stormlight Archive and The Rithmatist. I've also got Elantris and Warbreaker on the shelf.  (and maybe Steelheart?  I'll have to check...)

Is it available today?

Yes, and you should go get it and read it, especially if you like fantasy.

Sunday, March 6, 2016

Cartagena, part 5

When last we spoke, we had managed to draw a board with spaces and icons.  Now it's time to fill in the players and finish up the web implementation.

Recall that we had left the server code in a state that if we called the new-game route without a :players keyword, we would default the new game to return a two-player game featuring tanya as orange and rusty as black.  I decided to work forward from here, because it let me defer the decision about how to capture the player data up front.  As it turns out, this was a great decision, because I'm terrible at user experience.  :-)

Calling the endpoint resulted in the server giving us a set of game data with two players. The players start in jail.  Now I needed to add the code that would actually display the players in that spot.

(defn jail []
  (when-let [jail (get-in @app-state [:board 0])]
    (apply concat
           (let [pirate-frequencies (frequencies (:pirates jail))
                 pirate-colors (vec (keys pirate-frequencies))]
             (for [player-index (range (count pirate-frequencies))]
               (let [pirate-color (get pirate-colors player-index)
                     pirate-count (pirate-color pirate-frequencies)
                     color-name (name pirate-color)
                     x (to-scale (+ 5 (* 10 player-index)))]
                 (for [pirate-index (range pirate-count)]
                   (let [y (to-scale (+ 35 (* 10 pirate-index)))]
                     [:circle
                      {:cx x
                       :cy y
                       :r (to-scale 4)
                       :fill color-name}]))))))))

I also needed to add this bit of rendering code to the main-view function, which was easy.

(defn main-view []
  [:center
   [:h1 "CARTAGENA"]
   [:div
    [:button
     {:class "btn btn-primary"
      :on-click (fn button-click [e]
                  (select-players!))}
     "New Game"]]

   [:div
    {:class "row"
     :style {:float "left" :margin-left 10 :margin-right 10}}
    [:div
     {:class "col-md-5"}
     (-> [:svg
          {:view-box (str "0 0 " (to-scale 501) " " (to-scale 301))
           :width (to-scale 501)
           :height (to-scale 301)}]
         (into (static-board))
         (into (jail))
         )]]])

The result was marvelously exciting!

Alright, I might've oversold that a little.  But the fact of the matter is, I had a set of players on the board, in the right position!  SO CLOSE TO BEING FINISHED!

...except that I really wasn't.  How in the world was I going to handle moving the pieces around?  Recall that the rules state that you can take three actions on a turn: playing a card to move a pirate forward, selecting a pirate to move backward in order to draw cards, and passing.  At this point, I can't play any cards because I can't tell who all has what.  I can't move backward, because there's nowhere to move backward to.  I can't even pass, because there's no action button or keystroke that will allow me to do that.

It looks like I'm going to have to break down and build a player area.  <insert ominous music here>

As I've already stated, I don't consider myself to be much of a user experience expert.  This became doubly apparent as I iterated through several different versions of the player area.  I finally landed on a design such that the player area is to the right of the game board.  Here's the code.  Notice that I baked it right into the main-view function, which makes that function needlessly cluttered.  I'm unapologetic.  It's not time to refactor yet...  :-D

(defn main-view []
  [:center
   [:h1 "CARTAGENA"]
   [:div
    [:button
     {:class "btn btn-primary"
      :on-click (fn button-click [e]
                  (select-players!))}
     "New Game"]]

   [:div
    {:class "row"
     :style {:float "left" :margin-left 10 :margin-right 10}}
    [:div
     {:class "col-md-5"}
     (-> [:svg
          {:view-box (str "0 0 " (to-scale 501) " " (to-scale 301))
           :width (to-scale 501)
           :height (to-scale 301)}]
         (into (static-board))
         (into (jail))
         )]]
   (when-let [active-player (active-player @app-state)]
     (let [{:keys [color cards] player-name :name} active-player
           card-groups (frequencies cards)]
       [:div
        {:class "col-md-4"}
        [:div {:style {:margin-right 10}}
         [:table {:class "table table-bordered table-responsive"}
          [:tbody
           [:tr
            [:td "Current Player"]
            [:td player-name]]]
          [:tr
           [:td "Color"]
           [:td
            [:svg
             {:width (to-scale 20)
              :height (to-scale 20)}
             [:circle
              {:cx (to-scale 10)
               :cy (to-scale 10)
               :r (to-scale 7)
               :fill (name color)}]]
            [:span {:style {:color (name color)}} (name color)]]]
          [:tr
           [:td "Actions Remaining"]
           [:td (:actions-remaining @app-state)]]
          [:tr
           [:td "Cards"]
           [:td (for [[card num] card-groups]
                  ^{:key card}
                  [:span {:style {:float "left"}}
                   [:figure
                    [:img
                     {:src (card icon-images)
                      :width (to-scale 30)
                      :height (to-scale 30)
                      :on-click (fn img-click [e]
                                  (select-card! card))}]
                    [:center [:figcaption num]]]])]]
          [:tr
           [:td "Selected Card"]
           [:td [:span {:style {:float "left"}}
                 (when-let [selected-card (:selected-card @app-state)]
                   [:img
                    {:src (selected-card icon-images)
                     :width (to-scale 30)
                     :height (to-scale 30)
                     :on-click (fn img-click [e]
                                 (unselect-card!))}])]]]
          [:tr
           [:td {:colSpan 2}
            [:center
             [:button
              {:class "btn"
               :on-click (fn btn-click [e]
                                   (update-active-player! @app-state))} "Pass"]]]]]]
        [:div
         [:p "To move forward, click a card, then click the target pirate.  To undo card selection, click the selected card."]
         [:p "To move backward, click the target pirate."]]]))])

This also required a couple of handler functions: select-card!, unselect-card! and update-active-player!  The update-active-player! function is actually a server request, since all of that logic is built into the game engine.

(defn update-active-player [state response]
  (assoc state :actions-remaining (:actions-remaining response)
               :current-player (:current-player response)))

(defn on-update-active-player [response]
  (swap! app-state update-active-player response))

(defn update-active-player! [{:keys [actions-remaining current-player player-order]}]
  (ajax/POST "http://localhost:3000/update-active-player"
             {:params {:actions-remaining actions-remaining
                       :current-player current-player
                       :player-order player-order}
              :handler on-update-active-player
              :error on-error}))

(defn select-card! [card]
  (reset! app-state (assoc @app-state :selected-card card)))

(defn unselect-card! []
  (reset! app-state (dissoc @app-state :selected-card)))

This set of code led to a usable UI, if not an aesthetically pleasing one.


Now we have the ability to choose cards (and unchoose them, if we accidentally clicked one) and pass, but we don't have the ability to choose a specific pirate to move forward or backward.  We also don't have any code to display pirates on any board space except the jail.  Time to add those bits to the appropriate spots.

;; TODO: this looks almost exactly like jail
(defn ship []
  (when-let [ship (get-in @app-state [:board 37])]
    (apply concat
           (let [pirate-frequencies (frequencies (:pirates ship))
                 pirate-colors (vec (keys pirate-frequencies))]
             (for [player-index (range (count pirate-frequencies))]
               (let [pirate-color (get pirate-colors player-index)
                     pirate-count (pirate-color pirate-frequencies)
                     color-name (name pirate-color)
                     x (to-scale (+ 445 (* 10 player-index)))]
                 (for [pirate-index (range pirate-count)]
                   (let [y (to-scale (+ 245 (* 10 pirate-index)))]
                     [:circle
                      {:cx x
                       :cy y
                       :r (to-scale 4)
                       :fill color-name
                       :on-click (fn ship-click [e]
                                   (pirate-click pirate-color 37))}]))))))))
                                   
(defn circles-for [space-index x y colors]
  (for [color-index (range (count colors))]
    (let [color (get colors color-index)
          color-name (name color)
          cx (to-scale (+ 35 x))
          cy (to-scale (+ y 5 (* 10 color-index)))]
      ^{:key color-index}
      [:circle
       {:cx cx
        :cy cy
        :r (to-scale 4)
        :fill color-name
        :on-click (fn circle-click [e]
                    (pirate-click color space-index))}])))

(defn normal-spaces []
  (apply concat
         (for [i (range 1 37)]
           (when-let [space-data (get-in @app-state [:board i])]
             (let [position (get piece-positions i)
                   x (:x position)
                   y (:y position)
                   space (normal-space x y)
                   image (space-image x y (:icon space-data))
                   pirates (circles-for i x y (:pirates space-data))]
               (conj [space image] pirates))))))

Notice the prolific pirate-click! function.  This is the function from whence the remaining server calls initiate.  It and its associated functions are a decent chunk of code, but it all feels very similar to the code I did for the console version.

;; TODO: figure out a way to make this work elegantly with callbacks
;; swiped from server code
(defn game-over?
  "Returns truthy if a player has 6 pirates on the ship; otherwise nil."
  [board]
  (let [ship (first (filter #(= :ship (:icon %)) board))
        pirate-counts-by-color (frequencies (:pirates ship))]
    (some #(>= (second %) 6) pirate-counts-by-color)))

(defn end-game [state]
  (assoc state :game-over true))

(defn end-game! []
  (swap! app-state end-game))

(defn play-card [state response]
  (-> state
      (assoc :board (:board response)
             :discard-pile (:discard-pile response)
             :players (conj (remove #{(active-player state)} (:players state)) (:player response)))
      (dissoc state :selected-card)))

(defn on-play-card [response]
  (let [board (:board response)]
    (swap! app-state play-card response)
    (if (game-over? board)
      (end-game!)
      (update-active-player! @app-state))))

(defn play-card! [player card from-space board discard-pile]
  (ajax/POST "http://localhost:3000/play-card"
             {:params {:player player
                       :icon card
                       :from-space from-space
                       :board board
                       :discard-pile discard-pile}
              :handler on-play-card
              :error-handler on-error}))

(defn move-back [state board response]
  (assoc state :board board
         :draw-pile (:draw-pile response)
         :discard-pile (:discard-pile response)
         :players (conj (remove #{(active-player state)} (:players state)) (:player response))))

(defn on-move-back [response]
  (when-let [board (:board response)]
    (swap! app-state move-back board response)
    (update-active-player! @app-state)))

(defn move-back! [player from-space board draw-pile discard-pile]
  (ajax/POST "http://localhost:3000/move-back"
             {:params {:player player
                       :from-space from-space
                       :board board
                       :draw-pile draw-pile
                       :discard-pile discard-pile}
              :handler on-move-back
              :error-handler on-error}))

(defn pirate-click [color from-space-index]
  (when (= color (:color (active-player @app-state)))
    (let [player (active-player @app-state)
          board (:board @app-state)
          from-space (get board from-space-index)
          discard-pile (:discard-pile @app-state)]
      (if-let [selected-card (:selected-card @app-state)]
        (play-card! player selected-card from-space board discard-pile)
        (move-back! player from-space board (:draw-pile @app-state) discard-pile)))))

Now, FINALLY, I can click pirates and expect them to render properly.  Every click of a pirate calls the server, passing the current game state, and receiving an updated board from the engine.  I'm not completely satisfied with it, since I wound up duplicating some server code in the web implementation to check for the end game condition.  However, given the amount of typing I'd already done and the fact that I want to make it multi-player next, I decided it wasn't worth the effort to clean up.

All that's left is gathering the initial player data.  As you might've guessed, there was more than a little give-up in me at this point.  I decided the simplest thing that could work would be to create a silly little set of inputs, one for each color.  I won't bother showing you the hiccup for that, but the form looks like:


Are you impressed yet?!

As you might imagine, filling in names and pressing the Start button creates a players map that gets sent in the request to the new-game endpoint.  The server initializes a game state given the player data, and away we go!

So this is the end.  We've finally slogged through the initial web implementation for Cartagena.  If you've followed me thus far, you deserve a medal for your determination. I certainly learned a lot along the way, and hope that you have too (or at least that you were entertained by my bumbling through it).

I do intend to implement the multi-player non-local version of this at some point in the future, but it might be a while.  If you have a burning desire to see it done, let me know in the comments.  Otherwise, thanks for sharing my adventures through this series, and stay tuned for non-Cartagena posts coming your way soon!  :-D





Saturday, February 20, 2016

Cartagena, part 4

Welcome back for the beginning of the conclusion to the Cartagena series!  In this installment, I'll be discussing the start of the web implementation for the game.

When we last spoke, I had finished a console implementation of the game.  The ultimate goal is to be able to play the game with other people across the Internet.  However, the next incremental step was to enable gameplay via the browser in a single session.  Luckily, this was handily done using Reagent and Figwheel.

Quick sidebar: I decided to house the entire effort in a single git repository with two separate projects: one clojure project that holds the game engine (as well as the console game) and another reagent project for the web UI.

I started the web project simply enough.  From my repository's root directory (/cartagena), I typed the following:

lein new figwheel cartagena-web -- --reagent

This magical incantation created an immediately runnable web project. To prove it, I changed to the newly created directory and launched the site.

cd cartagena-web
lein figwheel

Opening a browser to http://localhost:3449 proved that something great was happening. All that's left to do is fill in the blanks with the game board, player display, pieces, cards, colors, and other bits of logic...

OK, maybe that's more work than simply filling in blanks. :-)

Luckily, the server code was done (in principle).  Because I had left some code in the server to bootstrap a game for me, I didn't immediately have to implement a bunch of stuff to gather player information.  Instead, I could focus on creating routes that would call the server as necessary (such as when a game starts, when a player takes and action, etc).

I set up a ring server to handle requests coming from the web client.  Note that everything is a POST from the client (although I had a default GET in there for sanity's sake).


(ns cartagena.server
  (:require
    [compojure.core :refer :all]
    [compojure.route :as route]
    [ring.middleware.cors :as cors]
    [ring.middleware.transit :as trans]
    [ring.util.response :as response]
    [cartagena.core :as engine]))

(def default-players [{:name "tanya" :color :orange} {:name "rusty" :color :black}])
(defroutes app-routes
           (GET "/" [] "<h1>Hello World</h1>")

           (POST "/update-active-player" req
             (let [{:keys [actions-remaining current-player player-order]} (:body req)]
               (response/response (engine/update-current-player actions-remaining current-player player-order))))

           (POST "/play-card" req
             (let [{:keys [player icon from-space board discard-pile]} (:body req)]
               (response/response (engine/play-card player icon from-space board discard-pile))))

           (POST "/move-back" req
             (let [{:keys [player from-space board draw-pile discard-pile]} (:body req)]
               (response/response (engine/move-back player from-space board draw-pile discard-pile))))

           (POST "/new-game" req
             (if-let [players (get (:body req) :players)]
               (response/response (engine/new-game! players))
               (response/response (engine/new-game! default-players))))

           (route/not-found "<h1>Page not found</h1>"))

(def app
  (-> app-routes
      (cors/wrap-cors :access-control-allow-origin [#"http://localhost:3449"]
                      :access-control-allow-methods [:get :put :post :delete])
      (trans/wrap-transit-response {:encoding :json :keywords? true :opts {}})
      (trans/wrap-transit-body {:keywords? true :encoding :json, :opts {}})))

The results of all of the routes are the game states that the game engine provides, wrapped in transit for good measure.  This actually *almost worked* out of the box.  The most recent versions of all of the libraries required some poking and prodding to get interoperability working, but in the end turned out to be something that I could figure out without too much violence/swearing.

At this point, I've got transit moving from the server to the client.  It was time to start building the web interface.  The figwheel template had some scaffolding that allowed me to build the first parts of the UI without thinking too hard about them.

Ha!  Fooled you!  You thought I didn't think about it very much.  As it turns out, I *did* think about how the board should be laid out.  I went so far as to figure out what the (x, y) coordinates of each space should be, as well as how the space should be designated (with images representing the space's icon) and where the player pieces would be.  I wound up with something like the following:


The space in the upper left is the jail, the lower right is the ship, and below would be the player area.  Board spaces themselves would have a picture as well as three squares where pirates would sit.  Pretty nifty, huh?  NOW all that's left to do is fill in the blanks, right?

Yeah... not quite, still.  I wanted the board images to be dynamic (because the board that's returned from the server is randomized), so I started with just the static pieces.


(defn static-board []
  [;; board
   [:rect
    {:x 0
     :y 0
     :width (to-scale 500)
     :height (to-scale 300)
     :stroke "black"
     :stroke-width "0.5"
     :fill "burlywood"}]
   ;; jail
   [:rect
    {:x 0
     :y 0
     :width (to-scale 50)
     :height (to-scale 90)
     :stroke "black"
     :fill "darkgray"}]
   [:g
    {:dangerouslySetInnerHTML {:__html (str "<image xlink:href=\"img/jail.png\" x=0 y=0 width=\"" (to-scale 30) "\" height=\"" (to-scale 30) "\" />")}}]
   ;; ship
   [:rect
    {:x (to-scale 410)
     :y (to-scale 240)
     :width (to-scale 80)
     :height (to-scale 60)
     :stroke "black"
     :fill "sienna"}]
   [:g
    {:dangerouslySetInnerHTML {:__html (str "<image xlink:href=\"img/ship.png\" x=\"" (to-scale 410) "\" y=\"" (to-scale 240) "\" width=\"" (to-scale 30) "\" height=\"" (to-scale 30) "\" />")}}]
   ])


(defn main-view []
  [:center
   [:h1 "CARTAGENA"]
   [:div
    [:button
     {:class "btn btn-primary"
      :on-click (fn button-click [e]
                  (select-players!))}
     "New Game"]]

   [:div
    {:class "row"
     :style {:float "left" :margin-left 10 :margin-right 10}}
    [:div
     {:class "col-md-5"}
     (-> [:svg
          {:view-box (str "0 0 " (to-scale 501) " " (to-scale 301))
           :width (to-scale 501)
           :height (to-scale 301)}]
         (into (static-board))
         )]]])

(defn ^:export main []
  (when-let [app (. js/document (getElementById "app"))]
    (reagent/render-component [main-view] app)))

(main)

(defn on-js-reload []
  (main))

Notice a couple of things.  First of all, I've got a little helper called to-scale.  I found that bumping the resolution up or down was something I wanted to be able to do easily, and that function handles scaling.  Secondly, even though React (which is what is underneath Reagent) is supposed to handle svg images gracefully, it does not.  My first pass at this left me pretty discouraged, until Tim came to the rescue yet again and tipped me off to the :dangerouslySetInnerHTML bit.

Another quick aside: for a really great and far more in-depth view of how quickly you can get a game up and running in a browser using Reagent and Figwheel, have a look at Tim's Tetris implementation and write up.

The result of this bit of code looks something like:

At this point, I needed to start the implementation for painting the more dynamic board spaces.  I quickly realized that the position of the spaces wasn't going to vary any more than the jail or ship where, so I created a simple vector for those coordinates.


(def piece-positions
  [
   ;; jail
   {:x 0 :y 0}
   ;; row 1, left to right
   {:x 50 :y 60}
   {:x 90 :y 60}
   {:x 130 :y 60}
   {:x 170 :y 60}
   {:x 210 :y 60}
   {:x 250 :y 60}
   {:x 290 :y 60}
   {:x 330 :y 60}
   {:x 370 :y 60}
   {:x 410 :y 60}
   {:x 450 :y 60}
   ;; transition 1
   {:x 450 :y 90}
   ;; row 2, right to left
   {:x 450 :y 120}
   {:x 410 :y 120}
   {:x 370 :y 120}
   {:x 330 :y 120}
   {:x 290 :y 120}
   {:x 250 :y 120}
   {:x 210 :y 120}
   {:x 170 :y 120}
   {:x 130 :y 120}
   {:x 90 :y 120}
   {:x 50 :y 120}
   ;; transition 2
   {:x 50 :y 150}
   ;; row 3, left to right
   {:x 50 :y 180}
   {:x 90 :y 180}
   {:x 130 :y 180}
   {:x 170 :y 180}
   {:x 210 :y 180}
   {:x 250 :y 180}
   {:x 290 :y 180}
   {:x 330 :y 180}
   {:x 370 :y 180}
   {:x 410 :y 180}
   {:x 450 :y 180}
   ;; transition 3
   {:x 450 :y 210}
   ;; ship
   {:x 400 :y 240}])

Why a vector, you might ask?  As it turns out, the board pieces are returned from the server as a vector.  Iterating through it while using its index to find the space's x and y was really convenient.  And if you haven't noticed by now, I'm ALL about convenience...

Adding the code for a normal space display item and then adding the code to display normal spaces:


(def icon-images {:jail "img/jail.png"
                  :ship "img/ship.png"
                  :bottle "img/bottle.png"
                  :gun "img/gun.jpg"
                  :hat "img/hat.png"
                  :key "img/key.png"
                  :knife "img/knife.png"
                  :skull "img/skull.png"})

(defn normal-space [x y]
  [:rect
   {:x (to-scale x)
    :y (to-scale y)
    :width (to-scale 40)
    :height (to-scale 30)
    :stroke "black"
    :stroke-width "0.5"
    :fill "lightgray"}])

(defn space-image [x y icon]
  [:g
   {:dangerouslySetInnerHTML
    {:__html (str "<image xlink:href=\"" (icon icon-images) "\" x=\"" (to-scale x) "\" y=\"" (to-scale y) "\" width=\"" (to-scale 30) "\" height=\"" (to-scale 30) "\" />")}}])

(defn normal-spaces []
  (apply concat
         (for [i (range 1 37)]
           (when-let [space-data (get-in @app-state [:board i])]
             (let [position (get piece-positions i)
                   x (:x position)
                   y (:y position)
                   space (normal-space x y)
                   image (space-image x y (:icon space-data))]
               [space image])))))

For the code above, realize that the @app_state atom is populated by a POST to get the game state (either via New Game or another action).  Hence, the :board is the vector of spaces and their states returned from the server.

All that's left to do is add the call to normal-spaces to the main display function, and you wind up with a display like:



That's a lot to take in all at once, so let's pause here for reflection.  If you haven't read Tim's Tetris post, you definitely should.  Next time, we'll cover getting some pirates on the board, figuring out the current player display, and actually playing the game.

Or if you're in too big a hurry, head over to github to have a look at the final product.  :-)

Sunday, January 31, 2016

Cartagena, part 3

Good grief, January flew by.  It was a constant flurry of excited activity for me, from work and personal perspectives.  However, I've got an unclosed loop that I'd like to address here, regarding Cartagena.

To recap: in part 1, I started writing an engine for the Cartagena board game.  Part 2 dealt with incremental progress, as well as some missteps.  I'll summarize what happened next (part 3).

I finished out the console version of the game, separating what I felt like were game engine components from what was necessary to maintain the game state and interface via the console.  This resulted in two namespaces: cartagena.core and cartagena.console.  (Note: the current code repository has more stuff in it; don't let that confuse you).  Also, based on a comment from Jake, I made the engine itself completely stateless.

You can run the console game using a lein alias: lein console-game.  Doing so will result in something along the following lines:


The main glob of information is actually the board state itself.  This example shows a two player game, with orange and black pirates starting in the jail.  The first player it tanya, and she has six cards, with three actions left.


I opted to provide a default action so that the player doesn't have to actually press multiple buttons.  Both playing a card and moving a pirate backward require more input.  Playing a card prompts you to choose a card from your hand.


I got a bit lazy here.  The nice thing to do would be to number the cards and allow the player to choose a number again.  As it is, the player must type in the card's name (as a clojure keyword, no less!), unless they want to play the first card in their hand, in which case they can just hit Enter.

The last bit of information required is which pirate should be moved.  Specifically, the game asks from which space to move a pirate.  To begin with, all of a player's pirates are in jail (space 0).


A nice-to-have would be for the game to interrogate the board to determine what your possible moves are, and then only present you with viable options.  If only one option were viable, it would perform that action.  As it is, the game isn't helping you out at all... ;-)

After the action is performed, the board and active player state are re-displayed.


Notice the orange pirate in space 3, as well as the fact that the :hat card is gone from tanya's hand.  The game lets you know that you only have 2 actions left.  

Here's a quick screen cap of tanya taking her last action on the turn.

After moving another pirate out of jail, tanya is out of actions.  The active player automatically rotates to the next person.


As you can see, rusty is now the active player, and tanya is obviously going to win.

Gameplay continues on in the manner shown above, with pirates moving forward and backward until one player has successfully ensconced all of their pirates aboard the ship.  At that point, that player is declared the winner and the game ends.  I don't actually have a screenshot of that, because I've never actually won.  As I've already stated, tanya wins every game we play.  :-)

So, where do I go from here?  I know the engine isn't perfect, and I also know the console game is buggy.  However, it's certainly good enough to move forward to whatever's next.  The entire reason I started down this path was to learn something new.  The game has been good practice so far, but ultimately I want to play this thing in the browser, preferably with multiple remote players.

SPOILER ALERT! I've already finished the first version of the web code as well, but I'm not going to blog about that until next time.  It will be well before the end of February when that entry comes out.  I'd like to get back into the habit of about an entry a week, so help keep me honest, people!  :-D