Skip to content

Commit d7166c1

Browse files
committed
code optimization without buttons
1 parent b33b61d commit d7166c1

File tree

6 files changed

+100
-76
lines changed

6 files changed

+100
-76
lines changed

SnakeGame.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
A0997FE61D3CDF2500AB2AC4 /* GameViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0997FE51D3CDF2500AB2AC4 /* GameViewController.swift */; };
1515
A0997FE81D3CDFBE00AB2AC4 /* GameFieldView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0997FE71D3CDFBE00AB2AC4 /* GameFieldView.swift */; };
1616
A0997FEA1D3CDFEE00AB2AC4 /* GameModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0997FE91D3CDFEE00AB2AC4 /* GameModel.swift */; };
17+
A09CCF411D69D81B00810397 /* Notes in Resources */ = {isa = PBXBuildFile; fileRef = A09CCF401D69D81B00810397 /* Notes */; };
1718
A0AB42391D432B8700239297 /* Snake.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0AB42381D432B8700239297 /* Snake.swift */; };
1819
/* End PBXBuildFile section */
1920

@@ -27,6 +28,7 @@
2728
A0997FE51D3CDF2500AB2AC4 /* GameViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameViewController.swift; sourceTree = "<group>"; };
2829
A0997FE71D3CDFBE00AB2AC4 /* GameFieldView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameFieldView.swift; sourceTree = "<group>"; };
2930
A0997FE91D3CDFEE00AB2AC4 /* GameModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameModel.swift; sourceTree = "<group>"; };
31+
A09CCF401D69D81B00810397 /* Notes */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Notes; sourceTree = "<group>"; };
3032
A0AB42381D432B8700239297 /* Snake.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Snake.swift; sourceTree = "<group>"; };
3133
/* End PBXFileReference section */
3234

@@ -77,6 +79,7 @@
7779
A0997FD91D3CDB5D00AB2AC4 /* Assets.xcassets */,
7880
A0997FDB1D3CDB5D00AB2AC4 /* LaunchScreen.storyboard */,
7981
A0997FDE1D3CDB5D00AB2AC4 /* Info.plist */,
82+
A09CCF401D69D81B00810397 /* Notes */,
8083
);
8184
name = SupportingFiles;
8285
sourceTree = "<group>";
@@ -141,6 +144,7 @@
141144
files = (
142145
A0997FDD1D3CDB5D00AB2AC4 /* LaunchScreen.storyboard in Resources */,
143146
A0997FDA1D3CDB5D00AB2AC4 /* Assets.xcassets in Resources */,
147+
A09CCF411D69D81B00810397 /* Notes in Resources */,
144148
A0997FD81D3CDB5D00AB2AC4 /* Main.storyboard in Resources */,
145149
);
146150
runOnlyForDeploymentPostprocessing = 0;

