(*********************************************** board.ml The game board stuff Copyright (C) 2001 Brock Wilcox [awwaiid@deathonastick.org] Released under the terms of the GNU GPL See www.gnu.org for more information HISTORY / NOTES 2001.04.07.22.17 - Split off from game_utils.ml 2001.02.25.06.19 - do_move now returns () and raises error - Added rand_move and rand_piece 2001.01.29.04.49 - Tweaked the human input a bit - All rules implemented 2000.11.02.14.00 - Most rules implemented - Only needs winner routine 2000.10.27.10.35 - Created ***********************************************) (* A lovely board - list of lists matrix *) type board = Piece.piece list list (* Create individuals in a column *) let rec new_board_cols = function | 0 -> [] | _ as n -> Piece.Blank :: new_board_cols (n - 1) (* Create the rows *) let rec new_board_row x = function | 0 -> [] | _ as n -> (new_board_cols x) :: (new_board_row x (n - 1)) (* Create a new board *) (* Maybe make this so you can choose the size *) let create : board = new_board_row 7 7 (* Copy individuals in a column *) let rec copy_board_cols = function | [] -> [] | piece :: rest -> (Piece.copy piece) :: (copy_board_cols rest) (* Copy the rows *) let rec copy_board_row = function | [] -> [] | col :: rest -> (copy_board_cols col) :: (copy_board_row rest) (* Copy a board *) let copy_board board = copy_board_row board (* Print individuals in a column *) let rec print_board_cols = function | [] -> print_string " |\n"; | piece :: rest -> print_string " | "; Piece.print piece; print_board_cols rest (* Print the rows of the board *) let rec print_board_row n = function | [] -> print_string " +---+---+---+---+---+---+---+\n" | row :: rest -> print_string " +---+---+---+---+---+---+---+\n"; print_int n; print_board_cols row; print_board_row (n + 1) rest (* Print out a board *) let print board = print_string " a b c d e f g\n"; print_board_row 1 board (* Get the contents of a board at a loc *) let at board l = let x,y = l in List.nth (List.nth board y) x let rec set_contents_col piece x y = function | [] -> [] | item :: rest -> (if (x,y) = (0,0) then piece else item) :: (set_contents_col piece (x - 1) y rest) let rec set_contents_row piece x y = function | [] -> [] | row :: rest -> (set_contents_col piece x y row) :: (set_contents_row piece x (y - 1) rest) let update board loc piece = let x = fst loc in let y = snd loc in set_contents_row piece x y board (* Is a location empty? *) let empty_at board loc = at board loc = Piece.Blank (* End of board.ml *)