Guess the number/With feedback: Difference between revisions

m
→‎{{header|R}}: Improved syntax.
m (→‎{{header|R}}: Syntax highlighting.)
m (→‎{{header|R}}: Improved syntax.)
Line 3,597:
This solution works on the assumption that the number to be found is an integer and also assumes that the upper and lower bounds are distinct integers used inclusively. For example, this means that low=4 and high=5 should be a solvable case, but low=high=4 will throw an error. See [[Talk:Guess the number/With feedback|Talk page]] entry dated 1st June 2020.
 
<lang rsplus>guessANumber <- function(low, high)
{
boundryErrorCheck(low, high)
goal <- sample(low:high, size = 1)
guess <- getValidInput(paste0("I have a whole number between ", low, " and ", high, ". What's your guess? "))
while(guess != goal)
{
if(guess < low || guess > high){guess <- getValidInput("Out of range! Try again "); next}
if(guess > goal){guess <- getValidInput("Too high! Try again "); next}
if(guess < goal){guess <- getValidInput("Too low! Try again "); next}
}
"Winner!"
}
 
boundryErrorCheck <- function(low, high)
{
if(!is.numeric(low) || as.integer(low) != low){ stop("Lower bound must be an integer. Try again.")}
if(!is.numeric(high) || as.integer(high) != high){ stop("Upper bound must be an integer. Try again.")}
if(high < low){ stop("Upper bound must be strictly greater than lower bound. Try again.")}
if(low == high){ stop("This game is impossible to lose. Try again.")}
invisible()
}
Line 3,624:
#A better way to check for integer inputs can be found in is.interger's docs, but this is easier to read.
#Note that readline outputs the user's input as a string, hence the need for type.convert.
getValidInput <- function(requestText)
{
guess <- type.convert(readline(requestText))
while(!is.numeric(guess) || as.integer(guess) != guess){guess <- type.convert(readline("That wasn't an integer! Try again "))}
as.integer(guess)
}</lang>
331

edits