Skip to main content
added 1 character in body
Source Link
TinMan
  • 4.3k
  • 1
  • 13
  • 25

Helper Variables and Cells

Helper variables and cells should be used to better describe simplify our code. The cell formulas are complicating the process. They are forcing you to process Merge Up and Merge Down differently from Merge Left and Merge Right. Use a simple 4 x 4 matrix instead.

Using a 4 x 4 matrix would allow you to load the data into an array, process it in memory, and overwrite the original data. Not only is this more efficient but it separates the data model from the data view. You will be able to apply the same logic whether the game board starts in cell A1 or Z100.

Use Fully Qualified References

It is a best practice to qualify your cell references to a worksheet. This makes it easier to debug and reuse your code.

Here is how I would setup the voradborad to use a 4 x 4 matrix:

Private Const GameSheetName As String = "2048"
Private Const TopLeftCellAddress As String = "A1"
Private Const ColumnCount As Long = 4
Private Const RowCount As Long = 4

Function GameSheet() As Worksheet
    Set GameBoard = ThisWorkbook.Worksheets(GameSheetName)
End Function

Function GameBoard() As Range
    Set GameBoard = GameSheet.Range(TopLeftCellAddress).Resize(RowCount, ColumnCount)
End Function

Function Scrore() As Range
    Set GameBoard = GameBoard.Offset(RowCount, ColumnCount).Offset(2, 3)
End Function

Merge Numbers

I might be missing something but shouldn't the loop decrement 1 so that the next value can be moved and possibly merged?

Don't Repeat Yourself Principle (DRY)

As Greedo correctly states, you should try and avoid repeat code. I recommend passing an enumerated value into a main subroutine and having it process the data.

In this main procedure I would have two nested loops. The enum would be used to determine the start, end and step values of the loosloops.

Private Enum MergeDirection
    Left
    Right
    Up
    Down
End Enum

Sub MergeTiles(Direction As MergeDirection)
    Dim Data As Variant
    Data = GameBoard.Value
    Dim a As Long, b As Long
    
    Rem Loop Logic
    
    GameBoard.Value = Data
End Sub

Helper Variables and Cells

Helper variables and cells should be used to better describe simplify our code. The cell formulas are complicating the process. They are forcing you to process Merge Up and Merge Down differently from Merge Left and Merge Right. Use a simple 4 x 4 matrix instead.

Using a 4 x 4 matrix would allow you to load the data into an array, process it in memory, and overwrite the original data. Not only is this more efficient but it separates the data model from the data view. You will be able to apply the same logic whether the game board starts in cell A1 or Z100.

Use Fully Qualified References

It is a best practice to qualify your cell references to a worksheet. This makes it easier to debug and reuse your code.

Here is how I would setup the vorad to use a 4 x 4 matrix:

Private Const GameSheetName As String = "2048"
Private Const TopLeftCellAddress As String = "A1"
Private Const ColumnCount As Long = 4
Private Const RowCount As Long = 4

Function GameSheet() As Worksheet
    Set GameBoard = ThisWorkbook.Worksheets(GameSheetName)
End Function

Function GameBoard() As Range
    Set GameBoard = GameSheet.Range(TopLeftCellAddress).Resize(RowCount, ColumnCount)
End Function

Function Scrore() As Range
    Set GameBoard = GameBoard.Offset(RowCount, ColumnCount).Offset(2, 3)
End Function

Merge Numbers

I might be missing something but shouldn't the loop decrement 1 so that the next value can be moved and possibly merged?

Don't Repeat Yourself Principle (DRY)

As Greedo correctly states, you should try and avoid repeat code. I recommend passing an enumerated value into a main subroutine and having it process the data.

In this main procedure I would have two nested loops. The enum would be used to determine the start, end and step values of the loos.

Private Enum MergeDirection
    Left
    Right
    Up
    Down
End Enum

Sub MergeTiles(Direction As MergeDirection)
    Dim Data As Variant
    Data = GameBoard.Value
    Dim a As Long, b As Long
    
    Rem Loop Logic
    
    GameBoard.Value = Data
