Skip to content

Commit cf30580

Browse files
kaivalyarnorvig
authored andcommitted
Corrected Direction arithmetic in agents.py (#348)
* added tests for Direction * fixed Direction arithmetic error
1 parent b4e6843 commit cf30580

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

agents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def __add__(self, heading):
343343
elif self.direction == self.L:
344344
return{
345345
self.R: Direction(self.U),
346-
self.L: Direction(self.L),
346+
self.L: Direction(self.D),
347347
}.get(heading, None)
348348
elif self.direction == self.U:
349349
return{

tests/test_agents.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from agents import Direction
2+
3+
def test_move_forward():
4+
d = Direction("up")
5+
l1 = d.move_forward((0,0))
6+
assert l1 == (0,-1)
7+
d = Direction(Direction.R)
8+
l1 = d.move_forward((0,0))
9+
assert l1 == (1,0)
10+
d = Direction(Direction.D)
11+
l1 = d.move_forward((0,0))
12+
assert l1 == (0,1)
13+
d = Direction("left")
14+
l1 = d.move_forward((0,0))
15+
assert l1 == (-1,0)
16+
l2 = d.move_forward((1,0))
17+
assert l2 == (0,0)
18+
19+
def test_add():
20+
d = Direction(Direction.U)
21+
l1 = d + "right"
22+
l2 = d + "left"
23+
assert l1.direction == Direction.R
24+
assert l2.direction == Direction.L
25+
d = Direction("right")
26+
l1 = d.__add__(Direction.L)
27+
l2 = d.__add__(Direction.R)
28+
assert l1.direction == "up"
29+
assert l2.direction == "down"
30+
d = Direction("down")
31+
l1 = d.__add__("right")
32+
l2 = d.__add__("left")
33+
assert l1.direction == Direction.L
34+
assert l2.direction == Direction.R
35+
d = Direction(Direction.L)
36+
l1 = d + Direction.R
37+
l2 = d + Direction.L
38+
assert l1.direction == Direction.U
39+
assert l2.direction == Direction.D #fixed
40+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy