21点游戏,命令行的,面像过程的写法。
ruby 的写法以后再试试。
# this is game 21 point # 操作命令符 空没有操作,n下一步,抽牌;w亮牌结束;l认输 $ope = '' $prompt = '>' $x1 = 0 $x2 = 0 $x3 = 0 $pc = 0 $u1 = 0 $u2 = 0 $u3 = 0 $over = '' def out_main(a = 0, b=0, c=0, pc = 0, us1=0, us2=0, us3=0) puts "21 point Game " puts " -- wide288 author" puts puts " PC is keys " if pc == 0 puts " * * * " else puts " %d %d %d "% [a, b, c] end puts puts " user is keys " puts " %d %d %d " % [us1, us2, us3] puts Time.new puts " n 抽牌; w 亮牌; l 认输; x 退出;" end #主线程运行 def main() while $ope != 'x' system 'clear' out_main($x1, $x2, $x3, $pc, $u1, $u2, $u3) if 'w' != $ope print $prompt $ope = STDIN.gets.chomp() end if 'x' == $ope break end if 'l' == $ope puts "I throw up." break end if 'n' == $ope number = rand(1..10) if 0 != number puts number if $x1 == 0 $x1 = number elsif $x2 == 0 $x2 = number elsif $x3 == 0 $x3 = number end end number = rand(1..9) if 0 != number puts number if $u1 == 0 $u1 = number elsif $u2 == 0 $u2 = number elsif $u3 == 0 $u3 = number end end end if 'w' == $ope #system "clear" $pc = 1 pcsum = 21 - ($x1+$x2+$x3) usersum = 21 - ($u1+$u2+$u3) puts "%d %d %d pc sum is - 21 = %d" % [$x1, $x2, $x3, pcsum] puts "%d %d %d user sum is - 21 = %d" % [$u1, $u2, $u3, usersum] if usersum >= pcsum puts "pc win" else puts "User Win!!!" end #out_main($x1, $x2, $x3, $pc, $u1, $u2, $u3) break end end end # main thread run main()
21点游戏,命令行的,面像过程的写法。Ruby
时间: 2024-10-12 18:17:41