Guess the number/With feedback (player): Difference between revisions
Content added Content deleted
ReeceGoding (talk | contribs) m (→{{header|R}}: Syntax highlighting.) |
ReeceGoding (talk | contribs) (→{{header|R}}: Improved syntax.) |
||
Line 2,669: | Line 2,669: | ||
=={{header|R}}== |
=={{header|R}}== |
||
Can be fooled if you lie to it. For example, always reporting "h" for guessANumberPlayer(1,5) will have it guess 0. |
Can be fooled if you lie to it. For example, always reporting "h" for guessANumberPlayer(1, 5) will have it guess 0. |
||
<lang rsplus>guessANumberPlayer<-function(low,high) |
<lang rsplus>guessANumberPlayer <- function(low, high) |
||
{ |
{ |
||
boundryErrorCheck(low,high) |
boundryErrorCheck(low, high) |
||
repeat |
repeat |
||
{ |
{ |
||
guess<-floor(mean(c(low,high))) |
guess <- floor(mean(c(low, high))) |
||
#Invalid inputs to this switch will simply cause the repeat loop to run again, breaking nothing. |
#Invalid inputs to this switch will simply cause the repeat loop to run again, breaking nothing. |
||
switch(guessResult(guess), |
switch(guessResult(guess), |
||
l = low<-guess+1, |
l = low <- guess + 1, |
||
h = high<-guess-1, |
h = high <- guess - 1, |
||
c = return(paste0("Your number is ",guess,"."," I win!"))) |
c = return(paste0("Your number is ", guess, ".", " I win!"))) |
||
} |
} |
||
} |
} |
||
#Copied from my solution at https://rosettacode.org/wiki/Guess_the_number/With_feedback#R |
#Copied from my solution at https://rosettacode.org/wiki/Guess_the_number/With_feedback#R |
||
boundryErrorCheck<-function(low,high) |
boundryErrorCheck <- function(low, high) |
||
{ |
{ |
||
if(!is.numeric(low)||as.integer(low)!=low) |
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) |
if(!is.numeric(high) || as.integer(high) != high) stop("Upper bound must be an integer. Try again.") |
||
if(high<low) |
if(high < low) stop("Upper bound must be strictly greater than lower bound. Try again.") |
||
if(low==high) |
if(low == high) stop("This game is impossible to lose. Try again.") |
||
invisible() |
invisible() |
||
} |
} |
||
⚫ | |||
guessResult<-function(guess) |
|||
{ |
|||
⚫ | |||
}</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |