How to make arrays of array?

I would like to store an Array (Tic Tac Toe Game (initialized) position 3 X 3 array of string(A = fill(“”,3,3)) in a list array. The game A gets filled with moves of X and O, so my list of game positions would have a list [ A1… first board position,moves filling X and O, A2 second move (this would have all the previous moves and then A3 etc.] [[“X” “” “” ; “” “” “”; “” “” “O”], [ "X “X” “”; “” “” “O”; “” “O” “O”]] …]
How would I fill an list or array with the A1,A2 ,A3,…game positions? My code updates the previous positions which is not what I want. I tried to paste the screen snippet for the output. but I do not see it.

type or paste code 
C = []
A = fill("",3,3)

for i in 1:2


    
listgamestate = [] 
    
listpossmove= [1,2,3,4,5,6,7,8,9]

selectedmove = rand(listpossmove)
    println("selected move",selectedmove)

A[selectedmove] = "X"
#println(A)
            push!(C, A)
    println(C)

println()
    
    listpossmove = filter(x ->x!= selectedmove, listpossmove)
    
    
    
      selectedmove = rand(listpossmove)
        #println("selected move",selectedmove)
        
        A[selectedmove] = "O"
        #println("O selected",A)
        
        C = push!(C,A)
            println(C)
    listpossmove = filter(x ->x!= selectedmove, listpossmove)
end

I’m not sure I follow. But, you may want something like push!(C, copy(A))

Also. In Julia arrays of element type Any are not called lists. Might cause confusion.

Hello and welcome!

The thing is, arrays are what is called “reference types”. That means, an array variable in fact holds only the location of the data in memory.
So, when you say B=A and change an element of A, B changes as well. To make a new array that holds the same elements as A, you must use B = copy(A), as the previous commenter suggests.

Another advice is to use a Set datatype to store moves. Then, you can use

# the following function removes a random element from a set and returns its value
poprandom!(x::Set) = pop!(x, rand(x))

possiblemoves = Set([1:9...])

selectedmove = poprandom!(possiblemoves)
# etc...
1 Like