On Fri, 19 Apr 2019 at 01:33, Roelof Wobben <r.wobben@home.nl> wrote:
oke
Maybe I understand something not right,
Lets say we have this scenario.
Robot is on position (0,0) now it turns left so the robot faces East
I think the required method was mentioned previously, but here is how to discover it yourself... + Tool > Finder > Examples + Enter... (0@1) . (1@0) <Search> finds... Point>>leftRotated e.g. (0@1) leftRotated ==> (1@0) On Fri, 19 Apr 2019 at 01:39, Roelof Wobben <r.wobben@home.nl> wrote:
Ben,
I have such a dictionary in my first attempt to solve this one. and another one for the facing.
This sounds strange that you have two Dictionaries. It can be useful to encapsulate the mapping to avoid that being littered around your Robot code... Object subclass: #Facing instanceVariables: 'map directionVector' Facing >> initialize map := Dictionary newFromPairs: { 'north'. (0@1) . 'west' . (0@1) leftRotated . 'south'. (0@1) leftRotated leftRotated. 'east' . (0@1) rightRotated }. directionVector := map atRandom. Facing >> directionString ^ map keyAtValue: directionVector Facing >> printOn: aStream " Facing new inspect " aStream nextPutAll: self className, '-', self directionString "The above is produces a minimum-viable object that displays nicely in Inspector to facilitate incrementally coding your scenario in TDD way..."
Lets say we have this scenario:
TestCase subclass: FacingTest FacingTest>>testAllTogether |position facing| "Robot begins at (0,0) facing north" position := 0@0. facing := Facing new face: 'north'. self assert: facing printString equals: 'Facing-north'. "then it turns right so it faces >>EAST<<" facing turnRight. self assert: facing printString equals: 'Facing-east'. "then it moves one step so the new position is (-1,0)" position := position + (facing movementSteps: 1). self assert: position equals: (-1@0). "then it turns left so it faces north again" facing turnLeft. self assert: facing printString equals: 'Facing-north'. "then it moves one step so the new position is (-1,1)" position := position + (facing movementSteps: 1). self assert: position equals: (-1@1). Which I'll leave as an exercise for you to complete with these hints... Facing >> face: aDirectionString directionVector := ... Facing >> turnRight directionVector := ... Facing >> turnLeft directionVector := ... Facing >> movementSteps: steps ^ ...
so if I see it , the position has nothing to do with the direction the robot is facing
yes. they only interact when the robot moves
this [position] is only important for finding out a new direction
no - position has no impact for determining the new direction
or to find out if the x or the y needs to change.
You don't need to treat x and y separately. Using Points for both the "position" and "directionVector" facilitates simple addition. cheers -ben
Roelof
Op 18-4-2019 om 19:17 schreef Richard Sargent:
On Thu, Apr 18, 2019 at 10:01 AM Roelof Wobben <r.wobben@home.nl> wrote:
yep, I have read that one but I never gets a answer how I can "convert" a point to something like north, east
because the challenge wants this to be the answer :
(Dictionary new add: 'direction' -> 'north'; add: 'position' -> (Dictionary new add: 'x' -> 0; add: 'y' -> 0; yourself); yourself)
If you have previously defined a "representation map", you would be golden.
e.g. Dictionary new at: self northDirectionVector put: 'north'; at: self eastDirectionVector put: 'east'; at: self southDirectionVector put: 'south'; at: self westDirectionVector put: 'west'; yourself.
Then: (Dictionary new add: 'direction' -> (self directionRepresentationMap at: self directionVector); ...
and I think I need then to use if then , which I try to avoid as much as possible.
Roelof
Op 18-4-2019 om 18:33 schreef Richard Sargent:
On Thu, Apr 18, 2019 at 8:57 AM Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I know I have asked earlier but im still stuck on this one : https://github.com/exercism/problem-specifications/blob/master/exercises/rob...
I tried with all double dispatch but that will be a lot of duplicate classes
The problem I cannot solve right is that a robot can move or turn. when a robot turns only the direction the robot is facing changes and the position not. when a robot moves the facing direction stays the same but the position changes. but the change is dependend on the facing. Also the new facing direction is dependend on the old facing direction/ How can I model this the best.
I already have a object Robot that contains the facing direction and the current position or tried without it but then I use a lot of if then's
so it there a better way to model this problem so it will be all nice and readable code.
If I remember correctly, Richard O'Keefe gave you a viable design. 1) Use a Point for your direction vector. 2) Use a second Point for your position.
e.g. if you align the compass with a Cartesian plane, 0@1 is North, 0@-1 is South, 1@0 is East, and -1@0 is West. When you move, you add the direction vector to your current position. If you allow movements of greater than a single unit, you multiply the direction vector by the distance before adding that product to the position.
Roelof