这是我在玩的游戏的屏幕截图:
https://i.stack.imgur.com/xYq4k.jpg
我有这个基于随机等级的游戏项目,您在其中添加相邻的图块以尝试达到给定的目标编号。为了计算给定级别的难度,我创建了一个自定义类,该类将预设模式加在一起并将其记录下来,以查看级别的难度,如果级别不适合所选的难度,则会尝试新的数字。问题是我感觉效率很低,偶尔会看到崩溃。
这是计算文件:
import UIKit
import Foundation
class calce {
var cords : [String: Int] = [
"x1y1" : Int(),
"x2y1" : Int(),
"x3y1" : Int(),
"x1y2" : Int(),
"x2y2" : Int(),
"x3y2" : Int(),
"x1y3" : Int(),
"x2y3" : Int(),
"x3y3" : Int()
]
var dif = 3
var goalnumber = 1
var done = false
var final = Array<Any>()
var firstroundarray = Array<Int>()
var firstroundwins = Array<Int>()
var secondroundarray = Array<Int>()
var secondroundwins = Array<Int>()
var thircroundarray = Array<Int>()
var thirdroundwins = Array<Int>()
var fourthcroundarray = Array<Int>()
var fourthroundwins = Array<Int>()
func calcs(_ difs : Int){
dif = difs
doe()
}
func doe(){
cords["x1y1"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x2y1"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x3y1"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x1y2"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x2y2"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x3y2"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x1y3"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x2y3"]! = Int(arc4random_uniform(UInt32(11)))+1
cords["x3y3"]! = Int(arc4random_uniform(UInt32(11)))+1
print(goalnumber)
print("Cords: \(cords)")
mainfunc()
}
func mainfunc(){
goalnumber = goalnumber + 1
addneighbors("x1y1")
addneighbors("x2y1")
addneighbors("x3y1")
addneighbors("x1y2")
addneighbors("x2y2")
addneighbors("x3y2")
addneighbors("x1y3")
addneighbors("x2y3")
addneighbors("x3y3")
secondround("x1y1")
secondround("x2y1")
secondround("x3y1")
secondround("x1y2")
secondround("x2y2")
secondround("x3y2")
secondround("x1y3")
secondround("x2y3")
secondround("x3y3")
thirdround("x1y1")
thirdround("x2y1")
thirdround("x3y1")
thirdround("x1y2")
thirdround("x2y2")
thirdround("x3y2")
thirdround("x1y3")
thirdround("x2y3")
thirdround("x3y3")
var dd = cords["x\(1)y\(1)"]! + cords["x\(2)y\(1)"]! + cords["x\(3)y\(1)"]! + cords["x\(1)y\(2)"]! + cords["x\(1)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(1)"]! + cords["x\(2)y\(1)"]! + cords["x\(3)y\(1)"]! + cords["x\(3)y\(2)"]! + cords["x\(3)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(3)y\(1)"]! + cords["x\(3)y\(2)"]! + cords["x\(3)y\(3)"]! + cords["x\(2)y\(3)"]! + cords["x\(1)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(1)"]! + cords["x\(1)y\(2)"]! + cords["x\(1)y\(3)"]! + cords["x\(2)y\(3)"]! + cords["x\(3)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(2)y\(1)"]! + cords["x\(2)y\(2)"]! + cords["x\(2)y\(3)"]! + cords["x\(1)y\(2)"]! + cords["x\(3)y\(2)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(1)"]! + cords["x\(1)y\(2)"]! + cords["x\(2)y\(2)"]! + cords["x\(2)y\(3)"]! + cords["x\(3)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(1)"]! + cords["x\(2)y\(1)"]! + cords["x\(2)y\(2)"]! + cords["x\(3)y\(2)"]! + cords["x\(3)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(3)"]! + cords["x\(2)y\(3)"]! + cords["x\(2)y\(2)"]! + cords["x\(3)y\(2)"]! + cords["x\(3)y\(1)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(3)"]! + cords["x\(1)y\(2)"]! + cords["x\(2)y\(2)"]! + cords["x\(2)y\(1)"]! + cords["x\(3)y\(1)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(1)"]! + cords["x\(2)y\(1)"]! + cords["x\(2)y\(2)"]! + cords["x\(2)y\(3)"]! + cords["x\(3)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(3)y\(1)"]! + cords["x\(2)y\(1)"]! + cords["x\(2)y\(2)"]! + cords["x\(2)y\(3)"]! + cords["x\(1)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(1)"]! + cords["x\(1)y\(2)"]! + cords["x\(2)y\(2)"]! + cords["x\(3)y\(2)"]! + cords["x\(3)y\(3)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
dd = cords["x\(1)y\(3)"]! + cords["x\(1)y\(2)"]! + cords["x\(2)y\(2)"]! + cords["x\(2)y\(1)"]! + cords["x\(3)y\(1)"]!
fourthcroundarray.append(dd)
if dd == goalnumber{fourthroundwins.append(dd)}
fourthround("x1y1")
fourthround("x2y1")
fourthround("x3y1")
fourthround("x1y2")
fourthround("x2y2")
fourthround("x3y2")
fourthround("x1y3")
fourthround("x2y3")
fourthround("x3y3")
end()
}
func end(){
if done == true{
return;
}
print(" ")
print("\(goalnumber)")
var secondnew = 0
var thirdnew = 0
var fourthnew = 0
secondnew = secondroundwins.count * 5
thirdnew = thirdroundwins.count * 3
fourthnew = fourthroundwins.count
if goalnumber == 44{
goalnumber = 0
firstroundarray.removeAll()
firstroundwins.removeAll()
secondroundarray.removeAll()
secondroundwins.removeAll()
thircroundarray.removeAll()
thirdroundwins.removeAll()
fourthcroundarray.removeAll()
fourthroundwins.removeAll()
doe()
}
if firstroundwins.count > 0 && done == false {
print("Is It")
firstroundarray.removeAll()
firstroundwins.removeAll()
secondroundarray.removeAll()
secondroundwins.removeAll()
thircroundarray.removeAll()
thirdroundwins.removeAll()
fourthcroundarray.removeAll()
fourthroundwins.removeAll()
mainfunc()}
var total = secondnew + thirdnew + fourthnew
if done == false{
print("First: \(firstroundwins.count)" )
print("Second: \(secondroundwins.count)")
print("Third:\(thirdroundwins.count)")
print("Fourth: \(fourthnew)")
print("Total: \(total)")
}
if dif == 1 && goalnumber != 44 && done == false{
if total >= 14 && total <= 35 {
done = true
print("We Be Done")
check()
UserDefaults.standard.set([cords["x1y1"]! , cords["x2y1"]! , cords["x3y1"]! , cords["x1y2"]! , cords["x2y2"]! , cords["x3y2"]! , cords["x1y3"]! , cords["x2y3"]! , cords["x3y3"]! , goalnumber], forKey: "Calc")
return;
}else{
firstroundarray.removeAll()
firstroundwins.removeAll()
secondroundarray.removeAll()
secondroundwins.removeAll()
thircroundarray.removeAll()
thirdroundwins.removeAll()
fourthcroundarray.removeAll()
fourthroundwins.removeAll()
mainfunc()}}
if dif == 2 && goalnumber != 44 && done == false{
if total >= 8 && total < 14 {
print("We Be Done")
done = true
check()
UserDefaults.standard.set([cords["x1y1"]! , cords["x2y1"]! , cords["x3y1"]! , cords["x1y2"]! , cords["x2y2"]! , cords["x3y2"]! , cords["x1y3"]! , cords["x2y3"]! , cords["x3y3"]! , goalnumber], forKey: "Calc")
return;
}else{
firstroundarray.removeAll()
firstroundwins.removeAll()
secondroundarray.removeAll()
secondroundwins.removeAll()
thircroundarray.removeAll()
thirdroundwins.removeAll()
fourthcroundarray.removeAll()
fourthroundwins.removeAll()
mainfunc()}}
if dif == 3 && goalnumber != 44 && done == false{
if total >= 3 && total < 8 && secondroundwins.count == 0{
print("We Be Done")
done = true
check()
UserDefaults.standard.set([cords["x1y1"]! , cords["x2y1"]! , cords["x3y1"]! , cords["x1y2"]! , cords["x2y2"]! , cords["x3y2"]! , cords["x1y3"]! , cords["x2y3"]! , cords["x3y3"]! , goalnumber], forKey: "Calc")
return;
}else{
firstroundarray.removeAll()
firstroundwins.removeAll()
secondroundarray.removeAll()
secondroundwins.removeAll()
thircroundarray.removeAll()
thirdroundwins.removeAll()
fourthcroundarray.removeAll()
fourthroundwins.removeAll()
mainfunc()}}
}
func addneighbors(_ cordname : String){
var xval = 0
var yval = 0
let ycord = Array(cordname)[3]
let xcord = Array(cordname)[1]
if xcord == "1"{xval = 1}
if xcord == "2"{xval = 2}
if xcord == "3"{xval = 3}
if ycord == "1"{yval = 1}
if ycord == "2"{yval = 2}
if ycord == "3"{ yval = 3}
if xval >= 2{
let value = cords["x\(xval)y\(yval)"]! + cords["x\(xval - 1)y\(yval)"]!
firstroundarray.append(value)
if value == goalnumber{firstroundwins.append(value)}}
if xval <= 2{
let value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]!
firstroundarray.append(value)
if value == goalnumber{firstroundwins.append(value)}}
if yval >= 2{
let value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval - 1)"]!
firstroundarray.append(value)
if value == goalnumber{firstroundwins.append(value)}}
if yval <= 2{
let value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]!
firstroundarray.append(value)
if value == goalnumber{firstroundwins.append(value)}}}
func secondround(_ cord : String){
var xval = 0
var yval = 0
let ycord = Array(cord)[3]
let xcord = Array(cord)[1]
if xcord == "1"{xval = 1}
if xcord == "2"{xval = 2}
if xcord == "3"{xval = 3}
if ycord == "1"{yval = 1}
if ycord == "2"{yval = 2}
if ycord == "3"{yval = 3}
if xval == 1{
var value = (cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 2)y\(yval)"]!)
secondroundarray.append(value)
if value == goalnumber{secondroundwins.append(value)}}
if yval == 1{
var value = (cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval)y\(yval + 2)"]!)
secondroundarray.append(value)
if value == goalnumber{secondroundwins.append(value)}}
if yval <= 2{
if xval <= 2{
var value = (cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1 )y\(yval)"]! + cords["x\(xval + 1)y\(yval + 1)"]!)
secondroundarray.append(value)
if value == goalnumber{secondroundwins.append(value)}
value = (cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1 )y\(yval + 1)"]! + cords["x\(xval)y\(yval + 1)"]!)
secondroundarray.append(value)
if value == goalnumber{secondroundwins.append(value)}}
if xval >= 2{
var value = (cords["x\(xval)y\(yval)"]! + cords["x\(xval - 1 )y\(yval)"]! + cords["x\(xval - 1)y\(yval + 1)"]!)
secondroundarray.append(value)
if value == goalnumber{secondroundwins.append(value)}
value = (cords["x\(xval)y\(yval)"]! + cords["x\(xval - 1 )y\(yval + 1)"]! + cords["x\(xval)y\(yval + 1)"]!)
secondroundarray.append(value)
if value == goalnumber{secondroundwins.append(value)}}}}
func thirdround(_ cord : String){
var xval = 0
var yval = 0
let ycord = Array(cord)[3]
let xcord = Array(cord)[1]
if xcord == "1"{xval = 1}
if xcord == "2"{xval = 2}
if xcord == "3"{xval = 3}
if ycord == "1"{yval = 1}
if ycord == "2"{yval = 2}
if ycord == "3"{yval = 3}
if xval <= 2 && yval <= 2{
var value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1 )y\(yval)"]! + cords["x\(xval + 1)y\(yval + 1)"]! + cords["x\(xval)y\(yval + 1)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}}
if yval == 1 && xval <= 2{
var value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1 )y\(yval)"]! + cords["x\(xval + 1)y\(yval + 1)"]! + cords["x\(xval + 1)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval)y\(yval + 2)"]! + cords["x\(xval + 1)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval + 1)y\(yval + 1)"]! + cords["x\(xval + 1)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval + 1)y\(yval + 1)"]! + cords["x\(xval)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}}
if yval == 1 && xval >= 2{
var value = cords["x\(xval)y\(yval)"]! + cords["x\(xval - 1 )y\(yval)"]! + cords["x\(xval - 1)y\(yval + 1)"]! + cords["x\(xval - 1)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval)y\(yval + 2)"]! + cords["x\(xval - 1)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval - 1)y\(yval + 1)"]! + cords["x\(xval - 1)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval - 1)y\(yval + 1)"]! + cords["x\(xval)y\(yval + 2)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}}
if xval == 1 && yval <= 2{
var value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval + 1)y\(yval + 1)"]! + cords["x\(xval + 2)y\(yval + 1)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 2)y\(yval)"]! + cords["x\(xval + 2)y\(yval + 1)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 1)y\(yval + 1)"]! + cords["x\(xval + 2)y\(yval + 1)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 1)y\(yval + 1)"]! + cords["x\(xval + 2)y\(yval)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)
}}
if xval == 1 && yval >= 2{
var value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval - 1)"]! + cords["x\(xval + 1)y\(yval - 1)"]! + cords["x\(xval + 2)y\(yval - 1)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 2)y\(yval)"]! + cords["x\(xval + 2)y\(yval - 1)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 1)y\(yval - 1)"]! + cords["x\(xval + 2)y\(yval - 1)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 1)y\(yval - 1)"]! + cords["x\(xval + 2)y\(yval)"]!
thircroundarray.append(value)
if value == goalnumber{
thirdroundwins.append(value)}}}
func fourthround(_ cord: String){
var xval = 0
var yval = 0
let ycord = Array(cord)[3]
let xcord = Array(cord)[1]
if xcord == "1"{xval = 1}
if xcord == "2"{xval = 2}
if xcord == "3"{xval = 3}
if ycord == "1"{yval = 1}
if ycord == "2"{yval = 2}
if ycord == "3"{yval = 3}
if xval <= 2 && yval == 1{
var value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval)y\(yval+2)"]! + cords["x\(xval+1)y\(yval+2)"]! + cords["x\(xval+1)y\(yval+1)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}
value = cords["x\(xval+1)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval)y\(yval+2)"]! + cords["x\(xval+1)y\(yval+2)"]! + cords["x\(xval+1)y\(yval+1)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval+1)y\(yval+1)"]! + cords["x\(xval+1)y\(yval)"]! + cords["x\(xval+1)y\(yval+2)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval+1)y\(yval+1)"]! + cords["x\(xval+1)y\(yval)"]! + cords["x\(xval)y\(yval+2)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}
}
if yval <= 2 && xval == 1{
var value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 2)y\(yval)"]! + cords["x\(xval+2)y\(yval+1)"]! + cords["x\(xval+1)y\(yval+1)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}
value = cords["x\(xval)y\(yval+1)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval + 2)y\(yval)"]! + cords["x\(xval+2)y\(yval+1)"]! + cords["x\(xval+1)y\(yval+1)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval + 1)y\(yval)"]! + cords["x\(xval+1)y\(yval+1)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval+2)y\(yval+1)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}
value = cords["x\(xval)y\(yval)"]! + cords["x\(xval)y\(yval + 1)"]! + cords["x\(xval+1)y\(yval+1)"]! + cords["x\(xval+1)y\(yval)"]! + cords["x\(xval + 2)y\(yval)"]!
fourthcroundarray.append(value)
if value == goalnumber{fourthroundwins.append(value)}}}
func check(){
if cords["x1y1"] == goalnumber{cords["x1y1"] = goalnumber + 1}
if cords["x2y1"] == goalnumber{cords["x2y1"] = goalnumber + 1}
if cords["x3y1"] == goalnumber{cords["x3y1"] = goalnumber + 1}
if cords["x1y2"] == goalnumber{cords["x1y2"] = goalnumber + 1}
if cords["x2y2"] == goalnumber{cords["x2y2"] = goalnumber + 1}
if cords["x3y2"] == goalnumber{cords["x3y2"] = goalnumber + 1}
if cords["x1y3"] == goalnumber{cords["x1y3"] = goalnumber + 1}
if cords["x2y3"] == goalnumber{cords["x2y3"] = goalnumber + 1}
if cords["x3y3"] == goalnumber{cords["x3y3"] = goalnumber + 1}
}
}
如您所见,它非常糟糕,但是让我有些懈怠,我是个白痴。
无论如何,关于如何修复我的功能/创建新功能的任何想法都很棒!