Commit 14f4a6c8 authored by Andres Käver's avatar Andres Käver

hw

parent 78f865c6
......@@ -13,6 +13,8 @@
A956DEB7254C6D6C00AC42D6 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A956DEB5254C6D6C00AC42D6 /* Main.storyboard */; };
A956DEB9254C6D6F00AC42D6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A956DEB8254C6D6F00AC42D6 /* Assets.xcassets */; };
A956DEBC254C6D6F00AC42D6 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A956DEBA254C6D6F00AC42D6 /* LaunchScreen.storyboard */; };
A956DEC5254C6DA400AC42D6 /* TicTacToe.swift in Sources */ = {isa = PBXBuildFile; fileRef = A956DEC4254C6DA400AC42D6 /* TicTacToe.swift */; };
A956DEC8254C6E0300AC42D6 /* Tile.swift in Sources */ = {isa = PBXBuildFile; fileRef = A956DEC7254C6E0300AC42D6 /* Tile.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
......@@ -24,6 +26,8 @@
A956DEB8254C6D6F00AC42D6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
A956DEBB254C6D6F00AC42D6 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
A956DEBD254C6D6F00AC42D6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A956DEC4254C6DA400AC42D6 /* TicTacToe.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TicTacToe.swift; sourceTree = "<group>"; };
A956DEC7254C6E0300AC42D6 /* Tile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tile.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -59,6 +63,8 @@
A956DEAF254C6D6C00AC42D6 /* AppDelegate.swift */,
A956DEB1254C6D6C00AC42D6 /* SceneDelegate.swift */,
A956DEB3254C6D6C00AC42D6 /* ViewController.swift */,
A956DEC4254C6DA400AC42D6 /* TicTacToe.swift */,
A956DEC7254C6E0300AC42D6 /* Tile.swift */,
A956DEB5254C6D6C00AC42D6 /* Main.storyboard */,
A956DEB8254C6D6F00AC42D6 /* Assets.xcassets */,
A956DEBA254C6D6F00AC42D6 /* LaunchScreen.storyboard */,
......@@ -139,6 +145,8 @@
files = (
A956DEB4254C6D6C00AC42D6 /* ViewController.swift in Sources */,
A956DEB0254C6D6C00AC42D6 /* AppDelegate.swift in Sources */,
A956DEC8254C6E0300AC42D6 /* Tile.swift in Sources */,
A956DEC5254C6DA400AC42D6 /* TicTacToe.swift in Sources */,
A956DEB2254C6D6C00AC42D6 /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17126"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
......@@ -11,15 +13,32 @@
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Tic Tac Toe 2020" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bcK-YT-d5M">
<rect key="frame" x="25.5" y="423" width="363.5" height="60"/>
<fontDescription key="fontDescription" type="system" pointSize="50"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="bcK-YT-d5M" firstAttribute="centerY" secondItem="6Tk-OE-BBY" secondAttribute="centerY" id="EMo-OD-zQk"/>
<constraint firstItem="bcK-YT-d5M" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="Ezd-uL-z0w"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
<point key="canvasLocation" x="52.173913043478265" y="375"/>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
......@@ -6,3 +6,27 @@
//
import Foundation
class TicTacToe {
var gameBoard: [[Tile?]] = Array(repeating: Array(repeating: nil, count: 3), count: 3)
var nextMoveByCross = true
func getTile(colNo col:Int, rowNo row:Int) -> Tile? {
return gameBoard[col][row]
}
func move(colNo col:Int, rowNo row:Int){
if getTile(colNo: col, rowNo: row) == nil {
gameBoard[col][row] = Tile(isCross: nextMoveByCross)
nextMoveByCross = !nextMoveByCross
}
}
func reset(){
gameBoard = Array(repeating: Array(repeating: nil, count: 3), count: 3)
}
}
......@@ -6,3 +6,11 @@
//
import Foundation
struct Tile {
var isCross: Bool
init(isCross: Bool) {
self.isCross = isCross
}
}
......@@ -8,12 +8,56 @@
import UIKit
class ViewController: UIViewController {
var game: TicTacToe = TicTacToe()
@IBOutlet weak var labelNextMoveBy: UILabel!
@IBOutlet var gameTiles: [UIButton]!
@IBAction func resetClicked(_ sender: Any) {
game.reset()
updateUI()
}
@IBAction func gameTileClicked(_ sender: UIButton) {
let (col, row) = getRowCol(tileNo: sender.tag)
game.move(colNo: col, rowNo: row)
updateUI()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
updateUI()
}
func updateUI(){
labelNextMoveBy.text = game.nextMoveByCross ? "❌" : "⭕️"
for gameTile in gameTiles {
let (col, row) = getRowCol(tileNo: gameTile.tag)
let tile = game.getTile(colNo: col, rowNo: row)
if let gameTileToDraw = tile {
gameTile.setTitle(gameTileToDraw.isCross ? "❌" : "⭕️", for: UIControl.State.normal)
} else {
gameTile.setTitle("", for: UIControl.State.normal)
}
}
}
func getRowCol(tileNo: Int) -> (col: Int, row: Int) {
let rowNo = tileNo / 3
let colNo = tileNo - rowNo * 3
return (colNo, rowNo)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment