Skip to content

Instantly share code, notes, and snippets.

@cesardeazevedo
Created February 13, 2016 03:49
Show Gist options
  • Save cesardeazevedo/54b5d6f9ad1d76d81452 to your computer and use it in GitHub Desktop.
Save cesardeazevedo/54b5d6f9ad1d76d81452 to your computer and use it in GitHub Desktop.
Challenger Bot for PokerJS in livescript
global <<< require \prelude-ls
module.exports = ->
info =
name :\FXCesinha
email :\cesardeazevedo@outlook.com
btcWallet :\1R4VU1ag5xhsjXs6hbJ8fsCHi1CA3FBSs
ROYAL_FLUSH = 1000
STRAIGHT_FLUSH = 1000
QUADS = 500
FULL_HOUSE = 100
FLUSH = 100
STRAIGHT = 100
TRIPS = 500
TWO_PAIR = 70
PAIR = 20
FLUSH_DRAW = 20
STRAIGHT_DRAW = 20
ACE_HIGH = 10
class Player
(self, table) ->
@table = table
@player = self
@bets = @table.getBets!
@hand = new Hand @player.cards.concat @table.getCommunity!
getHandStrength: ->
@hand.getStrength @table.getState!
/*
* Bet logic
*/
getBet: ->
handStrength = @getHandStrength!
call = @bets.call
multiplier = parseFloat(handStrength / 100)
chipLeader = @table.getChipLeader!
secondChipLeader = @table.getChipLeader 2
bet = call
raiseAmount = 0
\State |> console.log
@table.getState! |> console.log
handStrength |> console.log
if handStrength is 100
raiseAmount = chipLeader.chips
else
if chipLeader.chips >= @player.chips
raiseAmount = @player.chips * multiplier
else
if handStrength >= FLUSH
raiseAmount = secondChipLeader.chips
else
raiseAmount = chipLeader.chips * multiplier
switch @table.getState!
case \pre-flop then
if !handStrength
bet = call if call <= @table.getBigBlind!
else
bet = call if call > raiseAmount
case \flop, \turn, \river then
if !handStrength
return 0
if @bets.canRaise and handStrength >= STRAIGHT
bet = raiseAmount < call ? call : raiseAmount
else
bet = call if call < raiseAmount
bet
class Table
(game) ->
@game = game
@players = game.players
sortPlayers: ->
f = (x, y) ->
| y.chips > x.chips => 1
| y.chips < x.chips => -1
| otherwise => 0
sort-with f, @players
getChipLeader: (rank) ->
rank = if rank then rank - 1 else 0
@players = @sortPlayers!
@players[rank]
getBigBlind: ->
bigBlind = 0
map ((player) !-> bigBlind = player.blind if player.blind > bigBlind ), @players
bigBlind
getBets: -> @game.betting
getState: -> @game.state
getCommunity: -> @game.community
class Hand extends Player
(cards) ->
@cards = map ((card) -> new Card card), cards
@cards = @sortCards!
@organized = @organizeHand!
sortCards: ->
f = (x, y) ->
| x.getValue! > y.getValue! => 1
| x.getValue! < y.getValue! => -1
| otherwise => 0
sort-with f, @cards
organizeHand: ->
organized =
suits:
spades: []
clubs: []
hearts: []
diamonds: []
values:
2: [], 3: [], 4: [], 5: [], 6: [],
7: [], 8: [], 9: [], 10: [], 11: [],
12: [], 13: [], 14: []
push-cards = (card) !->
organized.suits[card.getSuit! ++ 's'].push card
organized.values[card.getValue!].push card
map (push-cards), @cards
organized
isRoyalFlush: -> @getHighCard! is 14 and @isStraightFlush!
isStraightFlush: -> @isFlush! and @isStraight!
isQuads: -> @getSameValueCount! is 4
isFullHouse: ->
hasTrips = false
hasPair = false
check-pair-trips = (key) !->
hasPair := key.length is 2
hasTrips := key.length is 3
Obj.map (check-pair-trips), @organized.values
hasPair and hasTrips
isFlush: -> @getSameSuitCount! is 5
isStraight: ->
cards = map (.getValue!), @cards
check-straight = (val) ->
init = val
last = init + 4
result = intersection [ init to last ] cards
true if result.length is 5
result = or-list map (check-straight), cards
result
isTrips: -> @getSameValueCount! is 3
isTwoPair: ->
pair-count = 0
count-pairs = (key) ->
pair-count := pair-count + 1 if key.length is 2
Obj.map (count-pairs), @organized.values
pair-count is 2
isPair: -> @getSameValueCount! is 2
getHighCard: ->
vals = map (.getValue!), @cards
maximum vals
getSameValueCount: ->
sameValueCount = 0
get-value-count = (key) ->
sameValueCount := key.length if key.length > 1
Obj.map (get-value-count), @organized.values
sameValueCount
getSameSuitCount: ->
sameSuitCount = 0
get-suit-count = (key) ->
sameSuitCount := key.length if key.length > 1
Obj.map (get-suit-count), @organized.suits
sameSuitCount
isPotentialStraight: (cardsRequired) ->
matches = 0
get-cards = (card) ->
card.getValue!
vals = map (get-cards), @cards
get-matches = (val) ->
if ~vals.indexOf val + 1 then matches++
vals |> each (get-matches)
matches >= cardsRequired
isPotentialFlush: (cardsRequired) -> @getSameSuitCount! >= cardsRequired
getStrength: (state) ->
state = if state then state else \turn
return ROYAL_FLUSH if @isRoyalFlush!
return STRAIGHT_FLUSH if @isStraightFlush!
return QUADS if @isQuads!
return FULL_HOUSE if @isFullHouse!
return FLUSH if @isFlush!
return STRAIGHT if @isStraight!
return TRIPS if @isTrips!
return TWO_PAIR if @isTwoPair!
return PAIR if @isPair!
switch state
| \pre-flop =>
return FLUSH_DRAW if @isPotentialFlush 2 and FLUSH_DRAW
return STRAIGHT_DRAW if @isPotentialStraight 2 and STRAIGHT_DRAW
return ACE_HIGH if @getHighCard is 14 and ACE_HIGH
| \flop, \river =>
return FLUSH_DRAW if @isPotentialFlush 4 and FLUSH_DRAW
return STRAIGHT_DRAW if @isPotentialStraight 4 and STRAIGHT_DRAW
0
class Card extends Hand
(card) ->
@card = card
@suit = @get-suit!
@value = @get-value!
get-suit: ->
suit = switch @card.charAt 1
| \c => \club
| \s => \spade
| \d => \diamond
| \h => \heart
get-value: ->
val = switch @card.charAt 0
| \T => 10
| \J => 11
| \Q => 12
| \K => 13
| \A => 14
| otherwise => parseInt @card.charAt(0), 10
update = (game) ->
table = new Table game
player = new Player game.self, table
# game.self.cards = [ \2s, \3s, \4s, \5s, \6s ]
# game.self.cards = [ \2s, \2s ]
game.self.cards |> console.log
if game.state != \complete
player.getBet!
update: update, info: info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment