@@ -10,6 +10,8 @@ import UIKit
1010
1111class GameBrain {
1212
13+ private let side : CGFloat ! = 10
14+
1315 private var actions : Dictionary < String , ( CGPoint ) > = [
1416 " Up " : CGPointMake ( 0 , - 10 ) ,
1517 " Down " : CGPointMake ( 0 , 10 ) ,
@@ -33,13 +35,22 @@ class GameBrain {
3335
3436 private var headPoint : CGPoint ! = CGPointZero
3537
36- private var backViewSize : CGSize !
38+ private var screen : CGSize !
39+
40+ private var insets : CGPoint ! {
41+ get {
42+ let horizontal = ( screen. width % side) / 2
43+
44+ let vertical = ( screen. height % side) / 2
45+
46+ return CGPoint ( x: horizontal, y: vertical)
47+ }
48+ }
3749
3850 private var bordersDictonary : [ String : CGRect ] !
3951
4052 var borders : [ CGRect ] ! {
4153 get {
42-
4354 var bordersArray : [ CGRect ] = [ ]
4455
4556 for border in bordersDictonary. values {
@@ -53,48 +64,50 @@ class GameBrain {
5364
5465 private func createBorders( ) {
5566
56- let screen = backViewSize
57-
58- let borderWidth : CGFloat = 10
67+ let width = screen. width - insets. x * 2
5968
60- let leftBorder = CGRect ( x: 0 , y: 0 , width: borderWidth, height: screen. height)
69+ let height = screen. height - insets. y * 2
70+
71+ let leftBorder = CGRect ( x: insets. x, y: insets. y, width: side, height: height)
6172
6273 bordersDictonary [ " Left " ] = leftBorder
6374
64- let rightBorder = CGRect ( x: screen. width - borderWidth , y: 0 , width: borderWidth , height: screen . height)
75+ let rightBorder = CGRect ( x: screen. width - insets . x - side , y: insets . y , width: side , height: height)
6576
6677 bordersDictonary [ " Right " ] = rightBorder
6778
68- let horBorderWidth = screen. width - borderWidth * 2
69-
70- let topBorder = CGRect ( x: borderWidth, y: 0 , width: horBorderWidth, height: borderWidth)
79+ let topBorder = CGRect ( x: side + insets. x, y: insets. y, width: width, height: side)
7180
7281 bordersDictonary [ " Top " ] = topBorder
7382
74- let botBorder = CGRect ( x: borderWidth , y: screen. height - borderWidth , width: horBorderWidth , height: borderWidth )
83+ let botBorder = CGRect ( x: side + insets . x , y: screen. height - insets . y - side , width: width , height: side )
7584
76- bordersDictonary [ " Bot " ] = botBorder
77-
78- print ( bordersDictonary)
85+ bordersDictonary [ " Bot " ] = botBorder
7986 }
8087
8188 var segments : [ GameSegment ] !
8289
8390 private func createRandomSegment( ) {
8491
85- // let randomMultiplierX = arc4random_uniform(screenWidth/10 - offsetX)
86- //
87- // let randomMultiplierY = arc4random_uniform(screenWidth/10 - offsetY)
88- //
89- // let newX: CGFloat = CGFloat(randomMultiplierX * step)
90- // let newY: CGFloat! = CGFloat(randomMultiplierY * step)
91- //
92- // let newPoint = CGPoint(x: newX, y: newY)
93- //
94- // let newSegment = GameSegment(point: newPoint, isEaten: false)
95- // segments.append(newSegment)
96- //
97- // print(newPoint)
92+ let side32 = side. convertToUInt32 ( )
93+
94+ let width32 = screen. width. convertToUInt32 ( )
95+
96+ let height32 = screen. height. convertToUInt32 ( )
97+
98+ let randomMultiX = arc4random_uniform ( width32 / side32 - side32)
99+
100+ let randomMultiY = arc4random_uniform ( height32 / side32 - side32)
101+
102+ let newX : CGFloat = CGFloat ( randomMultiX) * side
103+ let newY : CGFloat ! = CGFloat ( randomMultiY) * side
104+
105+ let newPoint = CGPoint ( x: newX, y: newY)
106+
107+ let newSegment = GameSegment ( point: newPoint, isEaten: false , side: side)
108+ segments. append ( newSegment)
109+
110+ print ( newPoint)
98111 }
99112
100113 private var direction : ( CGPoint ) !
@@ -103,7 +116,7 @@ class GameBrain {
103116
104117 //prepareView
105118 bordersDictonary = [ : ]
106- backViewSize = viewSize
119+ screen = viewSize
107120 createBorders ( )
108121
109122 //prepare segments
@@ -116,7 +129,7 @@ class GameBrain {
116129 setDirection ( " Up " )
117130
118131 //creating the head
119- let headSegment = GameSegment ( point: headPoint, isEaten: true )
132+ let headSegment = GameSegment ( point: headPoint, isEaten: true , side : side )
120133 segments. append ( headSegment)
121134
122135 //create segment to eat
@@ -136,7 +149,7 @@ class GameBrain {
136149 let headSegment = segments [ 0 ]
137150 headSegment. point = headPoint
138151
139- // checkFood()
152+ checkFood ( )
140153 }
141154
142155 private func checkFood( ) {
@@ -150,4 +163,11 @@ class GameBrain {
150163 print ( " Eaten " )
151164 }
152165 }
166+ }
167+
168+ extension CGFloat {
169+
170+ func convertToUInt32( ) -> UInt32 {
171+ return UInt32 ( self )
172+ }
153173}
0 commit comments