Created
February 20, 2014 19:10
-
-
Save lord/9120993 to your computer and use it in GitHub Desktop.
Ruby Mancala Fight Club Bot
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
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