Last active
June 30, 2018 23:01
-
-
Save CodePint/9ff424d579085a3a3a8534b74250cee8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Grabyo technical test | |
At Grabyo we love board games and will have an occasional poker night. | |
Since no one remembers the card ranking you will have to write a program that | |
compare poker hands and determines a winner. | |
## 1. Task | |
A poker hand has a constructor that accepts a string containing 5 cards: | |
``` | |
var hand = new PokerHand("KS 2H 5C JD TD"); | |
``` | |
and a method to compare itself to another hand | |
``` | |
PokerHand.prototype.compareWith = function(hand) { /* Your code here */ }; | |
``` | |
The characteristics of the string of cards are: | |
* A space is used as card seperator | |
* Each card consists of two characters | |
* The first character is the value of the card, valid characters are: `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `T`(en), `J`(ack), `Q`(ueen), `K`(ing), `A`(ce) | |
* The second character represents the suit, valid characters are: `S`(pades), `H`(earts), `D`(iamonds), `C`(lubs) | |
The result of your poker hand compare can be one of these 3 options: | |
* Win should return the integer `1` | |
* Loss should return the integer `2` | |
* Tie should return the integer `3` | |
The ranking of the hands should follow the [Texas Hold'em rules](http://freepokerhoney.com/website_images/8245/poker-strategy/poker-hand-rankings.png) | |
## 2. Requirements | |
All your code should be contained in the `src/` folder. | |
You are free to architect your code the way you want. You can use any libraries that you feel are relevant to solve this problem. | |
Unit tests are welcome | |
/!\\ You need to include a markdown file that shows the steps required to run your demo | |
Good luck ;-) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Poker tech test ### | |
## To do | |
## setup | |
# generate a deck | |
# deal hands | |
## main tech test | |
# work out how to assess hand values | |
# work out how to assess tie breaker values/splits | |
## additional features | |
# add betting and pots | |
# work out how to split pots | |
# add db persitance | |
# add unit tests | |
require 'pry' | |
FACE = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" ] | |
SUITS = ["S", "H", "D", "C"] | |
class Deck | |
attr_accessor :cards, :discards | |
def initialize | |
@cards = [] | |
@discards = [] | |
FACE.each do |face| | |
SUITS.each do |suit| | |
@cards << face+suit | |
end | |
end | |
end | |
def shuffle | |
@cards.shuffle! | |
end | |
def count | |
@cards.count | |
end | |
end | |
class Table | |
attr_accessor :bets, :pot, :cards | |
def initialize | |
@cards = "" | |
end | |
end | |
class Player | |
attr_accessor :name, :hand, :full_cards, :full_hands, :local_chips, :total_chips, :numerical_hand | |
def initialize(name, hand, full_cards, full_hands, local_chips, total_chips) | |
@name = name | |
@hand = hand | |
@full_cards = full_cards | |
@full_hands = full_hands | |
@local_chips = local_chips | |
@total_chips = total_chips | |
end | |
def cards | |
puts "my current cards are #{@hand}" | |
end | |
end | |
class Dealer | |
attr_accessor :deck, :players, :table, :buyin, :discards | |
def initialize(deck, table, players=[]) | |
@buyin = buyin | |
@deck = deck | |
@discards = [] | |
@players = players | |
@table = table | |
@pot = 0 | |
end | |
def set_buyins(buyin) | |
@buyin = buyin | |
@players.each do |player| | |
player.total_chips = player.total_chips - buyin | |
player.local_chips = buyin | |
end | |
end | |
def draw_hands | |
@players.each do |player| | |
if player.hand == "" | |
player.hand = @deck.cards.pop(2).join(" ") | |
else | |
puts "cards already held" | |
end | |
end | |
end | |
def discard_hands | |
@players.each do |player| | |
@deck.discards << player.hand.split(" ") | |
player.hand = "" | |
end | |
@deck.discards = @deck.discards.flatten | |
end | |
def restock | |
@deck.cards = @deck.discards.flatten + @deck.cards.flatten | |
@deck.shuffle | |
@deck.discards = [] | |
end | |
def draw_table(type) | |
case type | |
when "flop" | |
if @table.cards == "" | |
@table.cards = @deck.cards.pop(3).join(" ") | |
else | |
puts "table already has cards on" | |
end | |
when "turn", "river" | |
if (3..4) === @table.cards.split(" ").count | |
@table.cards = @table.cards + " " + @deck.cards.pop | |
else | |
puts "table has an invalid number of cards" | |
end | |
when "discard" | |
@deck.discards << @table.cards.split(" ") | |
@deck.discards = @deck.discards.flatten | |
@table.cards = "" | |
end | |
end | |
end | |
####################################### | |
class Rules | |
include Comparable | |
attr_accessor :players, :table, :duplicates | |
def initialize(players, table) | |
@players = players | |
@table = table | |
end | |
def <=> | |
end | |
def full_cards | |
@players.each do |player| | |
player.full_cards = (table.cards + " " + player.hand).split(" ") | |
end | |
end | |
def check_low_straight(player) | |
low_straight = ["A", "2", "3", "4", "5"] | |
player.numerical_hand = player.full_cards.map {|c| c[0]} | |
if (player.numerical_hand & low_straight) == low_straight | |
puts "true" | |
return true | |
else | |
puts "false" | |
return false | |
end | |
end | |
def numerical_cards | |
players.each do |player| | |
if check_low_straight(player) | |
player.numerical_cards = player.full_cards.map do |card| | |
end | |
end | |
end | |
def face_cards | |
def ranked_cards | |
end | |
def high_card | |
end | |
end | |
class Game | |
attr_accessor :players | |
end | |
##################################################### | |
deck = Deck.new | |
print deck.cards | |
puts deck.cards.count | |
deck.shuffle | |
print deck.cards | |
players = [ | |
Player.new("jack", "", "", [], 0, 5000), | |
Player.new("jill", "", "", [], 0, 5000), | |
Player.new("jimi", "", "", [], 0, 5000), | |
Player.new("jeff", "", "", [], 0, 5000), | |
] | |
table = Table.new | |
dealer = Dealer.new(deck, table, players) | |
dealer.set_buyins(1000) | |
dealer.draw_hands | |
dealer.draw_table("flop") | |
dealer.draw_table("turn") | |
dealer.draw_table("river") | |
rules = Rules.new(players, table) | |
rules.full_cards | |
rules.check_low_straight | |
puts "break" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment