0% found this document useful (0 votes)
105 views4 pages

Game of Life

This document contains code for simulating Conway's Game of Life in OCaml. It defines functions for: - Opening a graphics window - Representing and manipulating 2D boards/matrices - Drawing cells on a board based on their state - Calculating the number of living neighbors for each cell - Evolving the board state over generations by applying the Game of Life rules - Seeding an initial random configuration and running the simulation for a given number of steps The code provides the core logic and functions needed to initialize, represent, and evolve a Game of Life universe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views4 pages

Game of Life

This document contains code for simulating Conway's Game of Life in OCaml. It defines functions for: - Opening a graphics window - Representing and manipulating 2D boards/matrices - Drawing cells on a board based on their state - Calculating the number of living neighbors for each cell - Evolving the board state over generations by applying the Game of Life rules - Seeding an initial random configuration and running the simulation for a given number of steps The code provides the core logic and functions needed to initialize, represent, and evolve a Game of Life universe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

(*-------------------------------------OUVERTURE DE LA FENETRE

GRAPHIQUE-----------------------------------------*)

#load "graphics.cma" ;;
open Graphics;;
let open_window size = open_graph (" " ^ string_of_int size ^ "x" ^ string_of_int
(size+20)) ;;
open_window 500;;

(*------------------------------------FONCTIONS
UTILES---------------------------------------------*)

let rec length list = match list with


[] -> 0
|e::l1 -> length l1 +1;;

length [1;2;5;4;8;9];;

let rec init_list n x = match n with


y when n < 0 -> failwith "n doit etre un entier naturel"
|0 -> []
|_ -> x::(init_list (n-1) x);;

init_list 5 0;;

let rec put_list v i list = let e::l1 = list in match i with


x when i > length list -> failwith "liste trop courte"
|y when i < 0 -> invalid_arg "i ne peut pas etre negatif"
|0 -> v::l1
|_ -> e::(put_list v (i-1) l1);;

let rec nth i l = match i with


x when i > length l -> failwith "liste trop courte"
|y when i < 0 -> invalid_arg "i ne peut pas etre negatif"
|_ -> let (e::l1) = l in match i with
0 -> e
|_ -> nth (i-1) l1;;

let rec init_board (l, c) v = match l with


0 -> []
|_ -> (init_list c v)::(init_board ((l-1), c) v);;

init_board (5, 5) 0;;

let rec get_cell (x , y) board = let (e::l1) = board in


if x > length board then failwith "Matrice trop courte"
else if y > length e then failwith "Liste trop courte"
else if x < 0 || y < 0 then invalid_arg "argument
negatif"
else if x <> 0 then get_cell ((x-1),y) l1
else nth y e;;

let board = [[0;5;4;7]; [6;5;2;9]; [5;8;6;2]];;

get_cell (2 , 3) board;;

let rec put_cell v (x, y) board = let (e::l1) = board in


if x > length board then board
else if y > length e then board
else if x < 0 || y < 0 then board
else if x <> 0 then e::(put_cell v ((x-1),y) l1)
else (put_list v y e)::l1;;
put_cell 1 (1, 2) board;;

(*----------------------GAME OF LIFE----------------------*)

let grey = rgb 127 127 127;;


let blanc = rgb 255 255 255;;
let noir = rgb 0 0 0;;

let draw_fill_square (x,y) size color =


set_color color; fill_rect x y size size;
set_color grey; draw_rect x y size size;;

let draw_cell (x,y) size color =


draw_fill_square (x*(size+1), y*(size+1)) size color;;

let cell_color x = match x with


0 -> blanc
|_ -> noir;;

let rec colonne liste size x y =


match liste with
[] -> ()
|e::l -> draw_cell (x,y) size (cell_color e); colonne l size x (y+1);;

let draw_board board size =


let rec dess_matrice board size x = match board with
[] -> ()
|e::l -> colonne e size x 0; dess_matrice l size (x+1) in dess_matrice board
size 0;;

let board = [[1; 1; 1; 1; 1; 1; 1; 1; 1; 1];


[0; 0; 0; 0; 0; 0; 0; 0; 0; 0];
[1; 0; 1; 0; 1; 0; 1; 0; 1; 0];
[0; 1; 0; 1; 0; 1; 0; 1; 0; 1];
[0; 0; 0; 0; 0; 0; 0; 0; 0; 0];
[1; 1; 1; 1; 1; 1; 1; 1; 1; 1];
[0; 0; 0; 0; 0; 0; 0; 0; 0; 0];
[1; 0; 1; 0; 1; 0; 1; 0; 1; 0];
[0; 1; 0; 1; 0; 1; 0; 1; 0; 1];
[0; 0; 0; 0; 0; 0; 0; 0; 0; 0]];;

draw_board board 30;;

let new_cell = 1 ;; (* alive cell *)


let empty = 0 ;;
let is_alive cell = cell <> empty ;;

let rules0 c n = match c with


0 when n = 3 -> 1
|0 when n <> 3 -> 0
|1 when n = 2 || n = 3 -> 1
|_ -> 0;;
let count_neighbours (x,y) board size =
let add (x,y) = if is_alive (get_cell (x,y) board) then 1
else 0
in match (x,y) with
(0, y) when y = size -> add (x+1,y) + add (x, y-1) + add (x+1, y-1)
|(x, 0) when x = size -> add (x-1,y) + add (x, y+1) + add (x-1, y+1)
|(0, 0) -> add (x+1, y) + add (x+1, y+1) + add (x, y+1)
|(x, y) when x = size && y = size -> add (x-1, y) + add (x, y-1) + add (x-1,
y-1)
|(0, _) -> add (x,y-1) + add (x,y+1) + add (x+1,y-1) + add (x+1,y) + add
(x+1,y+1)
|(_, 0) -> add (x+1,y) + add (x+1,y+1) + add (x-1,y) + add (x-1,y+1) + add
(x,y+1)
|(x, _) when x = size -> add (x-1,y-1) + add (x-1,y) + add (x-1,y+1) + add
(x,y-1) + add (x,y+1)
|(_, y) when y = size -> add (x-1,y-1) + add (x-1,y) + add (x,y-1) + add
(x+1,y-1) + add (x+1,y)
|(_, _) -> add (x-1,y-1) + add (x-1,y) + add (x-1,y+1) + add (x,y-1) + add
(x,y+1) + add (x+1,y-1) + add (x+1,y) + add (x+1,y+1);;

count_neighbours (2, 1) board 9;;

open Random;;
Random.int 10;;

let rec seed_life board size nb_cell = match nb_cell with


0 -> board
|_ -> let (x,y) = (Random.int size, Random.int size) in
if get_cell (x,y) board = 1 then seed_life board size nb_cell
else let board = put_cell 1 (x,y) board in seed_life board size (nb_cell-
1);;

let board = [[0;0;0;0;0];


[0;0;0;0;0];
[0;0;0;0;0];
[0;0;0;0;0];
[0;0;0;0;0]];;

seed_life board 4 10;;

let new_board size nb = let board = init_board (size, size) 0 in


seed_life board size nb;;

let board = new_board 10 50;;

let next_cell (x,y) board size = rules0 (get_cell (x,y) board) (count_neighbours
(x,y) board size);;

let next_generation board size =


let rec next x y board size = match (x,y) with
(0,0) -> board
|(x,0) -> let board = (put_cell (next_cell (x,y) board size) (x,y) board) in
next (x-1) size board size
|(_,_) -> let board = (put_cell (next_cell (x,y) board size) (x,y) board) in
next x (y-1) board size in next size size board size;;

next_generation [[1;1];[0;1]] 1

let game board size n =


let rec game_rec board size n = match n with
0 -> ()
|_ -> draw_board (next_generation board size) size in game_rec board size
(n);;

let new_game size nb_cell n = let board = new_board size nb_cell in game board
(size-1) n;;

new_game 20 40 50;;

You might also like