Page 1 of 1

# Checking if certain dice come on the board?

Posted 29 December 2012 - 02:13 PM

I did do my homework on this problem, but I couldn't find anything. I was wondering how to check to see if certain dice, like a 3-of-a-kind, or a straight, come on the board? This is the only way I know how, but it's really redundant:

``` Private Sub ThreeOfAKind1Dice(ByVal Dice1 As Integer, ByVal Dice2 As Integer, ByVal Dice3 As Integer)

If n1 = 1 And n2 = 1 And n3 = 1 Then 'Enables the first, second and third dice buttons if they come up as 1
Dice1 = 1
Dice2 = 1
Dice3 = 1
End If

If n1 = 1 And n2 = 1 And n4 = 1 Then 'Enables the first, second and fourth dice buttons if they come up as 1
Dice1 = 2
Dice2 = 2
Dice3 = 2
End If

If n1 = 1 And n2 = 1 And n5 = 1 Then 'Enables the first, second and fifth dice buttons if they come up as 1
Dice1 = 3
Dice2 = 3
Dice3 = 3
End If

```

Is there anyway to shorten this code to make it more manageable and less redundant?

Is This A Good Question/Topic? 0

## Replies To: Checking if certain dice come on the board?

### #2 lar3ry Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Checking if certain dice come on the board?

Posted 29 December 2012 - 08:17 PM Arctiq, on 29 December 2012 - 03:13 PM, said:

I did do my homework on this problem, but I couldn't find anything. I was wondering how to check to see if certain dice, like a 3-of-a-kind, or a straight, come on the board? This is the only way I know how, but it's really redundant:

I'd be tempted to use an array (let's call it faces() ) that is filled as each die is assigned a value on the roll. The array would count the number of each value. For example, let's say the dice roll is 1 5 4 5 5 The array contents would end up as:

• faces(0) = 1
• faces(1) = 0
• faces(2) = 0
• faces(3) = 1
• faces(4) = 3
• faces(5) = 0

A straight would end up as:

• faces(0) = 1
• faces(1) = 1
• faces(2) = 1
• faces(3) = 1
• faces(4) = 1
• faces(5) = 1

and a "short straight" like in Yahtzee would have a 0 in (0) or (5).

Then, it's a simple matter to use a For loop to look for 3 of a kind, and another to look for that value on a per die basis.

## Re: Checking if certain dice come on the board?

Posted 29 December 2012 - 09:42 PM lar3ry, on 29 December 2012 - 08:17 PM, said: Arctiq, on 29 December 2012 - 03:13 PM, said:

I did do my homework on this problem, but I couldn't find anything. I was wondering how to check to see if certain dice, like a 3-of-a-kind, or a straight, come on the board? This is the only way I know how, but it's really redundant:

I'd be tempted to use an array (let's call it faces() ) that is filled as each die is assigned a value on the roll. The array would count the number of each value. For example, let's say the dice roll is 1 5 4 5 5 The array contents would end up as:

• faces(0) = 1
• faces(1) = 0
• faces(2) = 0
• faces(3) = 1
• faces(4) = 3
• faces(5) = 0

A straight would end up as:

• faces(0) = 1
• faces(1) = 1
• faces(2) = 1
• faces(3) = 1
• faces(4) = 1
• faces(5) = 1

and a "short straight" like in Yahtzee would have a 0 in (0) or (5).

Then, it's a simple matter to use a For loop to look for 3 of a kind, and another to look for that value on a per die basis.

Ohh that's smart! I didn't think about using an array! lar3ry, on 29 December 2012 - 08:17 PM, said:

Then, it's a simple matter to use a For loop to look for 3 of a kind, and another to look for that value on a per die basis.

I'm not too experienced with For loops... Could you give me some pointers?

### #4 lar3ry Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Checking if certain dice come on the board?

Posted 29 December 2012 - 09:55 PM Arctiq, on 29 December 2012 - 10:42 PM, said:

I'm not too experienced with For loops... Could you give me some pointers?

Our Tutorial about Loops would be an excellent place to start.

## Re: Checking if certain dice come on the board?

Posted 30 December 2012 - 01:04 PM lar3ry, on 29 December 2012 - 09:55 PM, said: Arctiq, on 29 December 2012 - 10:42 PM, said:

I'm not too experienced with For loops... Could you give me some pointers?

Our Tutorial about Loops would be an excellent place to start.

I mean, I just don't know how using a for loop would help for a 3-of-a-kind dice....

### #6 lar3ry Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Checking if certain dice come on the board?

Posted 30 December 2012 - 10:17 PM Arctiq, on 30 December 2012 - 02:04 PM, said:

