Change position according to facing in Ruby












0















I need to implement a move method that change position according to facing, position is a [x,y] and I thinking that if move to south is y+1, to north y-1, to east x-1 and to west x+1. this movements are into a matrix.
This is my code. Thank you so much for your help!



# Models the Robot behavior for the game
class Robot
FACINGS = [:south, :east, :north, :west]

def initialize(attr = {})
@position = attr[:position] || [1, 1]
# @move = attr[:move]
@facing_index = facing_index(attr[:facing]) || 0 # south
@facing = facing
# @errors =
end

def position
@position
end

def move

end

def facing
@facing = FACINGS[@facing_index]
end

def errors
end

private

def facing_index(facing)
facing if facing.is_a? Integer
FACINGS.index(facing&.to_sym)
end
end









share|improve this question

























  • Possible duplicate of Change the facing with a turn_left method in Ruby

    – engineersmnky
    Nov 13 '18 at 19:15











  • What's the difference from stackoverflow.com/questions/53285855/…?

    – iGian
    Nov 13 '18 at 19:45






  • 1





    This is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?

    – Jörg W Mittag
    Nov 13 '18 at 20:05











  • It looks like SO has been tasked with writing a robot game from scratch?

    – Casper
    Nov 13 '18 at 20:50













  • Thank u guys!! Im implementing a game into a platform to teach kids to code and I had some issues with that but I already finished! :)

    – Andrea Bazán
    Nov 13 '18 at 21:33


















0















I need to implement a move method that change position according to facing, position is a [x,y] and I thinking that if move to south is y+1, to north y-1, to east x-1 and to west x+1. this movements are into a matrix.
This is my code. Thank you so much for your help!



# Models the Robot behavior for the game
class Robot
FACINGS = [:south, :east, :north, :west]

def initialize(attr = {})
@position = attr[:position] || [1, 1]
# @move = attr[:move]
@facing_index = facing_index(attr[:facing]) || 0 # south
@facing = facing
# @errors =
end

def position
@position
end

def move

end

def facing
@facing = FACINGS[@facing_index]
end

def errors
end

private

def facing_index(facing)
facing if facing.is_a? Integer
FACINGS.index(facing&.to_sym)
end
end









share|improve this question

























  • Possible duplicate of Change the facing with a turn_left method in Ruby

    – engineersmnky
    Nov 13 '18 at 19:15











  • What's the difference from stackoverflow.com/questions/53285855/…?

    – iGian
    Nov 13 '18 at 19:45






  • 1





    This is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?

    – Jörg W Mittag
    Nov 13 '18 at 20:05











  • It looks like SO has been tasked with writing a robot game from scratch?

    – Casper
    Nov 13 '18 at 20:50













  • Thank u guys!! Im implementing a game into a platform to teach kids to code and I had some issues with that but I already finished! :)

    – Andrea Bazán
    Nov 13 '18 at 21:33
















0












0








0








I need to implement a move method that change position according to facing, position is a [x,y] and I thinking that if move to south is y+1, to north y-1, to east x-1 and to west x+1. this movements are into a matrix.
This is my code. Thank you so much for your help!



# Models the Robot behavior for the game
class Robot
FACINGS = [:south, :east, :north, :west]

def initialize(attr = {})
@position = attr[:position] || [1, 1]
# @move = attr[:move]
@facing_index = facing_index(attr[:facing]) || 0 # south
@facing = facing
# @errors =
end

def position
@position
end

def move

end

def facing
@facing = FACINGS[@facing_index]
end

def errors
end

private

def facing_index(facing)
facing if facing.is_a? Integer
FACINGS.index(facing&.to_sym)
end
end









share|improve this question
















I need to implement a move method that change position according to facing, position is a [x,y] and I thinking that if move to south is y+1, to north y-1, to east x-1 and to west x+1. this movements are into a matrix.
This is my code. Thank you so much for your help!



# Models the Robot behavior for the game
class Robot
FACINGS = [:south, :east, :north, :west]

def initialize(attr = {})
@position = attr[:position] || [1, 1]
# @move = attr[:move]
@facing_index = facing_index(attr[:facing]) || 0 # south
@facing = facing
# @errors =
end

def position
@position
end

def move

end

def facing
@facing = FACINGS[@facing_index]
end

def errors
end

private

def facing_index(facing)
facing if facing.is_a? Integer
FACINGS.index(facing&.to_sym)
end
end






ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 19:22







Andrea Bazán

















asked Nov 13 '18 at 19:03









Andrea BazánAndrea Bazán

488




