class Game {
var board: [[Int]]!
var turns: Int!
let coordinates = [1:(0,0), 2:(0,1), 3:(0,2), 4:(1,0), 5:(1,1), 6:(1,2), 7:(2,0), 8:(2,1), 9:(2,2)]
init() {
self.reset()
}
func reset() {
self.board = [[0,0,0],[0,0,0],[0,0,0]]
self.turns = 0
}
func moveByPlayer(player: Int, space: Int) {
let spaceDictionary = [1:(0,0), 2:(0,1), 3:(0,2), 4:(1,0), 5:(1,1), 6:(1,2), 7:(2,0), 8:(2,1), 9:(2,2)]
if let spaceTuple = spaceDictionary[space] {
if self.board[spaceTuple.0][spaceTuple.1] == 0 {
self.board[spaceTuple.0][spaceTuple.1] = player
}
}
print(self.board)
self.turns = self.turns + 1
}
func checkForWin(space: Int) {
let lines = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]]
for index in 0..<lines.count {
let x = self.coordinates[lines[index][0]]
let y = self.coordinates[lines[index][1]]
let z = self.coordinates[lines[index][2]]
**if(self.board[x.0][x.1] != 0 && self.board[x.0][x.1] == self.board[y.0][y.1] && self.board[x.0][x.1] == self.board[z.0][z.1]){
return self.board[x.0][x.1]**
}
}
}
}
我收到一条错误消息:
“可选类型(Int,Int)的值?未解包”
代码应该加粗但没有格式化的地方。我在之前的函数实现中有一个非常相似的“未包装”字典实现,但没有收到错误。我是学习 Swift 工作原理的新手。有人可以在这种情况下对选项的类型转换和解包进行更多教育吗?