celestialgod

R guess number game

Dec 8th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.41 KB | None | 0 0
  1. s <- sample(0:9, 4, replace = FALSE)
  2. while (TRUE) {
  3.   g <- readline('Please input your guess! Guess number:')
  4.   if (!grepl("\\d{4}", g)) {
  5.     cat('You should input 4 digit number.')
  6.     next
  7.   }
  8.   g <- strsplit(g, "")[[1]]
  9.   cnt_a <- sum(g == s)
  10.   cnt_b <- sum(g %in% s) - cnt_a
  11.   if (all(g == s)) {
  12.     cat('You are right!!!!!!!')
  13.     break
  14.   } else {
  15.     cat(sprintf("%iA%iB", cnt_a, cnt_b))
  16.   }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment