# Thread: Creating simple games in Excel

1. ## Creating simple games in Excel

I am hoping someone could please give me some direction on getting started using rand, randbetween countif, vlookup and anything else to create a game. I am however familiar with the functions, well not too familiar I just need to know how to get started, I have no clue for flipping coins, dice, or card games, I am at a total loss.

I just need to be pointed in the right direction to at least get started. Thanks in advance for any assistance anyone can provide.   Reply With Quote

2. Hi, Did this link not help any?

Do you have a sample file? Here you can upload a file to the board. It must be zipped, while in the reply page (hit Alt + X from here) scroll down to Manage Attachments and Browse.. and Upload file. An example may help anybody reading this.  Reply With Quote

3. Hi,

I think that is my issue, I have no clue how to get started. I wished I had an example to post but I have nothing....I guess it's off to the book store to see what is available. This is definitely more programming than I would have expected in an intro Excel class.

Thanks again! Originally Posted by firefytr
Hi, Did this link not help any?

Do you have a sample file? Here you can upload a file to the board. It must be zipped, while in the reply page (hit Alt + X from here) scroll down to Manage Attachments and Browse.. and Upload file. An example may help anybody reading this.  Reply With Quote

4. [vba]
Dim Highest As Long
Dim Lowest As Long
Randomize
Choice = Int((Highest + 1 - Lowest) * Rnd + Lowest)
[/vba]

Just assign the values to "Highest" and "Lowest" as needed. 1 to 2 for coin, 1 to 6 for dice (six sided) etc.  Reply With Quote

5. ## Excellent, this should get me started in the right direction. Thanks alot. I will keep you up on my progress.

Teko Originally Posted by DRJ
[vba]
Dim Highest As Long
Dim Lowest As Long
Randomize
Choice = Int((Highest + 1 - Lowest) * Rnd + Lowest)
[/vba]

Just assign the values to "Highest" and "Lowest" as needed. 1 to 2 for coin, 1 to 6 for dice (six sided) etc.  Reply With Quote

6. You're Welcome

Take Care  Reply With Quote

7. Maybe we have a candidate here, DRJ?  Reply With Quote

8. Definitely   Reply With Quote

9. Hi heres an example of single player Yahtzee I have built - run the PlayYahtzee macro. Its still work in progress but it gives you an idea. Stromma from MrExcel board has recently built a nice multi-player Yahtzee one with Swedish rules which is well done with sounds etc and I believe he's looking for testers. Send him a PM on MrExcel and Im sure he wouldnt mind giving you a copy.  Reply With Quote

10. Stromma is also a member here. http://www.vbaexpress.com/forum/member.php?u=81  Reply With Quote

11. ## Ok, I was clowing around with a simple game and ran into a problem. Instead of either the "Heads or Tails" buttons actually perform calculations could I not instead have another button that performs the same as F9?

I know the game is ugly but hey, this is my first go at it. Any insight assistance, whatever would be nice.

Thanks all!  Reply With Quote

12. ## Ants

Hi Teknologist,
Here's somthing, not exacatly a game, but it may be of interest to try variations. The "ant" steps in a cell, if red, he rurns it black and goes left, if Black, he turns it Red and goes left. Be patient until he "settles down".
MD  Reply With Quote

13. Hi,
I've had a play around and added some code to introduce you to some other possibilities, changing colours and fonts, keepng score, calling other routines etc. Sorry I've no time to Comment it properly, but if you step through, I'm sure you can follow what's happening.
MD
[VBA]
Sub Tails()
Range("B1").Formula = "T" 'Set the call
Flip ' Do the flip
End Sub

Range("B1").Formula = "H"
Flip
End Sub

Sub Flip()
Dim Success As Boolean
Dim MyFlip As Double

