create checkerboard with alternating patterns

Plotweaver

New Member
Joined
Jan 28, 2018
Messages
23
I'm creating a video game that uses an 8 by 8 grid. I need half of the 64 cells to be black and half white. I'm using conditional formatting to highlight the cells white or black. I've got a randomizer to randomly make the cells either black or white. But, I can't figure out a way so that - no matter the pattern - half are white and half are black. Can you help me devise a formula that will do this? Thanks in advance.
 
This will color half the cells randomly.
Code:
Sub test()
    Dim GridRange As Range
    Dim ColorHere As Long, RangeCount As Long
    Dim i As Long
    Set GridRange = Range("A1:H8"): rem adjust
    
    With GridRange
        RangeCount = .Cells.Count
        .Interior.ColorIndex = xlNone

        For i = 1 To RangeCount / 2
            ColorHere = WorksheetFunction.RandBetween(1, RangeCount)
            Do Until .Item(ColorHere).Interior.ColorIndex = xlNone
                ColorHere = (ColorHere Mod RangeCount) + 1
            Loop
            .Item(ColorHere).Interior.Color = vbBlack
        Next I

    End With
End Sub
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Mike:
This runs GREAT! Thanks. Elegant and simple.

BTW... is there a shortcut way to activate the Shuffle? i.e. F9 key for example.
 
Upvote 0
Mike:
I just added a Command Button to Run the Shuffle program. Works perfectly well.

Now, a new challenge. The Game involves moving Tokens (Shapes) on the Board. The Token must be the same color as the Cell it rests upon (Black Token on Black cell).

But, I want to randomly select a Token (Shape) to move on the next turn. How do I randomly select a Shape?
 
Upvote 0
Mike:
I just added a Command Button to Run the Shuffle program. Works perfectly well.

Now, a new challenge. The Game involves moving Tokens (Shapes) on the Board. The Token must be the same color as the Cell it rests upon (Black Token on Black cell).

But, I want to randomly select a Token (Shape) to move on the next turn. How do I randomly select a Shape?

Code:
With ActiveSheet
    Set RandomShape = .Shapes(WorksheetFunction.RandBetween(1, .ShapesCount)
End With
 
Upvote 0
Mike:
I can't get it to work. Here's how I entered it:

Sub select_test()

With ActiveSheet
Set RandomShape = .Shapes(WorksheetFunction.RandBetween(1, .ShapesCount)
End With
End Sub

I had 6 square shapes on the 8 by 8 grid. It gives me Syntax Error.

Any suggestions? Thanks in advance.
Michael
 
Upvote 0
I was missing a dot and a right paren.
Code:
Sub select_test()
    Dim RandomShape As Object

    With ActiveSheet
      Set RandomShape = .Shapes(WorksheetFunction.RandBetween(1, .Shapes.Count))
    End With

    MsgBox RandomShape.Name
End Sub
 
Upvote 0
Mike:
Thanks. I understand how difficult it can be to write code precisely right. 'Cause I gave up trying years ago and stuck to designing games.
Michael
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top