Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Jays-Game/EmilysGame/controller.rb
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"
Copy link
Member

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:

foo = Print::new_match.downcase

case foo
when 'r'
when 'p'

etc

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!
43 changes: 43 additions & 0 deletions Jays-Game/EmilysGame/model.rb
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you've defined an attr_acessor for this variable, the @ in unnecessary.

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) ||

Choose a reason for hiding this comment

The 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 did_player_win? or winning_move?. Abstracting obtuse logic into nicely named method is a great way to help the next person to work on your code understand your intent!

(@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

Choose a reason for hiding this comment

The 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
Copy link

@austenmadden austenmadden Jan 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way this could be handled is initializing a collection

moves = [:rock, :paper, :scissors].freeze
moves[res]

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
70 changes: 70 additions & 0 deletions Jays-Game/EmilysGame/view.rb
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