Randomize
MyFlip = Rnd 'Random number between 0 & 1
If MyFlip > 0.5 Then
Range("B2").Formula = "Tails"
Else
End If
'Compare first letter of Result weith Guess
If Left(Range("B2"), 1) = Range("B1") Then 'Win
With Range("B3:C3")
.Interior.ColorIndex = 6
.Font.Name = "Arial"
.Font.FontStyle = "Bold Italic"
.Font.Size = 14
End With
With Range("B3")
.Formula = "You win !!!"
End With

Success = True 'for keeping score
Else 'Lose
With Range("B3:C3")
.Interior.ColorIndex = 8
.Font.Name = "Arial"
.Font.FontStyle = "Bold"
.Font.Size = 12
End With
With Range("B3")
.Formula = "Sorry, you lose."
End With

Success = False
End If
' Call another routine to keep score
CountScore Success

End Sub

Sub CountScore(Success)
'Set up the cell format
Range("B8").NumberFormat = "0 ""wins"""
Range("B9").NumberFormat = "0 "" losses"""
'Make sure there's a numeric value
If Range("B8") = "" Then Range("B8").Formula = 0
If Range("B9") = "" Then Range("B9").Formula = 0
'Change the score by the result "passed" by the Success parameter
If Success = True Then
Range("B8").Formula = Range("B8") + 1
Else
Range("B9").Formula = Range("B9") + 1
End If
End Sub

[/VBA]  Reply With Quote

14. Whew, yup, definitely gottah have patience. I will attempt playing with it after class tonight, if not too late...

Thanks for sharing.

Did you get the chance to look at mine? Originally Posted by mdmackillop
Hi,
I've had a play around and added some code to introduce you to some other possibilities, changing colours and fonts, keepng score, calling other routines etc. Sorry I've no time to Comment it properly, but if you step through, I'm sure you can follow what's happening.
MD
[VBA]
Sub Tails()
Range("B1").Formula = "T" 'Set the call
Flip ' Do the flip
End Sub

Range("B1").Formula = "H"
Flip
End Sub

Sub Flip()
Dim Success As Boolean
Dim MyFlip As Double

Randomize
MyFlip = Rnd 'Random number between 0 & 1
If MyFlip > 0.5 Then
Range("B2").Formula = "Tails"
Else
End If
'Compare first letter of Result weith Guess
If Left(Range("B2"), 1) = Range("B1") Then 'Win
With Range("B3:C3")
.Interior.ColorIndex = 6
.Font.Name = "Arial"
.Font.FontStyle = "Bold Italic"
.Font.Size = 14
End With
With Range("B3")
.Formula = "You win !!!"
End With

Success = True 'for keeping score
Else 'Lose
With Range("B3:C3")
.Interior.ColorIndex = 8
.Font.Name = "Arial"
.Font.FontStyle = "Bold"
.Font.Size = 12
End With
With Range("B3")
.Formula = "Sorry, you lose."
End With

Success = False
End If
' Call another routine to keep score
CountScore Success

End Sub

Sub CountScore(Success)
'Set up the cell format
Range("B8").NumberFormat = "0 ""wins"""
Range("B9").NumberFormat = "0 "" losses"""
'Make sure there's a numeric value
If Range("B8") = "" Then Range("B8").Formula = 0
If Range("B9") = "" Then Range("B9").Formula = 0
'Change the score by the result "passed" by the Success parameter
If Success = True Then
Range("B8").Formula = Range("B8") + 1
Else
Range("B9").Formula = Range("B9") + 1
End If
End Sub

[/VBA]  Reply With Quote

15. here another link with some simple examples:  Reply With Quote

16. Some of those are pretty good. I really like the Calculate Your Bonus joke User Form.   Reply With Quote

17. Hi.
You could try making up a two player verion of Reversi for an excel spreadsheet;
challenging, but not too "heavy" on ther coding front
MD  Reply With Quote

18. Tekno, if you are looking for a very very simple example for your intro class, then the attached modification of your file may help. Uses VBA to give a Heads/Tails result, and then Conditional formatting and a formula to display whether they have won or not.

Regards
Ken.............  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•