Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions memo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -19,10 +19,16 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="top" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="nF6-mb-2ZL" customClass="CardsView" customModule="memo" customModuleProvider="target">
<rect key="frame" x="0.0" y="203.5" width="375" height="260"/>
<rect key="frame" x="0.0" y="183.5" width="375" height="300.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label1" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="zSh-3J-mK6">
<rect key="frame" x="0.0" y="0.0" width="49.5" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="R1d-cK-V72">
<rect key="frame" x="0.0" y="0.0" width="375" height="120"/>
<rect key="frame" x="0.0" y="40.5" width="375" height="120"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oLN-eX-Q2A">
<rect key="frame" x="0.0" y="0.0" width="79" height="120"/>
Expand Down Expand Up @@ -58,7 +64,7 @@
</constraints>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="zGT-pR-dBh">
<rect key="frame" x="0.0" y="140" width="375" height="120"/>
<rect key="frame" x="0.0" y="180.5" width="375" height="120"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="TKe-lY-Hpb">
<rect key="frame" x="0.0" y="0.0" width="79" height="120"/>
Expand Down Expand Up @@ -106,6 +112,7 @@
</mask>
</variation>
<connections>
<outlet property="uiCounter" destination="zSh-3J-mK6" id="jqh-q8-omD"/>
<outletCollection property="cards" destination="oLN-eX-Q2A" collectionClass="NSMutableArray" id="KW0-X6-vpm"/>
<outletCollection property="cards" destination="LKB-cK-9Zu" collectionClass="NSMutableArray" id="sIw-dY-TNc"/>
<outletCollection property="cards" destination="T55-pc-MsI" collectionClass="NSMutableArray" id="s57-qN-FFr"/>
Expand Down
8 changes: 8 additions & 0 deletions memo/CardsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class CardsView: UIStackView {
private let backColor = UIColor.brown

@IBOutlet var cards: [UIButton]!
@IBOutlet var uiCounter: UILabel!


@IBAction func cardTapped(_ sender: UIButton) {
if let index = cards.index(of: sender) {
Expand All @@ -19,6 +21,7 @@ class CardsView: UIStackView {
}

func updateView(game: Game) {
uiCounter.text = String(game.counter)
for (i, card) in game.cards.enumerated() {
let button = cards[i]
button.isHidden = card.isMatched
Expand All @@ -30,6 +33,11 @@ class CardsView: UIStackView {
}
}
}
func initView(game: Game) {
for (i, card) in game.cards.enumerated() {
cards[i].setTitle(card.id, for: .normal)
}
}

private func open(card: UIButton) {
card.backgroundColor = cardColor
Expand Down
5 changes: 2 additions & 3 deletions memo/Model/Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ import Foundation
class Game {
private (set) var cards: [Card]
private var openedCardIndex: Int?
private (set) var counter: Int = 0

init(cards: [Card]) {
self.cards = cards
}

func showCard(at index: Int) {
cards[index].isOpened = true
counter += 1

if let previousCardIndex = openedCardIndex {
// та же карточка
if index == previousCardIndex {
return
}

// карточки совпали
if cards[previousCardIndex] == cards[index] {
cards[previousCardIndex].isMatched = true
cards[index].isMatched = true
}
// карточки не совпали
else {
cards[previousCardIndex].isOpened = false
cards[index].isOpened = false
Expand Down
13 changes: 13 additions & 0 deletions memo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ class ViewController: UIViewController, CardsViewDelegate {
stackView.delegate = self
startGame()
}
private func isGameOver() -> Bool {
for card in game.cards {
if !card.isMatched {
return false
}
}
return true
}

func didTapCard(at index: Int) {
game.showCard(at: index)
stackView.updateView(game: game)
if isGameOver() {
startGame()
}
}

private func startGame() {
let cards = images.map { Card(isOpened: false, isMatched: false, id: $0) }
game = Game(cards: cards)
stackView.initView(game: game)
stackView.updateView(game: game)
}
}