Class: Ventiuna::Player

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

Direct Known Subclasses

Dealer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, strategy: "Learning", balance: 1000000.0) ⇒ Player

Returns a new instance of Player.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ventiuna/player.rb', line 6

def initialize(name, strategy: "Learning", balance: 1000000.0)
	@name = name
	@strategy = Ventiuna::Strategy.find_or_create_by_name(strategy)
	@balance = balance
	@hands = [Ventiuna::Hand.new]
	@bet = 0
	@wins = 0
	@loses = 0
	@ties = 0
	@decisions = Hash.new([])
end

Instance Attribute Details

#balanceObject

Returns the value of attribute balance.



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

def balance
  @balance
end

#betObject

Returns the value of attribute bet.



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

def bet
  @bet
end

#decisionsObject (readonly)

Returns the value of attribute decisions.



4
5
6
# File 'lib/ventiuna/player.rb', line 4

def decisions
  @decisions
end

#handsObject

Returns the value of attribute hands.



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

def hands
  @hands
end

#losesObject

Returns the value of attribute loses.



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

def loses
  @loses
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#tiesObject

Returns the value of attribute ties.



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

def ties
  @ties
end

#winsObject

Returns the value of attribute wins.



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

def wins
  @wins
end

Instance Method Details

#active_handObject Also known as: hand



46
47
48
# File 'lib/ventiuna/player.rb', line 46

def active_hand
	hands.select{ |h| h.active? }.first
end

#decision(dealers_card, strategy: "interactive") ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ventiuna/player.rb', line 56

def decision(dealers_card, strategy: "interactive")
	define_posible_moves(dealers_card)
	can_split = hands.size == 1
	#d = @strategy.decision(dealers_card, hand, can_split)
	d = nil
	
	unless d
		d = "s"
		case strategy
		when "random"
			d = random_strategy
		when "interactive"
			d = interactive
		end
	end
	move = Ventiuna::Move.find_by_strategy_id_and_dealers_card_value_and_hand_and_decision(@strategy.id, dealers_card.value, hand.to_db, d)
	#puts move.inspect
	@decisions[hand] = decisions[hand] << move
	#puts @decisions
	d
end

#loses_percentObject



27
28
29
30
# File 'lib/ventiuna/player.rb', line 27

def loses_percent
	return 0 if total_rounds == 0
	(self.loses.to_f/self.total_rounds) * 100
end

#place_bet(min_bet, max_bet) ⇒ Object



41
42
43
44
# File 'lib/ventiuna/player.rb', line 41

def place_bet(min_bet, max_bet)
	@bet = min_bet
	@balance -= @bet
end

#reset_decisionsObject



78
79
80
# File 'lib/ventiuna/player.rb', line 78

def reset_decisions
	@decisions = Hash.new([])
end

#reset_handsObject Also known as: reset_hand



51
52
53
# File 'lib/ventiuna/player.rb', line 51

def reset_hands
	@hands = [Ventiuna::Hand.new]
end

#statsObject



18
19
20
# File 'lib/ventiuna/player.rb', line 18

def stats
	"Wins: #{self.wins} (#{self.wins_percent.round}%),    Loses: #{self.loses} (#{self.loses_percent.round}%),    Ties: #{self.ties} (#{self.ties_percent.round}%)"
end

#ties_percentObject



32
33
34
35
# File 'lib/ventiuna/player.rb', line 32

def ties_percent
	return 0 if total_rounds == 0
	(self.ties.to_f/self.total_rounds) * 100
end

#total_roundsObject



37
38
39
# File 'lib/ventiuna/player.rb', line 37

def total_rounds
	@wins+@loses+@ties
end

#wins_percentObject



22
23
24
25
# File 'lib/ventiuna/player.rb', line 22

def wins_percent
	return 0 if total_rounds == 0
	(self.wins.to_f/self.total_rounds) * 100
end