搜索对的随机数,三种等等

时间:2013-11-03 18:12:19

标签: vb.net

对于一个类我必须使用随机数填充数组,但现在我需要搜索一对,三种,四种,五种类型的播放器和计算机,并根据比赛,我不知道该怎么做。这是我到目前为止所写的内容:

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))

1 个答案:

答案 0 :(得分:1)

我没有VS开火,所以只有这样的东西:

' 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

获取原始数据 - 只是计数。现在得分吧:

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

您将不得不对计算机阵列执行相同的两项操作,因此请将它们作为函数并将数组传递给它们,而不是粘贴代码副本。对,行程等应该是List或Class,或者是可以与计算机分数进行比较的另一个阵列。一个不错的List(Of Score)会很棒 - 评分函数可以创建一个返回它。 upvote和/或接受,如果这有帮助