標籤:ar new user ad rom 遊戲 ef 線程
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 endend# main thread runmain()