L
Legacy 502047
Guest
Hi everyone,
I'm working on making a version of 2048 in excel and trying to only use 2 variables to control all 4 directions of movement.
It works perfectly moving the tiles up or to the left, when direction2 = 1; but when direction2 = -1 to move them the opposite direction it throws up the error mentioned in the title of this thread.
I've been trying fix it for a few days now with no luck, wondering if someone could point me in the right direction?
Function MoveTiles(direction1 As Integer, direction2 As Integer, row As Integer, col As Integer)
Dim i, j, amount As Integer
If direction1 = 1 And direction2 = 1 Then 'up
i = 1
amount = boardcolumns
ElseIf direction1 = 1 And direction2 = -1 Then 'down
i = boardcolumns
amount = 1
ElseIf direction1 = -1 And direction2 = 1 Then 'left
i = 1
amount = boardrows
ElseIf direction1 = -1 And direction2 = -1 Then 'right
i = boardrows
amount = 1
End If
Do While (i <> amount)
If direction1 = 1 Then 'vertical/columns
If Cells(i, col).Value <> "" And Cells(i, col).Value = Cells(i + direction2, col).Value Then
Cells(i, col).Value = Cells(i, col).Value + Cells(i + direction2, col).Value
Cells(i + direction2, col).Value = ""
For j = i + direction2 To amount Step direction2
Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working
Next j
ElseIf Cells(i, col).Value = "" And TilesToBeMoved(direction1, direction2, (i), col) = True Then
For j = i To amount Step direction2
Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working
Next j
Else
i = i + direction2
End If
ElseIf direction1 = -1 Then 'horizontal/rows
If Cells(row, i).Value <> "" And Cells(row, i).Value = Cells(row, i + direction2).Value Then
Cells(row, i).Value = Cells(row, i).Value + Cells(row, i + direction2).Value
Cells(row, i + direction2).Value = ""
For j = i + direction2 To amount Step direction2
Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working
Next j
ElseIf Cells(row, i).Value = "" And TilesToBeMoved(direction1, direction2, row, (i)) = True Then
For j = i To amount Step direction2
Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working
Next j
Else
i = i + direction2
End If
End If
Loop
End Function
Many thanks,
yuro0804
I'm working on making a version of 2048 in excel and trying to only use 2 variables to control all 4 directions of movement.
It works perfectly moving the tiles up or to the left, when direction2 = 1; but when direction2 = -1 to move them the opposite direction it throws up the error mentioned in the title of this thread.
I've been trying fix it for a few days now with no luck, wondering if someone could point me in the right direction?
Function MoveTiles(direction1 As Integer, direction2 As Integer, row As Integer, col As Integer)
Dim i, j, amount As Integer
If direction1 = 1 And direction2 = 1 Then 'up
i = 1
amount = boardcolumns
ElseIf direction1 = 1 And direction2 = -1 Then 'down
i = boardcolumns
amount = 1
ElseIf direction1 = -1 And direction2 = 1 Then 'left
i = 1
amount = boardrows
ElseIf direction1 = -1 And direction2 = -1 Then 'right
i = boardrows
amount = 1
End If
Do While (i <> amount)
If direction1 = 1 Then 'vertical/columns
If Cells(i, col).Value <> "" And Cells(i, col).Value = Cells(i + direction2, col).Value Then
Cells(i, col).Value = Cells(i, col).Value + Cells(i + direction2, col).Value
Cells(i + direction2, col).Value = ""
For j = i + direction2 To amount Step direction2
Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working
Next j
ElseIf Cells(i, col).Value = "" And TilesToBeMoved(direction1, direction2, (i), col) = True Then
For j = i To amount Step direction2
Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working
Next j
Else
i = i + direction2
End If
ElseIf direction1 = -1 Then 'horizontal/rows
If Cells(row, i).Value <> "" And Cells(row, i).Value = Cells(row, i + direction2).Value Then
Cells(row, i).Value = Cells(row, i).Value + Cells(row, i + direction2).Value
Cells(row, i + direction2).Value = ""
For j = i + direction2 To amount Step direction2
Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working
Next j
ElseIf Cells(row, i).Value = "" And TilesToBeMoved(direction1, direction2, row, (i)) = True Then
For j = i To amount Step direction2
Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working
Next j
Else
i = i + direction2
End If
End If
Loop
End Function
Many thanks,
yuro0804