我的"董事会"变量一直让我的编译器发疯,我不知道为什么。这个课程没有引用我项目中的任何其他课程,我对它为什么要这样做感到困惑。我的主要目标是创建一个2D字符数组,其中行和列变量在上面定义为我的电路板尺寸。我已经尝试了一切,但仍然无法工作。代码注释是我最近的尝试。
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
}
}
答案 0 :(得分:0)
// var board:[[Character]] = [boardRow] [boardColumn]
我确信您已经注意到,[boardRow][boardColumn]
不是有效的语法。无论如何,这种方法最终只创建了2个数组。
2D数组实际上是作为数组数组实现的。您可以在[[Character]]
类型中看到Array<Array<Character>>
。
假设您有6行和7行。您首先需要创建一个数组来保存子数组(行)。应将每个数组(行)初始化为值
var board = [[Character]](count: 6, repeatedValue: [Character](count: 7, repeatedValue: " "))
注意:我很可能混淆了“行”和“列”。你可能会玩弄它。
答案 1 :(得分:0)
我自由地重构了所有代码(这不是标准的SO),请在评论中提出建议。 board
以及您使用+
将String
与Character
连接在一起且不起作用的实例存在问题。
// Removed NSObject superclass as there's no use for it
class Board {
let rows: Int = 6
let columns: Int = 7
// var boardRow: [Character] = [Character].init(count: 6, repeatedValue: " ") // rowArray, just 6 characters
// var boardColumn: [Character] = [Character].init(count: 7, repeatedValue: " ") // columnArray, just 7 characters
var board = [[Character]](count: 6, repeatedValue: [Character](count: 7, repeatedValue: " ")) // You need 6 * 7 characters
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]]
init() {
displayContent()
}
// Using init instead of function for initialization
// func Board() {
// createBoard()
// displayContent()
// }
// Not needed, board is already initialized to all " "
// 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("[" + board[j][k] + "]") // Doesn't work, "+" operates on strings, not char + string
}
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) { // For loops will be removed from future versions
for i in (0..<rows).reverse() {
if (board[i][column] == " ") {
board[i][column] = protagonist
break
}
}
// What happens if no character in this column is " "? Should still true be returned?
return true
}
}
func remove(column: Int) {
for i in 0..<rows {
if (board[i][column] != " ") {
board[i][column] = " "
break // This only removes the first row in the column where char isn't " ", you sure about that?
}
}
}
// Returning nil when no winner
func areWeDone() -> Character? {
//check for win horizontally
for row in 0..<rows {
for col in 0..<columns-3 {
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 row in 0..<rows-3 {
for col in 0..<columns {
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 board[row][col]
}
}
}
// My diagonal checker
for row in 0..<rows-3 {
for col in 0..<columns-3 {
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]
}
}
}
// My diagonal checker
for row in 3..<rows {
for col in 0..<columns-3 {
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 nil
}
func isTiePresent() -> Bool {
for j in 0..<columns {
if (board[0][j] == " ") {
return false
}
}
return true
}
func isMoveAllowed(column: Int) -> Bool {
return !(column > 6 || column < 0 || board[0][column] != " ")
// Same as this
// 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
}
}