在iOS游戏开发中,比如2048游戏。有时会需要存储N×N数组的数据模型(如3×3,4×4等)。这里我们演示了三种实现方式,分别是:一维数组、仿二维数组、自定义二维数组(即矩阵结构)。
功能是根据传入维度初始化数组,同时提供设置值和打印输出所有值的功能,判断数组是否已满(全不为0),以及目前空位的坐标集。
1,使用一维数组实现
import Foundation
class GameModel {
var dimension:Int = 0
var tiles:Array
init(dimension:Int) {
self.dimension = dimension
self.tiles = Array(repeating:0, count:self.dimension*self.dimension)
}
//找出空位置
func emptyPositions()-> [Int] {
var emptytiles = Array()
for i in 0..
{
if(tiles[i] == 0)
{
emptytiles.append(i)
}
}
return emptytiles
}
//位置是否已满
func isFull()-> Bool {
if(emptyPositions().count == 0)
{
return true
}
return false
}
//输出当前数据模型
func printTiles() {
print(tiles)
print("输出数据模型数据")
let count = tiles.count
for i in 0 ..< count
{
if (i+1) % Int(dimension) == 0
{
print(tiles[i])
}
else
{
print("\(tiles[i])\t")
}
}
print("")
}
//如果返回 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
let index = self.dimension * row + col
let val = tiles[index]
if(val > 0)
{
print("该位置(\(row), \(col))已经有值了")
return false
}
tiles[index] = value
return true
}
}
2,使用二维数组实现
import Foundation
class GameModelBA {
var dimension:Int = 0
var tiles:Array>
//由外部来传入维度值
init(dimension:Int) {
self.dimension = dimension
self.tiles = Array(repeating:Array(repeating:0, count:self.dimension),
count:self.dimension)
}
//找出空位置
func emptyPositions()-> [Int] {
var emptytiles = Array()
//var index:Int
for row in 0..
{
for col in 0..
{
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)
let val = tiles[row][col]
if(val > 0)
{
print("该位置(\(row), \(col))已经有值了")
return false
}
printTiles()
//tiles[row][col] = value
var rdata = Array(repeating:0, count:self.dimension)
for i in 0..
{
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(){
print(tiles)
print("输出数据模型数据")
for row in 0..
{
for col in 0..
{
print("\(tiles[row][col])\t")
}
print("")
}
print("")
}
}
3,使用自定义二维数组(即矩阵结构)
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(repeating: 0, count: rows * columns)
}
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: row, column: column), "超出范围")
return grid[(row * columns) + column]
}
set {
assert(indexIsValidForRow(row: 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()
//var index:Int
for row in 0..
{
for col in 0..
{
let 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)
let val = tiles[row,col]
if(val > 0) {
print("该位置(\(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() {
print(tiles)
print("输出数据模型数据")
for row in 0..
for col in 0..
print("\(tiles[row, col])\t")
}
print("")
}
print("")
}
}