class MazeClient

Attributes

next_moves[R]
player_moves[R]

Public Class Methods

new(hostname = 'localhost', port = 9999, player_name) click to toggle source
# File lib/maze/client/maze_client.rb, line 7
def initialize(hostname = 'localhost', port = 9999, player_name)
  @hostname = hostname
  @port = port
  @name = player_name
  @socket = nil
  @next_moves = nil
  @player_moves = nil
end

Public Instance Methods

connect() click to toggle source
# File lib/maze/client/maze_client.rb, line 16
def connect
  @socket = TCPSocket.open(@hostname, @port)
  puts @socket.gets.chop
  @socket.puts '{"playerName" : "' + @name + '"}'
  parse_response
end
game_over?() click to toggle source
# File lib/maze/client/maze_client.rb, line 27
def game_over?
  @player_moves != nil
end
move_bottom() click to toggle source
# File lib/maze/client/maze_client.rb, line 36
def move_bottom
  @socket.puts '{"move" : "bottom"}'
  parse_response
end
move_left() click to toggle source
# File lib/maze/client/maze_client.rb, line 41
def move_left
  @socket.puts '{"move" : "left"}'
  parse_response
end
move_right() click to toggle source
# File lib/maze/client/maze_client.rb, line 46
def move_right
  @socket.puts '{"move" : "right"}'
  parse_response
end
move_top() click to toggle source
# File lib/maze/client/maze_client.rb, line 31
def move_top
  @socket.puts '{"move" : "top"}'
  parse_response
end
say_goodbye() click to toggle source
# File lib/maze/client/maze_client.rb, line 23
def say_goodbye
  @socket.puts 'Goodbye'
end

Private Instance Methods

parse_response() click to toggle source
# File lib/maze/client/maze_client.rb, line 52
def parse_response
  response = JSON.parse(@socket.gets.chop)
  operation = response['operation']
  case operation
    when 'NEXT_MOVE'
      @next_moves = response['data']
      @player_moves = nil
    when 'PLAYER_MOVES'
      @player_moves = response['data']
      @next_moves = nil
    else
      @next_moves = nil
      @player_moves = nil
  end
end