Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.48 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;; ---------------------------------------------------------------------------
  4. ;;; Prosty system obiektowy z dziedziczeniem
  5.  
  6. (define (ask object message . args)
  7. (let ((method (get-method object message)))
  8. (if (method? method)
  9. (apply method (cons object args))
  10. (error "Brak metody" message (cadr method)))))
  11.  
  12. (define (get-method object message)
  13. (object message))
  14.  
  15. (define (no-method name)
  16. (list 'no-method name))
  17.  
  18. (define (method? x)
  19. (not (no-method? x)))
  20.  
  21. (define (no-method? x)
  22. (if (pair? x)
  23. (eq? (car x) 'no-method)
  24. false))
  25.  
  26. ;;; ----------------------------------------------------------------------------
  27. ;;; Osoby, miejsca i rzeczy są nazwanymi obiektami
  28.  
  29. (define (make-named-object name)
  30. (lambda (message)
  31. (cond ((eq? message 'name) (lambda (self) name))
  32. ((eq? message 'got-killed) (lambda (self) (set! name 'Ghost) #t))
  33. (else (no-method name)))))
  34.  
  35. ;;; Osoby i rzeczy są mobilne, ich miejsce może ulec zmianie
  36.  
  37. (define (make-mobile-object name location)
  38. (let ((named-obj (make-named-object name)))
  39. (lambda (message)
  40. (cond ((eq? message 'place) (lambda (self) location))
  41. ((eq? message 'install)
  42. (lambda (self)
  43. (ask location 'add-thing self)))
  44. ;; Poniższa metoda nie powinna być wołana przez użytkownika
  45. ;; Zobacz change-place
  46. ((eq? message 'set-place)
  47. (lambda (self new-place)
  48. (set! location new-place)
  49. 'place-set))
  50. (else (get-method named-obj message))))))
  51.  
  52. (define (make&install-mobile-object name place)
  53. (let ((mobile-obj (make-mobile-object name place)))
  54. (ask mobile-obj 'install)
  55. mobile-obj))
  56.  
  57. ;;; Rzecz to coś, co może mieć właściciela
  58.  
  59. (define (make-thing name birthplace)
  60. (let ((owner 'nobody)
  61. (mobile-obj (make-mobile-object name birthplace)))
  62. (lambda (message)
  63. (cond ((eq? message 'owner) (lambda (self) owner))
  64. ((eq? message 'ownable?) (lambda (self) true))
  65. ((eq? message 'owned?)
  66. (lambda (self)
  67. (not (eq? owner 'nobody))))
  68. ;; Poniższa metoda nie powinna być wołana przez użytkownika
  69. ;; Zobacz take i lose
  70. ((eq? message 'set-owner)
  71. (lambda (self new-owner)
  72. (set! owner new-owner)
  73. 'owner-set))
  74. (else (get-method mobile-obj message))))))
  75.  
  76. (define (make&install-thing name birthplace)
  77. (let ((thing (make-thing name birthplace)))
  78. (ask thing 'install)
  79. thing))
  80.  
  81. ;;; Implementacja miejsc
  82.  
  83. (define (make-place name)
  84. (let ((neighbor-map '())
  85. (things '())
  86. (named-obj (make-named-object name)))
  87. (lambda (message)
  88. (cond ((eq? message 'things) (lambda (self) things))
  89. ((eq? message 'neighbors)
  90. (lambda (self) (map cdr neighbor-map)))
  91. ((eq? message 'exits)
  92. (lambda (self) (map car neighbor-map)))
  93. ((eq? message 'neighbor-towards)
  94. (lambda (self direction)
  95. (let ((places (assq direction neighbor-map)))
  96. (if places
  97. (cdr places)
  98. false))))
  99. ((eq? message 'add-neighbor)
  100. (lambda (self direction new-neighbor)
  101. (cond ((assq direction neighbor-map)
  102. (display-message (list "Kierunek już przypisany"
  103. direction name))
  104. false)
  105. (else
  106. (set! neighbor-map
  107. (cons (cons direction new-neighbor) neighbor-map))
  108. true))))
  109. ((eq? message 'accept-person?)
  110. (lambda (self person)
  111. true))
  112.  
  113. ;; Poniższe metody nie powinny być wołane przez użytkownika
  114. ;; Zobacz change-place
  115. ((eq? message 'add-thing)
  116. (lambda (self new-thing)
  117. (cond ((memq new-thing things)
  118. (display-message (list (ask new-thing 'name)
  119. "już jest w" name))
  120. false)
  121. (else (set! things (cons new-thing things))
  122. true))))
  123. ((eq? message 'del-thing)
  124. (lambda (self thing)
  125. (cond ((not (memq thing things))
  126. (display-message (list (ask thing 'name)
  127. "nie jest w" name))
  128. false)
  129. (else (set! things (delq thing things))
  130. true))))
  131.  
  132. (else (get-method named-obj message))))))
  133.  
  134. ;;; ----------------------------------------------------------------------------
  135. ;;; Implementacja osób
  136.  
  137. (define (make-person name birthplace threshold healthpoints attackdamage)
  138. (let ((HP healthpoints)
  139. (AD attackdamage)
  140. (possessions '())
  141. (mobile-obj (make-mobile-object name birthplace)))
  142. (lambda (message)
  143. (cond ((eq? message 'person?) (lambda (self) true))
  144. ((eq? message 'possessions) (lambda (self) possessions))
  145. ((eq? message 'HP) (lambda (self) HP))
  146. ((eq? message 'AD) (lambda (self) AD))
  147. ((eq? message 'buy) (lambda (self thing)
  148. (cond[(not (eq? (ask (ask self 'place) 'name) 'plastyczna)) (display-message '("Możesz kupować tylko w plastycznej.") )]
  149. [(not (member $$ (ask self 'possessions))) (display-message '("Nie masz pieniędzy")) ]
  150. [else (and (ask self 'say (list "Poproszę" thing))
  151. (ask sprzedawca 'say (list "Oto twoje" thing))
  152. (set! zakup (make&install-thing thing plastyczna))
  153. (ask self 'take zakup))])))
  154. ((eq? message 'drink-beer)
  155. (lambda (self)
  156. (cond[(not (member zakup (ask self 'possessions))) (display-message '("Przecież nie kupiłeś piwa!."))]
  157. [(not (eq? (ask zakup 'name) 'piwo)) (display-message '("Jak mogłeś kupić" (ask zakup 'name) "zamiast piwa?! GAME OVER.") )]
  158. [(not (eq? (ask (ask self 'place) 'name) 'ławeczka-na-bulwarze)) (display-message '("Pijemy tylko na bulwarowej ławeczce") )]
  159. [else (and (ask self 'say '("Za zdrowie UWr!")) (display-message '("Zadowolony student wypełnił swój naturalny obowiązek.")))])))
  160.  
  161. ((eq? message 'drop-everything)
  162. (lambda (self) (if (null? possessions)
  163. #t
  164. (and (ask self 'lose (car possessions)) (ask self 'drop-everything)))))
  165.  
  166. ((eq? message 'list-possessions)
  167. (lambda (self)
  168. (ask self 'say
  169. (cons "Mam"
  170. (if (null? possessions)
  171. '("nic")
  172. (map (lambda (p) (ask p 'name))
  173. possessions))))
  174. possessions))
  175. ((eq? message 'got-hit)
  176. (lambda (self damage)
  177. (set! HP (- HP damage))
  178. (cond [(< HP 0) (and (display-message (list (ask self 'name) "ginie."))
  179. (ask self 'got-killed)
  180. (ask self 'drop-everything)
  181. (set! threshold 0) #t)]
  182. [(< HP damage) (and (ask self 'say (list "Proszę, daruj mi życie! Oddam wszystko, co mam!!!"))
  183. (ask self 'drop-everything))]
  184. [else (ask self 'say (list "Aaaa! Zostało mi" HP "HP"))])))
  185. ((eq? message 'hit)
  186. (lambda (self victim)
  187. (if (member victim (ask (ask self 'place) 'things))
  188. (and (display-message (list (ask self 'name) "uderzył" (ask victim 'name )"."))
  189. (ask victim 'got-hit (ask self 'AD)))
  190. (display-message '("Nie ma go tu.")))))
  191.  
  192. ((eq? message 'say)
  193. (lambda (self list-of-stuff)
  194. (display-message
  195. (append (list "W miejscu" (ask (ask self 'place) 'name)
  196. ":" name "mówi --")
  197. (if (null? list-of-stuff)
  198. '("Nieważne.")
  199. list-of-stuff)))
  200. 'said))
  201. ((eq? message 'have-fit)
  202. (lambda (self)
  203. (ask self 'say '("Jestem zły!!!"))
  204. 'I-feel-better-now))
  205. ((eq? message 'look-around)
  206. (lambda (self)
  207. (let ((other-things
  208. (map (lambda (thing) (ask thing 'name))
  209. (delq self
  210. (ask (ask self 'place)
  211. 'things)))))
  212. (ask self 'say (cons "Widzę" (if (null? other-things)
  213. '("nic")
  214. other-things)))
  215. other-things)))
  216.  
  217. ((eq? message 'take)
  218. (lambda (self thing)
  219. (cond ((memq thing possessions)
  220. (ask self 'say
  221. (list "Już mam" (ask thing 'name)))
  222. true)
  223. ((and (let ((things-at-place (ask (ask self 'place) 'things)))
  224. (memq thing things-at-place))
  225. (is-a thing 'ownable?))
  226. (if (ask thing 'owned?)
  227. (let ((owner (ask thing 'owner)))
  228. (ask owner 'lose thing)
  229. (ask owner 'have-fit))
  230. 'unowned)
  231.  
  232. (ask thing 'set-owner self)
  233. (set! possessions (cons thing possessions))
  234. (ask self 'say
  235. (list "Biorę" (ask thing 'name)))
  236. true)
  237. (else
  238. (display-message
  239. (list "Nie możesz wziąć" (ask thing 'name)))
  240. false))))
  241. ((eq? message 'take2)
  242. (lambda (self thing)
  243. (cond ((memq thing possessions)
  244. (ask self 'say
  245. (list "Już mam" (ask thing 'name)))
  246. true)
  247. ((and (let ((things-at-place (ask (ask self 'place) 'things)))
  248. (memq thing things-at-place))
  249. (is-a thing 'ownable?))
  250. (if (ask thing 'owned?)
  251. (let ((owner (ask thing 'owner)))
  252. (ask owner 'lose thing)
  253. (ask owner 'have-fit)
  254. (set! possessions (cons thing possessions))
  255. (ask self 'say
  256. (list "Biorę" (ask thing 'name)))
  257. (ask owner 'hit self)
  258. (ask self 'say (list "Tracę" (ask thing 'name)))
  259. (ask owner 'take thing))
  260. (ask self 'take thing))))))
  261.  
  262. ((eq? message 'lose)
  263. (lambda (self thing)
  264. (cond ((eq? self (ask thing 'owner))
  265. (set! possessions (delq thing possessions))
  266. (ask thing 'set-owner 'nobody)
  267. (ask self 'say
  268. (list "Tracę" (ask thing 'name)))
  269. true)
  270. (else
  271. (display-message (list name "nie ma"
  272. (ask thing 'name)))
  273. false))))
  274. ((eq? message 'move)
  275. (lambda (self)
  276. (cond ((and (> threshold 0) (= (random threshold) 0))
  277. (ask self 'act)
  278. true))))
  279. ((eq? message 'act)
  280. (lambda (self)
  281. (let ((new-place (random-neighbor (ask self 'place))))
  282. (if new-place
  283. (ask self 'move-to new-place)
  284. false))))
  285.  
  286. ((eq? message 'move-to)
  287. (lambda (self new-place)
  288. (let ((old-place (ask self 'place)))
  289. (cond ((eq? new-place old-place)
  290. (display-message (list name "już jest w"
  291. (ask new-place 'name)))
  292. false)
  293. ((ask new-place 'accept-person? self)
  294. (change-place self new-place)
  295. (for-each (lambda (p) (change-place p new-place))
  296. possessions)
  297. (display-message
  298. (list name "idzie z" (ask old-place 'name)
  299. "do" (ask new-place 'name)))
  300. (greet-people self (other-people-at-place self new-place))
  301. true)
  302. (else
  303. (display-message (list name "nie może iść do"
  304. (ask new-place 'name))))))))
  305. ((eq? message 'go)
  306. (lambda (self direction)
  307. (let ((old-place (ask self 'place)))
  308. (let ((new-place (ask old-place 'neighbor-towards direction)))
  309. (cond (new-place
  310. (ask self 'move-to new-place))
  311. (else
  312. (display-message (list "Nie możesz pójść" direction
  313. "z" (ask old-place 'name)))
  314. false))))))
  315. ((eq? message 'install)
  316. (lambda (self)
  317. (add-to-clock-list self)
  318. ((get-method mobile-obj 'install) self)))
  319. (else (get-method mobile-obj message))))))
  320.  
  321. (define (make&install-person name birthplace threshold HP AD)
  322. (let ((person (make-person name birthplace threshold HP AD)))
  323. (ask person 'install)
  324. person))
  325.  
  326. ;;; Łazik umie sam podnosić rzeczy
  327.  
  328. (define (make-rover name birthplace threshold HP AD)
  329. (let ((person (make-person name birthplace threshold HP AD)))
  330. (lambda (message)
  331. (cond ((eq? message 'act)
  332. (lambda (self)
  333. (let ((possessions (ask self 'possessions)))
  334. (if (null? possessions)
  335. (ask self 'grab-something)
  336. (ask self 'lose (car possessions))))))
  337. ((eq? message 'grab-something)
  338. (lambda (self)
  339. (let* ((things (ask (ask self 'place) 'things))
  340. (fthings
  341. (filter (lambda (thing) (is-a thing 'ownable?))
  342. things)))
  343. (if (not (null? fthings))
  344. (ask self 'take (pick-random fthings))
  345. false))))
  346. ((eq? message 'move-arm)
  347. (lambda (self)
  348. (display-message (list name "rusza manipulatorem"))
  349. '*bzzzzz*))
  350. (else (get-method person message))))))
  351.  
  352. (define (make&install-rover name birthplace threshold HP AD)
  353. (let ((rover (make-rover name birthplace threshold HP AD)))
  354. (ask rover 'install)
  355. rover))
  356.  
  357. ;; TODO: nowe rodzaje przedmiotów lub postaci
  358.  
  359. ;;; --------------------------------------------------------------------------
  360. ;;; Obsługa zegara
  361.  
  362. (define *clock-list* '())
  363. (define *the-time* 0)
  364.  
  365. (define (initialize-clock-list)
  366. (set! *clock-list* '())
  367. 'initialized)
  368.  
  369. (define (add-to-clock-list person)
  370. (set! *clock-list* (cons person *clock-list*))
  371. 'added)
  372.  
  373. (define (remove-from-clock-list person)
  374. (set! *clock-list* (delq person *clock-list*))
  375. 'removed)
  376.  
  377. (define (clock)
  378. (newline)
  379. (display "---Tick---")
  380. (set! *the-time* (+ *the-time* 1))
  381. (for-each (lambda (person) (ask person 'move))
  382. *clock-list*)
  383. 'tick-tock)
  384.  
  385.  
  386. (define (current-time)
  387. *the-time*)
  388.  
  389. (define (run-clock n)
  390. (cond ((zero? n) 'done)
  391. (else (clock)
  392. (run-clock (- n 1)))))
  393.  
  394. ;;; --------------------------------------------------------------------------
  395. ;;; Różne procedury
  396.  
  397. (define (is-a object property)
  398. (let ((method (get-method object property)))
  399. (if (method? method)
  400. (ask object property)
  401. false)))
  402.  
  403. (define (change-place mobile-object new-place)
  404. (let ((old-place (ask mobile-object 'place)))
  405. (ask mobile-object 'set-place new-place)
  406. (ask old-place 'del-thing mobile-object))
  407. (ask new-place 'add-thing mobile-object)
  408. 'place-changed)
  409.  
  410. (define (other-people-at-place person place)
  411. (filter (lambda (object)
  412. (if (not (eq? object person))
  413. (is-a object 'person?)
  414. false))
  415. (ask place 'things)))
  416.  
  417. (define (greet-people person people)
  418. (if (not (null? people))
  419. (ask person 'say
  420. (cons "Cześć"
  421. (map (lambda (p) (ask p 'name))
  422. people)))
  423. 'sure-is-lonely-in-here))
  424.  
  425. (define (display-message list-of-stuff)
  426. (newline)
  427. (for-each (lambda (s) (display s) (display " "))
  428. list-of-stuff)
  429. 'message-displayed)
  430.  
  431. (define (random-neighbor place)
  432. (pick-random (ask place 'neighbors)))
  433.  
  434. (define (filter predicate lst)
  435. (cond ((null? lst) '())
  436. ((predicate (car lst))
  437. (cons (car lst) (filter predicate (cdr lst))))
  438. (else (filter predicate (cdr lst)))))
  439.  
  440. (define (pick-random lst)
  441. (if (null? lst)
  442. false
  443. (list-ref lst (random (length lst))))) ;; See manual for LIST-REF
  444.  
  445. (define (delq item lst)
  446. (cond ((null? lst) '())
  447. ((eq? item (car lst)) (delq item (cdr lst)))
  448. (else (cons (car lst) (delq item (cdr lst))))))
  449.  
  450. ;;------------------------------------------
  451. ;; Od tego miejsca zaczyna się kod świata
  452.  
  453. (initialize-clock-list)
  454.  
  455. ;; Tu definiujemy miejsca w naszym świecie
  456. ;;------------------------------------------
  457.  
  458. (define hol (make-place 'hol))
  459. (define piętro-wschód (make-place 'piętro-wschód))
  460. (define piętro-zachód (make-place 'piętro-zachód))
  461. (define ksi (make-place 'ksi))
  462. (define continuum (make-place 'continuum))
  463. (define plastyczna (make-place 'plastyczna))
  464. (define wielka-wschodnia (make-place 'wielka-wschodnia))
  465. (define wielka-zachodnia (make-place 'wielka-zachodnia))
  466. (define kameralna-wschodnia (make-place 'kameralna-wschodnia))
  467. (define kameralna-zachodnia (make-place 'kameralna-zachodnia))
  468. (define schody-parter (make-place 'schody-parter))
  469. (define schody-piętro (make-place 'schody-piętro))
  470. (define parking (make-place 'parking))
  471. (define bulwar (make-place 'bulwar))
  472. (define ławeczka-na-bulwarze (make-place 'ławeczka-na-bulwarze))
  473. (define wydział-chemii (make-place 'wydział-chemii))
  474.  
  475. ;; Połączenia między miejscami w świecie
  476. ;;------------------------------------------------------
  477.  
  478. (define (can-go from direction to)
  479. (ask from 'add-neighbor direction to))
  480.  
  481. (define (can-go-both-ways from direction reverse-direction to)
  482. (can-go from direction to)
  483. (can-go to reverse-direction from))
  484.  
  485. (can-go-both-ways schody-parter 'góra 'dół schody-piętro)
  486. (can-go-both-ways hol 'zachód 'wschód wielka-zachodnia)
  487. (can-go-both-ways hol 'na-dwór 'do-środka parking)
  488. (can-go-both-ways hol 'wschód 'zachód wielka-wschodnia)
  489. (can-go-both-ways hol 'południe 'północ schody-parter)
  490. (can-go-both-ways piętro-wschód 'południe 'wschód schody-piętro)
  491. (can-go-both-ways piętro-zachód 'południe 'zachód schody-piętro)
  492. (can-go-both-ways piętro-wschód 'zachód 'wschód piętro-zachód)
  493. (can-go-both-ways piętro-zachód 'północ 'południe kameralna-zachodnia)
  494. (can-go-both-ways piętro-wschód 'północ 'południe kameralna-wschodnia)
  495. (can-go-both-ways schody-parter 'wschód 'zachód plastyczna)
  496. (can-go-both-ways hol 'północ 'południe ksi)
  497. (can-go-both-ways piętro-zachód 'zachód 'wschód continuum)
  498. (can-go-both-ways parking 'północ 'południe wydział-chemii)
  499. (can-go-both-ways wydział-chemii 'zachód 'wschód bulwar)
  500. (can-go-both-ways ławeczka-na-bulwarze 'północ 'południe bulwar)
  501.  
  502. ;; TODO: połączenia dla nowych lokacji
  503.  
  504. ;; Osoby dramatu
  505. ;;---------------------------------------
  506.  
  507. (define student (make&install-person 'student hol 0 8 6))
  508. (define fsieczkowski
  509. (make&install-person 'fsieczkowski wielka-wschodnia 3 42 1))
  510. (define mpirog (make&install-person 'mpirog wielka-wschodnia 3 1 2))
  511. (define mmaterzok (make&install-person 'mmaterzok wielka-wschodnia 3 4 10))
  512. (define jma (make&install-person 'jma piętro-wschód 2 100 100))
  513. (define klo (make&install-person 'klo kameralna-wschodnia 2 3 9))
  514. (define ref (make&install-person 'ref ksi 0 1 0))
  515. (define aleph1 (make&install-rover 'aleph1 continuum 3 100 0))
  516. (define target (make&install-person 'snob-z-polibudy continuum 3 19 3))
  517. (define sprzedawca (make&install-person 'sprzedawca plastyczna 0 9 1))
  518.  
  519. (define słój-pacholskiego (make&install-thing 'słój-pacholskiego schody-piętro))
  520. (define trytytki (make&install-thing 'trytytki continuum))
  521. (define cążki-boczne (make&install-thing 'cążki-boczne continuum))
  522. (define deklaracja (make&install-thing 'deklaracja hol))
  523. (define $$ (make&install-thing '$$ continuum))
  524. (define zakup '??)
  525. ;; TODO: dodatkowe osoby i przedmioty
  526. (ask target 'take $$)
  527. ;; Polecenia dla gracza
  528. ;;------------------------------------------------
  529.  
  530. (define *player-character* student)
  531.  
  532. (define (look-around)
  533. (ask *player-character* 'look-around)
  534. #t)
  535.  
  536. (define (go direction)
  537. (ask *player-character* 'go direction)
  538. (clock)
  539. #t)
  540.  
  541. (define (exits)
  542. (display-message (ask (ask *player-character* 'place) 'exits))
  543. #t)
  544.  
  545. (define (my-backpack)
  546. (ask *player-character* 'list-possessions)
  547. #t)
  548. (define (pick something)
  549. (ask *player-character* 'take2 something)
  550. #t)
  551. (define (drop something)
  552. (ask *player-character* 'lose something)
  553. #t)
  554. (define (say what)
  555. (ask *player-character* 'say what)
  556. #t)
  557. (define (hit whom)
  558. (ask *player-character* 'hit whom)
  559. #t)
  560. (define (wheres-money)
  561. (ask (ask $$ 'place) 'name)
  562. #t)
  563. (define (buy what)
  564. (ask *player-character* 'buy what)
  565. #t)
  566. (define (drink-beer)
  567. (ask *player-character* 'drink-beer)
  568. #t)
  569.  
  570. ;;README:
  571.  
  572. ;;UWAGA!!!
  573. ;;W grze pojawia się:
  574. ;;PRZEMOC
  575. ;;ALKOHOL
  576. ;;Gra dla osób powyżej 18 roku życia
  577.  
  578. ;;celem gry jest wypicie piwa na bulwarze
  579. ;;piwo trzeba kupić za pieniądze, które należy zdobyć
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement