Home > Software design >  I need to pass a List using byval, however i know i cant do this as the list will still be changed b
I need to pass a List using byval, however i know i cant do this as the list will still be changed b

Time:10-30

this is my code for a recursive search for a chess program project. my problem is that when I recall the function "recursive search", the list is changed due to new chess moves being calculated. i previously used "list = New list" to create a new list however this makes the search take longer after hundreds of searches. so what I want to do is pass the list, and use the same list in the next search and when it finishes the sub and returns to the call point, I want the list to return to what it was before entering the sub. (eg, as if I passed a normal data structure byval) so that the "For each" can continue as if the data has not changed. First post so hopefully this all makes sense, I know my code may not be the best but for now its working, I just need to stop making New lists as it slows down the program too much. (Any other workarounds would also be appreciated)

Sub recurvive_search(ByVal the_board(,) As theboardclass, ByVal depth As Integer, ByRef depth_count() As Integer, ByVal whosgo__ As Integer, ByVal all_moves_list As List(Of A_Move))


        If depth = 3 Then
            depth_count(4)  = 1
        Else

            all_moves_list = calculate_all_moves(the_board, whosgo__, all_moves_list)
            For Each M In all_moves_list

                If IsNothing(M.sym_of_moving_piece) = False Then
                    depth_count(depth)  = 1
                    the_board = Me.change_board(the_board, the_board(M.From_x, M.From_Y).getsym, the_board(M.From_x, M.From_Y).getteam, M.New_x, M.New_Y, M.From_x, M.From_Y)
                    whosgo__ = switchgoes(whosgo__)
                    recurvive_search(the_board, depth   1, depth_count, whosgo__, all_moves_list)

                    whosgo__ = switchgoes(whosgo__)
                    the_board = Me.undo_move(the_board, M)



                End If

            Next
        End If


    End Sub

I'm using Visual basic 2010 for School

CodePudding user response:

Try this:

Sub recurvive_search(ByVal the_board(,) As theboardclass, ByVal depth As Integer, ByRef depth_count() As Integer, ByVal whosgo__ As Integer, ByRef all_moves_list As List(Of A_Move))
    If depth = 3 Then
        depth_count(4)  = 1
    Else
        all_moves_list = calculate_all_moves(the_board, whosgo__, all_moves_list)
        For Each M In all_moves_list

            If IsNothing(M.sym_of_moving_piece) = False Then
                depth_count(depth)  = 1
                the_board = Me.change_board(the_board, the_board(M.From_x, M.From_Y).getsym, the_board(M.From_x, M.From_Y).getteam, M.New_x, M.New_Y, M.From_x, M.From_Y)
                whosgo__ = switchgoes(whosgo__)
                recurvive_search(the_board, depth   1, depth_count, whosgo__, all_moves_list)

                whosgo__ = switchgoes(whosgo__)
                the_board = Me.undo_move(the_board, M)
            End If
        Next
    End If
End Sub

CodePudding user response:

There are two easy ways to make a new list which has the same contents as an existing list.

One is to use the constructor which takes an IEnumerable(Of T) to populate the constructed list, e.g.

Dim newList = New List(Of A_Move)(oldList)

The other is to use the extension method IEnumerable(Of T).ToList() to create a new List(Of T) from an existing sequence:

Dim newList = oldList.ToList()
  • Related