summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEl-BG-1970 <elouangros@hotmail.com>2022-08-04 16:41:30 +0200
committerEl-BG-1970 <elouangros@hotmail.com>2022-08-04 16:41:30 +0200
commit5731684cca287633b7589c12ca1372eec5dc0f0a (patch)
treef0920be6027b5d1d03d321fb9160735599f27cc4
parent164b961cdd97c4ef324bbb0ae5f46b394201183b (diff)
downloadtransacc-5731684cca287633b7589c12ca1372eec5dc0f0a.tar.gz
implemented game:change-zone
-rw-r--r--src/game.lisp37
-rw-r--r--src/zone.lisp4
-rw-r--r--tests/test.lisp30
3 files changed, 55 insertions, 16 deletions
diff --git a/src/game.lisp b/src/game.lisp
index 2f7ce88..51c3a4d 100644
--- a/src/game.lisp
+++ b/src/game.lisp
@@ -2,32 +2,41 @@
(:use :cl)
(:export :new-game
:buy-item
- :sell-item))
+ :sell-item
+ :change-zone))
(in-package :game)
(defclass game ()
- ((player :initarg player
+ ((player :initarg :player
:accessor player)
- (zones :initarg zones
+ (zones :initarg :zones
:accessor zones)
- (curzone :initarg start-zone
- :accessor cur-zone)))
+ (cur-zone :initarg :start-zone
+ :accessor cur-zone)))
-(defun new-game (player zones start-zone)
+(defun new-game (player start-zone zones)
(make-instance 'game
: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)))))
+ (with-slots (player cur-zone) g
+ (let ((item (zone:get-commodity cur-zone name)))
+ (when item
+ (player:buy-item player item 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)))))
+ (with-slots (player cur-zone) g
+ (let ((item (zone:get-commodity cur-zone name)))
+ (when item
+ (player:sell-item player item amount)))))
+
+(defmethod change-zone ((g game) name)
+ (with-slots (cur-zone zones) g
+ (let ((z (find-if (lambda (z) (string= name
+ (zone:name z)))
+ zones)))
+ (when z
+ (setq cur-zone z)))))
diff --git a/src/zone.lisp b/src/zone.lisp
index 9a27f17..ee2fad5 100644
--- a/src/zone.lisp
+++ b/src/zone.lisp
@@ -18,6 +18,6 @@
:commodities commodities))
(defmethod get-commodity ((z zone) commodity-name)
- (find-if (lambda (c) (equal (commodities:name c)
- commodity-name))
+ (find-if (lambda (c) (string= (commodities:name c)
+ commodity-name))
(commodities z)))
diff --git a/tests/test.lisp b/tests/test.lisp
index 73f03b8..6e6fb9b 100644
--- a/tests/test.lisp
+++ b/tests/test.lisp
@@ -12,6 +12,14 @@
:description "Test object and methods for game"
:in transacc-suite)
+(test make-game
+ (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 (equal (type-of g) 'game::game))))
+
;; testing buy-item
(test buy-item
(let* ((p (player:init-player "Joze"))
@@ -25,3 +33,25 @@
(is (game:sell-item g "apple" 5))
(is (game:buy-item g "pear" 20))
(is (game:sell-item g "pear" 100))))
+
+;; testing change-zone
+(test change-zone
+ (let* ((p (player:init-player "Joze"))
+ (c (list (commodities:new-commodity "apple" 5)
+ (commodities:new-commodity "pear" 7)))
+ (c2 (list (commodities:new-commodity "apple" 6)
+ (commodities:new-commodity "pear" 2)))
+ (z (zone:new-zone "Bronx" c))
+ (z2 (zone:new-zone "Manhattan" c2))
+ (g (game:new-game p z (list z z2))))
+ (is (string= (zone:name (game::cur-zone g))
+ "Bronx"))
+ (is (game:change-zone g "Manhattan"))
+ (is (string= (zone:name (game::cur-zone g))
+ "Manhattan"))
+ (game:buy-item g "pear" 10)
+ (is (not (game:change-zone g "Paris")))
+ (is (game:change-zone g "Bronx"))
+ (game:sell-item g "pear" 10)
+ (is (= (player:get-cash (game::player g))
+ 2050))))