Skip to content

Commit

Permalink
Added file reading for pre-defined levels.
Browse files Browse the repository at this point in the history
Two pre-defined map configurations have been added as well as a couple
of helper functions that turn the configuration in that file into a map
by making all 1s unwalkable, and making S the start position and E the
end position.
  • Loading branch information
packetpirate committed Apr 26, 2015
1 parent 6c69d50 commit 9752a1a
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 26 deletions.
61 changes: 47 additions & 14 deletions constants.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,49 @@
(define PATH '())
(define PATH-POS 0)


; Setup the sound settings and load the guitar sounds.
(define-runtime-path demos "demos")
(define song (rs-read (build-path demos "Guitar.C4E4.wav")))
(define song1 (rs-read (build-path demos "1st_String_E_64kb.wav")))
(define song2 (rs-read (build-path demos "2nd_String_B__64kb.wav")))
(define song3 (rs-read (build-path demos "3rd_String_G_64kb.wav")))
(define song4 (rs-read (build-path demos "4th_String_D_64kb.wav")))
(define song5 (rs-read (build-path demos "5th_String_A_64kb.wav")))
(define song6 (rs-read (build-path demos "6th_String_E_64kb.wav")))
(define song7 (rs-read (build-path demos "C_64kb.wav")))
(define song8 (rs-read (build-path demos "D_64kb.wav")))
(define song9 (rs-read (build-path demos "Dm_64kb.wav")))
(define song10 (rs-read (build-path demos "E_64kb.wav")))
(define Sample-rate 44100.0)
(define (s sec) (* sec Sample-rate))

; The following are some pre-defined guitar sounds.
(define n1 (clip song (s 0) (s 1)))
(define n2 (clip song (s 9) (s 10)))
(define n3 (clip song (s 19) (s 20)))
(define n4 (clip song (s 25) (s 26)))
(define n5 (clip song (s 31) (s 32)))
(define clips (list n1 n2 n3 n4 n5))
(define s1 (clip song1 (s 2) (s 4)))
(define s2 (clip song2 (s 2) (s 4)))
(define s3 (clip song3 (s 2) (s 4)))
(define s4 (clip song4 (s 2) (s 4)))
(define s5 (clip song5 (s 2) (s 4)))
(define s6 (clip song6 (s 2) (s 4)))
(define s7 (clip song7 (s 1.5) (s 3.5)))
(define s8 (clip song8 (s 1) (s 3)))
(define s9 (clip song9 (s 1) (s 3)))
(define s10 (clip song10 (s 1) (s 3)))

(define clips (list s1 s2 s3 s4 s5 s6 s7 s8 s9 s10))

; Define the start and end position.
(define start (cons 3 4))
(define goal (cons 10 9))
(define start (cons 0 0))
(define (moveStart row col)
(set! start (cons col row)))

(define goal (cons 0 0))
(define (moveGoal row col)
(set! goal (cons col row)))

; Define the player's current position. Subject to change throughout execution.
(define player (cons 3 4))
(define player (cons 0 0))
(define (movePlayer row col)
(set! player (cons col row)))

; Resize the display window.
(define (resize w h)
Expand Down Expand Up @@ -68,6 +91,15 @@
(define (compF g h)
(+ g h))

; Checks if the given row and column are within range of the grid.
(define (validRowCol? row col)
(if (or (< row 0)
(> row (- GRID_SIZE 1))
(< col 0)
(> col (- GRID_SIZE 1)))
#f
#t))

; Used to access elements of a one-dimensional array representing a two-dimensional array.
(define (get row col)
(cond ((or (> row (- GRID_SIZE 1)) (< row 0)) (error "Row out of bounds!"))
Expand All @@ -81,10 +113,11 @@
(let ([ne '()])
; Use the offsets defined in NEIGHBORS to get the tiles around the current tile.
(map (lambda (p)
(let ([a ((get (+ (send t getRow) (car p)) (+ (send t getCol) (cadr p))) GRID)])
(let ([a (cons (+ (send t getRow) (car p)) (+ (send t getCol) (cadr p)))])
; Only get the walkable tiles.
(when (send a isWalkable)
(set! ne (append ne (list a))))))
(when (and (validRowCol? (car a) (cdr a))
(send ((get (car a) (cdr a)) GRID) isWalkable))
(set! ne (append ne (list ((get (car a) (cdr a)) GRID)))))))
NEIGHBORS)
ne))

Expand Down Expand Up @@ -165,7 +198,7 @@

; Called after the window renders. Used to update objects.
(define (update)
(if (>= A_TIMER 1/2) ; We want the player to move every 0.5 seconds, so the timer stops at 1/2 second and then acts.
(if (>= A_TIMER 1/3) ; We want the player to move every 0.5 seconds, so the timer stops at 1/2 second and then acts.
(begin (set! A_TIMER 0) ; Reset the timer, then check to see where the player is and move accordingly.
(when (< PATH-POS (- (length PATH) 0))
(move (- (send (list-ref PATH PATH-POS) getRow) (cdr player))
Expand Down
Binary file added demos/.DS_Store
Binary file not shown.
Binary file added demos/1st_String_E_64kb.wav
Binary file not shown.
Binary file added demos/2nd_String_B__64kb.wav
Binary file not shown.
Binary file added demos/3rd_String_G_64kb.wav
Binary file not shown.
Binary file added demos/4th_String_D_64kb.wav
Binary file not shown.
Binary file added demos/5th_String_A_64kb.wav
Binary file not shown.
Binary file added demos/6th_String_E_64kb.wav
Binary file not shown.
Binary file added demos/C_64kb.wav
Binary file not shown.
Binary file added demos/D_64kb.wav
Binary file not shown.
Binary file added demos/Dm_64kb.wav
Binary file not shown.
Binary file added demos/E_64kb.wav
Binary file not shown.
28 changes: 28 additions & 0 deletions levels.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#lang racket
(require racket/file)

(require "constants.rkt")

(define maps (list (file->lines "levels/level1.txt")
(file->lines "levels/level2.txt")))

; Checks to make sure the map in the file is a 15x15 grid.
(define (validMap? m)
(if (and (= (length m) GRID_SIZE)
(= (length (string->list (car m))) GRID_SIZE))
#t
#f))

; Constructs the map by making wall tiles in the grid unwalkable and setting the start, goal and player positions.
(define (buildMap GRID lvl)
(if (validMap? lvl)
(for ([row (in-range 0 GRID_SIZE)])
(let ([r (string->list (list-ref lvl row))])
(for ([col (in-range 0 GRID_SIZE)])
(let ([c (list-ref r col)])
(when (eq? c #\1) (send ((get row col) GRID) setWalk #f))
(when (eq? c #\S) (moveStart row col))
(when (eq? c #\E) (moveGoal row col))))))
(error "Invalid map format. Please check the input file...")))

(provide (all-defined-out))
15 changes: 15 additions & 0 deletions levels/level1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
111111111111111
100000000000001
100000000000S01
100000000111111
1000000001000E1
100000000101111
100000000101111
100000111101111
100000000001111
100000111111111
111111111111111
111111111111111
111111111111111
111111111111111
111111111111111
15 changes: 15 additions & 0 deletions levels/level2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
111111111111111
1000000101E0001
101111110111101
101000010000101
101011010110101
101010010010001
100011111011111
101000001010001
101111101000101
101000001111101
101110100000001
101000111111101
101111111111101
1S1000000000001
111111111111111
16 changes: 4 additions & 12 deletions tune_traveler.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

(require "constants.rkt")
(require "tile.rkt")
(require "levels.rkt")

; Used to create a one-dimensional array that represents a two-dimensional array.
(define (createGrid rows cols)
Expand All @@ -26,18 +27,9 @@
; The grid that will hold the game objects.
(define GRID (createGrid GRID_SIZE GRID_SIZE))

; Create walls around the grid.
(for ([i GRID_SIZE])
(send ((get 0 i) GRID) setWalk #f)
(send ((get (- GRID_SIZE 1) i) GRID) setWalk #f)
(send ((get i 0) GRID) setWalk #f)
(send ((get i (- GRID_SIZE 1)) GRID) setWalk #f))

; Create a wall for the algorithm to find its way around.
(map (lambda (row)
(send ((get row 6) GRID) setWalk #f))
'(4 5 6 7 8 9))

; Define the level to use for the algorithm.
(buildMap GRID (list-ref maps 1))
; Call the search algorithm to generate the path.
(search GRID ((get (cdr start) (car start)) GRID) ((get (cdr goal) (car goal)) GRID))

; Render everything to the frame.
Expand Down

0 comments on commit 9752a1a

Please sign in to comment.