In: Computer Science
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
Here is the scheme program for rock paper and scisors game
(define (rps>? p1 p2)
(or (and (eq? p1 'rock) (eq? p2 'scissors))
(and (eq? p1 'scissors) (eq? p2 'paper))
(and (eq? p1 'paper) (eq? p2 'rock))))
;Play a game of rock/paper/scissors against a given opponent
(define (play-rps brain)
(printf "Enter rock, paper, or scissors (quit to exit).\n")
; Read player input, evaulate it, print response, loop (unless
quit)
(let repl ([wins 0] [rounds 0])
(printf "> ")
(define player (read))
(case player
; Playing a round, run the computer brain
[(rock paper scissors)
(define computer (brain))
(printf " computer chooses ~a" computer)
(cond
; Player beats computer
[(rps>? player computer)
(printf ", you win!\n")
(brain 'lose)
(repl (+ wins 1) (+ rounds 1))]
; Computer beats player
[(rps>? computer player)
(printf ", computer wins :(\n")
(brain 'win)
(repl wins (+ rounds 1))]
; Player and computer tie
[else
(printf ", it's a tie\n")
(brain 'tie)
(repl (+ wins 1/2) (+ rounds 1))])]
; Done player, print stats and exit
[(quit)
(printf "You won ~a%. Good job.\n" (round (* 100 (/ wins
rounds))))]
; Who knows. Maybe if we repeat ourselves it will be helpful
[else
(printf "Unknown input.\nEnter rock, paper, or scissors (quit to
exit).\n")
(repl wins rounds)])))
(define (random-symbol)
(list-ref '(rock paper scissors) (random 3)))
; Just choose at random
(define random-brain
(case-lambda
[()
(random-symbol)]
[(result)
(void)]))