This is the second session of the day in GA 1112. 

Adrian German

Lindley Hall 204

dgerman@indiana.edu

C212/A592 Introduction to Software Systems

Canvas 

http://www.cs.indiana.edu/classes/c212

What's New? 

What's Due? 

Class Notes

Grades:

  Attendance          5%
  Labs               15%
  Homework           15% 
  Semester Project   15% 
  Early Eval         10%  Sep 21
  Midterm Exam       15%  Oct 06 
  Project Readiness  10%  Nov 06 (all dates are tentative until tomorrow 08/22) 
  Final Exam         15%  Dec 11 here from 12:30-2:30pm

2:30 and 3:30 p.m. M,W,F,daily 12:30-2:30 p.m., Mon., December 11

http://registrar.indiana.edu/calendars/exam-schedule.shtml

Everyone that was on the roster on Fri has a silo account.

Minute paper:

  (a) Name/Username 
  (b) Major/Minor
  (c) Reasons for taking this class
  (d) Is class required?
  (e) Expectations 
  (f) What would you like to be able to do at the end to feel accomplished?
  (g) Questions/comments
  (h) Fears/concerns

  (i) What class did you take before this one? 
      What was your experience with C211. Who was your instructor? 
      How much Unix do you know? What is your exposure to Unix.

http://registrar.indiana.edu/browser/soc4178fac/CSCI/CSCI-C212.shtml

telnet used to be a program to connect to remote machines

telnet still exists but is not used to connect because it's not secure

to connect securely to silo.cs.indiana.edu you need an ssh client

https://iuware.iu.edu/Windows/List/140

login as: jsivakum
jsivakum@silo.cs.indiana.edu's password:


*******************************************************************
**   Indiana University School of Informatics and Computing      **
**             ** For Authorized Use Only **                     **
*******************************************************************
**  For general SoIC computing information, please see:          **
**      http://help.soic.indiana.edu/                            **
**                                                               **
**  To submit a problem report or question, please see:          **
**      http://help.soic.indiana.edu/request                     **
*******************************************************************

[jsivakum@silo ~]$ ls
bin  README
[jsivakum@silo ~]$ ls -l
total 4
drwx------ 2 jsivakum students  10 Aug 18 13:57 bin
-rw------- 1 jsivakum students 485 Aug 18 13:57 README
[jsivakum@silo ~]$ file README
README: ASCII text
[jsivakum@silo ~]$ file bin
bin: directory
[jsivakum@silo ~]$ man file
[jsivakum@silo ~]$ man man
[jsivakum@silo ~]$

On the Mac use Terminal command prompt and type: 

    ssh jsivakum@silo.cs.indiana.edu

AP Physics has topics like:

Kinematics
Dynamics
Work, Energy and Power
Linear Momentum & Impulse
Rotational Kinematics
Rotational Dynamics
Universal Gravitation
Simple Harmonic Motion
Mechanical Waves
Electrostatics
Electricity

C211 proposes a design recipe:

  (a) choose a data representation
  (b) give some examples of the data representation 
  (c) name your function, its arguments (signature) 
  (d) types of arguments, purpose statement
  (e) some examples of how the function would work
  (f) choose the right template 
  (g) flesh it out (actually write code)
  (h) write and pass your check-expects (from e)

Like Physics C211 proposes the followingh taxonomy:

  -- simple atomic templates (work with fixed-size data) 

  -- structural recursion (implicit termination condition) 
http://www.ccs.neu.edu/home/matthias/HtDP2e/
BSL, ISL/ISL+, ASL, Racket. 
  -- generative recursion (you have to provide the termination condition)
  -- accumulator-passing style (invariants for each accumulator) 


(require 2htdp/image)
(require 2htdp/universe)

; A Point is a (make-point Number Number)
(define-struct point (x y))

; A Circle is a (make-circl Point Number)
(define-struct circl (center radius))

; A ManyCircl is one of:
;   -- empty

;   -- (cons Circle ManyCircl)

; A World is a ManyCircl 

; (big-bang initial
;           (to-draw ...)
;           (on-tick ...)
;           (on-mouse ...))

(define initial empty)

; render : World -> Image
; (define (render world)
;   (cond ((empty? world) ...)
;         (else (... (first world) ... (render (rest world)) ...))))
(define (render world)
  (cond ((empty? world) (empty-scene 400 400))
        (else (place-image (circle (circl-radius (first world)) "outline" "red")
                           (point-x (circl-center (first world)))
                           (point-y (circl-center (first world)))
                           (render (rest world))))))

(define sample-world (cons (make-circl (make-point 300 300) 30) 
                      (cons (make-circl (make-point 200 100) 80)
                       (cons (make-circl (make-point  50  50) 10) empty))))

; enlarge : circl -> circl
; increases the radius by 1 pixel 
(define (enlarge circl)
  (make-circl (circl-center circl) (add1 (circl-radius circl))))

; update : world -> world

(define (update world)
  (cond ((empty? world) world)
        (else (cons (enlarge (first world))
                    (update (rest world))))))

; meh : world x y event -> world

(define (meh world x y event)
  (cond ((mouse=? "button-down" event) 
         (cons (make-circl (make-point x y) 1)
               world))
        (else world)))

(big-bang sample-world
          (to-draw render)
          (on-tick update)
          (on-mouse meh))

In this class we will write our own Big Bang.

; Here's a legitimate and plausible student solution. Joel Bierman and 
; Kristopher Jung get credit for various parts of the code below as well
; as for coming to office hours to discuss this problem. Others too, but
; their influence in the code below is not as apparent. 

; So what can you find below? Let's remember that:

; (a) Homework 05 was solved in lecture twice. 
; (b) snake movement was designed in lecture and posted.
; (c) you were mostly responsible for
;       -- change direction
;       -- eat food
;       -- grow 
;       -- stop when snake steps out or over itself

; I am going to try to mark those spots for you below.
;
;
; Note: use ISL/+ with the code below. 
;

(require 2htdp/image)                                            ; this was given in the problem text
                                                                 ; 
; A World is:                                                    ;
;-- (make-world Snake Food)                                      ;
(define-struct world (snake food))                               ;
                                                                 ;
; A Snake is:                                                    ;
;-- (make-snake Direction Head Body)                             ;
(define-struct snake (direction head body))                      ;
                                                                 ;
; A Direction is one of:                                         ;
;-- "north"                                                      ; 
;-- "east"                                                       ;
;-- "south"                                                      ;
;-- "west"                                                       ;
                                                                 ;
; A Food is [List of Posn]                                       ;
                                                                 ;
; A Head is a Posn                                               ;
                                                                 ;
; A Body is [Listof Posn]                                        ; all the way up to here: given 

;1.                                                                                            ; this was developed in lecture as an example 
                                                                                               ; later it was provided as is for Homework 06 
(define s1 (make-snake "north" (make-posn 200 200) '()))                                       ;
                                                                                               ;
;2.                                                                                            ;
                                                                                               ;
(define f1 (list (make-posn 300 300) (make-posn 151 327)))                                     ;
                                                                                               ;
;3.                                                                                            ;
                                                                                               ;
(define w1 (make-world s1 f1))                                                                 ;
                                                                                               ;
;4.                                                                                            ;
                                                                                               ; 
(define R 5)                                                                                   ;
(define s2 (make-snake "north" (make-posn 200 200) (list (make-posn 200 (+ 200 (* 2 R)))       ;
                                                       (make-posn 200 (+ 200 (* 4 R)))         ;
                                                       (make-posn 200 (+ 200 (* 6 R)))         ;
                                                       (make-posn 200 (+ 200 (* 8 R)))         ;
                                                       (make-posn 200 (+ 200 (* 10 R)))        ;
                                                       (make-posn 200 (+ 200 (* 12 R))))))     ;
                                                                                               ;
;5.                                                                                            ;
                                                                                               ;
(define f2 (list (make-posn 123 21)                                                            ;
                 (make-posn 178 239)                                                           ;
                 (make-posn 98 51)                                                             ;
                 (make-posn 259 371)                                                           ;
                 (make-posn 309 108)))                                                         ;
                                                                                               ;
;6.                                                                                            ;
                                                                                               ;
(define w2 (make-world s2 f2))                                                                 ; all of this: done in class (lecture) 
                                                                                               ; 
;7.                                                                 ;
                                                                    ; this was also designed in class but not posted
; render-food : Food Image -> Image                                 ; I told you not to take pictures or notes
;                                                                   ; however I wanted you to watch, think, ask questions
(define (render-food food image)                                    ; later on we posted huge clues as part of Homework 06
  (cond                                                             ; in which we reminded you how these functions should work
    [(empty? food) image]                                           ;
    [else (place-image (circle 5 "solid" "gold")                    ;
                       (posn-x (first food)) (posn-y (first food))  ;
                       (render-food (rest food) image))]))          ;
                                                                    ;
; render-snake-body : Body Image -> Image                           ;
;                                                                   ;
(define (render-snake-body body image)                              ;
  (cond                                                             ;
    [(empty? body) image]                                           ;
    [else (place-image (circle 5 "solid" "red")                     ;
                       (posn-x (first body)) (posn-y (first body))  ;
                       (render-snake-body (rest body) image))]))    ;
                                                                    ;
; render-snake-head : Head Image -> Image                           ;
;                                                                   ;
(define (render-snake-head head image)                              ;
        (place-image (circle 5 "solid" "blue")                      ;
                                   (posn-x head) (posn-y head)      ;
                                   image))                          ;
; render-world : World -> Image                                     ;
;                                                                   ;
(define (render-world world)                                        ;
       (render-snake-head (snake-head (world-snake world))          ;
         (render-snake-body (snake-body (world-snake world))        ;
          (render-food (world-food world) (empty-scene 400 400))))) ;
                                                                    ; so up to here: should be pretty clear

; HOMEWORK 6                                ; this is where the new stuff starts (more or less) 
                                            ;
;8, 10, 11                                  ;
                                            ; 
; DistanceHelp : Posn Posn -> Number        ;
;                                           ;
(define (DistanceHelp a b)                  ; this is something we discussed in Lab 07 (helps with overlaps?)
  (sqrt (+ (sqr (- (posn-x a) (posn-x b)))  ;
           (sqr (- (posn-y a) (posn-y b))))))
                                            ;
; FoodClose? Posn Posn : Boolean            ;
; will be called with a food item and the position of the head and determines if the item vanishes or not
(define (FoodClose? a b)                    ;
;  (cond                                    ;
;    [(empty? b) "false"]                   ; and this is a legacy function Joel had in his code
;    [(list? b)                             ;
     (if (<= (DistanceHelp a b) ; (first a));
                           10) true false)) ; (DistanceHelp a (rest a)))) ; )) so up to here: still easy 
                                            ; 