488













  • Possible duplicate of Change the facing with a turn_left method in Ruby

    – engineersmnky
    Nov 13 '18 at 19:15











  • What's the difference from stackoverflow.com/questions/53285855/…?

    – iGian
    Nov 13 '18 at 19:45






  • 1





    This is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?

    – Jörg W Mittag
    Nov 13 '18 at 20:05











  • It looks like SO has been tasked with writing a robot game from scratch?

    – Casper
    Nov 13 '18 at 20:50













  • Thank u guys!! Im implementing a game into a platform to teach kids to code and I had some issues with that but I already finished! :)

    – Andrea Bazán
    Nov 13 '18 at 21:33





















  • Possible duplicate of Change the facing with a turn_left method in Ruby

    – engineersmnky
    Nov 13 '18 at 19:15











  • What's the difference from stackoverflow.com/questions/53285855/…?

    – iGian
    Nov 13 '18 at 19:45






  • 1





    This is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?

    – Jörg W Mittag
    Nov 13 '18 at 20:05











  • It looks like SO has been tasked with writing a robot game from scratch?

    – Casper
    Nov 13 '18 at 20:50













  • Thank u guys!! Im implementing a game into a platform to teach kids to code and I had some issues with that but I already finished! :)

    – Andrea Bazán
    Nov 13 '18 at 21:33



















Possible duplicate of Change the facing with a turn_left method in Ruby

– engineersmnky
Nov 13 '18 at 19:15





Possible duplicate of Change the facing with a turn_left method in Ruby

– engineersmnky
Nov 13 '18 at 19:15













What's the difference from stackoverflow.com/questions/53285855/…?

– iGian
Nov 13 '18 at 19:45





What's the difference from stackoverflow.com/questions/53285855/…?

– iGian
Nov 13 '18 at 19:45




1




1





This is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?

– Jörg W Mittag
Nov 13 '18 at 20:05





This is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?

– Jörg W Mittag
Nov 13 '18 at 20:05













It looks like SO has been tasked with writing a robot game from scratch?

– Casper
Nov 13 '18 at 20:50







It looks like SO has been tasked with writing a robot game from scratch?

– Casper
Nov 13 '18 at 20:50















Thank u guys!! Im implementing a game into a platform to teach kids to code and I had some issues with that but I already finished! :)

– Andrea Bazán
Nov 13 '18 at 21:33







Thank u guys!! Im implementing a game into a platform to teach kids to code and I had some issues with that but I already finished! :)

– Andrea Bazán
Nov 13 '18 at 21:33














3 Answers
3






active

oldest

votes


















1














DIRECTION_NUMBER = { :north=>0, :east=>1, :south=>2, :west=>3 }

@left = { :north=>:west, :west=>:south, :south=>:east, :east=>:north }
@right = @left.invert
#=> {:west=>:north, :south=>:west, :east=>:south, :north=>:east}

def turn_left
@facing = @left[@facing]
end

def turn_right
@facing = @right[@facing]
end

def move(direction)
x, y = @location
@location =
case direction
when :north
[x,y+1]
when :east
[x+1,y]
when :south
[x,y-1]
else
[x-1,y]
end
update_facing(direction)
end




private

def update_facing(direction)
change = (DIRECTION_NUMBER[direction] - DIRECTION_NUMBER[@facing]) % 4
case change
when 1
turn_right
when 2
turn_right; turn_right
when 3
turn_left
end
end




@location = [3, 3]    
@facing = :east

move(:south)
@location #=> [3, 2]
@facing #=> :south

move(:north)
@location #=> [3, 3]
@facing #=> :north

move(:west)
@location #=> [2, 3]
@facing #=> :west

move(:east)
@location #=> [3, 3]
@facing #=> :east





share|improve this answer
























  • Thank you Cary, you are so nice!!! :)

    – Andrea Bazán
    Nov 13 '18 at 21:34



















1














Add MOVES which says how to move based on how you're facing.



MOVES = {
north: [0, 1],
south: [0, -1],
east: [1, 0],
west: [-1,0]
}

def move
move = MOVES.fetch(@facing)
@position[0] += move[0]
@position[1] += move[1]
end


MOVES.fetch(@facing) is used instead of MOVES[@facing] so an error will be raised if there is no move for that facing.



You could also do this with a case statement, but this keeps move simple and data driven. You can add more directions like northeast: [1,1]. And if you make this an instance variable, you can customize how individual robots move.



# Define `moves` and `moves=` to get and set `@moves`
attr_accessor :moves

def initialize(attr = {})
...
# Initialize `moves` with either Robot.new(moves: {...})
# or the default MOVES
@moves ||= attr[:moves] || MOVES
...
end

