- 26th Jun 2024
- 19:02 pm
In this homework on classes and objects, our expert will guide you on how to simulate a race among a bunch of Ant objects. They’ll scoot left-to-right across a 150x150 matplotlib grid, moving forward a random amount in each time step, and whoever gets to the end first wins.
Problem 1 - Ant Class
Define an Ant class. Your class should have the following attributes, which are established in the constructor:
name. The name of the ant, a string.
- x. The ant’s x-position on the matplotlib grid.
- y. The ant’s y-position on the matplotlib grid.
color. The ant’s color when rendered on matplotlib.
These attributes/state variables should be initialized in your constructor. You can add others if you like, but you must have those!
name is a required parameter for the constructor, and the others are optional. Use reasonable default values such as x = 0, y = 0, and color = "magenta". I should be able to create an Ant object with any of the following lines:
- a = Ant("Laney", x = 0, y = 50, color = "fuchsia")
- a = Ant("John", color = "limegreen")
- a = Ant("Felix")
In addition to the constructor, your Ant class should also have the following methods:
- draw(self). Render the ant on the matplotlib grid at its current x, y position and with its color. You can make the ant any matplotlib marker shape you want.
- move(self, fwd, width). Move the ant to the right by fwd steps, but don’t go further than width, which is the right-hand side of the board.
- at_edge(self, width). Return a boolean indicating if the ant has reached the right-hand edge of the board.
- __str__(self). Return a string that you’d like to be used when you call print() on an Ant object.
Problem 2 - The Ant Race
In your racetime.py file, write your ant race. It goes like this:
Establish the size of the board they’re racing on.
- Create 8-12 ant objects. They all start off with x-position 0, and with a random y-position. Choose a distinct color and name for each ant (we named our ants Laney, John, Lucia, Ab, Nate, Felix, etc. but you can pick any names you want :)
- Start moving the ants across the board. At each step, every ant moves a random amount to the right. You can choose the range of how far they might go, but make sure it’s reasonable to get across the board: maybe in a range of 0-10 steps.
- After each step (once all the ants have a new x position), render the board on the screen and pause. This gives us a nice animated effect.
When one ant gets to the end, declare them the winner!
Ant Race with Python Classes and Objects - DS2000 - Get Free Solution
Please note that this is a sample Ant Race with Python Classes and Objects assignment solved by our Python Programmers. These solutions are intended to be used for research and reference purposes only. If you can learn any concepts by going through the reports and code, then our Python Tutors would be very happy.
- Option 1 - To download the complete solution along with Code, Report and screenshots - Please visit our Python Assignment Sample Solution page
- Option 2 - Reach out to our Python Tutors to get online tutoring related to this assignment and get your doubts cleared
- Option 3 - You can check the partial solution for this assignment in this blog below
Free Homework Solution - Python Classes and Objects - DS2000
Ant-
import matplotlib.pyplot as plt
class Ant:
def __init__(self, name, x=0, y=0, color="magenta"):
self.name = name
self.x = x
self.y = y
self.color = color
def draw(self):
plt.scatter(self.x, self.y, marker=">", color=self.color, label=self.name)
def move(self, fwd, width):
self.x += fwd
if self.at_edge(width):
self.x = width
def at_edge(self, width):
if self.x >= width:
return True
return False
def __str__(self):
return 'Ant(name=' + str(self.name) + ', x=' + str(self.x) + ', y=' + str(self.y) + ')'
Racetime -
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import clear_output
from ant import Ant
width, height = (150, 150)
N = 10
colors = cm.rainbow(np.linspace(0, 1, N))
names = ["Laney", "John", "Lucia", "Ab", "Nate", "Felix", "James", "Robert", "Michael", "William"]
antArray = []
Y = []
for name, color in zip(names, colors):
while True:
y = np.random.randint(0, height)
if y in Y:
continue
else:
break
antArray.append(Ant(name=name, y=y, color=color))
while True:
winner = []
clear_output(wait=True)
for ant in antArray:
fwd = np.random.randint(1, 11)
ant.move(fwd, width)
if ant.at_edge(width):
winner.append(ant.name)
ant.draw()
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xlim([-1, width+2])
plt.ylim([-1, height+1])
plt.show()
if len(winner):
print("Race Over! And the winner is...", np.random.choice(winner))
break
Get the best Ant Race with Python Classes and Objects assignment help and tutoring services from our experts now!
About The Author - John Doe
John Doe, a seasoned Python developer and educator, specializes in object-oriented programming and data visualization. With a Master's degree in Computer Science and over a decade of experience, John is adept at simplifying complex concepts. In this assignment, John will guide you through simulating an ant race using Python classes and objects. You’ll learn to create Ant objects that move across a 150x150 matplotlib grid, implementing key OOP principles along the way. John’s expertise ensures you gain a thorough understanding of the Ant class attributes and methods, as well as the logic for running the ant race simulation.