class Game

Attributes

answer[R]
command[R]
guess_count[R]
input[R]
judge[R]
messages[R]
output[R]
start_time[R]
table[R]

Public Class Methods

new(input, output, messages) click to toggle source
# File lib/game.rb, line 17
def initialize(input, output, messages)
  @answer      = Colors.new.secret_answer
  @judge       = Validate.new(@answer)
  @start_time  = Time.now
  @table       = Table.new
  @input       = input
  @output      = output
  @messages    = messages
  @command     = ''
  @guess_count = 0
  @old_guess   = []
end

Public Instance Methods

play() click to toggle source
# File lib/game.rb, line 30
def play
  output.puts messages.game_start
  output.puts table.show
  until win? || lose?
    output.print messages.guess_prompt
    @command = input.gets.chomp.downcase
    guess_counter
    turn_evaluation
  end
end

Private Instance Methods

end_time() click to toggle source
# File lib/game.rb, line 99
def end_time
  Time.now
end
guess_counter() click to toggle source
# File lib/game.rb, line 79
def guess_counter
  @guess_count += 1
end
invalid() click to toggle source
# File lib/game.rb, line 54
def invalid
  output.puts messages.guess_again
  @guess_count -= 1
end
lose?() click to toggle source
# File lib/game.rb, line 116
def lose?
  @guess_count >= 10
end
minutes() click to toggle source
# File lib/game.rb, line 91
def minutes
  total_time / 60
end
number_correct() click to toggle source
# File lib/game.rb, line 133
def number_correct
  judge.number_correct(parsed_guess)
end
parsed_guess() click to toggle source
# File lib/game.rb, line 129
def parsed_guess
  @command.chars
end
position_right() click to toggle source
# File lib/game.rb, line 137
def position_right
  judge.position_check(parsed_guess)
end
quit?() click to toggle source
# File lib/game.rb, line 120
def quit?
  command == 'q' || command == 'quit'
end
save_guess() click to toggle source
# File lib/game.rb, line 141
def save_guess
  @old_guess << @command
end
seconds() click to toggle source
# File lib/game.rb, line 87
def seconds
  total_time % 60
end
table_update() click to toggle source
# File lib/game.rb, line 83
def table_update
  table.update(guess_count, command, number_correct, position_right, answer) if valid_size? && valid_letters?
end
total_time() click to toggle source
# File lib/game.rb, line 95
def total_time
  (end_time - @start_time).to_i
end
turn_evaluation() click to toggle source
# File lib/game.rb, line 43
def turn_evaluation
  turn_visuals
  case
  when quit? then abort(messages.quit)
  when win? then you_win
  when !valid_size? || !valid_letters? then invalid
  when lose? then you_lose
  else turn_validate
  end
end
turn_validate() click to toggle source
# File lib/game.rb, line 63
def turn_validate
  validator
  output.puts messages.after_guess(command, number_correct, position_right)
  output.puts messages.guess_count(guess_count)
end
turn_visuals() click to toggle source
# File lib/game.rb, line 73
def turn_visuals
  output.puts messages.mastermind_logo
  table_update
  output.puts table.show
end
valid_letters?() click to toggle source
# File lib/game.rb, line 112
def valid_letters?
  judge.letters?(command)
end
valid_size?() click to toggle source
# File lib/game.rb, line 107
def valid_size?
  judge.size?(command)
  # command.length == 4
end
validator() click to toggle source
# File lib/game.rb, line 124
def validator
  number_correct
  position_right
end
win?() click to toggle source
# File lib/game.rb, line 103
def win?
  judge.correct?(parsed_guess)
end
you_lose() click to toggle source
# File lib/game.rb, line 59
def you_lose
  output.puts messages.lose
end
you_win() click to toggle source
# File lib/game.rb, line 69
def you_win
  output.puts messages.winner(answer, guess_count, minutes, seconds)
end