; new-head : Snake Number Number -> Head                                            ; from here on we refactored something designed/developed in lecture
; based on displacements (which reflect direction of movement) generate new head    ; if you want to review the original code go to May 25 What's New?
(define (new-head s dx dy)                                                          ; here's the page:
  (make-posn (+ dx (posn-x (snake-head s)))                                         ;   http://silo.cs.indiana.edu:8346/c211/sum2017/0525a.phps
             (+ dy (posn-y (snake-head s)))))                                       ; 
                                                                                    ; 
; new-body : World -> Body                                                          ; however below we grow the snake if needed 
; produces the new body of the moving snake                                         ; that part is new
(define (new-body w)                                                                ;
  (if (not (ormap (lambda (item)                                                    ;
                    (FoodClose? item (snake-head (world-snake w))))                 ;         <----      here's when it needs to happen 
                  (world-food w)))                                                  ; 
      (cons (make-posn (posn-x (snake-head (world-snake w))) ; if true I don't get longer 
                       (posn-y (snake-head (world-snake w))))                       ;
            (reverse (rest (reverse (snake-body (world-snake w))))))                ; 
      (cons (make-posn (posn-x (snake-head (world-snake w))) ; else I grow by one segment     <-----     here's how it happens
                       (posn-y (snake-head (world-snake w))))                       ; 
            (snake-body (world-snake w)))))                                         ; so there's one new thing here
                                                                                    ; outside of the refactoring 
                                                                                    ; (which was optional but benefic)
; move-snake : World -> Snake                                                       ;
;                                                                                   ;
(define (move-snake w)                                                              ; look how simple this becomes
  (cond                                                                             ; we wrote this in class on 5/25
    [(string=? "north" (snake-direction (world-snake w)))                           ; but it's so much shorter with
     (make-snake "north" (new-head (world-snake w)   0 -10) (new-body w))]          ; the new functions new-head/body
    [(string=? "south" (snake-direction (world-snake w)))                           ;
     (make-snake "south" (new-head (world-snake w)   0  10) (new-body w))]          ;
    [(string=?  "east" (snake-direction (world-snake w)))                           ;
     (make-snake  "east" (new-head (world-snake w)  10   0) (new-body w))]          ;
    [(string=?  "west" (snake-direction (world-snake w)))                           ;
     (make-snake  "west" (new-head (world-snake w) -10   0) (new-body w))]          ;
    (else (error "What's going on?"))))                                             ; so so far: just one new thing, growing
                                                                                    ;
