2017年4月3日月曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 2.(Going further - Writing Real Code)、How the while loop works の SHARPEN YOUR PENCIL(No. 1332) を Scheme で取り組んでみる。

SHARPEN YOUR PENCIL(No. 1332)

コード(Emacs)

(begin
  (define location1 3)
  (define location2 4)
  (define location3 5)
  (define hits 0)
  (define gusses 0)
  (define sunk? #f)
  (define (sunk bln)
    (if (not bln)
        ((lambda ()
           (display "Ready, aim, fire! (enter number from 0-6): ")
           (define guess (read))
           (if (and (integer? guess)
                    (<= 0 guess 6))
               ((lambda ()
                  (if (or (= guess location1)
                          (= guess location2)
                          (= guess location3))
                      (set! hits (+ hits 1)))
                  (display hits)
                  (newline)
                  (sunk bln)))
               ((lambda ()
                  (display "Please enter a valid cell number!\n")
                  (sunk bln))))))))
  (sunk #f)
  'done)

入出力結果(Terminal, REPL)

$ ./ksi
> (load "./battleship.scm")
Ready, aim, fire! (enter number from 0-6): -1
Please enter a valid cell number!
Ready, aim, fire! (enter number from 0-6): 0
0
Ready, aim, fire! (enter number from 0-6): 1
0
Ready, aim, fire! (enter number from 0-6): 2
0
Ready, aim, fire! (enter number from 0-6): 3
1
Ready, aim, fire! (enter number from 0-6): 4
2
Ready, aim, fire! (enter number from 0-6): 5
3
Ready, aim, fire! (enter number from 0-6): 6
3
Ready, aim, fire! (enter number from 0-6): 7
Please enter a valid cell number!
Ready, aim, fire! (enter number from 0-6):   C-c C-c
$

0 コメント:

コメントを投稿