-
Notifications
You must be signed in to change notification settings - Fork 30
completed challenge #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| require_relative 'view' | ||
| require_relative 'model' | ||
|
|
||
| class GameController | ||
| include GameView | ||
|
|
||
| def run! | ||
| scorekeeper = Scorekeeper.new | ||
| Print::title_screen | ||
|
|
||
| loop do | ||
| Print::score(scorekeeper.player_score, scorekeeper.computer_score) | ||
| case Print::new_match | ||
| when "r", "R" | ||
| scorekeeper.evaluate(:rock) | ||
| when "p", "P" | ||
| scorekeeper.evaluate(:paper) | ||
| when "s", "S" | ||
| scorekeeper.evaluate(:scissors) | ||
| else | ||
| Print::error_message | ||
| end | ||
| Print::matchup(scorekeeper.player_move.to_s.upcase, scorekeeper.computer_move.to_s.upcase) | ||
| if scorekeeper.win == true | ||
| Print::winning_message | ||
| elsif scorekeeper.win == false | ||
| Print::losing_message | ||
| else | ||
| Print::tie_message | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| GameController.new.run! | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #require 'pry' | ||
|
|
||
| class Scorekeeper | ||
| attr_accessor :computer_move, :player_move, :player_score, :computer_score, :win | ||
|
|
||
| def initialize | ||
| @player_score = 0 | ||
| @computer_score = 0 | ||
| @player_move = nil | ||
| @computer_move = nil | ||
| @win = nil | ||
| end | ||
|
|
||
| def evaluate(player_move) | ||
| @computer_move = generate_computer_move | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you've defined an Here's a pretty thorough explanation on stack overflow: http://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer It's a pretty simple concept once you get the hang of it! |
||
| @player_move = player_move | ||
| @win = nil | ||
| if @player_move!=@computer_move | ||
| if (@player_move == :rock && @computer_move == :scissors) || | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pretty heavy if statement. You could extract this away into a method call, something like |
||
| (@player_move == :paper && @computer_move == :rock) || | ||
| (@player_move == :scissors && @computer_move == :paper) | ||
|
|
||
| @player_score+=1 | ||
| @win = true | ||
| else | ||
| @computer_score+=1 | ||
| @win = false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def generate_computer_move | ||
| prng = Random.new | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is pretty short you could combine this line and the on below it since the instance is only used 1 time. res = Random.new.rand(3) |
||
| res = prng.rand(3) | ||
| if res == 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way this could be handled is initializing a collection This is cool since you can also make this a class constant, and reuse it/establish an interface for dealing with the "moves". |
||
| :rock | ||
| elsif res == 1 | ||
| :paper | ||
| else | ||
| :scissors | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| module GameView | ||
|
|
||
| module Print | ||
|
|
||
| class << self | ||
|
|
||
| #main menu | ||
| def title_screen | ||
| title = <<TITLE | ||
| ********** ||| ********** | ||
| * ROCK PAPER SCISSORS * | ||
| ********** ||| ********** | ||
| TITLE | ||
| puts title | ||
| end | ||
|
|
||
| #invalid input | ||
| def error_message | ||
| puts "That's not rock, paper, or scissors. Try again!" | ||
| end | ||
|
|
||
| #winner | ||
| def winning_message | ||
| # crown = <<CROWN | ||
| # , ,() , , | ||
| # |\/\/\/\/| | ||
| # |_o_<>_o_| | ||
| #CROWN | ||
| # puts crown | ||
| puts "YOU WIN" | ||
| end | ||
|
|
||
| #loser | ||
| def losing_message | ||
| # skull = <<SKULL | ||
| # _____ | ||
| # / \ | ||
| # | () () | | ||
| # \ ^ / | ||
| # ||||| | ||
| #SKULL | ||
| # puts skull | ||
| puts "YOU LOSE" | ||
| end | ||
|
|
||
| def tie_message | ||
| puts "YOU TIE" | ||
| end | ||
|
|
||
| def new_match | ||
| puts "Your Turn -- R for ROCK, P for PAPER, S for SCISSORS" | ||
| print "> " | ||
| gets.chomp | ||
| end | ||
|
|
||
| def matchup(player_move,computer_move) | ||
| puts "#{player_move} vs #{computer_move}" | ||
| end | ||
|
|
||
| def score(player_score,computer_score) | ||
| puts "YOU: #{player_score}" | ||
| puts "COMPUTER: #{computer_score}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could simplify by this by doing something like this:
etc