我正在尝试在井字游戏中列出游戏位置。当我使用推!朱莉娅(Julia)中的代码存储游戏数组列表的列表更改了先前的游戏位置,这使该过程无用。我想要一个列表,例如... [[包含第一个位置的第一个位置],[包含第二个位置的第二个位置],等等...然后末尾的函数应返回列表。
尝试推送!并追加!朱西娅不喜欢Python!
# 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" "" ""]]
答案 0 :(得分:1)
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