Question: Answer questions by Python: Q7 - WanderBot (1.25 points) Create a class called WanderBot. WanderBot is a bot that will wander around randomly. Instance Attibutes:
Answer questions by Python:
Q7 - WanderBot (1.25 points)
Create a class called WanderBot. WanderBot is a bot that will wander around randomly.
Instance Attibutes:
- character - string
- position - list of [int, int]
- moves - list of list of [int, int]
- grid_size - None or int
Of these instance attributes, only character should be taken in as an input to __init__, with a default value of 8982.
Inside the __init__:
- self.character should be set as the chr of input character
- self.position should be set to starting position [0, 0]
- self.moves should be set as the list of possible moves, which are [[-1, 0], [1, 0], [0, 1], [0, -1]]
- self.grid_size should be initialized as None
Add two methods to WanderBot:
Method: wander
This method will choose a random move from the possibilities, making sure that move is valid on the current grid.
Procedure:
- Initialize a boolean variable has_new_pos as False
- Use a while loop, with the condition not has_new_pos
- Set a variable move as the output of calling random.choice on self.moves
- Add move to self.position, using add_lists (a function we created earlier), and assign the output to a new variable new_pos
- Call check_bounds on new_pos, also passing self.grid_size into check_bounds
- Assign the output of check_bounds to has_new_pos
- This will lead to exiting the loop when a valid new position has been assigned
- Return new_pos
Method: move
- No inputs (other than self) or outputs, just sets self.position to be the output of calling self.wander().
Q8 - Multiple Bots (0.75 points)
Let's create a larger board with multiple bots exploring at the same time!
a) List of bots
Create a list bots_list that contains three distinct WanderBot instances.
Each WanderBot in bots_list will have a different shape, specified with the value passed into the character attribute.
The first WanderBot in bots_list should take the character input value 1078. The second: 1127. The third: 1279.
b) Create grid and play_board
Step 1: Assign two variables
- grid_n should be assigned the integer 15
- iter_n should be assigned the integer 25
Step 2: Call play_board
Call play_board() with the following inputs for the parameters in play_board:
- bots | input should be bots_list
- grid_size | input should be grid_n
- n_iter | input sould be iter_n
Upon executing the cell, you should see a a 15x15 grid, with three bots (of different shapes), wandering around the grid for 25 iterations.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
