Example of stone scissors game implemented by ruby

Time:2020-3-30

Ruby realizes the game of stone scissors

Copy codeThe code is as follows:
#encoding: utf-8
Arr = [‘stone’, ‘scissors’,’ cloth ‘]
Win_arr = [‘stone’, ‘scissors’], [‘ scissors’, ‘cloth’], [‘cloth’, ‘stone’]]
#The value of random computer, put into the result array
result = [arr.sample]
while (true)
“Please input stone, scissors and cloth”
  input_value = gets.force_encoding(“GBK”).encode(“UTF-8”).chomp
  if arr.include? input_value
    result << input_value
    if result[0] == result[1]
Puts’ draw ‘
    elsif win_arr.include? result
Puts’ computer wins’
    else
Puts’ you won ‘
      break
    end
  else
The input value of puts’ is wrong. Please input stone, scissors and cloth ‘
    next
  end
end

Recommended Today

Django log — output to log file

We all know that it is very important to log in the project, because bugs appear when the project is running in the server and there is no log to record. We may not know that bugs exist at all. In the past, I didn’t feel that I had made my own development in school. […]