-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_functions.R
53 lines (46 loc) · 1.32 KB
/
input_functions.R
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
# Prompts user to choose from a set of allowed inputs.
# Keeps prompting until user picks one.
# Returns in uppercase.
validated_input <- function(prompt, allowed_inputs, error_msg) {
input <- " "
while (!(input %in% allowed_inputs)) {
if (interactive()) {
con <- stdin()
} else {
con <- "stdin"
}
cat(prompt, "")
input <- readLines(con = con, n = 1)
if (input %in% c("q", "quit")) {
quit()
}
if (!(input %in% allowed_inputs)) {
cat(error_msg, "\n")
}
}
return(input)
}
# Prompts to choose between X and O
initial_input <- function() {
choices <- c("x", "X", "o", "O")
input <- validated_input("X or O?", choices, "Please choose either X or O.")
return(toupper(input))
}
# Get player move
player_turn_input <- function(size, valid_moves) {
cat("Your turn!\n")
choices <- paste(seq.int(size))
move <- 0
while (!(move %in% valid_moves)) {
row <- as.integer(validated_input("What row?", choices, paste("Please choose a row number between 1 and", size)))
col <- as.integer(validated_input("What column?", choices, paste("Please choose a column number between 1 and", size)))
move <- row * size + col
# If invalid, ask again
if (move %in% valid_moves) {
return(c(row, col))
}
else {
cat("That's not a valid move.\n")
}
}
}