Ruby implements the stone scissors cloth Game Example _ruby topic

Source: Internet
Author: User

Ruby implements Rock and scissors game

Copy Code code as follows:

#encoding: Utf-8
arr = [' Stone ', ' scissors ', ' cloth ']
Win_arr = [[' Stone ', ' scissors '], [' scissors ', ' cloth '], [' cloth ', ' stone ']]
#随机computer的值, put in the result array
result = [Arr.sample]
while (true)
Puts "Please input stone, scissors, 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 ' tie '
Elsif win_arr.include? Result
Puts ' computer wins '
Else
Puts ' you won '
Break
End
Else
Puts ' input value is incorrect, please input stone, scissors, cloth '
Next
End
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.