如何找到在为了一个int数组相同的值(副本)? [英] How to find same value(duplicate) in an int array in order?

查看:122
本文介绍了如何找到在为了一个int数组相同的值(副本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基本的老虎机游戏
我在迅速编码,我有7个数字的int数组。

This is a basic slot game I am coding in swift, I have an int array of 7 numbers.

的int数组可以有1到7的价值观,他们被随机放置。

The int array can have 1 to 7 values, and they are placed randomly.

   1> [1, 2, 4, 7, 7, 7, 7]
   2> [3, 3, 5, 7, 5, 3, 3]
   3> [5, 5, 5, 2, 5, 1, 5]
   4> [2, 2, 2, 2, 2, 2, 2]

这些是什么样的阵列可以具有实施例。

Those are examples of what the array can have.

在第1个数组你可以看到有4对7S的。

In the 1st array you can see that there are 4 pairs of 7s.

在第3个数组中的第3是另外3双5S

In the 3rd array the first 3 are another 3 pair 5s

我如何去这样做,这是我。

How do I go about doing this, this is what I have.

func processWin(val : Array<Int>){

        var counter = 0;
        var oldValue = 0;

        for num in val {
            if num == oldValue {
                counter++;
            }

            oldValue = num;        
        }

        if counter == 3 {
            print("Three in a row!!!")
        }

        if counter == 4 {
            print("Four in a row!!!")
        }

        if counter == 5 {
            print("Five in a row!!!")
        }
        if counter == 6 {
            print("Six in a row!!!")
        }
        if counter == 7 {
            print("BOOM!")
        }
        print(counter)
    }

更新

如果数组为[2,2,2,2,2,2,2]的打印 BOOM

If the array is [2, 2, 2, 2, 2, 2, 2] Print BOOM

如果数组为[1,2,4,7,7,7,7]的打印 在四连胜

If the array is [1, 2, 4, 7, 7, 7, 7] Print Four in a Row

如果数组为[5,5,5,2,5,1,5]的打印 三连胜

If the array is [5, 5, 5, 2, 5, 1, 5] Print Three in a Row

如果数组为[3,3,3,2,5,5,5]的打印 三连胜&安培; 三连胜

If the array is [3, 3, 3, 2, 5, 5, 5] Print Three in a Row & Three in a Row

如果数组为[7,7,7,7,5,5,5]的打印 在四连胜&安培; 三连胜

If the array is [7, 7, 7, 7, 5, 5, 5] Print Four in a Row & Three in a Row

推荐答案

这可能是一个好主意,分裂的逻辑在code一点点。例如,一个数字转换为英语其等效词是非常适合于延长

It's probably a good idea to split up the logic in your code a little. For instance, converting a number to its equivalent word in English is well suited to an extension:

extension Int {
  var english: String {
    switch self {
    case 0: return "Zero"
    case 1: return "One"
    case 2: return "Two"
    case 3: return "Three"
    case 4: return "Four"
    case 5: return "Five"
    case 6: return "Six"
    case 7: return "Seven"
    case 8: return "Eight"
    case 9: return "Nine"
    default: return description
    }
  }
}

而在一个序列分组相似的元素是什么,也可以自行定义的:

And grouping similar elements in a sequence is something that could also be defined on its own:

extension SequenceType where Generator.Element: Equatable {
  func group() -> [(Generator.Element, Int)] {
    var res: [(Generator.Element, Int)] = []
    for el in self {
      if res.last?.0 == el {
        res[res.endIndex-1].1 += 1
      } else {
        res.append((el,1))
      }
    }
    return res
  }
}

然后你的函数一点短,更具可读性:

And then your function is little shorter and more readable:

extension SequenceType where Generator.Element: Equatable {
  func processWin() -> String {
    return self
      .group()
      .lazy
      .filter { (_,c) in c > 1 }
      .map { (e,c) in c >= 7 ? "Boom!" : c.english + " \(e)s" }
      .joinWithSeparator(", and ")
  }
}

和它可以这样使用的:

[1, 2, 4, 7, 7, 7, 7].processWin() // "Four 7s"
[3, 3, 5, 7, 5, 3, 3].processWin() // "Two 3s, and Two 3s"
[5, 5, 5, 2, 5, 1, 5].processWin() // "Three 5s" 
[2, 2, 2, 2, 2, 2, 2].processWin() // "Boom!"

这篇关于如何找到在为了一个int数组相同的值(副本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