Arrays 尝试为游戏Tic Tac Toe创建移动列表
我正试着用tic-tact-toe列出比赛的位置。当我使用推!在存储游戏阵列列表的列表中编码列表会更改上一个游戏位置,从而使该过程无效。我想要一张像。。。[[position No.1 Contained first move]、[position No.2 move]等…然后末尾的函数应该返回列表 尝试了推送!和附加!不起作用。Julia不像PythonArrays 尝试为游戏Tic Tac Toe创建移动列表,arrays,julia,Arrays,Julia,我正试着用tic-tact-toe列出比赛的位置。当我使用推!在存储游戏阵列列表的列表中编码列表会更改上一个游戏位置,从而使该过程无效。我想要一张像。。。[[position No.1 Contained first move]、[position No.2 move]等…然后末尾的函数应该返回列表 尝试了推送!和附加!不起作用。Julia不像Python # set up 3 by 3 array Want to get list of moves of X after # they ar
# set up 3 by 3 array Want to get list of moves of X after
# they are chosen store in C
C = []
A =[]
A = fill("",3,3)
# set up 3 by 3 array
listpossmove= [1,2,3,4,5,6,7,8,9]
for i in 1:3
mov = rand(listpossmove)
println(mov)
A[mov] = "X"
println()
println(A)
push!(C, A)
println("C ",C)
# this removes previous list of moves choice so that move is not placed
#in previous square
listpossmove = filter(x ->x!= mov, listpossmove)
#println("final poss move",listpossmove
end
我希望结果会是这样
[["X" "" ""; "" "" ""; "" "" ""],["X" "" ""; "" "" ""; "X" "" ""],["X" "" ""; "" "X" ""; "X" "" ""]]
但是得到
["X" "" ""; "" "" ""; "" "" ""]
Array{Any,1}
C Any[["X" "" ""; "" "" ""; "" "" ""]]
3
["X" "" ""; "" "" ""; "X" "" ""]
Array{Any,1}
C Any[["X" "" ""; "" "" ""; "X" "" ""], ["X" "" ""; "" "" ""; "X" "" ""]]
5
["X" "" ""; "" "X" ""; "X" "" ""]
Array{Any,1}
C Any[["X" "" ""; "" "X" ""; "X" "" ""], ["X" "" ""; "" "X" ""; "X" "" ""], ["X" "" ""; "" "X" ""; "X" "" ""]]
保存阵列时,deepcopy()是您的朋友。您推送的阵列需要是您随后修改的阵列的副本。请尝试:
const C = []
const A = fill("",3,3)
# set up 3 by 3 array
const listpossmove = [1,2,3,4,5,6,7,8,9]
for i in 1:3
mov = rand(listpossmove)
println(mov)
A[mov] = "X"
println()
println(A)
push!(C, deepcopy(A))
println("C ",C)
# this removes previous list of moves choice so that move is not placed
#in previous square
filter!(x ->x!= mov, listpossmove)
#println("final poss move",listpossmove
end
Hei@Cliff,我试图修改你的格式,使代码更可读。请检查我是否没有更改任何含义。