hangman_y 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hangman.gemspec
4
+ gem 'pry'
5
+ gem 'coveralls', require: false
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 andela-ydaniju
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Hangman
2
+
3
+ [![Code Climate](https://codeclimate.com/github/andela-ydaniju/hangman/badges/gpa.svg)](https://codeclimate.com/github/andela-ydaniju/hangman) [![Build Status](https://travis-ci.org/andela-ydaniju/hangman.svg?branch=master)](https://travis-ci.org/andela-ydaniju/hangman) [![Coverage Status](https://coveralls.io/repos/andela-ydaniju/hangman/badge.svg?branch=master&service=github)](https://coveralls.io/github/andela-ydaniju/hangman?branch=master) [![Issue Count](https://codeclimate.com/github/andela-ydaniju/hangman/badges/issue_count.svg)](https://codeclimate.com/github/andela-ydaniju/hangman)
4
+
5
+ Hangman is a paper and pencil guessing game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hangman_y'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hangman_y
22
+
23
+ ## Usage
24
+
25
+ After Installation, Run the "hangman_y" gem in your Command Line Interface. After the welcome screen has been displayed, proceed by entering the desired option "start", "load", or "quit".
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Hangman is currently in its first edition and is open-source. Feel free to contribute, clone or Fork
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andela-ydaniju/hangman. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hangman'
5
+ # You can add fixtures and/or initialization code here to make experimenting
6
+ # with your gem easier. You can also use a different console, if you like.
7
+
8
+ # (If you use this, don't forget to add pry to your Gemfile!)
9
+ # require 'pry'
10
+ # Pry.start
11
+
12
+ require 'irb'
13
+ IRB.start
data/bin/hangman ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'hangman'
3
+ Hangman::Game.new.pre_start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/hangman.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hangman/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hangman_y'
8
+ spec.version = Hangman::VERSION
9
+ spec.authors = ['andela-ydaniju']
10
+ spec.email = ['yusuf.daniju@andela.com']
11
+
12
+ spec.summary = %q{Hangman game allows you guess the letters of a word. You have seven chances!}
13
+ # spec.description = %q{Hangman game allows you guess the letters of a word. You have seven chances!}
14
+ spec.homepage = "https://github.com/andela-ydaniju/hangman"
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = 'bin'
29
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.10'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec'
35
+ end
data/lib/hangman.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'hangman/version'
2
+ # Your comment goes here...
3
+ module Hangman
4
+ require_relative './hangman/choice.rb'
5
+ require_relative './hangman/play.rb'
6
+ require_relative './hangman/game.rb'
7
+ require_relative './hangman/show.rb'
8
+ require_relative './hangman/save.rb'
9
+ end
@@ -0,0 +1,20 @@
1
+ # Hangman module
2
+ module Hangman
3
+ # This class serves words from file and tracks their display
4
+ class Choice
5
+ def initialize
6
+ read_file('5desk.txt')
7
+ end
8
+
9
+ def read_file(dictionary)
10
+ @@word_list ||= File.readlines(dictionary)
11
+ end
12
+
13
+ def pick_word
14
+ loop do
15
+ choice = @@word_list.sample.chomp.downcase
16
+ break(choice) if choice.length > 5 && choice.length < 12
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,110 @@
1
+ require 'pry'
2
+ require 'stringio'
3
+ # Hangman module
4
+ module Hangman
5
+ # This class starts and ends game
6
+ class Game
7
+ attr_accessor :right_guess, :wrong_count, :show, :total_lives, :play, :word,
8
+ :display, :save, :entry
9
+ def initialize
10
+ @right_guess = ''
11
+ @wrong_count = 0
12
+ @total_lives = 7
13
+ @play = Hangman::Play.new
14
+ @word = Hangman::Choice.new.pick_word
15
+ @display = @play.show_word(@word, @right_guess)
16
+ @show = Hangman::Show.new
17
+ @save = Hangman::Save.new(self)
18
+ end
19
+
20
+ def pre_start
21
+ puts @show.begin
22
+ @entry = gets.strip.downcase
23
+ case @entry
24
+ when 'start' then start!
25
+ when 'load' then play_loaded
26
+ else
27
+ sleep 3
28
+ exit
29
+ end
30
+ end
31
+
32
+ def start!
33
+ puts 'Which word is:' + @play.show_word(@word, '') + '?'
34
+ condition_for_play
35
+ end
36
+
37
+ def condition_for_play
38
+ while @wrong_count < @total_lives
39
+ print "You have #{@total_lives - @wrong_count} chances left. "
40
+ char = @play.enter_guess
41
+ if char.match(/[^a-zA-Z0-9_]/)
42
+ save_options
43
+ elsif @word.include? char
44
+ right_entry(char)
45
+ else
46
+ wrong_entry(char)
47
+ end
48
+ end
49
+ end
50
+
51
+ def save_options
52
+ puts @show.save_or_quit?
53
+ options = gets.strip.downcase
54
+ case options
55
+ when 's' then @save.save_data(self)
56
+ when 'c'
57
+ puts @play.show_word(@word, @right_guess)
58
+ save_data(condition_for_play)
59
+ else
60
+ abort(@show.end)
61
+ end
62
+ end
63
+
64
+ def play_loaded
65
+ play_data = @save.load_data
66
+ @word = play_data[0].to_s
67
+ @right_guess = play_data[1].to_s
68
+ @wrong_count = play_data[2].to_i
69
+ @total_lives = play_data[3].to_i
70
+ puts @play.show_word(@word, @right_guess)
71
+ condition_for_play
72
+ end
73
+
74
+ def full_game
75
+ puts @show.continue
76
+ play_on = gets.downcase.strip
77
+ if play_on == 'y'
78
+ @word = Hangman::Choice.new.pick_word
79
+ @wrong_count = 0
80
+ @total_lives = 7
81
+ @right_guess = ''
82
+ puts @play.show_word(@word, @right_guess)
83
+ condition_for_play
84
+ end
85
+ end
86
+
87
+ def right_entry(char)
88
+ if @right_guess.include? char
89
+ puts char + ' is already given and accepted.'
90
+ puts 'Try another: ' + @play.show_word(@word, @right_guess)
91
+ else
92
+ @right_guess += char
93
+ puts @show.show_right_entry + @play.show_word(@word, @right_guess)
94
+ end
95
+ abort(@show.show_win) unless
96
+ @play.show_word(@word, @right_guess).include? '*'
97
+ end
98
+
99
+ def wrong_entry(char)
100
+ puts "Sorry! The word doesn't contain '#{char}'"
101
+ @wrong_count += 1
102
+ if @wrong_count == @total_lives
103
+ puts "#{@word} + '\n' + #{@show.hang}"
104
+ full_game
105
+ else
106
+ puts @show.show_wrong_entry + @play.show_word(@word, @right_guess)
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,25 @@
1
+ require 'stringio'
2
+
3
+ # Hangman module
4
+ module Hangman
5
+ # This class follows gameplay logic
6
+ class Play
7
+ def show_word(picked_word, enter_guess)
8
+ display = ''
9
+ picked_word.chars do |char|
10
+ display += (enter_guess.include? char) ? char : '*'
11
+ end
12
+ display
13
+ end
14
+
15
+ def enter_guess
16
+ $stdout.puts 'Please enter a letter'
17
+ puts "\n"
18
+ string = '3'
19
+ while string.to_i != 0 || string.length != 1
20
+ string = $stdin.gets.chomp.downcase
21
+ end
22
+ string
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ require 'yaml'
2
+ module Hangman
3
+ # Save and load class
4
+ class Save
5
+ attr_reader :word, :right_guess, :total_lives, :wrong_count
6
+
7
+ def initialize(game)
8
+ @game = game
9
+ @play = Hangman::Play.new
10
+ end
11
+
12
+ def save_data(obj = nil)
13
+ play_data = [obj.word, obj.right_guess, obj.wrong_count, obj.total_lives]
14
+ serialize_data = YAML.dump(play_data)
15
+ data = File.open('gamelog.yml', 'w') { |line| line.write(serialize_data) }
16
+ if data
17
+ puts 'Game successfully saved'
18
+ sleep 3
19
+ exit
20
+ end
21
+ end
22
+
23
+ def load_data
24
+ read_file = File.read('gamelog.yml')
25
+ YAML.load(read_file)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,66 @@
1
+ module Hangman
2
+ # hangman game displays
3
+ class Show
4
+ def begin
5
+ <<-HEREDOC
6
+ WELCOME TO HANGMAN
7
+ Type 'start' to Start a new game
8
+ or 'load' to Load an existing game
9
+ HEREDOC
10
+ end
11
+
12
+ def show_right_entry
13
+ <<-HEREDOC
14
+ GREAT!!
15
+ HEREDOC
16
+ end
17
+
18
+ def show_wrong_entry
19
+ <<-HEREDOC
20
+ WRONG! TRY AGAIN!!
21
+ HEREDOC
22
+ end
23
+
24
+ def show_win
25
+ <<-HEREDOC
26
+ CONGRATULATIONS!!! YOU HAVE WON!
27
+ HEREDOC
28
+ end
29
+
30
+ def save_or_quit?
31
+ <<-HEREDOC
32
+ PRESS 's' TO SAVE
33
+ 'c' TO CONTINUE
34
+ 'q' TO QUIT
35
+ HEREDOC
36
+ end
37
+
38
+ def hang
39
+ <<-HEREDOC
40
+ +------+
41
+ | |
42
+ | O
43
+ | /|\
44
+ | / \
45
+ |
46
+ |
47
+ --+------------
48
+ GAME-OVER!!!! :(
49
+ HEREDOC
50
+ end
51
+
52
+ def continue
53
+ <<-HEREDOC
54
+ DO YOU LIKE TO CONTINUE
55
+ PRESS 'Y' TO CONTINUE
56
+ HEREDOC
57
+ end
58
+
59
+ def end
60
+ <<-HEREDOC
61
+ GOODBYE!!
62
+ HEREDOC
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ # Version of gem
2
+ module Hangman
3
+ VERSION = '0.1.8'
4
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hangman_y
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ platform: ruby
6
+ authors:
7
+ - andela-ydaniju
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - yusuf.daniju@andela.com
58
+ executables:
59
+ - console
60
+ - hangman
61
+ - setup
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .coveralls.yml
66
+ - .gitignore
67
+ - .rspec
68
+ - .travis.yml
69
+ - 5desk.txt
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/hangman
77
+ - bin/setup
78
+ - hangman.gemspec
79
+ - lib/hangman.rb
80
+ - lib/hangman/choice.rb
81
+ - lib/hangman/game.rb
82
+ - lib/hangman/play.rb
83
+ - lib/hangman/save.rb
84
+ - lib/hangman/show.rb
85
+ - lib/hangman/version.rb
86
+ homepage: https://github.com/andela-ydaniju/hangman
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.14.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Hangman game allows you guess the letters of a word. You have seven chances!
111
+ test_files: []