Last active
August 29, 2015 14:01
-
-
Save olim7t/ac7c50eeb4d7d0c6c26c to your computer and use it in GitHub Desktop.
My solution for Sophie's Extreme Memory challenge (https://github.com/sophietk/xke-memory-server)
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
# This is the raw code as it was at the end of the session. | |
# | |
# In retrospect, the following could be improved: | |
# * @to_see is useless, two simple nested loops would work (or maybe keep it but shuffle it) | |
# * handle throttling better (instead of a lame DELAY) | |
# * I forgot to handle the case when I pick a pair by chance! | |
# * monitor game progress to avoid losing too many points | |
require "rest_client" | |
require "json" | |
ROOT = "http://10.1.1.46:3000" | |
BOARD_SIZE = 8 | |
DELAY = 1.0 | |
class Game | |
def initialize | |
@to_see = [] | |
(0..BOARD_SIZE-1).each do |x| | |
(0..BOARD_SIZE-1).each do |y| | |
@to_see.push [x, y] | |
end | |
end | |
# sorted by symbol & color | |
@seen = {} | |
# knowns pairs to try on next turns | |
@next_pairs = [] | |
end | |
def play | |
puts "Start playing" | |
while (true) do | |
if @next_pairs.size > 0 | |
pick(@next_pairs.pop) | |
else | |
card1 = @to_see.pop | |
card2 = @to_see.pop | |
status = pick([card1, card2]) | |
details1 = status["turn"]["cards"][0] | |
details2 = status["turn"]["cards"][1] | |
process(card1, details1) | |
process(card2, details2) | |
end | |
sleep(DELAY) if DELAY | |
end | |
end | |
def pick(cards) | |
puts "pick #{cards}" | |
response = RestClient.post "#{ROOT}/play", | |
"[[#{cards[0][0]}, #{cards[0][1]}], [#{cards[1][0]}, #{cards[1][1]}]]", | |
:content_type => :json | |
puts "response: #{response}" | |
JSON.parse(response) | |
end | |
def process(coords, details) | |
puts "Process result for #{coords} #{details}" | |
@to_see.delete(coords) | |
if not details["found"] | |
key = [details["symbol"], details["color"]] | |
if @seen[key] | |
puts "Already seen #{key} at #{@seen[key]} => PROFIT!" | |
@next_pairs.push( [coords, @seen[key]] ) | |
@seen.delete key | |
else | |
puts "Never seen #{key} before, remember position" | |
@seen[key] = coords | |
end | |
end | |
end | |
end | |
register_status = RestClient.post "#{ROOT}/scores/register", "omichallat@xebia.fr", :content_type => :json | |
puts "Registered: #{register_status}" | |
Game.new().play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment