|
| 1 | +// Project : Yacht Sea |
| 2 | +// Written by: Mason Z |
| 3 | +// Date: Sept 16, 2024 |
| 4 | +// Description: Yacht Sea - Missing "4 of a kind, Big Straight, and Choice" Detection. Original approach. |
| 5 | +// note, full house will be recoded to use a set or dictionary instead of current approach as per goal - however, fully functional at said moment. |
| 6 | + |
| 7 | +import Foundation |
| 8 | + |
| 9 | +for _ in 1...20 { |
| 10 | + // variables |
| 11 | + var highestScore: Int = 0 // highest score, default zero |
| 12 | + var category: String = "default" // category default temp placeholder |
| 13 | + var dice = [Int]() |
| 14 | + |
| 15 | + // create random array |
| 16 | + for _ in 1...5 { |
| 17 | + let number = Int.random(in: 1...6) |
| 18 | + dice.append(number) |
| 19 | + } |
| 20 | + |
| 21 | + // Yacht Sea - if all values is the same |
| 22 | + if (dice.allSatisfy({ $0 == dice.first }) == true) { |
| 23 | + highestScore = 50 // highest possible score |
| 24 | + category = "Yacht Sea" |
| 25 | + } |
| 26 | + |
| 27 | + // small straight |
| 28 | + let setArray: Set = Set(dice) |
| 29 | + let c1: Set = [1, 2, 3, 4] |
| 30 | + let c2: Set = [2, 3, 4, 5] |
| 31 | + let c3: Set = [3, 4, 5 ,6] |
| 32 | + let comparison = [c1, c2, c3] |
| 33 | + |
| 34 | + for c in comparison { |
| 35 | + if (setArray == c && highestScore < 30) { |
| 36 | + highestScore = 30 |
| 37 | + category = "Small Straight" |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // create an array that checks in the original "array" length of 5, for the values 1 through 6 |
| 42 | + // 1 2 3 4 5 6 |
| 43 | + var value = [0, 0, 0, 0, 0, 0] |
| 44 | + |
| 45 | + for i in 1...6 { // index 0 |
| 46 | + for number in dice { |
| 47 | + if (number == i) { |
| 48 | + value[i-1] += 1 |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (value.contains(2) && value.contains(3) && highestScore < 25) { |
| 54 | + highestScore = 25 |
| 55 | + category = "Full House" |
| 56 | + } |
| 57 | + |
| 58 | + for i in 1...6 { // get all the occurances worth their value, say 2 threes in an array? it'll now be 6. |
| 59 | + value[i-1] *= i |
| 60 | + } |
| 61 | + |
| 62 | + let max: Int = value.max() ?? 0 // find the max value in the value array |
| 63 | + let index: Int = value.firstIndex(of: max) ?? 0 // plus what index it was detected in. |
| 64 | + |
| 65 | + let numerals = ["Ones", "Twos", "Threes", "Fours", "Fives", "Sixes"] |
| 66 | + if (max > highestScore) { |
| 67 | + highestScore = max |
| 68 | + category = numerals[index] |
| 69 | + } |
| 70 | + print("input: \(dice) | output: \(highestScore) | explanation: \(category)") |
| 71 | +} |
0 commit comments