【解決方法】何も表示されないようにするにはどのような変更を加えればよいでしょうか? (リンクリストを使用)

プログラミングQA


# Snake_ladder game using doubly linked list

class Empty(Exception):
    """ Error attempting to access an element from an empty container."""
    pass


class DoublyLinkedBase:
    """Queue implementation using circularly linked list for storage."""

    # -------------------------- nested Node class --------------------------
    class _Node:
        """ Lightweight, nonpublic class for storing a doubly linked node. """
        __slots__ = '_element', '_prev', '_next'  # streamline memory usage

        def __init__(self, element, prev, next):  # initialize node’s fields
            self._element = element  # reference to user’s element
            self._prev = prev  # previous node reference
            self._next = next  # next node reference

    # ------------------------------- methods -------------------------------

    def __init__(self):
        """ Create an empty list."""
        self._header = self._Node(None, None, None)
        self._trailer = self._Node(None, None, None)
        self._header._next = self._trailer  # trailer is after header
        self._trailer._prev = self._header  # header is before trailer
        self._size = 0  # number of elements

    def __len__(self):
        """Return the number of elements in the list."""
        return self._size

    def is_empty(self):
        """Return True if list is empty."""
        return self._size == 0

    def insert_between(self, e, predecessor, successor):
        """Add element e between two existing nodes and return new node."""
        newest = self._Node(e, predecessor, successor)  # linked to neighbors
        predecessor._next = newest
        successor._prev = newest
        self._size += 1
        return newest

    def delete_node(self, node):
        """ Delete nonsentinel node from the list and return its element."""
        predecessor = node._prev
        successor = node._next
        predecessor._next = successor
        successor._prev = predecessor
        self._size -= 1
        element = node._element  # record deleted element
        node._prev = node._next = node._element = None  # deprecate node
        return element  # return deleted element

    def display_board(self):
        current = self._header._next
        while current != self._trailer:
            print(current._element, end=" ")
            current = current._next
        print()

    def dice_roll_sequance(self, case):  # not a random sequance
        if case == 1:
            self._dice_seq = [1, 5, 3]  # 84
            self.move_()
            print("case %d, ans = 84" % case)
        if case == 2:
            self._dice_seq = [4, 3, 6, 1]  # 38
            self.move_()
            print("case %d, ans = 38" % case)
        if case == 3:
            self._dice_seq = [5, 2, 6, 6, 3, 6, 5, 5]  # 100 win
            self.move_()
            print("case %d, ans = 100 win" % case)
        if case == 4:
            self._dice_seq = [5, 2, 6, 6, 3, 6, 5, 1]  # 61
            self.move_()
            print("case %d, ans = 61 " % case)
        if case == 5:
            self._dice_seq = [5, 2, 6, 6, 3, 6, 5, 6]  # 95
            self.move_()
            print("case %d, ans = 95" % case)
        if case == 6:
            self._dice_seq = [1]  # 38
            self.move_()
            print("case %d, ans = 38" % case)
        if case == 7:
            self._dice_seq = [6, 1, 2]  # 5
            self.move_()
            print("case %d, ans = 5" % case)
        if case == 8:
            self._dice_seq = [6, 1, 2, 3]  # 31
            self.move_()
            print("case %d, ans = 31" % case)
        if case == 9:
            self._dice_seq = [1, 6, 6, 1, 6, 2]  # 99
            self.move_()
            print("case %d, ans =99" % case)

    def inistialize_board(self):
        for i in range(1, 101):

            if i == 2:
                self.insert_last(i, 36)
            elif i == 8:
                self.insert_last(i, 23)
            elif i == 17:
                self.insert_last(i, 6)
            elif i == 21:
                self.insert_last(i, 21)
            elif i == 33:
                self.insert_last(i, -28)
            elif i == 46:
                self.insert_last(i, 38)
            elif i == 51:
                self.insert_last(i, 16)
            elif i == 54:
                self.insert_last(i, -20)
            elif i == 63:
                self.insert_last(i, -47)
            elif i == 69:
                self.insert_last(i, i + 2)
            elif i == 71:
                self.insert_last(i, 20)
            elif i == 75:
                self.insert_last(i, 5)
            elif i == 80:
                self.insert_last(i, 19)
            elif i == 93:
                self.insert_last(i, -19)
            elif i == 96:
                self.insert_last(i, 1)
            elif i == 97:
                self.insert_last(i, -36)
            else:
                self.insert_last(i, 0)

    def move_(self):
      current = self._header or self._trailer
      if current is not None:
          for roll in self._dice_seq:
              for _ in range(roll):
                  if current._next is not None and current._next._element is not None and current._next != self._trailer:
                      current = current._next
                      print("Moved to square", current._element)
                  else:
                      break
          if current is not None and current != self._trailer:
              self._header = current
              print("Current position:", current._element)
          else:
              print("Current position: None")
      else:
          print("Current position: None")


    def check_win(self):
        return self._header._element == 100

    # Add this method inside the DoublyLinkedBase class

    def insert_last(self, square_id, move_shift):
        current = self._header._next
        while current:
            if current._element == square_id:
                current._element += move_shift
                # Ensure the circular behavior
                if current._element > 100:
                    current._element = 200 - current._element
                print("Moved to square", current._element)
                self._header = current  
                return
            current = current._next

     
# Main dirver

dl = DoublyLinkedBase()

dl.inistialize_board()

dl.display_board()

for i in range(1, 10):
    dl.dice_roll_sequance(i)

私が試したこと:

何も表示されないようにするにはどのような変更を加えればよいですか?(コードのリンクされたリスト)
出力 :

Current position: None
case 1, ans = 84
Current position: None
case 2, ans = 38
Current position: None
case 3, ans = 100 win
Current position: None
case 4, ans = 61 
Current position: None
case 5, ans = 95
Current position: None
case 6, ans = 38
Current position: None
case 7, ans = 5
Current position: None
case 8, ans = 31
Current position: None
case 9, ans =99

解決策 1

まず、「現在の位置: なし」がどこに出力されているかを見つけることから始めます。明示的にそれを出力する場所が 2 つあり、もう 1 つは、値の値を指定した場合にそれを出力できる場所です。 current._element だった None。 デバッガーを使用してコードをステップ実行し、それを確認できます。 pdb — Python デバッガー — Python 3.12.1 ドキュメント[^] 使い方がわからない場合に役立ちます。

次に、なぜその位置に到達したのか、どのような経路をたどったのか、そしてその理由を考えてみましょう。 何がそのようになったのでしょうか? どの変数に期待値がありませんでしたか? 彼らは何を設定したのか。 何を含めるべきでしょうか? どこに設定する必要がありましたか? なぜそうしなかったのでしょうか?

これらすべてを理解すると、コードの作成者 (つまりあなた) にとって問題が何であるかがかなり明確になり、それを修正できるはずです。

ただし、コードを適切に実行することは課題の重要な部分を占めており、このような単純なコードを修正できるようになるまで、複雑なコードを修正する方法を学ぶことはできません。

コメント

タイトルとURLをコピーしました