Skip to content

Instantly share code, notes, and snippets.

@rodrickbrown
Created July 10, 2014 14:39
Show Gist options
  • Save rodrickbrown/9a83f6b523b205248344 to your computer and use it in GitHub Desktop.
Save rodrickbrown/9a83f6b523b205248344 to your computer and use it in GitHub Desktop.
//
// main.swift
// Bukimur
//[Dictionary<String, String>]
// Created by Rodrick Brown on 7/8/14.
// Copyright (c) 2014 Rodrick Brown. All rights reserved.
//
import Foundation
class Sets {
var sets = Array<Dictionary<String,Any>>()
init(sets: Array<Dictionary<String,Any>>) {
self.sets = sets
}
func validRun() -> Bool {
return true
}
func validateSetGroup() -> Bool {
var a_number = sets[0]["number"] as Int
var a_color = sets[0]["color"] as String
var colorCount = 0
if self.sets.count <= 2 {
println("Debug: set.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
}
if colorCount > 1 {
return false
}
}
return true
}
}
class Bukimur {
let MAX_TILES = 104
var players = 0
var bag = Array<Dictionary<String,Any>>()
init(players: Int) {
self.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 playerSets = Sets(sets:[["color": "black", "deck": 0, "number": 5], ["color": "red", "deck": 0, "number": 4], ["color": "blue", "deck": 1, "number": 6]])
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": "red", "deck": 0, "number": 4],["color":"blue", "deck": 0, "number": 4]])
// println(myRack)
if playerSets.validateSetGroup() && playerSets.validRun() {
println("Valid run!")
} else{
println("Invalid run!")
}
//println(game.dealTiles(1))
//println(game.bag.count)
//Sets(self.bag)
} else {
println("Error too many cards in deck " + String(game.bag.count))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment