Ruby-related coding: Need help in resolving the red-selected code parts of my practice problem, primarily dealing with case-sentitive, loops, and if-else statements. The ", :pending => true" needs to be removed, used for checking purposes. (Picture reference provided)
Ruby-related coding: Need help in resolving the red-selected code parts of my practice problem, primarily dealing with case-sentitive, loops, and if-else statements. The ", :pending => true" needs to be removed, used for checking purposes. (Picture reference provided)
Hangperson_game.rb
class HangpersonGame
# add the necessary class methods, attributes, etc. here
# to make the tests in spec/hangperson_game_spec.rb pass.
# Get a word from remote "random word" service
# def initialize()
# end
def initialize(word)
@word = word
@guesses = ""
@wrong_guesses = ""
end
def word
return @word
end
def guesses
return @guesses
end
def wrong_guesses
return @wrong_guesses
end
def guess(letter)
if @word.include?(letter)
@guesses += letter
return true
else
@wrong_guesses += letter
end
end
def word_with_guesses
partial_matches = ""
@word.each_char do |w|
partial_matches += "-"
end
return partial_matches
end
def check_win_or_lose
if word_with_guesses.downcase == @word.downcase
return :win
elsif @wrong_guesses.length >= 7
return :lose
else
return :play
end
end
# You can test it by running $ bundle exec irb -I. -r app.rb
# And then in the irb: irb(main):001:0> HangpersonGame.get_random_word
# => "cooking" <-- some random word
def self.get_random_word
require 'uri'
require 'net/http'
uri = URI('http://randomword.saasbook.info/RandomWord')
Net::HTTP.new('randomword.saasbook.info').start { |http|
return http.post(uri, "").body
}
end
end
Hangperson_game_spec.rb
require 'spec_helper'
require 'hangperson_game'
describe HangpersonGame do
# helper function: make several guesses
def guess_several_letters(game, letters)
letters.chars do |letter|
game.guess(letter)
end
end
describe 'new' do
it "takes a parameter and returns a HangpersonGame object" do
@hangpersonGame = HangpersonGame.new('glorp')
expect(@hangpersonGame).to be_an_instance_of(HangpersonGame)
expect(@hangpersonGame.word).to eq('glorp')
expect(@hangpersonGame.guesses).to eq('')
expect(@hangpersonGame.wrong_guesses).to eq('')
end
end
describe 'guessing' do
context 'correctly' do
before :each do
@game = HangpersonGame.new('garply')
@valid = @game.guess('a')
end
it 'changes correct guess list' do
expect(@game.guesses).to eq('a')
expect(@game.wrong_guesses).to eq('')
end
it 'returns true' do
expect(@valid).not_to be false
end
end
context 'incorrectly' do
before :each do
@game = HangpersonGame.new('garply')
@valid = @game.guess('z')
end
it 'changes wrong guess list' do
expect(@game.guesses).to eq('')
expect(@game.wrong_guesses).to eq('z')
end
it 'returns true' do
expect(@valid).not_to be false
end
end
context 'same letter repeatedly' do
before :each do
@game = HangpersonGame.new('garply')
guess_several_letters(@game, 'aq')
end
it 'does not change correct guess list', :pending => true do
@game.guess('a')
expect(@game.guesses).to eq('a')
end
it 'does not change wrong guess list', :pending => true do
@game.guess('q')
expect(@game.wrong_guesses).to eq('q')
end
it 'returns false', :pending => true do
expect(@game.guess('a')).to be false
expect(@game.guess('q')).to be false
end
it 'is case insensitive', :pending => true do
expect(@game.guess('A')).to be false
expect(@game.guess('Q')).to be false
expect(@game.guesses).not_to include('A')
expect(@game.wrong_guesses).not_to include('Q')
end
end
context 'invalid' do
before :each do
@game = HangpersonGame.new('foobar')
end
it 'throws an error when empty', :pending => true do
expect { @game.guess('') }.to raise_error(ArgumentError)
end
it 'throws an error when not a letter', :pending => true do
expect { @game.guess('%') }.to raise_error(ArgumentError)
end
it 'throws an error when nil', :pending => true do
expect { @game.guess(nil) }.to raise_error(ArgumentError)
end
end
end
describe 'displayed word with guesses' do
before :each do
@game = HangpersonGame.new('banana')
end
# for a given set of guesses, what should the word look like?
@test_cases = {
'bn' => 'b-n-n-',
'def' => '------',
'ban' => 'banana'
}
@test_cases.each_pair do |guesses, displayed|
it "should be '#{displayed}' when guesses are '#{guesses}'" do
guess_several_letters(@game, guesses)
expect(@game.word_with_guesses).to eq(displayed)
end
end
end
describe 'game status' do
before :each do
@game = HangpersonGame.new('dog')
end
it 'should be win when all letters guessed', :pending => true do
guess_several_letters(@game, 'ogd')
expect(@game.check_win_or_lose).to eq(:win)
end
it 'should be lose after 7 incorrect guesses', :pending => true do
guess_several_letters(@game, 'tuvwxyz')
expect(@game.check_win_or_lose).to eq(:lose)
end
it 'should continue play if neither win nor lose', :pending => true do
guess_several_letters(@game, 'do')
expect(@game.check_win_or_lose).to eq(:play)
end
end
end
Trending now
This is a popular solution!
Step by step
Solved in 3 steps