1

对于一个类,我必须用随机数填充一个数组,但现在我需要为玩家和计算机搜索对、三个同类、四个同类和五个同类,并根据匹配找到获胜者,我不知道该怎么办。这是我到目前为止所写的:

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

1 回答 1

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(Of Score)会很棒 - 评分功能可以创建它并返回它。如果这有帮助,请投票和/或接受

于 2013-11-06T01:13:02.493 回答