第 3 章: 条件分岐とループ

crane_turtle_and_dragonfly.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
# crane_turtle_and_dragonfly.rb

def read_integer(name)
  puts("The number of #{name} ?")
  s = gets()
  Integer(s)
end

heads = read_integer("heads")
legs = read_integer("legs")
wings = read_integer("wings")

found = false

for dragonfiles in (0..heads)
  max_turtles = heads - dragonfiles
  for turtles in (0..max_turtles)
    cranes = heads - turtles - dragonfiles
    if cranes >= 0 && wings == 4*dragonfiles+2*cranes && legs == 6*dragonfiles+4*turtles+2*cranes
      found = true
      break
    end
  end
  if found
    break
  end
end

if found
  puts("#{cranes} cranes, #{turtles} turtles, #{dragonfiles} dragonfiles")
else
  puts("invalid combination")
end

crane_turtle_and_dragonfly.rb の実行結果は:

[wtopia ruby.begin]$ ruby crane_turtle_and_dragonfly.rb
The number of heads ?
5
The number of legs ?
34
The number of wings ?
6
invalid combination
[wtopia ruby.begin]$ ruby crane_turtle_and_dragonfly.rb
The number of heads ?
12
The number of legs ?
52
The number of wings ?
26
3 cranes, 4 turtles, 5 dragonfiles

じゃんけんするプログラム

janken.rb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# janken.rb

puts("paper=0, scisors=1, stone=2 ?")

choice = gets()
player = Integer(choice)

computer = rand(3)

puts("computer: #{computer}")

if player == computer
  puts("even")
else
  diff = player - computer
  if diff == 1 || diff == -2
    puts("win")
  else
    puts("lose")
  end
end

janken.rb の実行結果は:

[wtopia ruby.begin]$ ruby janken.rb
paper=0, scisors=1, stone=2 ?
2
computer: 1
win
[wtopia ruby.begin]$ ruby janken.rb
paper=0, scisors=1, stone=2 ?
0
computer: 1
lose

janken2.rb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# janken2.rb

puts("paper=0, scisors=1, stone=2 ?")

choice = gets()
player = Integer(choice)

computer = rand(3)

puts("computer: #{computer}")

diff = player - computer

if diff == 0
  puts("even")
  
elsif diff == 1 || diff == -2
  puts("win")
  
else
  puts("lose")
  
end

janken2.rb の実行結果は:

[wtopia ruby.begin]$ ruby janken2.rb
paper=0, scisors=1, stone=2 ?
2
computer: 1
win
[wtopia ruby.begin]$ ruby janken2.rb
paper=0, scisors=1, stone=2 ?
2
computer: 0
lose

while 式

janken_match.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
PAPER = 0
SCISORS = 1
STONE = 2

com_win = 0 # コンピュータの勝ちの数
plr_win = 0 # プレヤーの勝ちの数

while com_win < 3 && plr_win < 3
  puts("paper=0, scisors=1, stone=2 ?")
  choice = gets()
  player = Integer(choice)
  computer = rand(3)
  puts("computer: #{computer}")
  if player == PAPER && computer == STONE || player == SCISORS && computer == PAPER || player == STONE && computer == SCISORS
    plr_win += 1
  elsif player == PAPER && computer == SCISORS || player == SCISORS && computer == STONE || player == STONE && computer == PAPER
    com_win += 1
  end
end

if plr_win >= 3
  puts("you win!")
else
  puts("you lose!")
end

janken_match.rb の実行結果は:

[wtopia ruby.begin]$ ruby janken_match.rb
paper=0, scisors=1, stone=2 ?
1
computer: 0
paper=0, scisors=1, stone=2 ?
2
computer: 0
paper=0, scisors=1, stone=2 ?
1
computer: 2
paper=0, scisors=1, stone=2 ?
2
computer: 2
paper=0, scisors=1, stone=2 ?
2
computer: 1
paper=0, scisors=1, stone=2 ?
2
computer: 2
paper=0, scisors=1, stone=2 ?
2
computer: 2
paper=0, scisors=1, stone=2 ?
2
computer: 0
you lose!

モンティホール問題を解くプログラム

monty_hall.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
# モンティホール問題のシミュレーション

def do_game(change)
  door0 = door1 = door2 = 0
  hit = rand(3)
  if hit == 0
    door0 = 1
  elsif hit == 1
    door1 = 1
  else
    door2 = 1
  end

  if change
    if door1 == 0
      return door2
    else
      return door1
    end
  end
  return door0 # ruby では, return 文を省略することができる
end

def winning_percentage(games, change)
  won = 0
  for i in (1..games)
    won += do_game(change)
  end
  return won / Float(games) # ruby では, return 文を省略することができる
end

GAMES = 100
puts("change: #{winning_percentage(GAMES, true)}, no-change: #{winning_percentage(GAMES, false)}")

monty_hall.rb の実行結果は:

[wtopia ruby.begin]$ ruby monty_hall.rb
change: 0.644, no-change: 0.323
[wtopia ruby.begin]$ ruby monty_hall.rb
change: 0.672, no-change: 0.339
[wtopia ruby.begin]$ ruby monty_hall.rb
change: 0.665, no-change: 0.312
[wtopia ruby.begin]$ ruby monty_hall.rb
change: 0.649, no-change: 0.339
[wtopia ruby.begin]$ ruby monty_hall.rb
change: 0.677, no-change: 0.329

無限に繰り返すループ

テンプレート:

while true
  (式1)...
  (式1)...
end

average.rb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# average.rb

total = 0.0
count = 0

while true
  puts("Enter the number")
  input_value = gets()
  if input_value == "\n"
    break
  end
  total += Float(input_value)
  count += 1
end

puts("total = #{total}, count = #{count}, average = #{total / count}")

average.rb の実行結果は:

[wtopia ruby.begin]$ ruby average.rb
Enter the number
20
Enter the number
40
Enter the number
30
Enter the number
40
Enter the number
20.5
Enter the number

total = 150.5, count = 5, average = 30.1