SnakeGame/Base.lproj/Main.storyboard

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
33
<dependencies>
44
<deployment identifier="iOS"/>
55
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
6+
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
67
</dependencies>
78
<scenes>
89
<!--Game View Controller-->
@@ -17,65 +18,59 @@
1718
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1819
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1920
<subviews>
20-
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="35L-pI-qif">
21+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2J6-BG-JmJ" customClass="GameFieldView" customModule="SnakeGame" customModuleProvider="target">
2122
<rect key="frame" x="0.0" y="20" width="600" height="580"/>
22-
<subviews>
23-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2J6-BG-JmJ" customClass="GameFieldView" customModule="SnakeGame" customModuleProvider="target">
24-
<rect key="frame" x="0.0" y="0.0" width="600" height="536"/>
25-
<color key="backgroundColor" red="0.96078431369999995" green="0.94509803920000002" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
26-
</view>
27-
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="orv-1n-LCh">
28-
<rect key="frame" x="0.0" y="536" width="600" height="44"/>
29-
<subviews>
30-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cpa-cq-xhN">
31-
<rect key="frame" x="0.0" y="0.0" width="150" height="44"/>
32-
<state key="normal" title="Left"/>
33-
<connections>
34-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="D4u-mN-dkv"/>
35-
</connections>
36-
</button>
37-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1Si-Kd-ONi">
38-
<rect key="frame" x="150" y="0.0" width="150" height="44"/>
39-
<state key="normal" title="Up"/>
40-
<connections>
41-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="4ID-GC-yMb"/>
42-
</connections>
43-
</button>
44-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="akL-Rj-9GF">
45-
<rect key="frame" x="300" y="0.0" width="150" height="44"/>
46-
<state key="normal" title="Down"/>
47-
<connections>
48-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="1tV-vF-cVr"/>
49-
</connections>
50-
</button>
51-
<button opaque="NO" alpha="0.29999999999999999" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EbZ-y1-vZA">
52-
<rect key="frame" x="450" y="0.0" width="150" height="44"/>
53-
<state key="normal" title="Right"/>
54-
<connections>
55-
<action selector="buttonAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Dso-YT-pnR"/>
56-
</connections>
57-
</button>
58-
</subviews>
59-
<constraints>
60-
<constraint firstAttribute="height" constant="44" id="PZb-Hc-TDY"/>
61-
</constraints>
62-
</stackView>
63-
</subviews>
64-
</stackView>
23+
<color key="backgroundColor" red="0.96078431369999995" green="0.94509803920000002" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
24+
<gestureRecognizers/>
25+
<connections>
26+
<outletCollection property="gestureRecognizers" destination="Vy2-2v-nIX" appends="YES" id="g9V-5d-Cvh"/>
27+
<outletCollection property="gestureRecognizers" destination="eE5-DG-6f6" appends="YES" id="hhB-Rl-yyw"/>
28+
<outletCollection property="gestureRecognizers" destination="xM9-O3-0Oh" appends="YES" id="4Pc-ah-evn"/>
29+
<outletCollection property="gestureRecognizers" destination="Ffh-dz-TJa" appends="YES" id="0RA-XU-Zy8"/>
30+
</connections>
31+
</view>
6532
</subviews>
6633
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
6734
<constraints>
68-
<constraint firstAttribute="trailing" secondItem="35L-pI-qif" secondAttribute="trailing" id="cqX-rf-XVA"/>
69-
<constraint firstItem="35L-pI-qif" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="dp3-eE-5tp"/>
70-
<constraint firstItem="35L-pI-qif" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="jzy-J2-TIu"/>
71-
<constraint firstItem="35L-pI-qif" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="lhx-16-1TE"/>
35+
<constraint firstAttribute="trailingMargin" secondItem="2J6-BG-JmJ" secondAttribute="trailing" constant="-20" id="IDa-sc-3JD"/>
36+
<constraint firstItem="2J6-BG-JmJ" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="topMargin" constant="20" id="Nqp-Pf-oL9"/>
37+
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="2J6-BG-JmJ" secondAttribute="bottom" id="OWE-i7-JMs"/>
38+
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="2J6-BG-JmJ" secondAttribute="bottom" id="Oh6-IM-in2"/>
39+
<constraint firstItem="2J6-BG-JmJ" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="-20" id="bc1-gk-Vse"/>
40+
<constraint firstItem="2J6-BG-JmJ" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="topMargin" id="f7o-5w-YKW"/>
7241
</constraints>
42+
<variation key="default">
43+
<mask key="constraints">
44+
<exclude reference="f7o-5w-YKW"/>
45+
<exclude reference="Oh6-IM-in2"/>
46+
</mask>
47+
</variation>
7348
</view>
7449
<connections>
7550
<outlet property="gameFieldView" destination="2J6-BG-JmJ" id="F2g-dZ-D8U"/>
7651
</connections>
7752
</viewController>
7853
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
54+
<swipeGestureRecognizer direction="right" id="Vy2-2v-nIX">
55+
<connections>
56+
<action selector="swipeRecognizer:" destination="BYZ-38-t0r" id="6xy-WI-IcC"/>
57+
</connections>
58+
</swipeGestureRecognizer>
59+
<swipeGestureRecognizer direction="left" id="eE5-DG-6f6">
60+
<connections>
61+
<action selector="swipeRecognizer:" destination="BYZ-38-t0r" id="jl4-Xc-wMF"/>
62+
</connections>
63+
</swipeGestureRecognizer>
64+
<swipeGestureRecognizer direction="up" id="xM9-O3-0Oh">
65+
<connections>
66+
<action selector="swipeRecognizer:" destination="BYZ-38-t0r" id="JEW-Nw-iXT"/>
67+
</connections>
68+
</swipeGestureRecognizer>
69+
<swipeGestureRecognizer direction="down" id="Ffh-dz-TJa">
70+
<connections>
71+
<action selector="swipeRecognizer:" destination="BYZ-38-t0r" id="koM-Vu-EDx"/>
72+
</connections>
73+
</swipeGestureRecognizer>
7974
</objects>
8075
<point key="canvasLocation" x="341" y="364"/>
8176
</scene>

