gpt4 book ai didi

arrays - SourceKit 在创建 2D 字符数组时崩溃

转载 作者:行者123 更新时间:2023-11-30 13:16:52 25 4
gpt4 key购买 nike

我的“board”变量一直导致我的编译器变得疯狂,我不知道为什么。这个类没有引用我的项目中的任何其他类,我对它为什么这样做感到困惑。我的主要目标是创建一个二维字符数组,其中行和列变量定义为我的棋盘的尺寸。我已经尝试了一切,但仍然行不通。注释掉代码是我最新的尝试。

import Foundation

class Board: NSObject {

let rows: Int = 6
let columns: Int = 7
var boardRow: [Character] = [Character].init(count: 6, repeatedValue: " ")
var boardColumn: [Character] = [Character].init(count: 7, repeatedValue: " ")
//var board: [[Character]] = [boardRow][boardColumn]

// evaluation table for checking state of board ~~ not my code
var evaluationTable = [[3, 4, 5, 7, 5, 4, 3], [4, 6, 8, 10, 8, 6, 4], [5, 8, 11, 13, 11, 8, 5], [5, 8, 11, 13, 11, 8, 5], [4, 6, 8, 10, 8, 6, 4], [3, 4, 5, 7, 5, 4, 3]]

func Board() {
createBoard()
displayContent()
}

func createBoard() {

for i in 0..<rows {
for j in 0..<columns {
board[i][j] = " "
}
}
}

func displayContent() {
print(" 1 2 3 4 5 6 7")
for j in 0..<rows {
for k in 0..<columns {
print("[" + board[j][k] + "]")
}
print()
}

print("---------------------------------------------------")
}

func insert(column: Int, protagonist: Character) -> Bool {
if (column > 6 || column < 0 || board[0][column] != " ") {
return false
}

else {
for (var i = rows-1; i >= 0; i -= 1) {
if (board[i][column] == " ") {
board[i][column] = protagonist;
break
}
}
return true
}
}

func remove(column: Int) {
for i in 0..<rows {
if (board[i][column] != " ") {
board[i][column] = " "
break
}
}
}

func areWeDone() -> Character {
//check for win horizontally
for (var row = 0; row < rows; row += 1) {
for (var col=0; col<columns-3; col += 1) {
if (board[row][col] != " " && board[row][col] == board[row][col+1] && board[row][col] == board[row][col+2] && board[row][col] == board[row][col+3]) {
return board[row][col]
}
}
}

// My up and down checker
for (var row = 0; row < rows-3; row++) {
for (var col = 0; col < columns; col++) {
if (board[row][col] != " " &&
board[row][col] == board[row+1][col] &&
board[row][col] == board[row+2][col] &&
board[row][col] == board[row+3][col]) {
return Character(board[row][col])
//return board[row][col]
}
}
}

// My diagonal checker
for (var row = 0; row < rows-3; row += 1) {
for (var col = 0; col < columns-3; col++) {
if (board[row][col] != " " &&
board[row][col] == board[row+1][col+1] &&
board[row][col] == board[row+2][col+2] &&
board[row][col] == board[row+3][col+3]) {
return board[row][col]
//return board[row][col]
}
}
}

// My diagonal checker
for (var row = 3; row < rows; row++) {
for (var col = 0; col < columns-3; col += 1) {
if (board[row][col] != " " &&
board[row][col] == board[row-1][col+1] &&
board[row][col] == board[row-2][col+2] &&
board[row][col] == board[row-3][col+3]) {
return board[row][col]
//return Character(board[row][col])
}
}
}

return " ";
}

func isTiePresent() -> Bool {
for j in 0..<columns {
if (board[0][j] == " ") {
return false
}
}
return true
}

func isMoveAllowed(column: Int) -> Bool {
if (column > 6 || column < 0 || board[0][column] != " ") {
return false
} else {
return true
}
}

func analyze() -> Int {
let utility = 128
var sum = 0
for i in 0..<rows {
for j in 0..<columns {
if (board[i][j] == "O") {
sum += evaluationTable[i][j]
} else if (board[i][j] == "X"){
sum -= evaluationTable[i][j]
}
}
}

return utility + sum
}

}

最佳答案

//var board: [[Character]] = [boardRow][boardColumn]

我相信您已经注意到了,[boardRow][boardColumn]不是有效的语法。无论如何,这种方法最终只创建 2 个数组。

二维数组实际上是作为数组的数组来实现的。您可以在类型 [[Character]] 中看到这一点意味着Array<Array<Character>> .

假设您有 6 行和 7 行。您首先需要创建一个数组来保存子数组(行)。每个数组(行)应初始化为值

var board = [[Character]](count: 6, repeatedValue: [Character](count: 7, repeatedValue: " "))

注意:我很可能混淆了“行”和“列”。您可能需要尝试一下。

关于arrays - SourceKit 在创建 2D 字符数组时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38128562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com