End Sub

Helper Variables and Cells

Helper variables and cells should be used to better describe simplify our code. The cell formulas are complicating the process. They are forcing you to process Merge Up and Merge Down differently from Merge Left and Merge Right. Use a simple 4 x 4 matrix instead.

Using a 4 x 4 matrix would allow you to load the data into an array, process it in memory, and overwrite the original data. Not only is this more efficient but it separates the data model from the data view. You will be able to apply the same logic whether the game board starts in cell A1 or Z100.

Use Fully Qualified References

It is a best practice to qualify your cell references to a worksheet. This makes it easier to debug and reuse your code.

Here is how I would setup the borad to use a 4 x 4 matrix:

Private Const GameSheetName As String = "2048"
Private Const TopLeftCellAddress As String = "A1"
Private Const ColumnCount As Long = 4
Private Const RowCount As Long = 4

Function GameSheet() As Worksheet
    Set GameBoard = ThisWorkbook.Worksheets(GameSheetName)
End Function

Function GameBoard() As Range
    Set GameBoard = GameSheet.Range(TopLeftCellAddress).Resize(RowCount, ColumnCount)
End Function

Function Scrore() As Range
    Set GameBoard = GameBoard.Offset(RowCount, ColumnCount).Offset(2, 3)
End Function

Merge Numbers

I might be missing something but shouldn't the loop decrement 1 so that the next value can be moved and possibly merged?

Don't Repeat Yourself Principle (DRY)

As Greedo correctly states, you should try and avoid repeat code. I recommend passing an enumerated value into a main subroutine and having it process the data.

In this main procedure I would have two nested loops. The enum would be used to determine the start, end and step values of the loops.

Private Enum MergeDirection
    Left
    Right
    Up
    Down
End Enum

Sub MergeTiles(Direction As MergeDirection)
    Dim Data As Variant
    Data = GameBoard.Value
    Dim a As Long, b As Long
    
    Rem Loop Logic
    
    GameBoard.Value = Data
End Sub
Source Link
TinMan
  • 4.3k
  • 1
  • 13
  • 25

Helper Variables and Cells

Helper variables and cells should be used to better describe simplify our code. The cell formulas are complicating the process. They are forcing you to process Merge Up and Merge Down differently from Merge Left and Merge Right. Use a simple 4 x 4 matrix instead.

Using a 4 x 4 matrix would allow you to load the data into an array, process it in memory, and overwrite the original data. Not only is this more efficient but it separates the data model from the data view. You will be able to apply the same logic whether the game board starts in cell A1 or Z100.

Use Fully Qualified References

It is a best practice to qualify your cell references to a worksheet. This makes it easier to debug and reuse your code.

Here is how I would setup the vorad to use a 4 x 4 matrix:

Private Const GameSheetName As String = "2048"
Private Const TopLeftCellAddress As String = "A1"
Private Const ColumnCount As Long = 4
Private Const RowCount As Long = 4

Function GameSheet() As Worksheet
    Set GameBoard = ThisWorkbook.Worksheets(GameSheetName)
End Function

Function GameBoard() As Range
    Set GameBoard = GameSheet.Range(TopLeftCellAddress).Resize(RowCount, ColumnCount)
End Function

Function Scrore() As Range
    Set GameBoard = GameBoard.Offset(RowCount, ColumnCount).Offset(2, 3)
End Function

Merge Numbers

I might be missing something but shouldn't the loop decrement 1 so that the next value can be moved and possibly merged?

Don't Repeat Yourself Principle (DRY)

As Greedo correctly states, you should try and avoid repeat code. I recommend passing an enumerated value into a main subroutine and having it process the data.

In this main procedure I would have two nested loops. The enum would be used to determine the start, end and step values of the loos.

Private Enum MergeDirection
    Left
    Right
    Up
    Down
End Enum

Sub MergeTiles(Direction As MergeDirection)
    Dim Data As Variant
    Data = GameBoard.Value
    Dim a As Long, b As Long
    
    Rem Loop Logic
    
    GameBoard.Value = Data
End Sub