From da5fd153fd247b910e27a084f38541cf93423301 Mon Sep 17 00:00:00 2001 From: Nick Legg Date: Fri, 16 Oct 2015 15:00:03 -0400 Subject: [PATCH 1/5] Game is playable, but object exposure is suspect --- Nicks-Game/.view.rb.swp | Bin 0 -> 12288 bytes Nicks-Game/controller.rb | 41 ++++++++++++++++++++++++++ Nicks-Game/model.rb | 36 +++++++++++++++++++++++ Nicks-Game/view.rb | 62 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 Nicks-Game/.view.rb.swp create mode 100644 Nicks-Game/controller.rb create mode 100644 Nicks-Game/model.rb create mode 100644 Nicks-Game/view.rb diff --git a/Nicks-Game/.view.rb.swp b/Nicks-Game/.view.rb.swp new file mode 100644 index 0000000000000000000000000000000000000000..6135a948f66502d707b111b496d5d809354550a0 GIT binary patch literal 12288 zcmeI2&x;&I6vrzfD8^{egLu)mlf%wLrgt|>aM?_vun9?E*B!|?8W4us?wX!v`p2ZI z+Rc((bMOyPK~LgE&w3Js2qK6##e)Y071V#gfQSb#e!HuCb`mxSxh=ec&vaLB)qAf# zRee}ywif%BH|Tk9S)d&k;-^b<{m&nMTHO3nh%{DVSUmrqwzhG;zp;7cwe{Z8(ux;n zzKjo?@n#utp(`W3mc@Cpm79f){#qy#weV8pk4^hxar;K3wmrRdUd(}pITZhrk`lc(UVWSazBxH4jW(tY=8~00XDz}*Z><~1BcXrog5K2QTqc`{pYId zk-M%{moIF94X^<=zy{a=8(;%$fDNz#Hoykhz#%js10nVv5#rsWC?4Pc*Z=?Deq4w@ z!SCQV@IAN%-UDNB6?DNSSOE`$e;*U#FYqh)4txtf1@8b2u7NSQ3c6quya<-TDex3{ z2>f$Qh+n|Z;79Nc_yXJlAAt@y4*qylh_AtC;6v~>cnj=+>mUT@K?^(!o&kSjoj-uD zz$f5i@Bw%qlnxHz9&*WSfDNz#Hoykh02^Qfht+^WO9MgVKKHM)cSNDG#`8y6GQm*S zCOS$jHO^6E|50uYUCWiRQI=9PoZeR>0yU(evi@k08>I(PI>~Lb9)C8C;+CjJ*3}fL zx<#VZRh_2;Gl|kv>1q%W<{N7CMlu~68fKa%vD}H$aIUVY%owGS866aPHp`vsE%Ler z&JSpQnh%3wqh`AGs90I6G(AW2cZ`Mt)D|W(^+ISdD)6GzkMlsi&~#H;(_r<$?Mw$y zEw%BK^hXqAwCl$1PRF`+Uo1kBq$+vy#Em*eBClH=DMRgP{<6PE?y**hgq1XGQPa0N zZu?|2>{eu3tF!6~oy{uffqPRHlBhHn7dEfdis@w0sBUGiF1=07ms`j))9<#BrS7|_ zud3M366FF3Q6!+V)aevSsBFc?mU&%&W+%&%Y-D34dY60a7pkhtz%Kgkt_wU<*tFV9 zZo6C(I_aX{bFL`!l%=^`)7r{E42-YwdbuD(?mAQI{`s}@hSJPpGqG+Qmw?7<$D^L! zAsJ$;>H}RYTWOu?L4v)Pp{iCl6SR6SUr+0eq|=^_Fr02wN4nuP&X9?fn$V(m`V8&W z1y)nnGa2A)P;(M1X%uZsoQwK3FDkg}RrF(tbJ*#SQSlH3C0UTi%DuPPJl89JXy3>0 G?8SfG#&)m( literal 0 HcmV?d00001 diff --git a/Nicks-Game/controller.rb b/Nicks-Game/controller.rb new file mode 100644 index 0000000..bc547f5 --- /dev/null +++ b/Nicks-Game/controller.rb @@ -0,0 +1,41 @@ +require_relative 'view' +require_relative 'model' + +class GameController + include GameView + + def run! + hangman = Hangman.new + + Print::run_spinner + Print::title_screen + + #Make todoList methods like RESTful endpoints (new/edit/update/delete) + #Think Backbone Model & View + + loop do + Print::menu + case Print::fetch_user_input + when "P" + hangman.reset + Print::play hangman + loop do + hangman.guess(Print::fetch_user_input) + if hangman.done + Print::finish + break + else + Print::play hangman + end + end + when "Q" + puts "We're done" + exit + else + Print::error_message + end + end + end +end + +GameController.new.run! diff --git a/Nicks-Game/model.rb b/Nicks-Game/model.rb new file mode 100644 index 0000000..467e03f --- /dev/null +++ b/Nicks-Game/model.rb @@ -0,0 +1,36 @@ +class Hangman + attr_reader :word, :guessed, :tries, :maxtries + + def initialize + @word = "special".upcase + @guessed = [] + @tries = 0 + @maxtries = 10 + end + + # apply a user's guessed character + def guess char + @guessed.push(char.upcase) + @tries += 1 + end + + # is the game over? + def done + if @tries >= @maxtries + true + elsif guessmatch + true + else + false + end + end + + # do the user's combined guesses reveal the secret word? + def guessmatch + @word.chars.all? { |letter| @guessed.include?(letter) } + end + + def reset + initialize + end +end diff --git a/Nicks-Game/view.rb b/Nicks-Game/view.rb new file mode 100644 index 0000000..50fd5ce --- /dev/null +++ b/Nicks-Game/view.rb @@ -0,0 +1,62 @@ +module GameView + + module Print + + class << self + def run_spinner + print "Loading (please wait) " + 5.times { print "."; sleep 0.5; } + print "\n" + end + + def error_message + puts "That's not a command key. Try again!" + end + + def title_screen +title = < " + gets.chomp + end + end + end +end From c7f421f9138b5ef1576ea7f48c4c88a2074857b9 Mon Sep 17 00:00:00 2001 From: Nick Legg <nlegg@covermymeds.com> Date: Fri, 16 Oct 2015 15:19:55 -0400 Subject: [PATCH 2/5] Clean up object exposure --- Nicks-Game/.view.rb.swp | Bin 12288 -> 12288 bytes Nicks-Game/controller.rb | 7 ++----- Nicks-Game/model.rb | 14 ++++++++++++++ Nicks-Game/view.rb | 12 +++--------- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Nicks-Game/.view.rb.swp b/Nicks-Game/.view.rb.swp index 6135a948f66502d707b111b496d5d809354550a0..616022f2ac830ed1cd3e642e0824e45fcba22a2b 100644 GIT binary patch delta 297 zcmZojXh;xCG6?hZRWR2xW&i>K28Ox5iec-xH;Vn@XSCQXDDat|*PNSy!I%dkYcW|+ zVSd^IUIqqFAXWupX&{yYVoo4_22^z#hz|ns0U(|Z#HB!-1jPP8><YxTKx_`gAQRPq zSR9D|18sN@#OHx{0T34hF%S1<MwV}!lP7AY1F<d;>jSZZKx$qJgMtEt1kpf2IWagF zMHVRaOS+)6q*x(6F*miiB(Wql+NvNYu`;-%C^IiTcCxjuq9BqKP&BnTc5;obDJNWX J^A24PCIHFcNCN-> delta 410 zcmZojXh;xCG6?hZRWR2xW&i>K28J6RiecM1Hj4e?XSCZaDDat|x0Z{6VIns~)^4(( z!u+^JybKIBKr8^nk9Zgu4g>KPAf5}v{XpCd#Fao?0mSJ*>;lA^Kr9c$azOkQXxe2U zJ^;jPfp{?x&j#YDKs=FqGb2j_7jtTA>f{C5`jf4-Ehf*>nOvWmm%^X`2dF#+g@U5Y zyb=ZVcm;K2MLET(NL-kzoYaz%)FK6K1$7kFnQ00ciFxU{iFtbIrK!cmsVRDydC57Y zDXI1vIjM#qJ(_USU@FV=i&FHGGZKr6^->d)GZa$t6>1>n*G%@%RfL<En^;j&l$lzr cpbHlQ^Cws7nsP!7(`1;uQCoQPSzTi$0Ots2X8-^I diff --git a/Nicks-Game/controller.rb b/Nicks-Game/controller.rb index bc547f5..a979f2e 100644 --- a/Nicks-Game/controller.rb +++ b/Nicks-Game/controller.rb @@ -10,22 +10,19 @@ def run! Print::run_spinner Print::title_screen - #Make todoList methods like RESTful endpoints (new/edit/update/delete) - #Think Backbone Model & View - loop do Print::menu case Print::fetch_user_input when "P" hangman.reset - Print::play hangman + Print::play(hangman.play) loop do hangman.guess(Print::fetch_user_input) if hangman.done Print::finish break else - Print::play hangman + Print::play(hangman.play) end end when "Q" diff --git a/Nicks-Game/model.rb b/Nicks-Game/model.rb index 467e03f..fda4508 100644 --- a/Nicks-Game/model.rb +++ b/Nicks-Game/model.rb @@ -30,7 +30,21 @@ def guessmatch @word.chars.all? { |letter| @guessed.include?(letter) } end + # start a new game def reset initialize end + + # sends hash of gamestate (string, tries) through controller to view + def play + playString = '' + @word.chars.each do |letter| + if guessed.include?(letter) + playString += letter + ' ' + else + playString += '_ ' + end + end + { playString: playString, tries: @maxtries - @tries } + end end diff --git a/Nicks-Game/view.rb b/Nicks-Game/view.rb index 50fd5ce..6ee742d 100644 --- a/Nicks-Game/view.rb +++ b/Nicks-Game/view.rb @@ -36,15 +36,9 @@ def menu puts menu end - def play(hangman) - puts "Guess a letter (#{hangman.maxtries - hangman.tries} tries remaining) (ctrl-c to quit):" - hangman.word.chars.each do |letter| - if hangman.guessed.include?(letter) - print letter + ' ' - else - print '_ ' - end - end + def play(gamestate) + puts "Guess a letter (#{gamestate[:tries]} tries remaining) (ctrl-c to quit):" + puts gamestate[:playString] end def finish From c9fd21cd91464cb79ff98c5a84b81361dd89a476 Mon Sep 17 00:00:00 2001 From: Nick Legg <nlegg@covermymeds.com> Date: Fri, 16 Oct 2015 15:30:23 -0400 Subject: [PATCH 3/5] Add a small word dictionary to make the game less dumb --- Nicks-Game/.view.rb.swp | Bin 12288 -> 0 bytes Nicks-Game/controller.rb | 2 +- Nicks-Game/model.rb | 2 +- Nicks-Game/view.rb | 3 ++- Nicks-Game/words.txt | 8 ++++++++ 5 files changed, 12 insertions(+), 3 deletions(-) delete mode 100644 Nicks-Game/.view.rb.swp create mode 100644 Nicks-Game/words.txt diff --git a/Nicks-Game/.view.rb.swp b/Nicks-Game/.view.rb.swp deleted file mode 100644 index 616022f2ac830ed1cd3e642e0824e45fcba22a2b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&x;gC6vs;mgrFvp93=7bb|lG+u(P`^vdg*~0-MDw>|`eFI4X$@wLLYx&Gb~a zUDc6AjsHRpft&*V1^xjBL=QP7!D9|l(2G|CIpp9$ztulx$6<pgxhSvTGu_qQuimRq z)eJ1#TeF?@1})U*1=?XDer`;4ZXEbfT)QQN4wdh_>n~`ljW0Tl=H|JL_T1cJJ&ZgV z?iulYHsDN42FYp^ro*k&%rrWyz8tEVuL5sircb-m?*etHo^0(o1YcwWY~WQ4q_#i3 zcu35jn45Ls$0}3w!TV=kWh9Pa18jf|umLu}2G{@_U;}L6H8o)4ec~Ew-(S?fSRD6F z9E&a=*Z><~18jf|umLu}2G{@_U;}J`4X}aN(17d-v2Z|$`GY7P|Nk%l{{Q<uAs&N= z-~qS`Zh{}d0GtOc&;*O%E%4-BA^rjP!5`pva0C1R5^xa=z<JODP4GFG2PeQ0@HY7O zkP!F4U*J#hEBFQ61lPej_!t}oAAxtkBh>#JxC3s3pFj$>KohKi6|ex@Tt5Lvz*Efq zF}Mq^0|O2MZft-JumLu}2G{@_U;}Ty0i}C_DF56&j-L_v%9^^j9SvhVnzuL!w57^t zRN1|hT0<98Wo!^>3i_jS_ry!3d8vnE@uJ5k?@#uf0M{n2Ki7S(GtyPY<njN5CJfY5 zed;UgZFf_nl5U{m)K*KaD>?{kLge}Kn)36OTaTrsB-LFL2U@G7c!&t`hAQoCsRxGo zQ9^MjcLMECl}ndp40K?&_Zm41cdsiOE3Nuw$whBkr1LLKq&SVWHzX-k)~bZ6$1abB zAwro8S1CJEqJ|O~wO^yEXOnQ+BO77E1h%#`UY1K%9gVW*%(ME2N~dD2xmiwtj=Pqt z!DrShtDUCm*EM7s>$hsiQ}*-FvnuqWp(;X!OF%2MyzG)t)||F1Z{8nUjjSXa*ieb~ zdV6E7s45#cAN|UeeBiOdM%8{PwLg?PPeRA@(f>16WO=gE@?4|ZvKw9FB}(ah5Tg7z zR_gBVu%8;zk;VF9-pJphf!e84JJ})WW3kHZ;-;;VBucv25=Q!}nBCZ*#gD_cx?D;+ zS+@Zmr^{5_t5;5u36+Xzwtn&yT`dbNZf``ghuuNdI8@Rox+JkP%HJAS!Nf-QLW!-n dyi7)geH1i|dTFRA`&I~1^xTW~9lU2Ro&j^bk{JL1 diff --git a/Nicks-Game/controller.rb b/Nicks-Game/controller.rb index a979f2e..2295118 100644 --- a/Nicks-Game/controller.rb +++ b/Nicks-Game/controller.rb @@ -19,7 +19,7 @@ def run! loop do hangman.guess(Print::fetch_user_input) if hangman.done - Print::finish + Print::finish(hangman.word) break else Print::play(hangman.play) diff --git a/Nicks-Game/model.rb b/Nicks-Game/model.rb index fda4508..eb3fa07 100644 --- a/Nicks-Game/model.rb +++ b/Nicks-Game/model.rb @@ -2,7 +2,7 @@ class Hangman attr_reader :word, :guessed, :tries, :maxtries def initialize - @word = "special".upcase + @word = File.readlines('words.txt').sample.strip.upcase @guessed = [] @tries = 0 @maxtries = 10 diff --git a/Nicks-Game/view.rb b/Nicks-Game/view.rb index 6ee742d..f082b4a 100644 --- a/Nicks-Game/view.rb +++ b/Nicks-Game/view.rb @@ -41,7 +41,8 @@ def play(gamestate) puts gamestate[:playString] end - def finish + def finish(word) + puts "The word was #{word}!" puts "Thanks for playing!" Print::run_spinner end diff --git a/Nicks-Game/words.txt b/Nicks-Game/words.txt new file mode 100644 index 0000000..3a9a451 --- /dev/null +++ b/Nicks-Game/words.txt @@ -0,0 +1,8 @@ +cats +dogs +moose +geese +horses +elephants +tarsiers +armadillos From 962d646fe4d2707e024bbb96f68691f099bd5f0d Mon Sep 17 00:00:00 2001 From: Nick Legg <nlegg@covermymeds.com> Date: Thu, 22 Oct 2015 08:56:22 -0400 Subject: [PATCH 4/5] Refactor for succinctness --- Nicks-Game/model.rb | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/Nicks-Game/model.rb b/Nicks-Game/model.rb index eb3fa07..29846a2 100644 --- a/Nicks-Game/model.rb +++ b/Nicks-Game/model.rb @@ -1,33 +1,27 @@ class Hangman - attr_reader :word, :guessed, :tries, :maxtries + attr_reader :word, :guessed + MAX_TRIES = 10 def initialize @word = File.readlines('words.txt').sample.strip.upcase @guessed = [] @tries = 0 - @maxtries = 10 end # apply a user's guessed character - def guess char + def guess(char) @guessed.push(char.upcase) @tries += 1 end # is the game over? def done - if @tries >= @maxtries - true - elsif guessmatch - true - else - false - end + @tries >= MAX_TRIES || guessmatch end # do the user's combined guesses reveal the secret word? def guessmatch - @word.chars.all? { |letter| @guessed.include?(letter) } + word.chars.all? { |letter| @guessed.include?(letter) } end # start a new game @@ -38,13 +32,13 @@ def reset # sends hash of gamestate (string, tries) through controller to view def play playString = '' - @word.chars.each do |letter| + word.chars.each do |letter| if guessed.include?(letter) playString += letter + ' ' else playString += '_ ' end end - { playString: playString, tries: @maxtries - @tries } + { playString: playString, tries: MAX_TRIES - @tries } end end From 44766d4f350e6a800e8a6f876222e5979fb590aa Mon Sep 17 00:00:00 2001 From: Nick Legg <nlegg@covermymeds.com> Date: Mon, 26 Oct 2015 12:44:26 -0400 Subject: [PATCH 5/5] Simplify guessmatch method --- Nicks-Game/model.rb | 6 ++++-- Nicks-Game/view.rb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Nicks-Game/model.rb b/Nicks-Game/model.rb index 29846a2..585af2e 100644 --- a/Nicks-Game/model.rb +++ b/Nicks-Game/model.rb @@ -4,14 +4,16 @@ class Hangman def initialize @word = File.readlines('words.txt').sample.strip.upcase + @letters_left = @word.dup @guessed = [] @tries = 0 end # apply a user's guessed character def guess(char) - @guessed.push(char.upcase) + @guessed.push(char) @tries += 1 + @letters_left.delete! char end # is the game over? @@ -21,7 +23,7 @@ def done # do the user's combined guesses reveal the secret word? def guessmatch - word.chars.all? { |letter| @guessed.include?(letter) } + @letters_left.empty? end # start a new game diff --git a/Nicks-Game/view.rb b/Nicks-Game/view.rb index f082b4a..6024ae4 100644 --- a/Nicks-Game/view.rb +++ b/Nicks-Game/view.rb @@ -50,7 +50,7 @@ def finish(word) def fetch_user_input(question=nil) puts question if question print "> " - gets.chomp + gets.chomp.upcase end end end