ruby 公共要点 - 图灵 - 猜猜游戏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 公共要点 - 图灵 - 猜猜游戏相关的知识,希望对你有一定的参考价值。


rand_num = rand(1..100) + 1

puts "I have generated a random number for you to guess.  Please enter your guess."

attempts = 0
guess = ""

until guess == rand_num || attempts == 100 do

	#Level 1
	guess = gets.chomp.to_i

	if guess.to_i != rand_num
		puts "Please guess again."
	end

	#Level 2
	guess = gets.chomp.to_i
	if guess.to_i > rand_num
		puts "Your guess, #{guess}, is greater than the random number.  Please guess again."
	elsif guess.to_i < rand_num.to_i
		puts "Your guess, #{guess}, is less than the random number.  Please guess again."
	end

	#Level 3
	puts "Note the following updated game instructions:\nEnter 'c' to see the random number or enter your own guess."
	guess = gets.chomp.to_s
	if guess == 'c'
		puts "The random number is #{rand_num}. Enter enter a number to continue.\n Please enter another number:"
		guess = gets.chomp.to_i
	else guess.to_i != rand_num
		puts "That number does not match.  Here's another hint."
	end

	if guess != rand_num
		if rand_num.even?
			puts "The random number is divisible by two.  Please, guess another number:"
		else rand_num.odd?
			puts "The random number is not divisible by two.  Please guess another number:"
		end
	end

	attempts += 1
end

if guess == rand_num
	puts "Yay! You win the game!"
end

以上是关于ruby 公共要点 - 图灵 - 猜猜游戏的主要内容,如果未能解决你的问题,请参考以下文章

markdown 公共要点 - 图灵 - 完成专业发展的项目汇编

markdown 公共要点 - 图灵 - 预备工作第6天完成的项目汇编

markdown 公共要点 - 图灵 - 预备工作第5天完成的项目汇编

markdown 公共要点 - 图灵 - 预备工作第4天完成的项目汇编

markdown 公共要点 - 图灵 - 预备工作第3天完成的项目汇编

markdown 公共要点 - 图灵 - 预备工作第2天完成的项目汇编