Reputation: 21
I am trying to create a Plinko game by having a method with three parameters called num, prevNum (which is our previous random number) and score (which is kept track of on a certain condition).
The Win condition is if the score is greater than 100 than you print out something to the user and the score
The lose condition is if both num and prevNum are the same number, then print out something and num
There are also two conditions, if num is odd, add it to prevNum. If num is even, multiply it by 2, add it to score and print out something to the user
if none of these if statements pass, then do a recursive call
I started off by creating the random number between 1-10
(defn ball [] (inc (rand-int 10)))
and i created the plinko method but it doesn't work
(defn plinko [num prevNum score] (println "You got a " num) (if (= num prevNum) (println "You got double " num ". NO PLINKO!")
(if (odd? num) (+ prevNum num)
(if (even? num) ((+ score (* 2 num)) (println "DOUBLE POINT BONUS!"))
(if (> score 100) (println "PLINKO! Your total money was: $" score) (plinko num prevNum score))))))
I don't know how to connect the ball and plinko method nor do any of my past questions help me, i lack the knowledge to do this. I would really appreciate it if i could receive some help.
Upvotes: 1
Views: 584
Reputation: 9950
I did it like this:
(defn rand-num [max-num]
(inc (rand-int max-num)))
(defn plinko [& {:keys [max-num score last-num] :or {max-num 10 score 0 last-num 0}}]
(loop [num (rand-num max-num)
score score
prev last-num]
(println "You got a " num ". Your score is " score ".")
(cond
(> score 100) (println "PLINKO! Your total money was: $" score)
(even? num) (do (println "DOUBLE POINT BONUS!")
(recur (rand-num max-num)
(+ score (* 2 num))
num))
(= num prev) (println "You got double " num ". NO PLINKO!")
:else (recur (rand-num max-num)
score
num))))
(plinko :max-num 10 :score 0 :last-num 0)
Upvotes: 0
Reputation: 807
Here is my understanding of the problem statement.
Create a function plinko
with the parameters num
, prev-num
, and score
. If num
and prev-num
are equal, then you lose. Otherwise, if num
is odd, increment the score
by num
. Otherwise, increment the score
by twice the value of num
. If the updated score is greater than 100, then you win. Otherwise call the plinko
function recursively with updated values.
Assuming that is what you wanted, here is a definition of the function and two example calls.
user> (defn ball [] (inc (rand-int 10)))
#'user/ball
user> (defn plinko [num prev-num score]
(println "You got a " num)
(if (= num prev-num)
(println "You got double " num ". NO PLINKO!")
(let [new-score (if (odd? num)
(+ score num)
(do (println "DOUBLE POINT BONUS!")
(+ score (* 2 num))))]
(if (> new-score 100)
(println "PLINKO! Your total money was: $" new-score)
(plinko (ball) num new-score)))))
#'user/plinko
user> (plinko (ball) nil 0)
You got a 8
DOUBLE POINT BONUS!
You got a 9
You got a 5
You got a 3
You got a 3
You got double 3 . NO PLINKO!
nil
user> (plinko (ball) nil 0)
You got a 2
DOUBLE POINT BONUS!
You got a 1
You got a 4
DOUBLE POINT BONUS!
You got a 8
DOUBLE POINT BONUS!
You got a 6
DOUBLE POINT BONUS!
You got a 8
DOUBLE POINT BONUS!
You got a 6
DOUBLE POINT BONUS!
You got a 8
DOUBLE POINT BONUS!
You got a 9
You got a 5
You got a 10
DOUBLE POINT BONUS!
PLINKO! Your total money was: $ 119
nil
user>
Upvotes: 1