class MastermindSname::GameLogic

Public Class Methods

new(guess, game_colour) click to toggle source
# File lib/mastermind_sname/sname/game_logic.rb, line 3
def initialize(guess, game_colour)
  @guess = guess
  @game_colour = game_colour
end

Public Instance Methods

get_feedback() click to toggle source
# File lib/mastermind_sname/sname/game_logic.rb, line 40
def get_feedback
  "#{@guess} has #{get_no_correct_elements} of"\
  " the correct elements with #{get_no_correct_positions} in"\
  " the correct positions"
end
get_no_correct_elements() click to toggle source
# File lib/mastermind_sname/sname/game_logic.rb, line 22
def get_no_correct_elements
  no_of_elements = 0
  game_colour = @game_colour.sort
  guess = @guess.split("").sort
  game_colour.each_with_index do |val, index|
    no_of_elements += 1 if val == guess[index]
  end
  no_of_elements
end
get_no_correct_positions() click to toggle source
# File lib/mastermind_sname/sname/game_logic.rb, line 32
def get_no_correct_positions
  correct_positions = 0
  @game_colour.each_with_index do |val, index|
    correct_positions += 1 if val == @guess[index]
  end
  correct_positions
end
input_command?() click to toggle source
# File lib/mastermind_sname/sname/game_logic.rb, line 13
def input_command?
  commands = [:cheat, :exit, :h, :c, :q, :quit]
  true if commands.include?(@guess.to_sym)
end
length_feedback() click to toggle source
# File lib/mastermind_sname/sname/game_logic.rb, line 8
def length_feedback
  return "guess is too long" if @guess.length > @game_colour.length
  "guess is too short"
end
valid_length?() click to toggle source
# File lib/mastermind_sname/sname/game_logic.rb, line 18
def valid_length?
  @game_colour.length == @guess.length
end