diff options
| author | El-BG-1970 <elouangros@hotmail.com> | 2022-08-04 11:13:32 +0200 |
|---|---|---|
| committer | El-BG-1970 <elouangros@hotmail.com> | 2022-08-04 11:13:32 +0200 |
| commit | 164b961cdd97c4ef324bbb0ae5f46b394201183b (patch) | |
| tree | 0051bfc0d5c91e150eec85f8860f3244332a0764 | |
| parent | 8104b4db1c5752b777d17e1f84f8a575587c735f (diff) | |
| download | transacc-164b961cdd97c4ef324bbb0ae5f46b394201183b.tar.gz | |
implemented game:buy-item and game:sell-item
| -rw-r--r-- | src/game.lisp | 16 | ||||
| -rw-r--r-- | src/zone.lisp | 8 | ||||
| -rw-r--r-- | tests/test-player.lisp | 2 | ||||
| -rw-r--r-- | tests/test.lisp | 19 |
4 files changed, 42 insertions, 3 deletions
diff --git a/src/game.lisp b/src/game.lisp index 7c2cbbb..2f7ce88 100644 --- a/src/game.lisp +++ b/src/game.lisp @@ -1,6 +1,8 @@ (defpackage game (:use :cl) - (:export :new-game)) + (:export :new-game + :buy-item + :sell-item)) (in-package :game) @@ -17,3 +19,15 @@ :player player :zones zones :start-zone start-zone)) + +(defmethod buy-item ((g game) name amount) + (with-slots (player curzone) g + (let ((item (zone:get-commodity curzone name))) + (when item + (player:buy-item player name amount))))) + +(defmethod sell-item ((g game) name amount) + (with-slots (player curzone) g + (let ((item (zone:get-commodity curzone name))) + (when item + (player:sell-item player name amount))))) diff --git a/src/zone.lisp b/src/zone.lisp index 3121a5c..9a27f17 100644 --- a/src/zone.lisp +++ b/src/zone.lisp @@ -1,7 +1,8 @@ (defpackage zone (:use :cl) (:export :new-zone - :name)) + :name + :get-commodity)) (in-package :zone) @@ -15,3 +16,8 @@ (make-instance 'zone :name name :commodities commodities)) + +(defmethod get-commodity ((z zone) commodity-name) + (find-if (lambda (c) (equal (commodities:name c) + commodity-name)) + (commodities z))) diff --git a/tests/test-player.lisp b/tests/test-player.lisp index 2f261d7..a74ecd7 100644 --- a/tests/test-player.lisp +++ b/tests/test-player.lisp @@ -5,7 +5,7 @@ :description "Test object and methods for player" :in transacc-suite) -;; testing buy-item +;; testing player creation (test creating-player (let ((p (player:init-player "Joze")) (p2 (player:init-player "Mehmout" 555 8976))) diff --git a/tests/test.lisp b/tests/test.lisp index fb37c94..73f03b8 100644 --- a/tests/test.lisp +++ b/tests/test.lisp @@ -6,3 +6,22 @@ (def-suite transacc-suite :description "Test transacc") + +;;; Game tests +(def-suite* game-suite + :description "Test object and methods for game" + :in transacc-suite) + +;; testing buy-item +(test buy-item + (let* ((p (player:init-player "Joze")) + (c (list (commodities:new-commodity "apple" 5) + (commodities:new-commodity "pear" 7))) + (z (zone:new-zone "Bronx" c)) + (g (game:new-game p z (list z)))) + (is (game:buy-item g "apple" 10)) + (is (not (game:sell-item g "pear" 1))) + (is (game:sell-item g "apple" 5)) + (is (game:sell-item g "apple" 5)) + (is (game:buy-item g "pear" 20)) + (is (game:sell-item g "pear" 100)))) |
