Swift - 几种使用数组的数据存储模型?

在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

importFoundation

classGameModel

{

vardimension:Int= 0

vartiles:Array<Int>

init(dimension:Int)

{

self.dimension = dimension

self.tiles =Array<Int>(count:self.dimension*self.dimension, repeatedValue:0)

}

//找出空位置

funcemptyPositions()-> [Int]

{

varemptytiles =Array<Int>()

//var index:Int

foriin0..<(dimension*dimension)

{

if(tiles[i] == 0)

{

emptytiles.append(i)

}

}

returnemptytiles

}

//位置是否已满

funcisFull()->Bool

{

if(emptyPositions().count == 0)

{

returntrue

}

returnfalse

}

//输出当前数据模型

funcprintTiles()

{

println(tiles)

println("输出数据模型数据")

varcount = tiles.count

forvari=0; i<count; i++

{

if(i+1) %Int(dimension) == 0

{

println(tiles[i])

}

else

{

print("\(tiles[i])\t")

}

}

println("")

}

//如果返回 false ,表示该位置 已经有值

funcsetPosition(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

varindex =self.dimension * row + col

varval = tiles[index]

if(val > 0)

{

println("该位置(\(row), \(col))已经有值了")

returnfalse

}

tiles[index] = value

returntrue

}

}

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

importFoundation

classGameModelBA

{

vardimension:Int= 0

vartiles:Array<Array<Int>>

//由外部来传入维度值

init(dimension:Int)

{

self.dimension = dimension

self.tiles =Array(count:self.dimension,

repeatedValue:Array(count:self.dimension, repeatedValue:0))

}

//找出空位置

funcemptyPositions()-> [Int]

{

varemptytiles =Array<Int>()

//var index:Int

forrowin0..<self.dimension

{

forcolin0..<self.dimension

{

if(tiles[row][col] == 0)

{

emptytiles.append(tiles[row][col])

}

}

}

returnemptytiles

}

//如果返回 false ,表示该位置 已经有值

funcsetPosition(row:Int, col:Int, value:Int) ->Bool

{

assert(row >= 0 && row < dimension)

assert(col >= 0 && col < dimension)

varval = tiles[row][col]

if(val > 0)

{

println("该位置(\(row), \(col))已经有值了")

returnfalse

}

printTiles()

//tiles[row][col] = value

varrdata =Array(count:self.dimension, repeatedValue:0)

foriin0..<self.dimension

{

rdata[i] = tiles[row][i]

}

rdata[col] = value

tiles[row] = rdata

returntrue

}

//位置是否已满

funcisFull()->Bool

{

if(emptyPositions().count == 0)

{

returntrue

}

returnfalse

}

//输出当前数据模型

funcprintTiles()

{

println(tiles)

println("输出数据模型数据")

varcount = tiles.count

forrowin0..<self.dimension

{

forcolin0..<self.dimension

{

print("\(tiles[row][col])\t")

}

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

importFoundation

//自定义矩阵数据结构

structMatrix{

letrows:Int, columns:Int

vargrid: [Int]

init(rows:Int, columns:Int) {

self.rows = rows

self.columns = columns

grid =Array(count: rows * columns, repeatedValue: 0)

}

funcindexIsValidForRow(row:Int, column:Int) ->Bool{

returnrow >= 0 && row < rows && column >= 0 && column < columns

}

subscript(row:Int, column:Int) ->Int{

get{

assert(indexIsValidForRow(row, column: column),"超出范围")

returngrid[(row * columns) + column]

}

set{

assert(indexIsValidForRow(row, column: column),"超出范围")

grid[(row * columns) + column] = newValue

}

}

}

classGameModelMatrix

{

vardimension:Int= 0

vartiles:Matrix

//由外部来传入维度值

init(dimension:Int)

{

self.dimension = dimension

self.tiles =Matrix(rows:self.dimension, columns:self.dimension)

}

//找出空位置

funcemptyPositions()-> [Int]

{

varemptytiles =Array<Int>()

//var index:Int

forrowin0..<self.dimension

{

forcolin0..<self.dimension

{

varval = tiles[row,col]

if(val == 0)

{

emptytiles.append(tiles[row, col])

}

}

}

returnemptytiles

}

//如果返回 false ,表示该位置已经有值

funcsetPosition(row:Int, col:Int, value:Int) ->Bool

{

assert(row >= 0 && row < dimension)

assert(col >= 0 && col < dimension)

varval = tiles[row,col]

if(val > 0)

{

println("该位置(\(row), \(col))已经有值了")

returnfalse

}

printTiles()

tiles[row, col] = value

printTiles()

returntrue

}

//位置是否已满

funcisFull()->Bool

{

if(emptyPositions().count == 0)

{

returntrue

}

returnfalse

}

//输出当前数据模型

funcprintTiles()

{

println(tiles)

println("输出数据模型数据")

forrowin0..<self.dimension

{

forcolin0..<self.dimension

{

print("\(tiles[row, col])\t")

}

println("")

}

println("")

}

}