Skip to content

Commit

Permalink
Change protocol code for new piranhas game (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
SKoschnicke committed Sep 3, 2018
1 parent 679f790 commit b19c409
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 118 deletions.
72 changes: 1 addition & 71 deletions example/client.rb
Expand Up @@ -8,9 +8,6 @@ class Client < ClientInterface

attr_accessor :gamestate

# Anzahl der Spielfelder
NUM_FIELDS = 65

def initialize(log_level)
logger.level = log_level
logger.info 'Einfacher Spieler wurde erstellt.'
Expand All @@ -25,73 +22,6 @@ def move_requested
end

def best_move
possible_moves = gamestate.possible_moves # Enthält mindestens ein Element
salad_moves = []
winning_moves = []
selected_moves = []

index = gamestate.current_player.index
possible_moves.each do |move|
move.actions.each do |action|
case action.type
when :advance
target_field_index = action.distance + index
if target_field_index == NUM_FIELDS - 1
winning_moves << move
elsif gamestate.field(target_field_index).type == FieldType::SALAD
salad_moves << move
else
selected_moves << move
end
when :card
if action.card_type == CardType::EAT_SALAD
# Zug auf Hasenfeld und danach Salatkarte
salad_moves << move
# Muss nicht zusätzlich ausgewählt werden, wurde schon durch Advance ausgewählt
end
when :exchange_carrots
if action.value == 10 &&
gamestate.current_player.carrots < 30 &&
index < 40 &&
!gamestate.current_player.last_non_skip_action.instance_of?(ExchangeCarrots)
# Nehme nur Karotten auf, wenn weniger als 30 und nur am Anfang und nicht zwei
# mal hintereinander
selected_moves << move
elsif action.value == -10 &&
gamestate.current_player.carrots > 30 &&
index >= 40
# Abgeben von Karotten ist nur am Ende sinnvoll
selected_moves << move
end
when :fall_back
if index > 56 && # letztes Salatfeld
gamestate.current_player.salads > 0
# Falle nur am Ende (index > 56) zurück, außer du musst noch einen Salat loswerden
selected_moves << move
elsif index <= 56 &&
gamestate.previous_field_of_type(FieldType::HEDGEHOG, index) &&
index - gamestate.previous_field_of_type(FieldType::HEDGEHOG, index).index < 5
# Falle zurück, falls sich Rückzug lohnt (nicht zu viele Karotten aufnehmen)
selected_moves << move
end
else
# Füge Salatessen oder Skip hinzu
selected_moves << move
end
end
end

if !winning_moves.empty?
logger.info("Waehle Gewinnzug")
winning_moves.sample
elsif !salad_moves.empty?
# es gibt die Möglichkeit einen Salat zu essen
logger.info("Waehle Zug zum Salatessen")
salad_moves.sample
elsif !selected_moves.empty?
selected_moves.sample
else
possible_moves.sample
end
gamestate.possible_moves.sample
end
end
5 changes: 5 additions & 0 deletions lib/software_challenge_client/board.rb
Expand Up @@ -20,6 +20,7 @@ def initialize
(0..9).to_a.each do |x|
@fields[x] = []
(0..9).to_a.each do |y|
@fields[x][y] = Field.new(FieldType::EMPTY, x, y)
end
end
end
Expand All @@ -45,4 +46,8 @@ def field(x, y)
return nil if x.negative? || y.negative?
fields.dig(x, y) # NOTE that #dig requires ruby 2.3+
end

def add_field(field)
@fields[field.x][field.y] = field
end
end
57 changes: 10 additions & 47 deletions lib/software_challenge_client/protocol.rb
Expand Up @@ -56,8 +56,6 @@ def tag_end(name)
case name
when 'board'
logger.debug @gamestate.board.to_s
when 'type'
@context[:player].cards << CardType.find_by_key(@context[:last_text].to_sym)
end
end

Expand Down Expand Up @@ -118,35 +116,18 @@ def tag_start(name, attrs)
@context[:current_tile_direction] = nil
when 'fields'
type = FieldType.find_by_key(attrs['type'].to_sym)
index = attrs['index'].to_i
x = attrs['x'].to_i
y = attrs['y'].to_i
raise "unexpected field type: #{attrs['type']}. Known types are #{FieldType.map { |t| t.key.to_s }}" if type.nil?
@gamestate.board.fields[index] = Field.new(type, index)
@gamestate.board.add_field(Field.new(type, x, y))
when 'lastMove'
@gamestate.last_move = Move.new
when 'advance'
@gamestate.last_move.add_action_with_order(
Advance.new(attrs['distance'].to_i), attrs['order'].to_i
)
when 'card'
@gamestate.last_move.add_action_with_order(
Card.new(CardType.find_by_key(attrs['type'].to_sym), attrs['value'].to_i),
attrs['order'].to_i
)
when 'skip'
@gamestate.last_move.add_action_with_order(
Skip.new, attrs['order'].to_i
)
when 'eatSalad'
@gamestate.last_move.add_action_with_order(
EatSalad.new, attrs['order'].to_i
)
when 'fallBack'
@gamestate.last_move.add_action_with_order(
FallBack.new, attrs['order'].to_i
)
when 'exchangeCarrots'
@gamestate.last_move.add_action_with_order(
ExchangeCarrots.new(attrs['value'].to_i), attrs['order'].to_i
direction = Direction.find_by_key(attrs['direction'].to_sym)
x = attrs['x'].to_i
y = attrs['y'].to_i
raise "unexpected direction: #{attrs['direction']}. Known directions are #{Direction.map { |d| d.key.to_s }}" if direction.nil?
@gamestate.last_move = Move.new(
Coordinate.new(x, y),
direction
)
when 'winner'
winning_player = parsePlayer(attrs)
Expand All @@ -155,24 +136,6 @@ def tag_start(name, attrs)
when 'left'
logger.debug 'got left event, terminating'
@network.disconnect
when 'lastNonSkipAction'
@context[:player].last_non_skip_action =
case attrs['class']
when 'advance'
Advance.new(attrs['distance'].to_i)
when 'card'
Card.new(CardType.find_by_key(attrs['type'].to_sym), attrs['value'].to_i)
when 'skip'
Skip.new
when 'eatSalad'
EatSalad.new
when 'fallBack'
FallBack.new
when 'exchangeCarrots'
ExchangeCarrots.new(attrs['value'].to_i)
else
raise "Unknown action type #{attrs['class']}"
end
end
end

Expand Down

0 comments on commit b19c409

Please sign in to comment.