I mean, I just don't know how using a for loop would help for a 3-of-a-kind dice....

Well, assuming the array contains the values in the first example: 1 0 0 1 3 0
```   For i = 0 to 5
if faces(i) >= 3 then
threeof = i + 1 ' there are three or more of these numbers
end if
Next
' using the example, threeof should contain 5 at this point

For j = 0 to 4    ' 5 dice
if dieValue(j) = threeof then
'turn on the appropriate button
' or higlight the die, etc.
'again, using the example, the If will be executed
' for j values of 1, 3, and 4, corresponding to
' dice 2, 4, and 5
end if
Next

```

Just remember that arrays are indexed starting at 0.

## Re: Checking if certain dice come on the board?

Posted 31 December 2012 - 01:43 PM lar3ry, on 30 December 2012 - 10:17 PM, said: Arctiq, on 30 December 2012 - 02:04 PM, said:

I mean, I just don't know how using a for loop would help for a 3-of-a-kind dice....

Well, assuming the array contains the values in the first example: 1 0 0 1 3 0
```   For i = 0 to 5
if faces(i) >= 3 then
threeof = i + 1 ' there are three or more of these numbers
end if
Next
' using the example, threeof should contain 5 at this point

For j = 0 to 4    ' 5 dice
if dieValue(j) = threeof then
'turn on the appropriate button
' or higlight the die, etc.
'again, using the example, the If will be executed
' for j values of 1, 3, and 4, corresponding to
' dice 2, 4, and 5
end if
Next

```

Just remember that arrays are indexed starting at 0.

So is the "threeof" a sub procedure with the "For i = 0 To 5" being the loop inside the sub procedure?

### #8 lar3ry Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Checking if certain dice come on the board?

Posted 31 December 2012 - 02:43 PM Arctiq, on 31 December 2012 - 02:43 PM, said:

So is the "threeof" a sub procedure with the "For i = 0 To 5" being the loop inside the sub procedure?

No. If it was a Sub or Function, it would have parentheses(). Of course, arrays have parentheses too, but in this case, the clue is that it is being assigned an integer value (For loops use integers).

The code I gave you is not meant to be followed blindly, but rather as a bit of pseudo-code for using a For loop to (1) determine if there are three of a kind (or four, or five-), and then (2) to find the dice with that number and do whatever you want with the information.

threeof is a variable. When it is assigned a value, the value is the number shown on the faces of three, four, or five dice. As I mentioned in my original answer, if you build the faces() array as you roll the dice, you can then run through the For loops to do whatever you want with the information. The two loops are just loops, and will have to be within a Sub or Function. Where that Sub or Function is, and what else it does, is strictly up to you.

From your questions, I see that you would benefit greatly from a few tutorials and a little advice.

Above your Class statement, place these two lines:
```Option Strict On
Option Explicit On

```

These will show you what the compiler expects, and will not let you compile until you have all the data types right. It may seem like a bit of a hassle at first, but you will soon get used to using the editor to help guide you, as well as to actually reduce your keyboarding by showing you the errors and by (usually) giving you a single-click automatic fix.

Now the tutorials. Have a look at these:

I Don't Know Where to Start
Functions and Subroutines
Debugging Skills

This post has been edited by lar3ry: 31 December 2012 - 02:46 PM

## Re: Checking if certain dice come on the board?

Posted 01 January 2013 - 12:23 PM

So this is my code to add the dice values to an array and count the number of the faces that appear:

```'===Case for adding the values of the dice to an array as soon as they're called===
Select Case n1 And n2 And n3 And n4 And n5 And n6
Case 1
dieValue(0) = 1
faces(0) += 1
Case 2
dieValue(1) = 2
faces(1) += 1
Case 3
dieValue(2) = 3
faces(2) += 1
Case 4
dieValue(3) = 4
faces(3) += 1
Case 5
dieValue(4) = 5
faces(4) += 1
Case 6
dieValue(5) = 5
faces(5) += 1
End Select

```

And my sub procedures to check if there are a three of a kind:

``` 'Sub procedure to check if there is a three of a kind
Private Sub checkingthreeof()
For i = 0 To 5
If faces(i) >= 3 Then
threeof = i + 1
checkthreeof(True)
End If

Next
End Sub

Private Sub checkthreeof(ByVal isThreeOf As Boolean)
If isThreeOf = True Then
'A for loop to enable the dice buttons if they are a three of a kind
For j = 0 To 5
If dieValue(j) = threeof Then
'==What do I put here?==
End If
Next
End If
End Sub

```

I'm just confused on what to put in the "If dieValue(j) = threeof Then" part to enable the buttons corresponding to the dice?

