;File: guessing_number_game.scm
;Date: October 24, 2006
;Author: Jonathan Zaleski
;Guessing number game. User guesses a number between 1 and 10
;Output:
;
;Game asks user to guess a number between 1 and 10. The game checks to see if the number chosen is equal to the random
;number the computer has generated. Each time, the computer generates a new number between 1 and 10. The program also
;displays the number of games the user has won out of how many games played. Finally, the games asks the user if he or
;she would like to continue playing the game. Type (start) to begin playing the game!!!
;> (start)
;
;Round 1
;Guess a number between 1 and 10: 5
;You have chosen poorly!
;You have won 0 out of 1 games! Your accuracy is 0.0%.
;Continue (yes or no)?: yes
;
;Round 2
;Guess a number between 1 and 10: 7
;You have chosen poorly!
;You have won 0 out of 2 games! Your accuracy is 0.0%.
;Continue (yes or no)?: yes
;
;Round 3
;Guess a number between 1 and 10: 1
;You have chosen wisely!
;You have won 1 out of 3 games! Your accuracy is 33.33333333333333%.
;Continue (yes or no)?: no
;Bye!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define count 0)
(define b 0)
(display "Game asks user to guess a number between 1 and 10. The game checks to see if the number chosen is equal to the random number the computer has generated. Each time, the computer generates a new number between 1 and 10. The program also displays the number of games the user has won out of how many games played. Finally, the games asks the user if he or she would like to continue playing the game. Type (start) to begin playing the game!!!")(newline)
(define (start)
(newline)
(let ((a (+ 1 (random 10))) (x 0)) ; sets a equal to a random number and x to 0
(set! b (+ 1 b)) ; adds 1 to b
(display "Round ")
(display b)(newline) ; displays round #
(display "Guess a number between 1 and 10?: ")
(set! x (read)) ; reads the inputed number by the user
(if (> x 10)
(begin (display "The number you have chosen is greater than 10! Please choose another number between 1 and 10?: ")
(set! x (read)))
)
(if (equal? a x) ; checks to see if the random number is equal to the number selected by the user
(begin (display "You have chosen wisely!")
(set! count (+ 1 count))) ; adds 1 to count
(display "You have chosen poorly!")))
(newline)(display "You have won ")
(display count) ; displays number of games the user has won
(display " out of ")
(display b) ; displays the total number of games played
(display " games! Your accuracy is ")
(display (* 100 (exact->inexact (/ count b)))) ; displays the accuracy in a percentage (ex. 1 of 4 games = 25%)
(display "%.")
(if (yesorno) (start) ; asks the user to continue, if yes program goes back to start
(bye)) ; if no program goes to bye
)
(define (yesorno)
(newline)(display "Continue (yes or no)?: ")
(eq? (read) 'yes) ; reads the inputed answer by the user to the continue question
)
(define (bye)
(display "Bye!")
)