Jump to content
  • Advertisement
Sign in to follow this  
metalyric

problem on arrays -ocaml- help please

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all, i've been trying to do some "really-easy" thing in ocaml, but i get a strange and annoying problem. here is the details: (* generate an array of arrays, with default values *) let gameBoard = Array.make 8 [|"| "; "| "; "| "; "| "; "| "; "| "; "| "; "| |"|];; (* prints the board, according to gameBoard array's contents *) let printBoard = function () -> for i = 0 to 7 do print_string "---------------------------------\n"; for j = 0 to 7 do print_string gameBoard.(i).(j); done; print_string "\n"; done; print_string "---------------------------------\n"; ;; (* update the element at 5th row and 3th column *) (* myArray.(i) <- x : puts x into array's ith position *) gameBoard.(5).(3) <- "| 5 ";; (* print out the board's contents *) let currentBoard = printBoard ();; and the following is what i get!!! --------------------------------- | | | | 5 | | | | | --------------------------------- | | | | 5 | | | | | --------------------------------- | | | | 5 | | | | | --------------------------------- | | | | 5 | | | | | --------------------------------- | | | | 5 | | | | | --------------------------------- | | | | 5 | | | | | --------------------------------- | | | | 5 | | | | | --------------------------------- | | | | 5 | | | | | --------------------------------- can you please help me on that? i'm probably doing a strange mistake, but i couldn't find it yet.. thanks all.

Share this post


Link to post
Share on other sites
Advertisement
I'm certainly not an OCaml expert myself (been recently prodding it with a stick), but I think your problem is due to the fact that you make an array where each slot points to the same "object" (which is again an array).
Hence, if you change one row, all are changed simultaneously. You need to make a copies instead of using the same instance when creating the board.





From the OCaml docs:

val make : int -> 'a -> 'a array

Array.make n x returns a fresh array of length n, initialized with x. All the elements of this new array are initially physically equal to x (in the sense of the == predicate). Consequently, if x is mutable, it is shared among all elements of the array, and modifying x through one of the array entries will modify all other entries at the same time.

Raise Invalid_argument if n < 0 or n > Sys.max_array_length. If the value of x is a floating-point number, then the maximum size is only Sys.max_array_length / 2.

Share this post


Link to post
Share on other sites
hi sam,

i started coding ocaml just today, and i don't know about it well yet.

anyway, you seem right and i will try your suggestion right now, and let you know what happened.

thank you very much for your answer.

Share this post


Link to post
Share on other sites
Quote:
Original post by SamLowry
I'm certainly not an OCaml expert myself (been recently prodding it with a stick), but I think your problem is due to the fact that you make an array where each slot points to the same "object" (which is again an array).
Hence, if you change one row, all are changed simultaneously. You need to make a copies instead of using the same instance when creating the board.





From the OCaml docs:

val make : int -> 'a -> 'a array

Array.make n x returns a fresh array of length n, initialized with x. All the elements of this new array are initially physically equal to x (in the sense of the == predicate). Consequently, if x is mutable, it is shared among all elements of the array, and modifying x through one of the array entries will modify all other entries at the same time.

Raise Invalid_argument if n < 0 or n > Sys.max_array_length. If the value of x is a floating-point number, then the maximum size is only Sys.max_array_length / 2.



Sam, you are a lifesaver!!!

i tried

gameBoard(0) <- [|"| "; "| "; "| "; "| "; "| "; "| "; "| "; "| |"|];;
gameBoard(1) <- [|"| "; "| "; "| "; "| "; "| "; "| "; "| "; "| |"|];;
.
.
.
gameBoard(7) <- [|"| "; "| "; "| "; "| "; "| "; "| "; "| "; "| |"|];;

and it worked!! damn, i had been searching around for this by hours! and it's a shame i didn't think about mutability.

anyway, thank you very very much friend!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!