Skip to content

Instantly share code, notes, and snippets.

@rodrickbrown
Created July 11, 2014 11:32
Show Gist options
  • Save rodrickbrown/2ffe85240150ab33a7c5 to your computer and use it in GitHub Desktop.
Save rodrickbrown/2ffe85240150ab33a7c5 to your computer and use it in GitHub Desktop.
//
// main.swift
// Bukimur
// Copyright (c) 2014 Bukimur.io All rights reserved.
import Foundation
class Sets {
var sets = Array<Dictionary<String,Any>>()
init(sets: Array<Dictionary<String,Any>>) {
self.sets = sets
}
func sumOfTiles() -> Int {
var total = 0
for idx in 0..<sets.count {
total += sets[idx]["number"] as Int
}
return total
}
func validateSetRun() -> Bool {
if sets.count <= 2 {
return false
}
var a_number = sets[0]["number"] as Int
var rev_a_number = sets[0]["number"] as Int
var a_color = sets[0]["color"] as String
for idx in 0..<sets.count {
var current_number = sets[idx]["number"] as Int
var current_color = sets[idx]["color"] as String
if a_number != current_number && rev_a_number != current_number && a_color != current_color {
return false
}
a_number++
rev_a_number--
}
print("Debug validateSetRun => Success")
return true
}
/* This function validates set Groups by comparing the first
element in a set to the others. Valid groups must contain
a minimum of 3 tiles. i.e. red(7), black(7), blue(7)
*/
func validateSetGroup() -> Bool {
var a_number = sets[0]["number"] as Int
var a_color = sets[0]["color"] as String
var colorCount = 0
if sets.count <= 2 {
return false
}
for tile in sets {
if a_number != tile["number"] as Int {
return false
}
if a_color == tile["color"] as String {
colorCount += 1
}
// Check how many times we see a_color in the list
if colorCount > 1 {
return false
}
}
print("Debug validateSetGroup => Success")
return true
}
}
class Bukimur {
let MAX_TILES = 104
var players = 0
var bag = Array<Dictionary<String,Any>>()
init(players: Int) {
self.players = players
println("----- Initalizing game with \(players) players -----")
}
func dealTiles(n: Int) -> Array<Dictionary<String,Any>>{
var tiles = Array<Dictionary<String,Any>>()
var sets = Array<Dictionary<String,Any>>()
for i in 0...n-1 {
tiles.append(self.bag[i])
self.bag.removeAtIndex(i)
}
return tiles
}
func shuffle<T>(var list: Array<T>) -> Array<T> {
for i in 0..<list.count {
let j = Int(arc4random_uniform(UInt32(list.count - i))) + i
list.insert(list.removeAtIndex(j), atIndex: i)
}
return list
}
func initalizeGame() -> Bool {
let decks = [0,1]
let colors = ["yellow", "black", "red", "blue"]
let numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13]
for color in colors {
for deck in decks {
for num in numbers {
bag.append(["color": color, "deck": deck, "number": num])
}
}
}
self.bag = self.shuffle(self.bag)
return true
}
}
var game = Bukimur(players: 4)
if game.initalizeGame() {
//println(game.bag.count)
var myRack = game.dealTiles(14)
var playerSets = Sets(sets:[["color": "black", "deck": 0, "number": 4], ["color": "black", "deck": 0, "number": 5],["color":"black", "deck": 0, "number": 6]])
// println(myRack)
if !playerSets.validateSetGroup() && !playerSets.validateSetRun() {
println("Not a successful run!")
} else{
println(playerSets.sumOfTiles())
println("Successful run!")
}
//println(game.dealTiles(1))
//println(game.bag.count)
//Sets(self.bag)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment