在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])\t")
            }
        }
        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])\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
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])\t")
            }
            println("")
        }
        println("")
    }
}

Swift - 几种使用数组的数据存储模型的更多相关文章

  1. Swift - 使用NSUserDefaults来进行本地数据存储

    NSUserDefaults适合存储轻量级的本地客户端数据,比如记住密码功能,要保存一个系统的用户名.密码.使用NSUserDefaults是首选.下次再登陆的时候就可以直接从NSUserDefaul ...

  2. Android核心技术Intent和数据存储篇

    女孩:上海站到了? 男孩:嗯呢?走向世界~ 女孩:Intent核心技术和数据存储技术? 男孩:对,今日就讲这个~ Intent是各个组件之间用来进行通信的,Intent的翻译为"意图&quo ...

  3. Android开发工程师文集-提示框,菜单,数据存储,组件篇

    提示框,菜单,数据存储,组件篇 Toast Toast.makeText(context, text, 时间).show(); setDuration();//设置时间 setGravity();// ...

  4. Android之ListView,AsyncTask,GridView,CardView,本地数据存储,SQLite数据库

    版权声明:未经博主允许不得转载 补充 补充上一节,使用ListView是用来显示列表项的,使用ListView需要两个xml文件,一个是列表布局,一个是单个列表项的布局.如我们要在要显示系统所有app ...

  5. 数据存储与访问之——SharedPreferences

    使用SharedPreferences(保存用户偏好参数)保存数据, 当我们的应用想要保存用户的一些偏好参数,比如是否自动登陆,是否记住账号密码,是否在Wifi下才能 联网等相关信息,如果使用数据库的 ...

  6. Android 数据存储五种方式

    1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...

  7. iOS开发 - OC - 实现本地数据存储的几种方式一

    iOS常用的存储方式介绍 在iOS App开发过程中经常需要操作一些需要持续性保留的数据,比如用户对于App的相关设置.需要在本地缓存的数据等等.本文针对OC中经常使用的一下存储方式做了个整理. 常用 ...

  8. Android数据存储五种方式总结

    本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用Cont ...

  9. android 数据存储的几种方式

    总体的来讲,数据存储方式有三种:一个是文件,一个是数据库,另一个则是网络.其中文件和数据库可能用的稍多一些,文件用起来较为方便,程序可以自己定义格式:数据库用起稍烦锁一些,但它有它的优点,比如在海量数 ...

随机推荐

  1. 集合判断null

    Java 引用和指针差不多,null 引用 相当于 C++的空指针. isEmpty() 用于判断List内容是否为空,即表里一个元素也没有, 但是必须在 List<MallNews> g ...

  2. asp.net jquery+ajax异步刷新1

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. VC++界面编程之--使用分层窗口实现界面皮肤

    使用分层界面来实现界面皮肤的好处是:可以保证图片边缘处理不失真,且能用于异形窗口上,如一些不规则的窗口,你很难用SetWindowRgn来达到理想效果. 在很多情况下,界面的漂亮与否,取决于PS的制作 ...

  4. IntelliJ idea 14 集成 tomcat 7

    来到公司第一个任务就是先把web项目跑起来,所以如何在IntelliJ idea14中把Tomcat集成起来也是个不小的问题. 首先确认你的idea 14 不是 Communit Edition 社区 ...

  5. 解决struts2中UI标签出现的问题: The Struts dispatcher cannot be found

    解决struts2中UI标签出现的问题: The Struts dispatcher cannot be found 异常信息: The Struts dispatcher cannot be fou ...

  6. HDU2191:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)

    Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品 ...

  7. 以前学习cisco ccna 课程的时候做的笔记

    由于学习的专业是计算机网络技术,可是在上学的时候,并没有学习到多少网络知识,所以为了对得起学的专业,在06年工作的时候,在哈工大银河教育报了个ccna的班,两个星期的课程,每天上那么几个小时. 结果依 ...

  8. jz2440烧写开发板uboot,内核和文件系统等的相关命令

    下载文件{ftpget -u 1 -p 1 192.168.2.110 a.out a.outnfs 30000000(destination) 192.168.2.109:/home/fs/work ...

  9. Selenium Grid跨浏览器-兼容性测试

    Selenium Grid跨浏览器-兼容性测试 这里有两台机子,打算这样演示: 一台机子启动一个作为主点节的hub 和 一个作为次节点的hub(系统windows 浏览器为ie) ip为:192.16 ...

  10. c++实现查询天气预报

    原地址:http://blog.csdn.net/x_iya/article/details/8583015 用到的函数.API等 1.中央气象台API返回的JSON数据(http://m.weath ...