Discover a world of knowledge at Westonci.ca, where experts and enthusiasts come together to answer your questions. Get quick and reliable solutions to your questions from a community of experienced experts on our platform. Experience the ease of finding precise answers to your questions from a knowledgeable community of experts.

n a particular board game, there is exactly one row and it comprises N spaces, numbered 0 through N - 1 from left to right. There are also N marbles, numbered 0 through N - 1, initially placed in some arbitrary order. After that, there are two moves available that only can be done one at a time: Switch: Switch the marbles in positions 0 and 1. Rotate: Move the marble in position 0 to position N - 1, and move all other marbles one space to the left (one index lower). The objective is to arrange the marbles in order, with each marble i in position i. 1. Write a class, MarblesBoard, to represent the game above. The class should be initialized with a particular sequence of Marbles. Write an __init__ function that takes a starting sequence of marbles (the number of each marble listed in the positions from 0 to N - 1). (Notice in the sequence all the marbles are different numbers and are sequential numbered but not in order!) Next, write switch() and rotate() methods to simulate the player's moves as described above. Write a method, is_solved(), that returns True if the marbles are in the correct order, False otherwise. Additionally, write __str__ and __repr__ methods to display the current state of the board. Your class should behave like the following example: >>> board

Sagot :

Answer:

class MarblesBoard(object):

   def __init__(self, seq):

        self.seq = list(seq)

   def switch(self):

       temp = self.seq[0]

       self.seq[0] = self.seq[1]

       self.seq[1] = temp

   def rotate(self):

       temp = self.seq[0]

       for i in range(1, len(self.seq)):

           self.seq[i-1] = self.seq[i]

       self.seq[-1] = temp

   def is_solved(self):

       for i in range(len(self.seq)):

           if i != self.seq[i]:

               return False

       return True

   def __str__(self):  

       return ' '.join(list(map(str,self.seq)))

   def __repr__(self):

       return ' '.join(list(map(str,self.seq)))

class Solver(object):  

   def __init__(self, board):

       self.board = board

   def solve(self):      

       steps = 0

       while not self.board.is_solved():        

           if self.board.seq[0] > self.board.seq[1] and self.board.seq[0] != len(self.board.seq) - 1:

               self.board.switch()

           else:

               self.board.rotate()

           print(self.board)

           steps += 1

       print('Total steps:', steps)

Explanation:

The Python class MarblesBoard creates an object of the board game used in the Solver class object instance and it holds data or attributes of the player piece movement and the magic methods (__str__ and __repr__). The Solver object gets the switch and rotate movementt of the player and displays the total steps at the end of the game.

Thanks for using our service. We aim to provide the most accurate answers for all your queries. Visit us again for more insights. We hope you found this helpful. Feel free to come back anytime for more accurate answers and updated information. Thank you for trusting Westonci.ca. Don't forget to revisit us for more accurate and insightful answers.