repl.it
Ruby

No description

fork
loading
Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
start_SRP = "y"
p_score = 0
c_score = 0

puts "哈囉! 這裡是和電腦玩剪刀石頭布消磨時間的地方! "
puts "\n規則:贏了得2分, 輸了扣1分, 平手時, 若有落後者其得1分, 直到其中一方先達到20分為止遊戲結束, 你也可以隨時結束遊戲!"
puts "\n欲開始遊戲, 請先用英文輸入任意暱稱:\n"

player = gets.chomp

puts "#{player} 你好, 那我們開始吧!"

round = 1
puts "\n ###### Round #{round} ###### "
puts "\n 目前 #{player}的總分: #{p_score} vs 電腦的總分: #{c_score}"

while start_SRP == "y"
  
  choices = ["剪刀 scissor", "石頭 rock", "布 paper"]

  puts "\n#{player}, 你要出剪刀(s)、石頭(r)還是布(p)呢? \n 請輸入英文代碼: "
  choice = gets.chomp

  if choice == "s"
    player_c = choices[0]
  elsif choice == "r"
    player_c = choices[1]
  elsif choice == "p"  
    player_c = choices[2]
      
  end
  
  until choice == "s" || choice == "r" || choice == "p"
    puts "無法辨識的選擇, 請鍵入代碼s, r, p 其中之一來決定出剪刀、石頭還是布"
    choice = gets.chomp
  end  

  i = rand(0..2)
  com = choices[i]

  puts "電腦出了 #{com}, 你出了 #{player_c} "
  if player_c == com
    if p_score < c_score
     puts "此局平手, 落後者得1分"
     p_score += 1
    elsif c_score < p_score
     puts "此局平手, 落後者得1分"
     c_score += 1
    else
     puts "平手, 得分不變" 
    end 
  elsif player_c == "石頭 rock" && com == "剪刀 scissor" || player_c == "布 paper" && com == "石頭 rock" || player_c == "剪刀 scissor" && com == "布 paper"
    puts "你贏了! 得到2分!"
    p_score += 2
    c_score -= 1
  else
    puts "你輸了~ 被扣1分"
    p_score -= 1
    c_score += 2
  end

  if p_score >= 20 || c_score >= 20
    puts "\n本次剪刀石頭布共進行了#{round} 回合! \n總比數 為  #{player}:#{p_score} vs 電腦:#{c_score}  "  
     if p_score > c_score
      puts "\n恭喜 #{player} 成為最終勝利者!!!"
     elsif c_score > p_score
      puts "\n這次的最終勝利者為電腦, 再來挑戰喔!"
     else 
      puts "\nw終場以平手結束w"  
     end 
    puts "\n這次有玩得開心嗎!? 期待下次再見!\n"
    puts "\n### Alpha Camp ! ###\n"
    break
  else
    puts "\n#{player}, 要再比一次嗎?(y/n)"
    start_SRP = gets.chomp
    until start_SRP == "y" || start_SRP == "n"
      puts "無法辨識的指令, 請鍵入y或n以繼續"
      start_SRP = gets.chomp
    end
    
    if start_SRP == "y"
      round += 1  
      puts "\n ###### Round #{round} ###### "
      puts "\n 目前 #{player}的總分: #{p_score} vs 電腦的總分: #{c_score}"
    elsif start_SRP == "n" 
      puts "\n本次剪刀石頭布共進行了#{round} 回合! \n總比數 為  #{player}:#{p_score} vs 電腦:#{c_score}  "  
       if p_score > c_score
        puts "\n恭喜 #{player} 成為最終勝利者!!!"
       elsif c_score > p_score
        puts "\n這次的最終勝利者為電腦, 再來挑戰喔!"
       else 
        puts "\nw終場以平手結束w"  
       end 
      puts "\n這次有玩得開心嗎!? 期待下次再見!\n"
      puts "\n### Alpha Camp ! ###\n"
      break
    end
  end  
end
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]