-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnumguess.R
More file actions
119 lines (99 loc) · 2.98 KB
/
numguess.R
File metadata and controls
119 lines (99 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
##########################################################################################
##
## Use the "source" command in the R console to run this game! E.g.:
##
## library(RCurl)
## source(textConnection(
## getURL('https://raw.githubusercontent.com/DarthJDG/NumGuess/master/numguess.R')))
##
##########################################################################################
#' Transform string into integer
#' @param x character vector
#' @return integer
int <- function(x)
suppressWarnings(ifelse(as.numeric(x) == as.integer(x), as.integer(x), NA))
#' Cat sprintf results with ending line-break
#' @param fmt "character vector of format strings" to be passed to \code{sprintf}
#' @param ... values to be passed to \code{sprintf}
catsn <- function(fmt, ...)
cat(sprintf(fmt, ...), '\n')
## say hello
catsn('Welcome to NumGuess R version!\n')
## get username
name <- readline('Enter your name: ')
## and set the default one if nothing was provided
if (name == '')
name <- 'Player'
## get upper limit
limit <- readline(sprintf('\nWelcome %s, enter upper limit: ', name))
## validate limit
limit <- int(limit)
if (is.na(limit) || limit < 10)
limit <- 10L
## maximum number of tries
max_tries <- floor(log2(limit)) + 1
## infinite loop
while (TRUE) {
catsn('\nGuess my number between 1 and %d!\n', limit)
## generate a number between 1 and limit
number <- sample(1:limit, 1)
tries <- 0
## internal loop for guessing
while (TRUE) {
## ask for a number
guess <- readline('Guess: ')
## validate guess
guess <- int(guess)
if (is.na(guess)) {
catsn('That\'s just plain wrong.')
next()
}
if (guess < 1 | guess > limit) {
catsn('Out of range.')
next()
}
## check if we have a winner
tries <- tries + 1
if (number == guess)
break()
if (number < guess)
catsn('Too high!')
if (number > guess)
catsn('Too low!')
}
## congrats
catsn(
'\nWell done %s, you guessed my number from %d %s!',
name,
tries,
ifelse(tries == 1, 'try', 'tries'))
## custom message
cm <- ifelse(
tries == 1,
'You\'re one lucky bastard!',
ifelse(
tries < max_tries,
'You are the master of this game!',
ifelse(
tries == max_tries,
'You are a machine!',
ifelse(
tries <= max_tries * 1.1,
'Very good result!',
ifelse(
tries <= limit,
'Try harder, you can do better!',
'I find your lack of skill disturbing!'
)
)
)
)
)
catsn(cm)
## next round
nr <- readline('Play again [y/N]? ')
if (tolower(nr) != 'y')
break()
}
## say goodbye
catsn('\nOkay, bye.')