XO is a cross-platform tic-tac-toe library for Swift.
- Tic-tac-toe game management
- Tic-tac-toe board structuring
- Square availability
- Marking validation
- Platforms:
- macOS 10.9+
- iOS 8.0+
- watchOS 2.0+
- tvOS 9.0+
- Linux
- Any other platform where Swift is available
- Xcode 8.0
- Swift 3.0
The Swift Package Manager is a decentralized dependency manager for Swift.
-
Add the project to your
Package.swift
.import PackageDescription let package = Package( name: "MyAwesomeProject", dependencies: [ .Package(url: "https://github.com/nvzqz/XO.git", majorVersion: 1) ] )
-
Import the XO module.
import XO
CocoaPods is a centralized dependency manager for Objective-C and Swift. Go here to learn more.
-
Add the project to your Podfile.
use_frameworks! pod 'XO', '~> 1.0.0'
If you want to be on the bleeding edge, replace the last line with:
pod 'XO', :git => 'https://github.com/nvzqz/XO.git'
-
Run
pod install
and open the.xcworkspace
file to launch Xcode. -
Import the XO framework.
import XO
Carthage is a decentralized dependency manager for Objective-C and Swift.
-
Add the project to your Cartfile.
github "nvzqz/XO"
-
Run
carthage update
and follow the additional steps in order to add XO to your project. -
Import the XO framework.
import XO
Running a tic-tac-toe game can be as simple as setting up a loop.
import XO
let game = Game()
while !game.board.isFinished {
let square = ...
try game.applyMark(to: square)
}
Marks can be applied in a Game
instance starting with Mark.x
using
applyMark(to:)
and its unsafe (yet faster) sibling, applyUncheckedMark(to:)
.
The mark then switches repeatedly until the game is finished.
The available squares can be retrieved using availableSquares
on a Board
.
if let squares = game.board.availableSquares {
for square in squares {
...
}
}
A board can be created from a two dimensional UnicodeScalar
array.
let board: Board = [["x", " ", "o"],
[" ", " ", "x"],
["x", " ", "o"]]
A board can also be created from a hash value. Hash values do not change across processes.
let other = Board(hashValue: board.hashValue)
other == board // true
The flippedHorizontally()
method returns the board with its left side swapped with its right side.
The flippedVertically()
method returns the board with its top side swapped with its bottom side.
There is also flippedHorizontallyAndVertically()
which efficiently produces the same result
as calling both flippedHorizontally()
and flippedVertically()
.
The rotatedLeft(by:)
and rotatedRight(by:)
will return the result of rotating the board left or
right count
times.
The parameter can be of any type conforming to ExpressibleByIntegerLiteral
and IntegerArithmetic
.
print(board.ascii)
x . o
. . x
x . o
print(board.emoji)
❌⬜⭕
⬜⬜❌
❌⬜⭕