cinch_hangman 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,10 @@
1
1
  module Cinch::Plugins
2
2
  class Hangman
3
3
  include Cinch::Plugin
4
- match /hang guess (.*)/i, :method => :guess
5
- match /hang new (.*) (.*)/i, :method => :new_game
6
- match /hang status/i, :method => :status
4
+ match /hang (.*)/i, :method => :guess
5
+ match /hang new (#\S*) (.*)/i, :method => :new_game
7
6
  def new_game(m, channel, answer)
8
- @game = Game.new(answer)
7
+ @game = CinchHangman::Game.new(answer)
9
8
  Channel(channel).send(@game.describe)
10
9
  end
11
10
  def guess(m, guess)
@@ -16,43 +15,4 @@ module Cinch::Plugins
16
15
  m.reply @game.describe
17
16
  end
18
17
  end
19
- class Game
20
- def initialize(answer, max_guesses = 6)
21
- @answer = answer
22
- @max_guesses = max_guesses
23
- @correct_chars = Set.new
24
- @incorrect_guesses = []
25
- end
26
- def guess(guess)
27
- if @answer.include?(guess)
28
- @correct_chars.merge(guess.chars)
29
- else
30
- @incorrect_guesses << guess
31
- end
32
- end
33
- def describe
34
- if @correct_chars.empty? && @incorrect_guesses.empty?
35
- "(#{hint}) a new hangman riddle has started."
36
- elsif won
37
- "(#{hint}) that hangman riddle was solved, awesome!"
38
- elsif lost
39
- "(#{hint}) that hangman riddle was just too difficult, that sucks!"
40
- else
41
- "(#{hint}) #{guesses_left} guesses left on the current hangman riddle."
42
- end
43
- end
44
- def won
45
- @correct_chars.superset?(Set.new(@answer.chars))
46
- end
47
- def lost
48
- guesses_left == 0
49
- end
50
- def guesses_left
51
- @max_guesses - @incorrect_guesses.size
52
- end
53
- def hint
54
- @answer.chars.map { |char| @correct_chars.include?(char) ? char : "_" }.join(" ")
55
- end
56
-
57
- end
58
18
  end
@@ -0,0 +1,33 @@
1
+ module CinchHangman
2
+ class Game
3
+ MASK = "_"
4
+ def initialize(answer, max_guesses = 6)
5
+ @answer = answer.downcase
6
+ @max_guesses = max_guesses
7
+ @guesses = ""
8
+ end
9
+ def guess(guess)
10
+ @guesses << guess.downcase.gsub(/[^a-z]/, "")
11
+ end
12
+ def describe
13
+ if @guesses.empty?
14
+ "(#{hint}) hangman started."
15
+ elsif solved
16
+ "(#{@answer}) was solved!"
17
+ elsif guesses_left <= 0
18
+ "(#{@answer}) was too difficult!"
19
+ else
20
+ "(#{hint}) #{guesses_left} guesses left."
21
+ end
22
+ end
23
+ def solved
24
+ !hint.include?(MASK)
25
+ end
26
+ def guesses_left
27
+ @max_guesses - @guesses.gsub(/[#{@answer}]/, "").size
28
+ end
29
+ def hint
30
+ @answer.gsub(/[^\s#{@guesses}]/, MASK)
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module CinchHangman
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/cinch_hangman.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "cinch_hangman/version"
2
+ require "cinch_hangman/game"
2
3
  require "cinch/plugins/hangman"
data/spec/game_spec.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'rubygems'
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '..')
4
+
5
+ require 'cinch'
6
+ require 'cinch_hangman'
7
+
8
+ describe CinchHangman::Game do
9
+ subject do
10
+ CinchHangman::Game.new("oh")
11
+ end
12
+ it 'should have a start' do
13
+ subject.describe.should match /hangman.*started/
14
+ end
15
+ describe 'guesses' do
16
+ describe 'ignore' do
17
+ it 'digits' do
18
+ subject.guess("x3")
19
+ subject.describe.should include '5 guesses'
20
+ end
21
+ it 'whitespace' do
22
+ subject.guess("c a t")
23
+ subject.describe.should include '3 guesses'
24
+ end
25
+ end
26
+ describe 'correct' do
27
+ it 'when containing a single character from the answer' do
28
+ subject.guess("h")
29
+ subject.describe.should include '6 guesses'
30
+ end
31
+ it 'when containing a single character from the answer in the wrong case' do
32
+ subject.guess("H")
33
+ subject.describe.should include '6 guesses'
34
+ end
35
+ end
36
+ describe 'incorrect' do
37
+ it 'when containing a single character not in the answer' do
38
+ subject.guess("x")
39
+ subject.describe.should include '5 guesses'
40
+ end
41
+ it 'when containing multiple characters not in the answer' do
42
+ subject.guess("cat")
43
+ subject.describe.should include '3 guesses'
44
+ end
45
+ end
46
+ end
47
+ describe 'hints' do
48
+ it 'show the number of letters in the answer' do
49
+ subject.describe.should include '__'
50
+ end
51
+ it 'show letters that have been guessed correctly' do
52
+ subject.guess("h")
53
+ subject.describe.should include '_h'
54
+ end
55
+ end
56
+ describe 'winning' do
57
+ it 'occurs when the answer is guessed using individual characters' do
58
+ subject.guess("o")
59
+ subject.guess("h")
60
+ subject.describe.should include 'solved!'
61
+ end
62
+ it 'occurs when the answer is guessed in one go' do
63
+ subject.guess("oh")
64
+ subject.describe.should include 'solved!'
65
+ end
66
+ it 'occurs when the answer is given with a mixture of approaches' do
67
+ subject.guess("o")
68
+ subject.guess("oh")
69
+ subject.describe.should include 'solved!'
70
+ end
71
+ it 'occurs no matter what order guesses are given' do
72
+ subject.guess("h")
73
+ subject.guess("o")
74
+ subject.describe.should include 'solved!'
75
+ end
76
+ it 'should not be automatic for hassox' do
77
+ subject = CinchHangman::Game.new("hassox")
78
+ subject.guess("s")
79
+ subject.describe.should_not include 'solved!'
80
+ end
81
+ it 'should ignore answer case' do
82
+ subject = CinchHangman::Game.new("WoW")
83
+ subject.guess("wow")
84
+ subject.describe.should include 'solved!'
85
+ end
86
+ it 'should ignore whitespace' do
87
+ subject = CinchHangman::Game.new("oh my")
88
+ subject.guess("ohmy")
89
+ subject.describe.should include 'solved!'
90
+ end
91
+ end
92
+ describe 'losing' do
93
+ before do
94
+ 6.times do
95
+ subject.guess("z")
96
+ end
97
+ end
98
+ it 'occurs when all the guesses run out' do
99
+ subject.describe.should include 'too difficult!'
100
+ end
101
+ it 'is permanent' do
102
+ 2.times do
103
+ subject.guess("z")
104
+ end
105
+ subject.describe.should include 'too difficult!'
106
+ end
107
+ it 'informs players of the answer' do
108
+ subject.describe.should include 'oh'
109
+ end
110
+ end
111
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch_hangman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-08 00:00:00.000000000Z
12
+ date: 2011-09-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cinch
16
- requirement: &2153442480 !ruby/object:Gem::Requirement
16
+ requirement: &2151891120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153442480
24
+ version_requirements: *2151891120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2153441180 !ruby/object:Gem::Requirement
27
+ requirement: &2151888940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153441180
35
+ version_requirements: *2151888940
36
36
  description:
37
37
  email:
38
38
  - shanonmcquay@gmail.com
@@ -46,8 +46,9 @@ files:
46
46
  - cinch_hangman.gemspec
47
47
  - lib/cinch/plugins/hangman.rb
48
48
  - lib/cinch_hangman.rb
49
+ - lib/cinch_hangman/game.rb
49
50
  - lib/cinch_hangman/version.rb
50
- - spec/hangman_spec.rb
51
+ - spec/game_spec.rb
51
52
  homepage: https://github.com/compactcode/cinch_hangman
52
53
  licenses: []
53
54
  post_install_message:
@@ -62,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
63
  version: '0'
63
64
  segments:
64
65
  - 0
65
- hash: 1370904378984459373
66
+ hash: -899560349152351154
66
67
  required_rubygems_version: !ruby/object:Gem::Requirement
67
68
  none: false
68
69
  requirements:
@@ -71,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
72
  version: '0'
72
73
  segments:
73
74
  - 0
74
- hash: 1370904378984459373
75
+ hash: -899560349152351154
75
76
  requirements: []
76
77
  rubyforge_project: cinch_hangman
77
78
  rubygems_version: 1.8.6
@@ -79,4 +80,4 @@ signing_key:
79
80
  specification_version: 3
80
81
  summary: Let your IRC bot conduct a game of hangman.
81
82
  test_files:
82
- - spec/hangman_spec.rb
83
+ - spec/game_spec.rb
data/spec/hangman_spec.rb DELETED
@@ -1,83 +0,0 @@
1
- require 'rubygems'
2
-
3
- $:.unshift File.join(File.dirname(__FILE__), '..')
4
-
5
- require 'cinch'
6
- require 'cinch_hangman'
7
-
8
- describe Cinch::Plugins::Game do
9
- subject do
10
- Cinch::Plugins::Game.new("oh")
11
- end
12
- describe 'game' do
13
- it 'should have a start' do
14
- subject.describe.should include 'new hangman'
15
- end
16
- describe 'guesses' do
17
- describe 'correct' do
18
- it 'when containing a single character in the answer' do
19
- subject.guess("h")
20
- subject.describe.should include '6 guesses'
21
- end
22
- end
23
- describe 'incorrect' do
24
- it 'when containing a single character not in the answer' do
25
- subject.guess("x")
26
- subject.describe.should include '5 guesses'
27
- end
28
- it 'when containing a word not in the answer' do
29
- subject.guess("lol")
30
- subject.describe.should include '5 guesses'
31
- end
32
- it 'when containing the correct answer in reverse' do
33
- subject.guess("ho")
34
- subject.describe.should include '5 guesses'
35
- end
36
- end
37
- end
38
- describe 'hints' do
39
- it 'show the number of letters in the answer' do
40
- subject.guess("x")
41
- subject.describe.should include '_ _'
42
- end
43
- it 'show letters that have been guessed correctly' do
44
- subject.guess("h")
45
- subject.describe.should include '_ h'
46
- end
47
- end
48
- describe 'winning' do
49
- it 'occurs when the answer is guessed using individual characters' do
50
- subject.guess("o")
51
- subject.guess("h")
52
- subject.describe.should include 'awesome!'
53
- end
54
- it 'occurs when the answer is guessed in one go' do
55
- subject.guess("oh")
56
- subject.describe.should include 'awesome!'
57
- end
58
- it 'occurs when the answer is given with a mixture of approaches' do
59
- subject.guess("o")
60
- subject.guess("oh")
61
- subject.describe.should include 'awesome!'
62
- end
63
- it 'occurs no matter what order guesses are given' do
64
- subject.guess("h")
65
- subject.guess("o")
66
- subject.describe.should include 'awesome!'
67
- end
68
- it 'should not be automatic for hassox' do
69
- subject = Cinch::Plugins::Game.new("hassox")
70
- subject.guess("s")
71
- subject.describe.should_not include 'awesome!'
72
- end
73
- end
74
- describe 'losing' do
75
- it 'occurs when all the guesses run out' do
76
- 6.times do
77
- subject.guess("z")
78
- end
79
- subject.describe.should include 'sucks!'
80
- end
81
- end
82
- end
83
- end