WILLIAM: As of recently, I have been recapping with python a bit, as we are planning on using it for our robot. I have had a bit of experience with python in the past, from various personal projects and school assignments, but I decided to take this week to give myself a refresher on the various bits and parts that we might need for our robot. Firstly, I had a think about all the things that our robot would might need to do. I focused on general movement for now, instead of focusing on my specific challenge (Tidy up the Toys) When programming, we can use things called subroutines, also known as functions to repeat functions that are commonly used. In python, these can be done with a `def()` statement. To start, I decided to write a "grid", which would track the position of the robot, and give us a visual representation of our robot's position after (eventually) our functions would be called.
```python
x = 11
y = 11
grid = [["x " for _ in range(x)] for _ in range(y)]
print("\n".join(["".join(x) for x in grid]))
```
Output:
```
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
```
A blank grid. That's pretty boring. Let's add some stuff to it. I decided to represent our robot with an arrow, since this allows us to also show what direction the robot is facing, which will be useful in the future. We can use the characters "v", "^", ">" and "<" to show down, up, right and left respectively. Let's start by placing the robot in the grid.
```python
x = 11
y = 11
grid = [["x " for _ in range(x)] for _ in range(y)]
grid[5][5] = "^ "
print("\n".join(["".join(x) for x in grid]))
```
Output:
```python
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x ^ x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
```
Cool, we have our forward facing robot. Now, let's write some basic functions to move the robot up, down, left and right.
To start, I'll need to write a updategrid function to change the grid as the robot moves. Since we only have the robot on the grid, I decided to whip up a messy function that just regenerates the grid and places the new robot at the position it is at, and also changes the direction. I also wrote an out function to print the grid.
```python
x = 11
y = 11
grid = [["x " for _ in range(x)] for _ in range(y)]
grid[5][5] = "^ "
directions = {"u":"^ ","l":"< ","r ":" >","d":"v "}
def updaterobot(pos, direc):
rx,ry = pos
char = directions[direc]
grid = [["x " for _ in range(x)] for _ in range(y)]
grid[ry][rx] = char
return grid
pos = (2,5)
direc = "l"
grid = updaterobot(pos, direc)
def out(grid):
print("\n".join(["".join(x) for x in grid]))
out(grid)
```
Output:
```
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x < x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
```
And we see our robot has
- moved to (2,5)
- is now facing left
So great! Now we have a visual representation of the robot on the grid.
Let's now write some functions to move left, right, up and down.
```python
def left(pos):
direc = "l"
rx,ry = pos
pos = (rx-1,ry)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
def right(pos):
direc = "r"
rx,ry = pos
pos = (rx+1,ry)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
def up(pos):
direc = "u"
rx,ry = pos
pos = (rx,ry-1)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
def down(pos):
direc = "d"
rx,ry = pos
pos = (rx,ry+1)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
```
These simply take the position, update it, update the grid, and then return the updated position and grid. We also output the grid too. We can now try and combine these to see our robot in action!
```python
x = 11
y = 11
grid = [["x " for _ in range(x)] for _ in range(y)]
grid[5][5] = "^ "
directions = {"u":"^ ","l":"< ","r":" >","d":"v "}
def updaterobot(pos, direc):
rx,ry = pos
char = directions[direc]
grid = [["x " for _ in range(x)] for _ in range(y)]
grid[ry][rx] = char
return grid
def out(grid):
print("\n".join(["".join(x) for x in grid]))
print("\n")
def left(pos):
direc = "l"
rx,ry = pos
pos = (rx-1,ry)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
def right(pos):
direc = "r"
rx,ry = pos
pos = (rx+1,ry)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
def up(pos):
direc = "u"
rx,ry = pos
pos = (rx,ry-1)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
def down(pos):
direc = "d"
rx,ry = pos
pos = (rx,ry+1)
grid = updaterobot(pos, direc)
out(grid)
return grid, pos
pos = (5,5)
direc = "u"
grid = updaterobot(pos, direc)
out(grid)
### Program starts here
grid, pos = down(pos)
grid, pos = down(pos)
grid, pos = down(pos)
grid, pos = down(pos)
grid, pos = right(pos)
grid, pos = left(pos)
grid, pos = left(pos)
grid, pos = right(pos)
grid, pos = up(pos)
grid, pos = up(pos)
grid, pos = up(pos)
grid, pos = up(pos)
grid, pos = up(pos)
```
Output:
```
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x ^ x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x v x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x v x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x v x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x v x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x > x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x < x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x < x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x > x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x ^ x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x ^ x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x ^ x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x ^ x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x ^ x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x
```
And we can see our robot moving about the grid! This will do for now, next week I'll try and make this a bit neater, and also focus on some specifics for my challenge, but this will do for now as a nice visual representation for basic movement, and also a nice refresher of basic python.
Comentários