class Board: def __init__(self, n, m): self.n = n self.m = m self.empty = 0 self.poop = 1 self.board = get_frame(n, m, self.empty, self.poop) self.bug = Bug() self.show_bug() def show_bug(self): self.board[self.bug.y][self.bug.x] = self.bug.sign def poop_bug(self): self.board[self.bug.y][self.bug.x] = self.poop def __str__(self): s = '' for e in self.board: s += str(e) + '\n' return s def move_bug(self) -> bool: self.poop_bug() turn_number = 0 while turn_number < 4: self.bug.move() if self.board[self.bug.y][self.bug.x] == self.poop: self.bug.move_back() self.bug.turn() turn_number += 1 else: break self.show_bug() return turn_number < 4
from bug import Bug print("Bug 1:") bug1 = Bug(10) # creates an instance of a bug whose initial position is at 10 print(bug1) for i in range(1, 3): bug1.move() print(bug1) bug1.turn() for i in range(1, 5): bug1.move() print(bug1) print("Bug 2:") bug2 = Bug(5) # creates an instance of a bug whose initial position is at 5 bug2.turn() bug2.move() print(bug2) assert not bug2 > bug1 if bug1 > bug2: print("Bug1 has travelled further than bug2") print("Bug 3:") # creates an instance of a bug whose initial position is the sum of the position of bug1 and bug2 bug3 = bug1 + bug2 print(bug3) bug3.move()
## Ch09 P9.12 from bug import Bug b1 = Bug(10) b1.move() print(b1.getPosition()) b1.turn() b1.move() b1.move() print(b1.getPosition())