Guest User

Untitled

a guest
Jan 24th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.50 KB | None | 0 0
  1.  
  2. $e = 'Error: '
  3.  
  4. def newline
  5.     puts "\n"
  6. end
  7.  
  8. def main
  9.     puts 'New game.'
  10.     newline
  11.     cows = 0
  12.     bulls = 0
  13.     counter = 0
  14.    
  15.     x = []
  16.     a = (0..9).to_a
  17.     until x.size == 4
  18.         tox = a[rand(a.size)]
  19.         x.push(tox.to_s)
  20.         a.delete(tox)
  21.     end
  22.    
  23.     while true
  24.    
  25.         print 'Input number: '
  26.         input = gets.chomp
  27.  
  28.         begin
  29.             if input[0,1] == '0'
  30.                 inpcopy = input.tr_s('','') # create copy of input
  31.                 inpcopy[0] = ''             # delete first symbol zero
  32.                 Integer(inpcopy)
  33.             else
  34.                 Integer(input)
  35.             end
  36.         rescue ArgumentError
  37.             puts $e + 'not an integer'
  38.             next
  39.         end
  40.  
  41.         turn = input.split('')
  42.        
  43.         if turn.length != 4
  44.             puts $e + 'wrong length, must be 4'
  45.             next
  46.         end
  47.         if turn != turn.uniq
  48.             puts $e + 'doubled digit'
  49.             next
  50.         end
  51.  
  52.         turn.each do |elem|
  53.             cows = cows.next if x.include?(elem)
  54.         end
  55.         for i in 0..3
  56.             bulls = bulls.next if turn[i] == x[i]
  57.         end
  58.         cows = cows - bulls
  59.         counter = counter.next
  60.         puts "#{counter}) Cows: #{cows}, Bulls: #{bulls}"
  61.         newline
  62.         cows = 0
  63.         bulls = 0
  64.  
  65.         if turn == x
  66.             puts "You did it for #{counter} moves!"
  67.             newline
  68.             main
  69.         end
  70.     end
  71. end
  72.  
  73. main
Advertisement
Add Comment
Please, Sign In to add comment