Apple Education Event、Land of Lisp

随分遅くなりましたが、本年も宜しくお願いします。

Apple Education Event, January 2012
http://events.apple.com.edgesuite.net/1201oihbafvpihboijhpihbasdouhbasv/event/index.html
Apple Keynotes - iTunes Podcast
http://itunes.apple.com/jp/podcast/apple-keynotes/id275834665

1月19日、Apple はニューヨークで開催したメディアイベントで、「iBook 2」、電子書籍作成アプリ「Books Author」、iOS向けに「iTunes U」のアプリを発表しました。

早速、iBooks Author をダウンロードし、試してみました。ウィジェットを追加できる仕組みは、可能性を感じますね。OS 純正の Calculator.wdgt を追加して遊んでみました。ウィジェクト作成ソフト Dashcode も随分進歩しています。すっかり、使い方を忘れてしまったので、

ヘルプ/Dashcode User Guide
▼ デュアルプロダクトWebアプリケーションチュートリアル
▼ プロジェクトを開始する
▼ ウィジェットまたはWebアプリケーションのユーザインターフェイスをデザインする
▼ テストする/共有する
Webアプリケーションを動作形式で保存する

を拾い読みし、基本操作を学びました。

Dashcode で Dashboard のテンプレートから、カスタムを選びます。プロジェクトウインドウ左側 (ナビゲータ) の ‘実行と共有’ をクリックします。ウインドウ右下に ‘ディスクに保存…’ ボタンが現れるのでクリックすると、拡張子が wdgt 形式のファイルで保存されます。iBooks Author では、中央の ‘ウィジェット‘ ボタンから ‘HTML’ を選択します。現れたエリアにウィジェットをドラッグ・ドロップすれば追加できます。プレビューするには、iPad が必要になります。拙作 YKMinesweeper.wdgt を作り直してみたいですね!

Land of LISP: Learn to Program in Lisp, One Game at a Time!

Land of LISP: Learn to Program in Lisp, One Game at a Time!

以前にも書いたように、Land of Lisp [ asin:1593272812 ] を読書中です。サンプル orc-battle.lispsbcl で実行してみました。sbcl については Homebrew

brew update
brew install sbcl

を利用してインストールしました。

sbcl を起動し、 load 関数で orc-battle.lisp を読み込み、(orc-battle) で実行しました。

~ $ sbcl
This is SBCL 1.0.53, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (load "~/lisp/Land of Lisp/orc-battle.lisp")
T
* (orc-battle)
You are a valiant knight with a health of 30, an agility of 30, and a strength of 30
Your foes:
1. (Health=3) A fierce BRIGAND
2. (Health=6) A malicious hydra with 6 heads.
3. (Health=2) A fierce BRIGAND
4. (Health=6) A slime mold with a sliminess of 4
5. (Health=6) A wicked orc with a level 6 club
6. (Health=7) A fierce BRIGAND
7. (Health=1) A wicked orc with a level 8 club
8. (Health=8) A fierce BRIGAND
9. (Health=10) A malicious hydra with 10 heads.
10. (Health=4) A slime mold with a sliminess of 5
11. (Health=7) A malicious hydra with 7 heads.
12. (Health=4) A fierce BRIGAND

キーの入力状態になるのですが、入力状態の前に出力した文字列が表示されません。sbcl の場合 (read) を呼ぶ前に (force-output) を呼ぶ必要があります。

orc-battle.lisp の場合

(defun player-attack ()
(fresh-line)
(princ "Attack style: [s]tab [d]ouble swing [r]oundhouse:")
(force-output)
(case (read)
(s (monster-hit (pick-monster)
(+ 2 (randval (ash *player-strength* -1)))))
(defun pick-monster ()
(fresh-line)
(princ "Monster #:")
(force-output)
(let ((x (read)))

上記のように (read) の前に、(force-output) を2箇所、追加しました。これで正しく動作しました。

* (orc-battle)
You are a valiant knight with a health of 30, an agility of 30, and a strength of 30
Your foes:
1. (Health=3) A fierce BRIGAND
2. (Health=6) A malicious hydra with 6 heads.
3. (Health=2) A fierce BRIGAND
4. (Health=6) A slime mold with a sliminess of 4
5. (Health=6) A wicked orc with a level 6 club
6. (Health=7) A fierce BRIGAND
7. (Health=1) A wicked orc with a level 8 club
8. (Health=8) A fierce BRIGAND
9. (Health=10) A malicious hydra with 10 heads.
10. (Health=4) A slime mold with a sliminess of 5
11. (Health=7) A malicious hydra with 7 heads.
12. (Health=4) A fierce BRIGAND
Attack style: [s]tab [d]ouble swing [r]oundhouse:

終了するには、(sb-ext:quit) を実行します。

 You have been killed. Game Over.
NIL
* (sb-ext:quit)
~ $ 

sbcl ではなく clisp も試したくなったのですが、Homebrew でインストールするとビルドに失敗します。llvm-gcc ではなく gcc が必要なようです。
自分で gcc をビルドするのは大変なので

kennethreitz/osx-gcc-installer - GitHub
https://github.com/kennethreitz/osx-gcc-installer

の osx-gcc-installer を利用しました。Xcode をインストールした状態で osx-gcc-installer をインストールしたので、問題があるかもしれません。

Homebrew で gcc を使ってビルドするには、環境変数 CC を設定するのではなく

brew install clisp --use-gcc

オプション –use-gcc を使うと良いようです。

clisp を使って orc-battle.lisp を実行するには、sbcl のような (force-output) を追加する必要はありません。

~ $ clisp
i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
I I I I I I I      8     8   8           8     8     o  8    8
I  \ `+' /  I      8         8           8     8        8    8
\  `-+-'  /       8         8           8      ooooo   8oooo
`-__|__-'        8         8           8           8  8
|            8     o   8           8     o     8  8
------+------       ooooo    8oooooo  ooo8ooo   ooooo   8
Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>
Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010
Type :h and hit Enter for context help.
[1]> (load "~/lisp/Land of Lisp/orc-battle.lisp")
;; Loading file /Users/yoshiaki/lisp/Land of Lisp/orc-battle.lisp ...
;; Loaded file /Users/yoshiaki/lisp/Land of Lisp/orc-battle.lisp
T
[2]> (orc-battle)
You are a valiant knight with a health of 30, an agility of 30, and a strength of 30
Your foes:
1. (Health=8) A wicked orc with a level 1 club
2. (Health=9) A malicious hydra with 9 heads.
3. (Health=1) A fierce BRIGAND
4. (Health=9) A slime mold with a sliminess of 3
5. (Health=9) A malicious hydra with 9 heads.
6. (Health=8) A wicked orc with a level 1 club
7. (Health=2) A slime mold with a sliminess of 5
8. (Health=7) A malicious hydra with 7 heads.
9. (Health=5) A fierce BRIGAND
10. (Health=2) A slime mold with a sliminess of 4
11. (Health=5) A malicious hydra with 5 heads.
12. (Health=5) A slime mold with a sliminess of 5
Attack style: [s]tab [d]ouble swing [r]oundhouse:
NIL
[3]> (exit)
Bye.
~ $ 

clisp を終了するに、(exit) を使ってください。

orc-battle.lisp は、簡単なプログラムです。それで、ゲームに勝つのもそんなに難しくないと思っていました。ところが、なかなか勝つことができません。それで、ソースを熟読して攻撃パターンや、ヘルスポイントの計算方法を理解し、ようやく勝てるようになりました。単純なゲームですが、熱中してしまいました。絶妙なバランスです。ソースの熟読には、別の理由もあったのですが!

orc-battle.lisp で勝利した動画を添付しておきます。後、おまけとして、最近取り組んでいたプログラムの動作模様も公開します。( 04 分 01 秒から始まります。)

現在、Kindle 版 Land of Lisp

IV. Lisp is Science
15. Dice of Doom, a Game Written in the Function Style
Implementing Dice of Doom, Version 1
P312
Location 7551
Calculating Passing Moves

まで読んだのですが、Common Lisp を理解するには、最良の本ですね!Practical Common Lisp も持っているのですが、Land of Lisp の方が基本的な知識を得ることができます。Practical Common Lisp では、途中で投げ出してしまったので、気付かなかったのですが、Common Lisp では、構造体も使えるのですね!

P163
Location 4314
Common Lisp Structures
> (defstruct person
name
age
waist-size
favorite-color)
PERSON
> (defparameter *bob* (make-person :name "Bob"
:age 35
:waist-size 32
:favorite-color "blue"))
> *bob*
#S(PERSON :NAME "Bob" :AGE 35 :WAIST-SIZE 32 :FAVORITE-COLOR "blue")
> (person-age *bob* )
35
> (setf (person-age *bob* ) 36)
> (person-age *bob* )
36