Class: Codebreaker::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/entities/player.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, difficulty, game) ⇒ Player

Returns a new instance of Player.



14
15
16
17
18
19
20
21
22
23
# File 'lib/entities/player.rb', line 14

def initialize(name, difficulty, game)
  @name = name
  @difficulty = difficulty
  @attempts_ttl = game.attempts_total
  @attempts_used = nil
  @hints_ttl = game.hints_available
  @hints_used = nil
  @rating = nil
  @date = nil
end

Instance Attribute Details

#attempts_ttlObject

Returns the value of attribute attempts_ttl.



3
4
5
# File 'lib/entities/player.rb', line 3

def attempts_ttl
  @attempts_ttl
end

#attempts_usedObject

Returns the value of attribute attempts_used.



3
4
5
# File 'lib/entities/player.rb', line 3

def attempts_used
  @attempts_used
end

#dateObject

Returns the value of attribute date.



3
4
5
# File 'lib/entities/player.rb', line 3

def date
  @date
end

#difficultyObject

Returns the value of attribute difficulty.



3
4
5
# File 'lib/entities/player.rb', line 3

def difficulty
  @difficulty
end

#hints_ttlObject

Returns the value of attribute hints_ttl.



3
4
5
# File 'lib/entities/player.rb', line 3

def hints_ttl
  @hints_ttl
end

#hints_usedObject

Returns the value of attribute hints_used.



3
4
5
# File 'lib/entities/player.rb', line 3

def hints_used
  @hints_used
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/entities/player.rb', line 3

def name
  @name
end

#ratingObject

Returns the value of attribute rating.



3
4
5
# File 'lib/entities/player.rb', line 3

def rating
  @rating
end

Class Method Details

.valid?(name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/entities/player.rb', line 34

def self.valid?(name)
  name.instance_of?(String) && name.length > 2 && name.length < 21
end

Instance Method Details

#<=>(other) ⇒ Object



25
26
27
# File 'lib/entities/player.rb', line 25

def <=>(other)
  [other.hints_used, other.attempts_used] <=> [@hints_used, @attempts_used]
end

#on_win(attempts:, hints:) ⇒ Object



29
30
31
32
# File 'lib/entities/player.rb', line 29

def on_win(attempts:, hints:)
  @attempts_used = @attempts_ttl - attempts
  @hints_used = hints
end