Skip to content

Commit

Permalink
Custom grid sizes with custom requiredInARow
Browse files Browse the repository at this point in the history
  • Loading branch information
amarjayr committed Jun 22, 2016
1 parent 0009751 commit 5c8299d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MessagesExtension/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MessagesViewController: MSMessagesAppViewController {

} else {
let game = TicTacToe(message: conversation.selectedMessage) ?? TicTacToe(player: Player(uuid: conversation.localParticipantIdentifier.uuidString, color: #colorLiteral(red: 0.3607843137, green: 0.6235294118, blue: 0.9607843137, alpha: 1)), opponent: Player(uuid: conversation.remoteParticipantIdentifiers[0].uuidString, color: #colorLiteral(red: 0.9607843137, green: 0.3607843137, blue: 0.6235294118, alpha: 1)))

controller = instantiateGameViewController(with: game)

#if !((arch(i386) || arch(x86_64)) && os(iOS))
Expand Down
17 changes: 12 additions & 5 deletions MessagesExtension/TicTacToe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ class TicTacToe {
return grid.count
}

private var requiredInRowCustom: Int?
var requiredInARow: Int {
return size
get {
return requiredInRowCustom ?? size
}
set(value) {
requiredInRowCustom = value
}
}

var winner: Player? {
Expand Down Expand Up @@ -327,31 +333,32 @@ extension TicTacToe {
}

static func boardFrom(json string: String) -> [[TTTCellState]]? {
var returnGrid = Array(repeatElement(Array(repeatElement(TTTCellState.empty, count: 3)), count: 3))

if let data = string.data(using: String.Encoding.utf8) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String]]
let grid = json!

var returnGrid = Array(repeatElement(Array(repeatElement(TTTCellState.empty, count: grid.count)), count: grid.count))

for i in 0..<grid.count {
for j in 0..<grid.count {
if grid[i][j] != "empty" {
var elComponents = grid[i][j].components(separatedBy: ":/:")

returnGrid[i][j] = TTTCellState.occupied( Player(uuid: elComponents[0], color: UIColor(hex: elComponents[1])))
} else {
returnGrid[i][j] = TTTCellState.empty
}
}
}

return returnGrid
} catch let error as NSError {
fatalError("JSON PARSING ERROR: " + error.description)
}
} else {
fatalError("boardFrom, unkown error")
}

return returnGrid
}
}

Expand Down

0 comments on commit 5c8299d

Please sign in to comment.