SnakeGame/GameFieldView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class SegmentView: UIView {
6969
setNeedsDisplay()
7070
}
7171
}
72-
//set needs display
7372
init(parameters: SegmentParameters) {
7473
super.init(frame: parameters.rect)
7574

@@ -83,6 +82,7 @@ class SegmentView: UIView {
8382
parameters.color().setFill()
8483
path.fill()
8584
}
85+
8686
required init?(coder aDecoder: NSCoder) {
8787
fatalError("init(coder:) has not been implemented")
8888
}

SnakeGame/GameModel.swift

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@
88

99
import UIKit
1010

11+
let side: CGFloat = UIScreen.mainScreen().bounds.width / 30
12+
1113
class GameBrain {
1214

13-
private let side: CGFloat! = 10
1415

1516
private var direction: (CGPoint)!
1617

17-
private var actions: Dictionary <String, (CGPoint)> = [
18-
"Up" : CGPointMake(0,-10),
19-
"Down" : CGPointMake(0,10),
20-
"Left" : CGPointMake(-10,0),
21-
"Right" : CGPointMake(10,0)
18+
private var movements = [
19+
"Up" : CGPointMake(0,-side),
20+
"Down" : CGPointMake(0,side),
21+
"Left" : CGPointMake(-side,0),
22+
"Right" : CGPointMake(side,0)
2223
]
2324

24-
func setDirection(buttonAction: String){
25+
func setDirection(gestureDirection: String){
2526

26-
if let action = actions[buttonAction] {
27+
if let action = movements[gestureDirection] {
2728

2829
if (direction != nil) {
2930
if !(direction.x + action.x == 0 && direction.y + action.y == 0) {
@@ -149,35 +150,42 @@ class GameBrain {
149150

150151
private func randomPoint() -> CGPoint {
151152

152-
let side32 = side.toUInt32()
153-
154-
let width32 = (fRect.width - side * 2).toUInt32()
155-
156-
let height32 = (fRect.height - side * 2).toUInt32()
157-
158-
let randomMultiX = arc4random_uniform(width32 / side32) + 1
159-
160-
let randomMultiY = arc4random_uniform(height32 / side32) + 1
161-
162-
let newX: CGFloat = CGFloat(randomMultiX) * side + fRect.origin.x
163-
let newY: CGFloat! = CGFloat(randomMultiY) * side + fRect.origin.y
153+
let newX = randomMultiplier(fRect.width, coord: fRect.origin.x)
154+
let newY = randomMultiplier(fRect.height, coord: fRect.origin.y)
164155

165156
return CGPoint(x: newX, y: newY)
166157
}
167158

168159
private func centerPoint() -> CGPoint {
169160

170-
let x = calculatedMultiplier(fRect.width) * side + fRect.origin.x
171-
let y = calculatedMultiplier(fRect.height) * side + fRect.origin.y
161+
let x = calculatedMultiplier(fRect.width, coord: fRect.origin.x)
162+
let y = calculatedMultiplier(fRect.height, coord: fRect.origin.y)
172163

173164
return CGPoint(x: x, y: y)
174165
}
175166

176-
private func calculatedMultiplier(value: CGFloat) -> CGFloat{
167+
private func randomMultiplier(value: CGFloat, coord: CGFloat) -> CGFloat {
168+
169+
let parameter = (value - side * 2).toUInt32()
170+
171+
let side32 = side.toUInt32()
172+
173+
var multiplier: UInt32!
174+
175+
multiplier = arc4random_uniform(parameter / side32)
176+
177+
if multiplier == 0 {
178+
multiplier = 1
179+
}
180+
181+
return CGFloat(multiplier) * side + coord
182+
}
183+
184+
private func calculatedMultiplier(value: CGFloat, coord: CGFloat) -> CGFloat{
177185

178186
let multiplier = value / side
179187

180-
return floor(multiplier / 2)
188+
return floor(multiplier / 2) * side + coord
181189
}
182190

183191

SnakeGame/GameViewController.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@ class GameViewController: UIViewController {
1212

1313
@IBOutlet weak var gameFieldView: GameFieldView!
1414

15-
@IBAction func buttonAction(sender: UIButton) {
16-
brain.setDirection(sender.currentTitle!)
15+
@IBAction func swipeRecognizer(sender: UISwipeGestureRecognizer) {
16+
17+
switch sender.direction {
18+
case UISwipeGestureRecognizerDirection.Right:
19+
brain.setDirection("Right")
20+
case UISwipeGestureRecognizerDirection.Down:
21+
brain.setDirection("Down")
22+
case UISwipeGestureRecognizerDirection.Left:
23+
brain.setDirection("Left")
24+
case UISwipeGestureRecognizerDirection.Up:
25+
brain.setDirection("Up")
26+
default:
27+
break
28+
}
29+
1730
}
1831

1932
private var brain: GameBrain!

SnakeGame/Notes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dead by wall
2+
death by self
3+
error random with segment on the wall
4+
drawed tail and head

0 commit comments

Comments
 (0)