Skip to content

Instantly share code, notes, and snippets.

@lord
Created February 20, 2014 19:10
Show Gist options
  • Save lord/9120993 to your computer and use it in GitHub Desktop.
Save lord/9120993 to your computer and use it in GitHub Desktop.
Ruby Mancala Fight Club Bot
require 'net/http'
SERVER = "http://hsfightclub.herokuapp.com"
puts "Enter your player ID:"
PLAYER_ID = gets.strip
# This function gets the status of the board from the server, and returns it as a string
def get_status
uri = URI("#{SERVER}/mancala/status?player_id=#{PLAYER_ID}")
return Net::HTTP.get(uri)
end
# This function makes a move starting at the specified house
def send_move(house)
uri = URI("#{SERVER}/mancala/move?player_id=#{PLAYER_ID}&house=#{house}")
return Net::HTTP.get(uri)
end
def move
status = get_status
# Return if we're waiting for the other player
# or if the game is over
if ['win', 'lose', 'tie'].include? status
puts "Game over!"
puts status.to_s
return false
elsif status == 'waiting'
puts "waiting..."
return true
end
houses = status.split(' ')
# Randomly select a house that is not empty
begin
choice = rand 6
end while (houses[choice] == 0)
puts "Making move: #{choice}"
send_move(choice)
return true
end
# This loop will keep running until move returns false
while move
sleep 0.1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment