Rock, Paper, Scissors Bot
This code uses a rock paper scissors strategy identified in a psychological study, stating that if someone wins, they are more likely to play the same winning choice, however if they lose they are more likely to choose a different play. If the player wins the bot will choose the hand that beats the users last play. If the user loses the bot will change its play from the last round since the user is expected to change their play.
(defn bot-choice [prev lost]
(cond
(and (= prev 1) lost) 2
(and (= prev 2) lost) 3
(and (= prev 3) lost) 1
(= prev 1) (rand-nth [2 3])
(= prev 2) (rand-nth [1 3])
(= prev 3) (rand-nth [1 2])
:else (rand-nth [1 2 3])
))
(defn game [prev lost]
(let [bot (bot-choice prev lost)
choice (js/parseInt (js/prompt "Please select rock, paper or scissors.\n1 Rock\n2 Paper\n3 Scissors"))]
(cond
(= choice bot)
(do (js/alert "DRAW: ") (recur choice false))
(and (= choice 1) (= bot 2))
(do (js/alert "LOSE: Your rock lost to paper.") (recur choice false))
(and (= choice 1) (= bot 3))
(do (js/alert "WIN: Your rock beat scissors.") (recur choice true))
(and (= choice 2) (= bot 1))
(do (js/alert "WIN: Your paper beat rock.") (recur choice true))
(and (= choice 2) (= bot 3))
(do (js/alert "LOSE: Your paper lost to scissors.") (recur choice false))
(and (= choice 3) (= bot 1))
(do (js/alert "LOSE: Your scissors lost to rock.") (recur choice false))
(and (= choice 3) (= bot 2))
(do (js/alert "WIN: Your scissors beat paper.") (recur choice true))
:else
(do (js/alert "Hey bozo, please enter a value from 1 to 3.") (recur choice false)))))
(defn run []
(js/alert (game (rand-nth [1 2 3]) false))
(js/alert "Done!"))