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)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

it 'returns false', pending -> true do
expect (@game.guess ('a')).to be false
expect (@game.guess ('q')).to be false
end
require 'spec_helper'
require 'hangperson_game'
1
62 -
2
63
3
64
4 . describe HangpersonGame do
65
# helper function: make several guesses
def guess_several_letters(game, letters)
letters.chars do |letter|
game.guess (letter)
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')
5
66 -
67
68
8
69
9
70
10
end
71
end
11
72
end
12 -
describe 'new' do
73 ,
context 'invalid' 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('')
13 -
74 -
before :each do
@game = HangpersonGame.new ('foobar')
end
14
75
15
76
it 'throws an error when empty'ponding = truo do
expect { @game.guess ('') }.to raise_error (ArgumentError)
16
77 -
17
78
end
it 'throws an error when not a letter',ponding - truo do
expect { @game.guess ('%') }.to raise_error (ArgumentError)
18
79
19
end
80 -
20
end
81
21
82
end
describe 'guessing' do
context 'correctly' do
it 'throws an error when nil',"ponding -> true do
expect { @game.guess (nil) }.to raise_error (ArgumentError)
22 -
83 -
23 ,
84
24 -
before :each do
85
end
@game = HangpersonGame.new ('garply')
@valid = @game.guess ('a')
25
86
end
26
87
end
27
end
88
describe 'displayed word with guesses' do
it 'changes correct guess list' do
expect (@game.guesses).to eq('a')
expect (@game.wrong_guesses).to eq('')
28 ,
89 ,
29
90 -
before :each do
30
91
@game = HangpersonGame.new ('banana')
31
end
92
end
it 'returns true' do
expect (@valid).not_to be false
# for a given set of guesses, what should the word look like?
@test_cases = {
'bn' =>
32
93
33
94
34
end
95
'b-n-n-'
35
end
96
'def' =>
'ban' => 'banana'
context 'incorrectly' do
before :each do
36 -
97
37 -
98
}
@game = HangpersonGame.new ('garply')
@valid = @game.guess ('z')
99 -
@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
38
39
100 -
40
end
101
41 -
it 'changes wrong guess list' do
expect (@game.guesses).to eq ('')
expect (@game.wrong_guesses).to eq('z')
102
42
103
43
104
end
44
end
105
end
it 'returns true' do
expect (@valid).not_to be false
end
45 -
106
46
107 .
describe 'game status' do
47
108 -
before :each do
@game = HangpersonGame.new ('dog')
end
context 'same letter repeatedly' do
48
109
49 ,
110
end
it 'should be win when all letters guessed',ponding- truo do
guess_several_letters(@game, 'ogd')
expect (@game.check_win_or_lose).to eq(:win)
50 -
before :each do
111 -
@game = HangpersonGame.new ('garply')
guess_severalletters (@game, 'aq')
51
112
52
113
53
end
114
end
it 'does not change correct guess list'+pending true do
@game.guess ('a')
expect (@game. guesses).to eq('a')
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)
54 -
115 -
55
116
56
117
57
end
118
end
it 'does not change wrong guess list',7ponding- truo do
@game.guess ('q')
expect (@game.wrong_guesses).to eq ('q')
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)
58
119 ,
59
120
60
121
61
end
122
end
it 'returns false',pending => true do
expect (@game.guess ('a')).to be false
62 -
123
end
63
124
end
Transcribed Image Text:it 'returns false', pending -> true do expect (@game.guess ('a')).to be false expect (@game.guess ('q')).to be false end require 'spec_helper' require 'hangperson_game' 1 62 - 2 63 3 64 4 . describe HangpersonGame do 65 # helper function: make several guesses def guess_several_letters(game, letters) letters.chars do |letter| game.guess (letter) 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') 5 66 - 67 68 8 69 9 70 10 end 71 end 11 72 end 12 - describe 'new' do 73 , context 'invalid' 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('') 13 - 74 - before :each do @game = HangpersonGame.new ('foobar') end 14 75 15 76 it 'throws an error when empty'ponding = truo do expect { @game.guess ('') }.to raise_error (ArgumentError) 16 77 - 17 78 end it 'throws an error when not a letter',ponding - truo do expect { @game.guess ('%') }.to raise_error (ArgumentError) 18 79 19 end 80 - 20 end 81 21 82 end describe 'guessing' do context 'correctly' do it 'throws an error when nil',"ponding -> true do expect { @game.guess (nil) }.to raise_error (ArgumentError) 22 - 83 - 23 , 84 24 - before :each do 85 end @game = HangpersonGame.new ('garply') @valid = @game.guess ('a') 25 86 end 26 87 end 27 end 88 describe 'displayed word with guesses' do it 'changes correct guess list' do expect (@game.guesses).to eq('a') expect (@game.wrong_guesses).to eq('') 28 , 89 , 29 90 - before :each do 30 91 @game = HangpersonGame.new ('banana') 31 end 92 end it 'returns true' do expect (@valid).not_to be false # for a given set of guesses, what should the word look like? @test_cases = { 'bn' => 32 93 33 94 34 end 95 'b-n-n-' 35 end 96 'def' => 'ban' => 'banana' context 'incorrectly' do before :each do 36 - 97 37 - 98 } @game = HangpersonGame.new ('garply') @valid = @game.guess ('z') 99 - @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 38 39 100 - 40 end 101 41 - it 'changes wrong guess list' do expect (@game.guesses).to eq ('') expect (@game.wrong_guesses).to eq('z') 102 42 103 43 104 end 44 end 105 end it 'returns true' do expect (@valid).not_to be false end 45 - 106 46 107 . describe 'game status' do 47 108 - before :each do @game = HangpersonGame.new ('dog') end context 'same letter repeatedly' do 48 109 49 , 110 end it 'should be win when all letters guessed',ponding- truo do guess_several_letters(@game, 'ogd') expect (@game.check_win_or_lose).to eq(:win) 50 - before :each do 111 - @game = HangpersonGame.new ('garply') guess_severalletters (@game, 'aq') 51 112 52 113 53 end 114 end it 'does not change correct guess list'+pending true do @game.guess ('a') expect (@game. guesses).to eq('a') 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) 54 - 115 - 55 116 56 117 57 end 118 end it 'does not change wrong guess list',7ponding- truo do @game.guess ('q') expect (@game.wrong_guesses).to eq ('q') 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) 58 119 , 59 120 60 121 61 end 122 end it 'returns false',pending => true do expect (@game.guess ('a')).to be false 62 - 123 end 63 124 end
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY