在iOS游戏开发中,比如2048游戏。有时会需要存储N×N数组的数据模型(如3×3,4×4等)。这里我们演示了三种实现方式,分别是:一维数组、仿二维数组、自定义二维数组(即矩阵结构)。
功能是根据传入维度初始化数组,同时提供设置值和打印输出所有值的功能,判断数组是否已满(全不为0),以及目前空位的坐标集。
1,使用一维数组实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import Foundation class GameModel { var dimension: Int = 0 var tiles: Array < Int > init (dimension: Int ) { self .dimension = dimension self .tiles = Array < Int >(count: self .dimension* self .dimension, repeatedValue:0) } //找出空位置 func emptyPositions()-> [ Int ] { var emptytiles = Array < Int >() //var index:Int for i in 0..<(dimension*dimension) { if (tiles[i] == 0) { emptytiles.append(i) } } return emptytiles } //位置是否已满 func isFull()-> Bool { if (emptyPositions().count == 0) { return true } return false } //输出当前数据模型 func printTiles() { println (tiles) println ( "输出数据模型数据" ) var count = tiles.count for var i=0; i<count; i++ { if (i+1) % Int (dimension) == 0 { println (tiles[i]) } else { print ( "(tiles[i]) " ) } } println ( "" ) } //如果返回 false ,表示该位置 已经有值 func setPosition(row: Int , col: Int , value: Int ) -> Bool { assert(row >= 0 && row < dimension) assert(col >= 0 && col < dimension) //3行4列,即 row=2 , col=3 index=2*4+3 = 11 //4行4列,即 3*4+3 = 15 var index = self .dimension * row + col var val = tiles[index] if (val > 0) { println ( "该位置((row), (col))已经有值了" ) return false } tiles[index] = value return true } } |
2,使用二维数组实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
import Foundation class GameModelBA { var dimension: Int = 0 var tiles: Array < Array < Int >> //由外部来传入维度值 init (dimension: Int ) { self .dimension = dimension self .tiles = Array (count: self .dimension, repeatedValue: Array (count: self .dimension, repeatedValue:0)) } //找出空位置 func emptyPositions()-> [ Int ] { var emptytiles = Array < Int >() //var index:Int for row in 0..< self .dimension { for col in 0..< self .dimension { if (tiles[row][col] == 0) { emptytiles.append(tiles[row][col]) } } } return emptytiles } //如果返回 false ,表示该位置 已经有值 func setPosition(row: Int , col: Int , value: Int ) -> Bool { assert(row >= 0 && row < dimension) assert(col >= 0 && col < dimension) var val = tiles[row][col] if (val > 0) { println ( "该位置((row), (col))已经有值了" ) return false } printTiles() //tiles[row][col] = value var rdata = Array (count: self .dimension, repeatedValue:0) for i in 0..< self .dimension { rdata[i] = tiles[row][i] } rdata[col] = value tiles[row] = rdata return true } //位置是否已满 func isFull()-> Bool { if (emptyPositions().count == 0) { return true } return false } //输出当前数据模型 func printTiles() { println (tiles) println ( "输出数据模型数据" ) var count = tiles.count for row in 0..< self .dimension { for col in 0..< self .dimension { print ( "(tiles[row][col]) " ) } println ( "" ) } println ( "" ) } } |
3,使用自定义二维数组(即矩阵结构)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
import Foundation //自定义矩阵数据结构 struct Matrix { let rows: Int , columns: Int var grid: [ Int ] init (rows: Int , columns: Int ) { self .rows = rows self .columns = columns grid = Array (count: rows * columns, repeatedValue: 0) } func indexIsValidForRow(row: Int , column: Int ) -> Bool { return row >= 0 && row < rows && column >= 0 && column < columns } subscript(row: Int , column: Int ) -> Int { get { assert(indexIsValidForRow(row, column: column), "超出范围" ) return grid[(row * columns) + column] } set { assert(indexIsValidForRow(row, column: column), "超出范围" ) grid[(row * columns) + column] = newValue } } } class GameModelMatrix { var dimension: Int = 0 var tiles: Matrix //由外部来传入维度值 init (dimension: Int ) { self .dimension = dimension self .tiles = Matrix (rows: self .dimension, columns: self .dimension) } //找出空位置 func emptyPositions()-> [ Int ] { var emptytiles = Array < Int >() //var index:Int for row in 0..< self .dimension { for col in 0..< self .dimension { var val = tiles[row,col] if (val == 0) { emptytiles.append(tiles[row, col]) } } } return emptytiles } //如果返回 false ,表示该位置已经有值 func setPosition(row: Int , col: Int , value: Int ) -> Bool { assert(row >= 0 && row < dimension) assert(col >= 0 && col < dimension) var val = tiles[row,col] if (val > 0) { println ( "该位置((row), (col))已经有值了" ) return false } printTiles() tiles[row, col] = value printTiles() return true } //位置是否已满 func isFull()-> Bool { if (emptyPositions().count == 0) { return true } return false } //输出当前数据模型 func printTiles() { println (tiles) println ( "输出数据模型数据" ) for row in 0..< self .dimension { for col in 0..< self .dimension { print ( "(tiles[row, col]) " ) } println ( "" ) } println ( "" ) } } |