def move
move = moves.fetch(@facing)
@position[0] += move[0]
@position[1] += move[1]
end





share|improve this answer





















  • 1





    Thank you so much @Schwern!!

    – Andrea Bazán
    Nov 13 '18 at 21:33



















1














FACINGS enum example.



module FACINGS
NORTH = [0, 1]
SOURTH = [0, -1]
EAST = [1, 0]
WEST = [-1,0]
end

class Robot
attr_reader :position

def initialize(attr = {})
@position = attr[:position] || [1, 1]
end

def move(facings)
@position[0] += facings[0]
@position[1] += facings[1]
end
end

r = Robot.new
r.move(FACINGS::NORTH)
r.move(FACINGS::SOURTH)
r.move(FACINGS::WEST)
r.move(FACINGS::EAST)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287869%2fchange-position-according-to-facing-in-ruby%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    DIRECTION_NUMBER = { :north=>0, :east=>1, :south=>2, :west=>3 }

    @left = { :north=>:west, :west=>:south, :south=>:east, :east=>:north }
    @right = @left.invert
    #=> {:west=>:north, :south=>:west, :east=>:south, :north=>:east}

    def turn_left
    @facing = @left[@facing]
    end

    def turn_right
    @facing = @right[@facing]
    end

    def move(direction)
    x, y = @location
    @location =
    case direction
    when :north
    [x,y+1]
    when :east
    [x+1,y]
    when :south
    [x,y-1]
    else
    [x-1,y]
    end
    update_facing(direction)
    end




    private

    def update_facing(direction)
    change = (DIRECTION_NUMBER[direction] - DIRECTION_NUMBER[@facing]) % 4
    case change
    when 1
    turn_right
    when 2
    turn_right; turn_right
    when 3
    turn_left
    end
    end




    @location = [3, 3]    
    @facing = :east

    move(:south)
    @location #=> [3, 2]
    @facing #=> :south

    move(:north)
    @location #=> [3, 3]
    @facing #=> :north

    move(:west)
    @location #=> [2, 3]
    @facing #=> :west

    move(:east)
    @location #=> [3, 3]
    @facing #=> :east





    share|improve this answer
























    • Thank you Cary, you are so nice!!! :)

      – Andrea Bazán
      Nov 13 '18 at 21:34
















    1














    DIRECTION_NUMBER = { :north=>0, :east=>1, :south=>2, :west=>3 }

    @left = { :north=>:west, :west=>:south, :south=>:east, :east=>:north }
    @right = @left.invert
    #=> {:west=>:north, :south=>:west, :east=>:south, :north=>:east}

    def turn_left
    @facing = @left[@facing]
    end

    def turn_right
    @facing = @right[@facing]
    end

    def move(direction)
    x, y = @location
    @location =
    case direction
    when :north
    [x,y+1]
    when :east
    [x+1,y]
    when :south
    [x,y-1]
    else
    [x-1,y]
    end
    update_facing(direction)
    end




    private

    def update_facing(direction)
    change = (DIRECTION_NUMBER[direction] - DIRECTION_NUMBER[@facing]) % 4
    case change
    when 1
    turn_right
    when 2
    turn_right; turn_right
    when 3
    turn_left
    end
    end




    @location = [3, 3]    
    @facing = :east

    move(:south)
    @location #=> [3, 2]
    @facing #=> :south

    move(:north)
    @location #=> [3, 3]
    @facing #=> :north

    move(:west)
    @location #=> [2, 3]
    @facing #=> :west

    move(:east)
    @location #=> [3, 3]
    @facing #=> :east





    share|improve this answer
























    • Thank you Cary, you are so nice!!! :)

      – Andrea Bazán
      Nov 13 '18 at 21:34














    1












    1








    1







    DIRECTION_NUMBER = { :north=>0, :east=>1, :south=>2, :west=>3 }

    @left = { :north=>:west, :west=>:south, :south=>:east, :east=>:north }
    @right = @left.invert
    #=> {:west=>:north, :south=>:west, :east=>:south, :north=>:east}

    def turn_left
    @facing = @left[@facing]
    end

    def turn_right
    @facing = @right[@facing]
    end

    def move(direction)
    x, y = @location
    @location =
    case direction
    when :north
    [x,y+1]
    when :east
    [x+1,y]
    when :south
    [x,y-1]
    else
    [x-1,y]
    end
    update_facing(direction)
    end




    private

    def update_facing(direction)
    change = (DIRECTION_NUMBER[direction] - DIRECTION_NUMBER[@facing]) % 4
    case change
    when 1
    turn_right
    when 2
    turn_right; turn_right
    when 3
    turn_left
    end
    end




    @location = [3, 3]    
    @facing = :east

    move(:south)
    @location #=> [3, 2]
    @facing #=> :south

    move(:north)
    @location #=> [3, 3]
    @facing #=> :north

    move(:west)
    @location #=> [2, 3]
    @facing #=> :west

    move(:east)
    @location #=> [3, 3]
    @facing #=> :east





    share|improve this answer













    DIRECTION_NUMBER = { :north=>0, :east=>1, :south=>2, :west=>3 }

    @left = { :north=>:west, :west=>:south, :south=>:east, :east=>:north }
    @right = @left.invert
    #=> {:west=>:north, :south=>:west, :east=>:south, :north=>:east}

    def turn_left
    @facing = @left[@facing]
    end

    def turn_right
    @facing = @right[@facing]
    end

    def move(direction)
    x, y = @location
    @location =
    case direction
    when :north
    [x,y+1]
    when :east
    [x+1,y]
    when :south
    [x,y-1]
    else
    [x-1,y]
    end
    update_facing(direction)
    end




    private

    def update_facing(direction)
    change = (DIRECTION_NUMBER[direction] - DIRECTION_NUMBER[@facing]) % 4
    case change
    when 1
    turn_right
    when 2
    turn_right; turn_right
    when 3
    turn_left
    end
    end




    @location = [3, 3]    
    @facing = :east

    move(:south)
    @location #=> [3, 2]
    @facing #=> :south

    move(:north)
    @location #=> [3, 3]
    @facing #=> :north

    move(:west)
    @location #=> [2, 3]
    @facing #=> :west

    move(:east)
    @location #=> [3, 3]
    @facing #=> :east






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 20:16









    Cary SwovelandCary Swoveland

    68.4k53965




    68.4k53965













    • Thank you Cary, you are so nice!!! :)

      – Andrea Bazán
      Nov 13 '18 at 21:34



















    • Thank you Cary, you are so nice!!! :)

      – Andrea Bazán
      Nov 13 '18 at 21:34

















    Thank you Cary, you are so nice!!! :)

    – Andrea Bazán
    Nov 13 '18 at 21:34





    Thank you Cary, you are so nice!!! :)

    – Andrea Bazán
    Nov 13 '18 at 21:34













    1














    Add MOVES which says how to move based on how you're facing.



    MOVES = {
    north: [0, 1],
    south: [0, -1],
    east: [1, 0],
    west: [-1,0]
    }

    def move
    move = MOVES.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end


    MOVES.fetch(@facing) is used instead of MOVES[@facing] so an error will be raised if there is no move for that facing.



    You could also do this with a case statement, but this keeps move simple and data driven. You can add more directions like northeast: [1,1]. And if you make this an instance variable, you can customize how individual robots move.



    # Define `moves` and `moves=` to get and set `@moves`
    attr_accessor :moves

    def initialize(attr = {})
    ...
    # Initialize `moves` with either Robot.new(moves: {...})
    # or the default MOVES
    @moves ||= attr[:moves] || MOVES
    ...
    end

    def move
    move = moves.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end





    share|improve this answer





















    • 1





      Thank you so much @Schwern!!

      – Andrea Bazán
      Nov 13 '18 at 21:33
















    1














    Add MOVES which says how to move based on how you're facing.



    MOVES = {
    north: [0, 1],
    south: [0, -1],
    east: [1, 0],
    west: [-1,0]
    }

    def move
    move = MOVES.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end


    MOVES.fetch(@facing) is used instead of MOVES[@facing] so an error will be raised if there is no move for that facing.



    You could also do this with a case statement, but this keeps move simple and data driven. You can add more directions like northeast: [1,1]. And if you make this an instance variable, you can customize how individual robots move.



    # Define `moves` and `moves=` to get and set `@moves`
    attr_accessor :moves

    def initialize(attr = {})
    ...
    # Initialize `moves` with either Robot.new(moves: {...})
    # or the default MOVES
    @moves ||= attr[:moves] || MOVES
    ...
    end

    def move
    move = moves.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end





    share|improve this answer





















    • 1





      Thank you so much @Schwern!!

      – Andrea Bazán
      Nov 13 '18 at 21:33














    1












    1








    1







    Add MOVES which says how to move based on how you're facing.



    MOVES = {
    north: [0, 1],
    south: [0, -1],
    east: [1, 0],
    west: [-1,0]
    }

    def move
    move = MOVES.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end


    MOVES.fetch(@facing) is used instead of MOVES[@facing] so an error will be raised if there is no move for that facing.



    You could also do this with a case statement, but this keeps move simple and data driven. You can add more directions like northeast: [1,1]. And if you make this an instance variable, you can customize how individual robots move.



    # Define `moves` and `moves=` to get and set `@moves`
    attr_accessor :moves

    def initialize(attr = {})
    ...
    # Initialize `moves` with either Robot.new(moves: {...})
    # or the default MOVES
    @moves ||= attr[:moves] || MOVES
    ...
    end

    def move
    move = moves.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end





    share|improve this answer















    Add MOVES which says how to move based on how you're facing.



    MOVES = {
    north: [0, 1],
    south: [0, -1],
    east: [1, 0],
    west: [-1,0]
    }

    def move
    move = MOVES.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end


    MOVES.fetch(@facing) is used instead of MOVES[@facing] so an error will be raised if there is no move for that facing.



    You could also do this with a case statement, but this keeps move simple and data driven. You can add more directions like northeast: [1,1]. And if you make this an instance variable, you can customize how individual robots move.



    # Define `moves` and `moves=` to get and set `@moves`
    attr_accessor :moves

    def initialize(attr = {})
    ...
    # Initialize `moves` with either Robot.new(moves: {...})
    # or the default MOVES
    @moves ||= attr[:moves] || MOVES
    ...
    end

    def move
    move = moves.fetch(@facing)
    @position[0] += move[0]
    @position[1] += move[1]
    end






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 20:11

























    answered Nov 13 '18 at 20:02









    SchwernSchwern

    88.9k16101231




    88.9k16101231








    • 1





      Thank you so much @Schwern!!

      – Andrea Bazán
      Nov 13 '18 at 21:33














    • 1





      Thank you so much @Schwern!!

      – Andrea Bazán
      Nov 13 '18 at 21:33








    1




    1





    Thank you so much @Schwern!!

    – Andrea Bazán
    Nov 13 '18 at 21:33





    Thank you so much @Schwern!!

    – Andrea Bazán
    Nov 13 '18 at 21:33











    1














    FACINGS enum example.



    module FACINGS
    NORTH = [0, 1]
    SOURTH = [0, -1]
    EAST = [1, 0]
    WEST = [-1,0]
    end

    class Robot
    attr_reader :position

    def initialize(attr = {})
    @position = attr[:position] || [1, 1]
    end

    def move(facings)
    @position[0] += facings[0]
    @position[1] += facings[1]
    end
    end

    r = Robot.new
    r.move(FACINGS::NORTH)
    r.move(FACINGS::SOURTH)
    r.move(FACINGS::WEST)
    r.move(FACINGS::EAST)





    share|improve this answer




























      1














      FACINGS enum example.



      module FACINGS
      NORTH = [0, 1]
      SOURTH = [0, -1]
      EAST = [1, 0]
      WEST = [-1,0]
      end

      class Robot
      attr_reader :position

      def initialize(attr = {})
      @position = attr[:position] || [1, 1]
      end

      def move(facings)
      @position[0] += facings[0]
      @position[1] += facings[1]
      end
      end

      r = Robot.new
      r.move(FACINGS::NORTH)
      r.move(FACINGS::SOURTH)
      r.move(FACINGS::WEST)
      r.move(FACINGS::EAST)





      share|improve this answer


























        1












        1








        1







        FACINGS enum example.



        module FACINGS
        NORTH = [0, 1]
        SOURTH = [0, -1]
        EAST = [1, 0]
        WEST = [-1,0]
        end

        class Robot
        attr_reader :position

        def initialize(attr = {})
        @position = attr[:position] || [1, 1]
        end

        def move(facings)
        @position[0] += facings[0]
        @position[1] += facings[1]
        end
        end

        r = Robot.new
        r.move(FACINGS::NORTH)
        r.move(FACINGS::SOURTH)
        r.move(FACINGS::WEST)
        r.move(FACINGS::EAST)





        share|improve this answer













        FACINGS enum example.



        module FACINGS
        NORTH = [0, 1]
        SOURTH = [0, -1]
        EAST = [1, 0]
        WEST = [-1,0]
        end

        class Robot
        attr_reader :position

        def initialize(attr = {})
        @position = attr[:position] || [1, 1]
        end

        def move(facings)
        @position[0] += facings[0]
        @position[1] += facings[1]
        end
        end

        r = Robot.new
        r.move(FACINGS::NORTH)
        r.move(FACINGS::SOURTH)
        r.move(FACINGS::WEST)
        r.move(FACINGS::EAST)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 14:29









        ろはんろはん

        111




        111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287869%2fchange-position-according-to-facing-in-ruby%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine