1

For a class I had to populate an array with random numbers but now I need to search for pairs, three of a kind, four of a kind, and five of a kind for a player and computer and find a winner based on the matches, I am not sure what to do. Here is what I have written so far:

Dim random As New Random()
Dim playerrolls() As Integer = {0, 0, 0, 0, 0, 0}
Dim computerrolls() As Integer = {0, 0, 0, 0, 0, 0}

playerrolls(0) = player.Items.Add(random.Next(1, 7))
playerrolls(1) = player.Items.Add(random.Next(1, 7))
playerrolls(2) = player.Items.Add(random.Next(1, 7))
playerrolls(3) = player.Items.Add(random.Next(1, 7))
playerrolls(4) = player.Items.Add(random.Next(1, 7))

computerrolls(0) = computer.Items.Add(random.Next(1, 7))
computerrolls(1) = computer.Items.Add(random.Next(1, 7))
computerrolls(2) = computer.Items.Add(random.Next(1, 7))
computerrolls(3) = computer.Items.Add(random.Next(1, 7))
computerrolls(4) = computer.Items.Add(random.Next(1, 7))
  • a) if these are dice, use `random.Next(1, 7)` b) create another array to store the count (intx?) you get as you scan the player or computer rolls (not sure what matches what - player match count vs computer?). You'll probably need 2 such arrays one for each c) immediately get out of the habit of using `Val`. It will cause you more trouble than it is worth: http://stackoverflow.com/a/19756107/1070452 d) what role does `txtWinner` play if you are matching whats in the arrays? – Ňɏssa Pøngjǣrdenlarp Nov 03 '13 at 18:21
  • 1. for the txtWinner i only put that in there while i was trying some trial and error, as for the array to store the count and the actual searching of it i'm not sure what it should look like – user2860758 Nov 03 '13 at 22:42
  • your rolls arrays now have 6 elements, but only 5 are used. a match array would be `playerCount(5) AS integer` as a place to store the count of each possible value (if dice then 6) - playerCount(0) would be the number 1's, playerCount(1) count of 2s etc. Then just evaluate to see how many pairs, trips etc (if playerCount(n) = 3 then Trips) – Ňɏssa Pøngjǣrdenlarp Nov 03 '13 at 23:35
  • i really appreciate all your help but i am still confused. I am still very much a beginner is there any way to say it easier? – user2860758 Nov 04 '13 at 01:33
  • Sorry to keep asking, but how do i populate the new arrays – user2860758 Nov 06 '13 at 00:54

1 Answers1

1

I DONT have VS fired up, so it is only SOMETHING like this:

' loop thru the possible die pip counts
for pip as integer = 1 to 6

   'walk thru the player array
    for n as integer = 0 to playerrolls.length -1

        ' if this dice roll matches the one we are looking for (pip)
        ' then add one to the player score
        if playerrolls(n) = pip then
             ' at the end, this will tell how many 1s, 2s, 3s he had
             playercount(pips) += 1
        end if
    next n
next pip

that gets the raw data - just the count. now to score it:

Dim pairs as integer = 0
dim trips as integer = 0
dim quads as integer = 0
dim quints as integer = 0
dim sixes as integer = 0     ' since you have 6 rolls

for n as integer = 0 to playercount.length - 1
    select case playercount(n)
       case 2
          pairs += 1
       case 3
          trips += 1
       ... hopefully you get the idea
     end select
next n

You are going to have to do the same 2 things to the computer array, so make them functions and pass the array to them rather than pasting a copy of the code. The pairs, trips etc should be a List or Class or maybe another array to be able to compare to the computer score. A nice List(Of Score) would be great - the scoring function could create it an return it. upvote and/or accept if this helps

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178