[quote name='Arctiq' date='01 January 2013 - 12:20 PM' timestamp='1357068025' post='1776075']
```'===Case for adding the values of the dice to an array as soon as they're called===
Select Case n1 And n2 And n3 And n4 And n5 And n6
Case 1
dieValue(0) = 1
faces(0) += 1
Case 2
dieValue(1) = 2
faces(1) += 1
Case 3
dieValue(2) = 3
faces(2) += 1
Case 4
dieValue(3) = 4
faces(3) += 1
Case 5
dieValue(4) = 5
faces(4) += 1
Case 6
dieValue(5) = 6
faces(5) += 1
End Select

```

Woops, forgot to change that last case (case 6) to the value of 6 on the dieValue(5)

### #10 lar3ry Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Checking if certain dice come on the board?

Posted 01 January 2013 - 09:13 PM

Let me ask you this question.
What are n1, n2, etc.? Are they Controls? Variables? What?

What's wrong with something like this?
```    For i = 0 to 5
' generate a random number between 0 and 6
' assign that number to DieValue(i)
' add 1 to faces(the random number just generated.
' optionally, display the value on the corersponding die
Next

```

At this point, you have everything you need to
• Display the numbers on the Dice (if you didn't do it in the loop)
• Loop through faces() and find 3 of a kind.
• Loop through dieValue() and find the appropriate face values
• Enable the buttons corresponding to the dice having that dieValue

Try to think more in terms of what you are trying to accomplish, and less in terms of what you are displaying.

## Re: Checking if certain dice come on the board?

Posted 02 January 2013 - 02:55 PM lar3ry, on 01 January 2013 - 09:13 PM, said:

Let me ask you this question.
What are n1, n2, etc.? Are they Controls? Variables? What?

What's wrong with something like this?
```    For i = 0 to 5
' generate a random number between 0 and 6
' assign that number to DieValue(i)
' add 1 to faces(the random number just generated.
' optionally, display the value on the corersponding die
Next

```

At this point, you have everything you need to
• Display the numbers on the Dice (if you didn't do it in the loop)
• Loop through faces() and find 3 of a kind.
• Loop through dieValue() and find the appropriate face values
• Enable the buttons corresponding to the dice having that dieValue

Try to think more in terms of what you are trying to accomplish, and less in terms of what you are displaying.

n1, n2, n3, n4, n5 and n6 are all variables that hold the value of the dice as soon as they are called which calls the dice pictures to their appropriate button. I'm just confused on how to enable the buttons if they are a three of a kind.

If I try something like this:

```Private Sub checkthreeof(ByVal isThreeOf As Boolean)
If isThreeOf = True Then
'A for loop to enable the dice buttons if they are a three of a kind
For j = 0 To 5
If dieValue(j) = threeof Then
If n1 = dieValue(j) Then
btnShowDice1.Enabled
End If
End If
Next
End If
End Sub

```

It gives me a syntax error on the btnShowDIce1.Enabled: "Property access must assign to the property or use its value"

### #12 lar3ry Reputation: 314
• Posts: 1,296
• Joined: 12-September 12

## Re: Checking if certain dice come on the board?

Posted 02 January 2013 - 03:29 PM Arctiq, on 02 January 2013 - 03:55 PM, said:

n1, n2, n3, n4, n5 and n6 are all variables that hold the value of the dice as soon as they are called which calls the dice pictures to their appropriate button. I'm just confused on how to enable the buttons if they are a three of a kind.

OK, you don't need a single, named variable for each rolled number. The whole idea of an array is that you can have one name, and a number of elements that can be accessed with an index. The index can be anything appropriate. In my example, dieValue holds the rolled number for each of the 5 dice.

Quote

If I try something like this:

```Private Sub checkthreeof(ByVal isThreeOf As Boolean)
If isThreeOf = True Then
'A for loop to enable the dice buttons if they are a three of a kind
For j = 0 To 5
If dieValue(j) = threeof Then
If n1 = dieValue(j) Then
btnShowDice1.Enabled
End If
End If
Next
End If
End Sub

```

It gives me a syntax error on the btnShowDIce1.Enabled: "Property access must assign to the property or use its value"

As the error message states, It's a property that must be assigned a value, or have its value assigned to something else (used).
So it should be btnShowDIce1.Enabled = True or btnShowDIce1.Enabled = False
Now, you can put all your buttons into an array as well. Put the Private buttons(5) as Button outside any Sub or Function, and the assignment statements in the Form_Load event.

```  Private buttons(5) as Button
buttons(0) = btnShowDice1
buttons(1) = btnShowDice2
' and so on ...

```

```            For j = 0 To 5