Boy2Man 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf09c31b373b25cb9019efd9b20a4b1699713a1f
4
- data.tar.gz: 9fa59a0c7cfb78b4fa28ce9dde125766aa48f1a8
3
+ metadata.gz: ad1515c7fbf182d910e436e725d895e6db0c3d1d
4
+ data.tar.gz: 560da65ae2ccd74bd5494ada9fc2e843bd82c89f
5
5
  SHA512:
6
- metadata.gz: 4838e5d2da32e34c6540bd13993043af74f1b0dda751c5feb347f326dcd0b74e3fa6e0368e3f663605e325f4457be54469e0275b8d70a0f480fde9ea159acef2
7
- data.tar.gz: 3c86dd5dce8505b9c1158d0b0f3a20d73da68aa7658e9ce5b892d3e24c8236048917f153354a4a38d367d24ce62f6850ba17c90fba189449a4f7da4ba2bc3073
6
+ metadata.gz: 64345a1514b2dd1315482e84c16c8d7790a37b9b68c0730bad848c187c4f95f948849a4536db7e9a0e02e55d2b02ac95ca2da41eac50cb03cdabf6108fe3ef38
7
+ data.tar.gz: a2a0b03535cf2a5bcdac4cb6d73f19a6a43e7daec560669762e01f39c9f0c5d5c4c4f3bf1c8900b1898a3d4657682a0608ea52faf3d5250224e2e3201cdb8111
@@ -1,109 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- coding: utf-8 -*-
3
- require "Boy2Man/version"
4
-
5
- module Boy2Man
6
-
7
- def self.play
8
- stand = Boy2Man.new
9
- loop do
10
- print '> '
11
- hand = gets.chomp
12
- case hand
13
- when "", "bye", "exit"
14
- exit
15
- when "グー", "チョキ", "パー"
16
- puts stand.match(hand)
17
- when "history"
18
- puts stand.history
19
- when "reset"
20
- stand.reset
21
- else
22
- puts stand.select_hand
23
- end
24
- end
25
- end
26
-
27
- # @!attribute [r] history
28
- # @return [Array] the history of player's hand
29
- class Boy2Man
30
- attr_reader :history
31
-
32
- def initialize
33
- @history = Array.new
34
- end
35
-
36
- def history
37
- # retrun deep copy of history, to prevent history to be changed.
38
- Marshal.load(Marshal.dump(@history))
39
- end
40
-
41
- # @return [Array] resets history
42
- def reset
43
- @history.clear
44
- end
45
-
46
- # @return [String]
47
- def match(hand)
48
- case hand
49
- when "グー", "チョキ", "パー"
50
- # 先に手を決めておかないと後出しになる
51
- selected = select_hand
52
- @history.push hand
53
- case judge(hand, selected)
54
- when hand
55
- selected + "\nYou Win!"
56
- when selected
57
- selected + "\nYou Lose!"
58
- else
59
- selected + "\nDraw!"
60
- end
61
- else
62
- end
63
- end
64
-
65
- # @return [String]
66
- def select_hand
67
- case predict
68
- when "グー" then "パー"
69
- when "チョキ" then "グー"
70
- when "パー" then "チョキ"
71
- end
72
- end
73
-
74
- private
75
- def predict
76
- @history.empty? ? %w(グー チョキ パー).sample : @history.sample
77
- end
78
-
79
- def judge(a, b)
80
- case a
81
- when "グー"
82
- if b == "チョキ"
83
- return a
84
- elsif b == "パー"
85
- return b
86
- else
87
- return nil
88
- end
89
- when "チョキ"
90
- if b == "パー"
91
- return a
92
- elsif b == "グー"
93
- return b
94
- else
95
- return nil
96
- end
97
- when "パー"
98
- if b == "グー"
99
- return a
100
- elsif b == "チョキ"
101
- return b
102
- else
103
- return nil
104
- end
105
- end
106
- end
107
-
108
- end
109
- end
3
+ require "Boy2Man/cli"
4
+ require "Boy2Man/janken"
5
+ require "Boy2Man/version"
@@ -0,0 +1,32 @@
1
+ module Boy2Man
2
+
3
+ def self.play
4
+ janken = Janken.new
5
+ loop do
6
+ print '> '
7
+ hand = gets.chomp
8
+ case hand
9
+ when "", "bye", "exit"
10
+ exit
11
+ when *HANDS
12
+ puts opponent = janken.pon(hand)
13
+
14
+ winner = Boy2Man.judge(hand, opponent)
15
+ if winner == hand
16
+ puts "You Win!"
17
+ elsif winner == opponent
18
+ puts "You Lose!"
19
+ else
20
+ puts "Draw!"
21
+ end
22
+ when "history"
23
+ puts janken.history
24
+ when "reset"
25
+ janken.reset
26
+ else
27
+ puts janken.select_hand
28
+ end
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,78 @@
1
+ module Boy2Man
2
+ HANDS = ["グー", "チョキ", "パー"]
3
+
4
+ def self.judge(a, b)
5
+ case a
6
+ when "グー"
7
+ if b == "チョキ"
8
+ return a
9
+ elsif b == "パー"
10
+ return b
11
+ else
12
+ return nil
13
+ end
14
+ when "チョキ"
15
+ if b == "パー"
16
+ return a
17
+ elsif b == "グー"
18
+ return b
19
+ else
20
+ return nil
21
+ end
22
+ when "パー"
23
+ if b == "グー"
24
+ return a
25
+ elsif b == "チョキ"
26
+ return b
27
+ else
28
+ return nil
29
+ end
30
+ end
31
+ end
32
+
33
+ # @!attribute [r] history
34
+ # @return [Array] the history of player's hand
35
+ class Janken
36
+ attr_reader :history
37
+
38
+ def initialize
39
+ @history = Array.new
40
+ end
41
+
42
+ def history
43
+ # retrun deep copy of history, to prevent history to be changed.
44
+ Marshal.load(Marshal.dump(@history))
45
+ end
46
+
47
+ # @return [Array] resets history
48
+ def reset
49
+ @history.clear
50
+ end
51
+
52
+ # @return [String]
53
+ def pon(hand)
54
+ case hand
55
+ when *HANDS
56
+ # 先に手を決めておかないと後出しになる
57
+ selected = case predict
58
+ when "グー" then "パー"
59
+ when "チョキ" then "グー"
60
+ when "パー" then "チョキ"
61
+ end
62
+ @history.push hand
63
+ selected
64
+ else
65
+ end
66
+ end
67
+
68
+ alias :hoi :pon
69
+ alias :ぽん :pon
70
+ alias :ほい :hoi
71
+
72
+ private
73
+ def predict
74
+ @history.empty? ? %w(グー チョキ パー).sample : @history.sample
75
+ end
76
+
77
+ end
78
+ end
@@ -1,3 +1,3 @@
1
1
  module Boy2Man
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -5,36 +5,5 @@ require 'test_helper'
5
5
  class TestBoy2Man < MiniTest::Unit::TestCase
6
6
  include Boy2Man
7
7
 
8
- def setup
9
- @stand = Boy2Man.new
10
- end
11
8
 
12
- def test_select_hand
13
- 100.times do
14
- assert_includes ["グー", "チョキ", "パー"], @stand.select_hand
15
- end
16
- end
17
-
18
- def test_history
19
- assert_respond_to @stand, :history
20
- assert_raises(NoMethodError) {
21
- @stand.history = ['something']
22
- }
23
-
24
- assert_equal([], @stand.history)
25
- @stand.match("グー")
26
- assert_equal(["グー"], @stand.history)
27
- @stand.match("チョキ")
28
- assert_equal(["グー", "チョキ"], @stand.history)
29
-
30
- @stand.history.push("something")
31
- assert_equal(["グー", "チョキ"], @stand.history)
32
- end
33
-
34
- def test_reset
35
- assert_respond_to @stand, :reset
36
- @stand.match("チョキ")
37
- @stand.reset
38
- assert_equal([], @stand.history)
39
- end
40
9
  end
@@ -0,0 +1,73 @@
1
+ require 'test_helper'
2
+
3
+ class TestBoy2Man < MiniTest::Unit::TestCase
4
+ def test_judge
5
+ assert_nil Boy2Man.judge("グー", "グー")
6
+ assert_equal "グー", Boy2Man.judge("グー", "チョキ")
7
+ assert_equal "パー", Boy2Man.judge("グー", "パー")
8
+
9
+ assert_equal "グー", Boy2Man.judge("チョキ", "グー")
10
+ assert_nil Boy2Man.judge("チョキ", "チョキ")
11
+ assert_equal "チョキ", Boy2Man.judge("チョキ", "パー")
12
+
13
+ assert_equal "パー", Boy2Man.judge("パー", "グー")
14
+ assert_equal "チョキ", Boy2Man.judge("パー", "チョキ")
15
+ assert_nil Boy2Man.judge("パー", "パー")
16
+ end
17
+
18
+ include Boy2Man
19
+ def setup
20
+ @janken = Janken.new
21
+ @じゃんけん = Janken.new
22
+ end
23
+
24
+ def test_history
25
+ assert_respond_to @janken, :history
26
+ assert_raises(NoMethodError) {
27
+ @janken.history = ['something']
28
+ }
29
+
30
+ assert_equal([], @janken.history)
31
+ @janken.pon("グー")
32
+ assert_equal(["グー"], @janken.history)
33
+ @janken.pon("チョキ")
34
+ assert_equal(["グー", "チョキ"], @janken.history)
35
+
36
+ @janken.history.push("something")
37
+ assert_equal(["グー", "チョキ"], @janken.history)
38
+ end
39
+
40
+ def test_reset
41
+ assert_respond_to @janken, :reset
42
+ @janken.pon("チョキ")
43
+ assert_equal(["チョキ"], @janken.history)
44
+ @janken.reset
45
+ assert_equal([], @janken.history)
46
+ end
47
+
48
+ def test_pon
49
+ assert_respond_to @janken, :pon
50
+ 100.times do
51
+ assert_includes ["グー", "チョキ", "パー"], @janken.pon("グー")
52
+ end
53
+ end
54
+
55
+ def test_hoi
56
+ assert_respond_to @janken, :hoi
57
+ end
58
+
59
+ def test_ぽん
60
+ assert_respond_to @じゃんけん, :ぽん
61
+ end
62
+
63
+
64
+ def test_ほい
65
+ assert_respond_to @じゃんけん, :ほい
66
+ end
67
+
68
+ def test_predict
69
+ 100.times do
70
+ assert_includes ["グー", "チョキ", "パー"], @janken.send(:predict)
71
+ end
72
+ end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Boy2Man
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zakuni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-08 00:00:00.000000000 Z
11
+ date: 2013-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,9 +111,12 @@ files:
111
111
  - Rakefile
112
112
  - bin/Boy2Man
113
113
  - lib/Boy2Man.rb
114
+ - lib/Boy2Man/cli.rb
115
+ - lib/Boy2Man/janken.rb
114
116
  - lib/Boy2Man/version.rb
115
117
  - test/test_Boy2Man.rb
116
118
  - test/test_helper.rb
119
+ - test/test_janken.rb
117
120
  homepage: https://github.com/zakuni/Boy2Man
118
121
  licenses:
119
122
  - MIT
@@ -141,4 +144,5 @@ summary: Rock Paper Scissors
141
144
  test_files:
142
145
  - test/test_Boy2Man.rb
143
146
  - test/test_helper.rb
147
+ - test/test_janken.rb
144
148
  has_rdoc: