2017-02-11 2 views
-2

私のチェスゲームの一部が正しく動作していません。説明を簡単にするために、私は重要なもののほかにすべての詳細を与えません。私はチェス盤をリンクされたチェスのリストとして保管しています。プログラムは、 "2 2 4 4"のような整数の行を読み込むはずです。この例ではライン4を発見するスポット2,2のピースを移動するリンク先リストを再リンクしていますか?チェスゲーム

、4

ピースの場合4,4と可動子とは異なる色、4でピースで、4は希望削除され、2、2の作品の座標が4,4に変更されます。

私は削除する必要がある部分を保持しているリンクを削除した後、リストを適切に再リンクしていないと思います。その下のコードは重要です。クラスで

for (int a = 0, b = 1, c = 2, d = 3; a < token2.length && b < token2.length && c < token2.length 
       && d < token2.length; a += 4, b += 4, c += 4, d += 4) { 
      //reads integers to determine which piece to move and where. 

      int MoveFromCol = Integer.parseInt(token2[a]); 
      int MoveFromRow = Integer.parseInt(token2[b]); 
      int MoveToCol = Integer.parseInt(token2[c]); 
      int MoveToRow = Integer.parseInt(token2[d]); 

      //finds the chesspiece object that is moving. 
      chessPiece MovingPiece = theBoard.findMovingPiece(MoveFromCol, MoveFromRow); 
      //stores the piece type (rook, queen, etc). 
      String SpacePieceType = theBoard.CheckPieceAtSpot(MoveToCol, MoveToRow); 

      //if there isn't another piece at the spot we're moving to, just change the moving piece's coordinates 
      //to the new spot if the piece could move. 
      if (SpacePieceType.equals("no piece") && MovingPiece.canMove(theBoard, MoveToCol, MoveToRow, SpacePieceType)) { 
       theBoard.updateLink(MoveFromCol, MoveFromRow, MoveToCol, MoveToRow); 
      } 
      //If there was a piece at spot we're moving to, different color, and we can move there, delete that piece 
      //and change moving pieces coordinates to new spot. 
      if (MovingPiece.canMove(theBoard, MoveToCol, MoveToRow, SpacePieceType) && !SpacePieceType.equals("no space")) { 

       theBoard.delete(MoveToCol, MoveToRow); 
       theBoard.updateLink(MoveFromCol, MoveFromRow, MoveToCol, MoveToRow); 

      } 
     } 

リスト方式

public Link delete(int Col, int Row) { 

    Link current = head; 
    Link previous = head; 

    while (current.piece.col != Col && current.piece.row != Row) { 

     if (current.next == null) { 
      return null; 
     } else { 

      previous = current; 
      current = current.next; 
     } 
    } 

    if (current == head) { 

     head = head.next; 
    } else { 

     previous.next = current.next; 
    } 

    return current; 
} 


    public void updateLink(int oldCol, int oldRow, int newCol, int newRow) { 

    Link current = head; 

    while (current != null) { 

     if (oldCol == current.piece.col && oldRow == current.piece.row) { 

      current.piece.col = newCol; 
      current.piece.row = newRow; 
     } 
     current = current.next; 
    } 
} 
+0

ゲームとは別にリンクリストをテストします。さまざまな状況で要素を追加、削除できることを確認します。たとえば、3つの要素を追加し、3つの要素を削除し、さらに要素を追加できることを確認します。各変更後にリストを印刷します。 –

+0

また、リンクされたリスト内のノードを特定の位置で削除する機能が必要な場合は、リストの先頭、2番目のアイテム、最後のアイテム、最後から2番目のアイテムでノードを削除できることを確認します。 –

+0

また、リンクされたリストコードをゲームのロジックから完全に分離することをお勧めします。リンクされたリストは、追加、削除、およびその内容を印刷するtoStringメソッドなどの明確に定義された操作を持つ抽象データ型のインタフェースで実装する必要があります。 –

答えて

0

を含むあなたは他のエラーを持っているかもしれませんが、あなたの削除ルーチンは間違いなく間違っています。 whileループの論理条件を修正すると、このエラーが修正されます。さて、あなたはupdateLinkメソッドで同様の見た目の条件を修正する必要はありません。なぜなら、それは否定の結合ではないから、De Morgan's Lawsの対象にはならないからです。

public Link delete(int Col, int Row) { 

    Link current = head; 
    Link previous = head; 

    while (current.piece.col != Col || current.piece.row != Row) { 

     if (current.next == null) { 
      return null; 
     } else { 

      previous = current; 
      current = current.next; 
     } 
    } 

    if (current == head) { 

     head = head.next; 
    } else { 

     previous.next = current.next; 
    } 

    return current; 
} 
関連する問題