-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extensions.swift
59 lines (46 loc) · 1.09 KB
/
Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Extensions.swift
// Tetris Engine
//
// Created by Martin Kiss on 17 Jan 2017.
// https://github.com/Tricertops/TetrisEngine
//
// The MIT License (MIT)
// Copyright © 2017 Martin Kiss
//
import Foundation
import CoreGraphics
let yes = true
let no = false
typealias Degrees = CGFloat
typealias Radians = CGFloat
extension Bool {
var not: Bool {
return !self
}
static func random() -> Bool {
return (Int.random(of: 2) == 0)
}
}
extension Int {
static func random(of: Int = .max) -> Int {
let count = Swift.min(Swift.max(0, of), Int(Int32.max))
return Int(arc4random_uniform(UInt32(count)))
}
}
extension Array {
func random() -> Element {
return self[.random(of: count)]
}
var shuffled: [Element] {
var copy = self
copy.shuffle()
return copy
}
mutating func shuffle() {
for indexA in (0..<count).reversed() {
let indexB = Int.random(of: indexA + 1)
(self[indexA], self[indexB]) = (self[indexB], self[indexA])
}
}
}