I made a rock-paper-scissors program in Ruby. At that time, I used test-unit to confirm whether the judgment of winning or losing was correct.
First, we will put together the rock-paper-scissors methods in a class. Here, standard input / output is not performed, and only the return value is returned. This method is used as a part in the code of the test code and the rock-paper-scissors program.
janken_judge.rb
class Janken
def self.judge(my_hand, enemy_hand)
hands = {"g" => "Goo", "c" => "Choki", "p" => "Par"}
if my_hand == enemy_hand
"you are#{hands[my_hand]},I#{hands[enemy_hand]}, It's a draw."
elsif (my_hand == "g" && enemy_hand == "c") || (my_hand == "c" && enemy_hand == "p") || (my_hand == "p" && enemy_hand == "g")
"you are#{hands[my_hand]},I#{hands[enemy_hand]}, Your win."
else
"you are#{hands[my_hand]},I#{hands[enemy_hand]}, You lose."
end
end
end
I will write a test code that confirms whether the judgment is correct in all patterns using test-unit.
test_janken.rb
# 'test/unit'Requires method parts to test with
require 'test/unit'
require_relative 'janken_judge.rb'
# Test::Unit::Create a class that inherits TestCase. The name is test_Create a method that starts with.
class TestJanken < Test::Unit::TestCase
def test_janken
#Write a test. assert_equal Expected value A,Test target B, A=If B, pass the test. This time, enter the combination of all hands and the result of winning or losing.
assert_equal 'You are goo, I am goo, a draw.', Janken.judge("g", "g")
assert_equal 'You are a choki, I am a choki, a draw.', Janken.judge("c", "c")
assert_equal 'You are par, I am par, draw.', Janken.judge("p", "p")
assert_equal 'You are goo, I am choki, you win.', Janken.judge("g", "c")
assert_equal 'You are choki, I am par, you win.', Janken.judge("c", "p")
assert_equal 'You are par, I am goo, you win.', Janken.judge("p", "g")
assert_equal 'You are goo, I am par, you lose.', Janken.judge("g", "p")
assert_equal 'You are choki, I am goo, you lose.', Janken.judge("c", "g")
assert_equal 'You are par, I am choki, you lose.', Janken.judge("p", "c")
end
end
Create a class that inherits Test :: Unit :: TestCase. Create a method whose name starts with test_. Here, it is test_janken.
Finally, I will write a rock-paper-scissors program, which is the main logic that is actually executed. When you run this program, it waits for standard input and the user types g, c, or p. The computer randomly issues goo, choki, and par, determines the win or loss, and outputs as follows. Example) You are Choki, I am Par, you win.
janken.rb
#Requires the method part created earlier
require_relative 'janken_judge.rb'
#User g,c,p Enter either hand
my_hand = gets.chomp
#The computer makes random moves.
enemy_hand = ["g","c","p"].sample
#The judgment of rock-paper-scissors is output.
puts Janken.judge(my_hand, enemy_hand)
Let's run it.
% ruby janken.rb
g
You are goo, I am par, you lose.
From the above results, the rock-paper-scissors program seems to be working properly. Let's run a test to see if all the patterns are judged correctly.
% ruby test_janken.rb
Loaded suite test_janken
Started
.
Finished in 0.001524 seconds.
--------------------------------------------------------------------------------
1 tests, 9 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
--------------------------------------------------------------------------------
I got 9 assertions, 0 failures, 100% passed, so I was able to confirm that all the patterns passed the test.
So if you're writing the wrong code, try it. In the line after the else in janken_judge.rb, change "You lose" to "You win" and then run the test.
% ruby test_janken.rb
Loaded suite test_janken
Started
F
================================================================================
12: assert_equal 'You are goo, I am choki, you win.', Janken.judge("g", "c")
13: assert_equal 'You are choki, I am par, you win.', Janken.judge("c", "p")
14: assert_equal 'You are par, I am goo, you win.', Janken.judge("p", "g")
=> 15: assert_equal 'You are goo, I am par, you lose.', Janken.judge("g", "p")
16: assert_equal 'You are choki, I am goo, you lose.', Janken.judge("c", "g")
17: assert_equal 'You are par, I am choki, you lose.', Janken.judge("p", "c")
18: end
test_janken.rb:15:in `test_janken'
<"You are goo, I am par, you lose."> expected but was
<"You are goo, I am par, you win.">
diff:
?You are goo, I am par, you lose.
?win
Failure: test_janken(TestJanken)
================================================================================
Finished in 0.01695 seconds.
--------------------------------------------------------------------------------
1 tests, 7 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
--------------------------------------------------------------------------------
He told me, "I want you to say" lose ", but you've become" win "." I found that the test was working well.