Home > Software engineering >  Functions to create a simplified darts game with randomized numbers and a win condition
Functions to create a simplified darts game with randomized numbers and a win condition

Time:03-27

I managed to create a pair of numbers with code

(defn dart-throwing [] 
   [(- (* 2 (rand-int 2)) 1) (- (* 2 (rand-int 2)) 1)])


(def onetrial (dart-throwing))

But I do not know how to create my win condition which would be x^2 y^2 < 1

I tried writing

(defn won? [x y] 
  (< (  (* x x) (* y y)) 1))

(won? onetrial)

I expected it to check the pair [] from dart-throwing and check if it was less than 1 giving a true or false

I would appreciate it if I had some help, thank you

CodePudding user response:

Your won? function expects two arguments, an x and a y. Your onetrial is a vector of two elements, but it is a single argument here.

You have a couple of options:

You can use apply to 'spread' the vector into the argument list.

(apply won? onetrial)

OR

You can rewrite won? slightly using destructuring

(defn won? [[x y]] 
  (< (  (* x x) (* y y)) 1))

CodePudding user response:

Your code does the same like:

(defn dart-throwing []
  [(rand-nth [-1 1])
   (rand-nth [-1 1])

(def onetrial (dart-throwing))

rand-nth chooses deliberately from the given collection - in this case -1 and 1.

And here is the problem: -1 squared is 1 - so ( (* x x) (* y y)) will never be < than 1.

So: What is the point of the won? function?

Or shouldn't dart-throwing return some float between -1 and 1? a selection from the range? In this case rand-int is wrong. It must be then rand. so, instead of (- (* 2 (rand-int 2)) 1) it should be then (- (* 2 (rand)) 1). However, rand excludes the upper limit - in this case 1. One could send the upper limit to 1 Float/MIN_VALUE to include 1. Or one keeps it but randomly choose between -1 and 1 and multiply them with the result - then one would get 1 included ... - but it wouldn't be a even distribution ...

  • Related