(define (change s a-key)                                                 ;
  (cond                                                                  ;
        [(key=? a-key "left")                                            ; this was not done in class
         (make-world (make-snake "west" (snake-head (world-snake s))     ; I may have told you it's easy
                                        (snake-body (world-snake s)))    ; (since it obviously is) 
                     (world-food s))]                                    ; and how easy it is and why
        [(key=? a-key "up")                                              ; but you were to develop and 
         (make-world (make-snake "north" (snake-head (world-snake s))    ; integrate this yourselves 
                                         (snake-body (world-snake s)))   ; so that was something to do
                     (world-food s))]                                    ;
        [(key=? a-key "right")                                           ; 
         (make-world (make-snake "east" (snake-head (world-snake s))     ;
                                        (snake-body (world-snake s)))    ;
                     (world-food s))]                                    ; 
        [(key=? a-key "down")                                            ; it feels good when the snake moves
         (make-world (make-snake "south" (snake-head (world-snake s))    ; it feels even better when it turns
                                         (snake-body (world-snake s)))   ;
                     (world-food s))]))                                  ; now you can control the snake! 

; remaining-food : World -> Food                   ;
;                                                  ; now this is something I helped many of you with
(define (remaining-food w)                         ; during office hours. In all cases I encouraged you
  (filter (lambda (item)                           ; to switch to ISL/+ since you get to use filter. We
            (not (FoodClose? item                  ; postponed this homework many times and by the time
                             (snake-head           ; it became urgent (as we were approaching the final
                              (world-snake w)))))  ; deadline) you were already relatively fluent in ISL
          (world-food w)))                         ; and even ISL/+ where we don't need to do anything too
                                                   ; fancy with lambdas just use them anonymously in place
; update-world : World -> World                    ;
;                                                  ; the function above is a helper, it helps filter food
(define (update-world w)                           ;
  (make-world                                      ; remaining food is what isn't too close to the snake's head
   (move-snake w)                                  ;
   (let ((a (remaining-food w)))                   ; see how we calculate it here and remember it with a let 
     (if (< (length a)                             ; then compare it's length with it's previous length...
            (length (world-food w)))               ;             ... if we lose at least one food item (and
         (cons (make-posn (random 400) (random 400)) a) ;            since the distribution is entirely random
         a))))                                     ;                 we might lose multiple if they're placed
                                                   ;                 too closely apart (like in a cluster)
                                                   ; then we need to add a new item randomly placed.
                               ; 
(define initialworld w2)       ; getting ready to start big-bang                    
                               ; 
;9.                                                ; there's a small chance one can eat all the food    
                                                   ; very unlikely but in that rare case we can maybe
(require 2htdp/universe)                           ; consider the user actually won the game (code not included for that )
                                          ;               
;ending scene                             ; this is the ending scene. One of the few that used it well was
(define (say-goodbye world)               ; Kristopher. I changed what he had though, but not in essence. His
  (place-image (text "GAME" 70 "black")   ; game is played in a much bigger window, that's basically all. 
                200 150                   ;
               (place-image (text "OVER" 70 "black")
                             200 250      ;
                            (render-world world))))
                                          ;
                                          ;
; it-ends : World -> Boolean                                    ; this predicate determines if the game ends or not
;                                                               ; it checks for two things: 
(define (it-ends world)                                         ;
  (or (< (posn-x (snake-head (world-snake world))) -400)        ; (a) if the snake goes too far the game ends
      (< (posn-y (snake-head (world-snake world))) -400)        ;
      (> (posn-x (snake-head (world-snake world)))  800)        ; 
      (> (posn-y (snake-head (world-snake world)))  800)        ;
      (ormap (lambda (body-element)                             ; (b) if the snake trips over itself that's fatal too
               (overlaps? (snake-head (world-snake world))      ; 
                           body-element))                       ; so in my view here the snake is allowed to get
             (snake-body (world-snake world)))))                ; out of the game scene, sometimes that's useful
                                                                ; but it can't get too far (-800 < x, y < 800). 
; overlaps : Posn Posn -> Boolean     ; I wrote a second 
;                                     ; function to determine 
(define (overlaps? a b)               ; overlapping because Joel's 
  (<= (DistanceHelp a b) 8))          ; was a bit too greedy (and snake could not survive it)
      
(big-bang initialworld                         ; and that's it
          (to-draw render-world)               ; you know this 
          (on-tick update-world)               ; variation: (on-tick update-world 0.5) 0.5 is in seconds
          (on-key change)                      ; you also know this
          (stop-when it-ends say-goodbye))     ; this is new, there's a predicate and a scene function 


; -- you can just copy and paste the above in DrRacket/ISL+ and run it. 

DrJava and read Chapter 1 for Wed. 

--