I'm not able to isolate Array in OCaml from modifying

Here is the full code. I'll try to show some pieces. I'm writing a tic tac toe server working via telnet. This is a representation of game board

let empty_board = [|
                [|EMPTY; EMPTY; EMPTY|];
                [|EMPTY; EMPTY; EMPTY|];
                [|EMPTY; EMPTY; EMPTY|]|]

It's used only once by Array.copy to pass players descriptors and new board in game loop:

let prepare_game_process pair_of_players= 
 pair_of_players >>= fun (player1, player2) ->  
 send_to_client player1 "You play for X";
 send_to_client player2 "You play for O";
 let new_board = Array.copy empty_board in
 game_loop player1 player2 new_board

But every time new game begins all changes in game_loop are reflected to original empty_board:

let make_move x y board token=
 board.(y).(x) <- token;
 board

I looked through the code billion times but I couldn't see the reason at all.

Upvotes: 1

Views: 112

Answers (1)

Thomas Leonard
Thomas Leonard

Reputation: 7196

You are only copying the outer array, not the individual row arrays.

Upvotes: 5

Related Questions