-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve strategy choice based on path (#27)
- Loading branch information
Showing
4 changed files
with
95 additions
and
46 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
require "./spec_helper" | ||
|
||
describe "Crystal Snake Server" do | ||
it "returns metadata on root path" do | ||
get "/" | ||
response.status_code.should eq 200 | ||
request_headers = HTTP::Headers{"Content-Type" => "application/json"} | ||
SUPPORTED_STRATEGIES = [ | ||
"random", | ||
"random_valid", | ||
"blast_random_valid", | ||
"chase_closest_food", | ||
"chase_random_food", | ||
"cautious_carol" | ||
] | ||
|
||
describe "Crystal Snake Battlesnake endpoints for all supported strategies" do | ||
it "responds with metadata for all supported strategies" do | ||
SUPPORTED_STRATEGIES.each do |strategy_name| | ||
get "/#{strategy_name}" | ||
response.status_code.should eq 200 | ||
res = Hash(String, String).from_json(response.body) | ||
res.keys.should eq(["apiversion", "author", "color", "head", "tail", "version"]) | ||
end | ||
end | ||
|
||
it "returns 200 on /start" do | ||
payload = File.read("./spec/fixtures/start.json") | ||
post "/start", body: payload, headers: HTTP::Headers{"Content-Type" => "application/json"} | ||
response.status_code.should eq 200 | ||
SUPPORTED_STRATEGIES.each do |strategy_name| | ||
payload = File.read("./spec/fixtures/start.json") | ||
post "/#{strategy_name}/start", body: payload, headers: request_headers | ||
response.status_code.should eq 200 | ||
end | ||
end | ||
|
||
it "returns 200 on /move" do | ||
payload = File.read("./spec/fixtures/move.json") | ||
post "/move", body: payload, headers: HTTP::Headers{"Content-Type" => "application/json"} | ||
response.status_code.should eq 200 | ||
it "returns 200 and valid move on /move" do | ||
SUPPORTED_STRATEGIES.each do |strategy_name| | ||
payload = File.read("./spec/fixtures/move.json") | ||
post "/#{strategy_name}/move", body: payload, headers: request_headers | ||
response.status_code.should eq 200 | ||
selected_move = Hash(String, String).from_json(response.body)["move"] | ||
(Strategy::VALID_MOVES.includes?(selected_move)).should be_true | ||
end | ||
end | ||
|
||
it "returns 200 on /end" do | ||
payload = File.read("./spec/fixtures/end.json") | ||
post "/end", body: payload, headers: HTTP::Headers{"Content-Type" => "application/json"} | ||
response.status_code.should eq 200 | ||
SUPPORTED_STRATEGIES.each do |strategy_name| | ||
payload = File.read("./spec/fixtures/end.json") | ||
post "/#{strategy_name}/end", body: payload, headers: HTTP::Headers{"Content-Type" => "application/json"} | ||
response.status_code.should eq 200 | ||
end | ||
end | ||
end |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
# Abstract class of all strategies. They're all initialized with a *@context* | ||
# and their entrypoint is the `#move` method | ||
abstract class Strategy::Base | ||
def initialize(@context : BattleSnake::Context) | ||
module Strategy | ||
VALID_MOVES = ["up", "left", "down", "right"] | ||
|
||
def self.build(name, context) | ||
case name | ||
when "random" | ||
Strategy::Random.new(context) | ||
when "random_valid" | ||
Strategy::RandomValid.new(context) | ||
when "blast_random_valid" | ||
Strategy::BlastRandomValid.new(context) | ||
when "chase_closest_food" | ||
Strategy::ChaseClosestFood.new(context) | ||
when "chase_random_food" | ||
Strategy::ChaseClosestFood.new(context) | ||
when "cautious_carol" | ||
Strategy::CautiousCarol.new(context) | ||
else | ||
nil | ||
end | ||
end | ||
|
||
# Returns the move (direction) to chose based on the *@context* | ||
def move | ||
"up" | ||
abstract class Base | ||
def initialize(@context : BattleSnake::Context) | ||
end | ||
|
||
# Returns the move (direction) to chose based on the *@context* | ||
def move | ||
"up" | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# A strategy that chooses a random direction to move without any considerations | ||
class Strategy::Random < Strategy::Base | ||
def move | ||
["up", "left", "down", "right"].sample | ||
VALID_MOVES.sample | ||
end | ||
end |