Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $e = 'Error: '
- def newline
- puts "\n"
- end
- def main
- puts 'New game.'
- newline
- cows = 0
- bulls = 0
- counter = 0
- x = []
- a = (0..9).to_a
- until x.size == 4
- tox = a[rand(a.size)]
- x.push(tox.to_s)
- a.delete(tox)
- end
- while true
- print 'Input number: '
- input = gets.chomp
- begin
- if input[0,1] == '0'
- inpcopy = input.tr_s('','') # create copy of input
- inpcopy[0] = '' # delete first symbol zero
- Integer(inpcopy)
- else
- Integer(input)
- end
- rescue ArgumentError
- puts $e + 'not an integer'
- next
- end
- turn = input.split('')
- if turn.length != 4
- puts $e + 'wrong length, must be 4'
- next
- end
- if turn != turn.uniq
- puts $e + 'doubled digit'
- next
- end
- turn.each do |elem|
- cows = cows.next if x.include?(elem)
- end
- for i in 0..3
- bulls = bulls.next if turn[i] == x[i]
- end
- cows = cows - bulls
- counter = counter.next
- puts "#{counter}) Cows: #{cows}, Bulls: #{bulls}"
- newline
- cows = 0
- bulls = 0
- if turn == x
- puts "You did it for #{counter} moves!"
- newline
- main
- end
- end
- end
- main
Advertisement
Add Comment
Please, Sign In to add comment