diff --git a/compilation.sh b/compilation.sh index 55994f5..9bacd50 100644 --- a/compilation.sh +++ b/compilation.sh @@ -1,2 +1,2 @@ rm *.cmi *.cmx -ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics csts.ml dynamic.ml math.ml drawing.ml menus.ml main.ml -o math +ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics csts.ml dynamic.ml math.ml drawing.ml menus.ml maeth.ml main.ml -o math diff --git a/csts.cmi b/csts.cmi index a0fb49c..8af9521 100644 Binary files a/csts.cmi and b/csts.cmi differ diff --git a/csts.cmx b/csts.cmx index 07e0755..cae2103 100644 Binary files a/csts.cmx and b/csts.cmx differ diff --git a/csts.ml b/csts.ml index 79d6855..7a53619 100644 --- a/csts.ml +++ b/csts.ml @@ -27,4 +27,8 @@ let sleep_d = 0.01 ;; (* math *) let difficulty = ref 1 ;; let calc_length = ref 3 ;; -let time_to_ans = ref 10 ;; \ No newline at end of file +let time_to_ans = ref 10 ;; + +let score = ref 0 +and combo = ref 0 +and max_combo = ref 0 ;; \ No newline at end of file diff --git a/csts.o b/csts.o index 0dad598..1eafff9 100644 Binary files a/csts.o and b/csts.o differ diff --git a/drawing.cmi b/drawing.cmi index 35ff7c0..bb2d2ae 100644 Binary files a/drawing.cmi and b/drawing.cmi differ diff --git a/drawing.cmx b/drawing.cmx index c0210e0..9a0c149 100644 Binary files a/drawing.cmx and b/drawing.cmx differ diff --git a/drawing.ml b/drawing.ml index df6418b..cdf2851 100644 --- a/drawing.ml +++ b/drawing.ml @@ -1,6 +1,7 @@ open Graphics ;; +let fodder = ref 0 ;; -let draw_integer x0 y n0 r = +let draw_integer ?retval:(rv=fodder) x0 y n0 r = (* 7-seg display *) let n = ref n0 in let size = Math.ln_b 10 n0 in @@ -30,9 +31,10 @@ let draw_integer x0 y n0 r = draw_poly_line [|(x-len/2 + !(Csts.camx), y-len + !(Csts.camy)); (x-len/2 + !(Csts.camx), y + !(Csts.camy))|]; n := !n/10; - done ;; + done ; + rv := x0 + offset + size*(len*11/7) ;; -let draw_integer_alignedleft x0 y n0 len = +let draw_integer_alignedleft ?retval:(rv=fodder) x0 y n0 len = (* 7-seg display 2 *) set_line_width (max 1 (len/4)); let n = ref n0 in @@ -71,7 +73,8 @@ let draw_integer_alignedleft x0 y n0 len = n := !n/10; cur_x := !cur_x - (len*11/7); -done ;; + done ; + rv := (x0 + (size+1)*(len*11/7)) ;; let decode_letter x y len arr = set_line_width 3; diff --git a/drawing.o b/drawing.o index 0105c31..5e5219c 100644 Binary files a/drawing.o and b/drawing.o differ diff --git a/dynamic.cmx b/dynamic.cmx index ac45574..70495ec 100644 Binary files a/dynamic.cmx and b/dynamic.cmx differ diff --git a/dynamic.o b/dynamic.o index eb90e3c..7a3f91d 100644 Binary files a/dynamic.o and b/dynamic.o differ diff --git a/execution.sh b/execution.sh index f7afc37..8c5d01f 100644 --- a/execution.sh +++ b/execution.sh @@ -1,3 +1,3 @@ rm *.cmi *.cmx -ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics csts.ml dynamic.ml math.ml drawing.ml menus.ml main.ml -o math +ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics csts.ml dynamic.ml math.ml drawing.ml menus.ml maeth.ml main.ml -o math ./math \ No newline at end of file diff --git a/maeth.cmi b/maeth.cmi new file mode 100644 index 0000000..a4c9a50 Binary files /dev/null and b/maeth.cmi differ diff --git a/maeth.cmx b/maeth.cmx new file mode 100644 index 0000000..1476a5f Binary files /dev/null and b/maeth.cmx differ diff --git a/maeth.ml b/maeth.ml new file mode 100644 index 0000000..7d159c5 --- /dev/null +++ b/maeth.ml @@ -0,0 +1,120 @@ +open Graphics ;; +Random.self_init () ;; + +type operation = + Val of int | + Sum of operation * operation | + Diff of operation * operation | + Prod of operation * operation | + Exp of operation * int ;; + +let rec calculate = function + | Val k -> k + | Exp (op, k) -> Math.pw (calculate op) k + | Sum (f, g) -> (calculate f) + (calculate g) + | Diff (f, g) -> (calculate f) - (calculate g) + | Prod (f, g) -> (calculate f) * (calculate g) ;; + +let generate_random_calc ?plus_w:(pw=10) ?minus_w:(mw=8) ?prod_w:(uw=2) ?exp_w:(ew=0) (length : int) (inf : int) (sup : int) = + let sum = pw + mw + uw + ew in + let rec aux = function + | 0 | 1 -> + Val (inf + Random.int (sup - inf)) + | k -> + let r = Random.int sum in + if r < pw then begin + if Random.int 2 = 0 then + Sum(aux 0, aux (k-1)) + else + Sum(aux (k-1), aux 0) + end + else if r < pw+mw then begin + if Random.int 2 = 0 then + Diff(aux 0, aux (k-1)) + else + Diff(aux (k-1), aux 0) + end + else if r < pw+mw+uw then begin + if Random.int 2 = 0 then + Prod(aux 0, aux (k-1)) + else + Prod(aux (k-1), aux 0) + end + else begin + Exp(aux (k-1), 2 + Random.int 2) + end + in + aux length ;; + +let draw_bracket_open ?retval:(rv=Drawing.fodder) (x : int) (y : int) (size : int) = + moveto (x + size/4) (y+5*size/4) ; + lineto (x) (y+size/2) ; + lineto (x) (y-size/2) ; + lineto (x + size/4) (y-5*size/4) ; + rv := x + size/4 +6 ;; + +let draw_bracket_close ?retval:(rv=Drawing.fodder) (x : int) (y : int) (size : int) = + moveto (x) (y+5*size/4) ; + lineto (x + size/4) (y+size/2) ; + lineto (x + size/4) (y-size/2) ; + lineto (x) (y-5*size/4) ; + rv := x + size/4 +6 ;; + +let draw_sign ?retval:(rv=Drawing.fodder) (x : int) (y : int) (len : int) = function + | '+' -> + draw_poly_line [|(x, y); (x + len, y)|] ; + draw_poly_line [|(x+len/2, y + len/2); (x+len/2, y - len/2)|] ; + | '-' -> + draw_poly_line [|(x, y); (x + len, y)|] ; + | 'x' -> + Drawing.draw_letter x y 'x' len ; + | _ -> failwith "incorrect\n" ;; + +let require_bracket = function + | Val _ -> false + | _ -> true ;; + +let print_calc (f : operation) (x0 : int) (y0 : int) (size : int) = + set_line_width 3 ; + set_color black ; + let x = ref x0 in + let rec aux = function + | Val k -> + Drawing.draw_integer_alignedleft !x y0 k size ~retval:x ; + (*Printf.printf "%d --> \n" !x ;*) + | Exp (f, k) -> () + | Sum (f, g) -> + if require_bracket f then draw_bracket_open !x y0 size ~retval:x ; + aux f ; + if require_bracket f then draw_bracket_close !x y0 size ~retval:x ; + + draw_sign !x y0 size '+' ~retval:x ; + x := !x + size*10/7 ; + + if require_bracket g then draw_bracket_open !x y0 size ~retval:x ; + aux g ; + if require_bracket g then draw_bracket_close !x y0 size ~retval:x ; + | Diff (f, g) -> + if require_bracket f then draw_bracket_open !x y0 size ~retval:x ; + aux f ; + if require_bracket f then draw_bracket_close !x y0 size ~retval:x ; + + draw_sign !x y0 size '-' ~retval:x ; + x := !x + size*10/7 ; + + if require_bracket g then draw_bracket_open !x y0 size ~retval:x ; + aux g ; + if require_bracket g then draw_bracket_close !x y0 size ~retval:x ; + | Prod (f, g) -> + if require_bracket f then draw_bracket_open !x y0 size ~retval:x ; + aux f ; + if require_bracket f then draw_bracket_close !x y0 size ~retval:x ; + + draw_sign !x y0 (size/2) 'x' ~retval:x ; + x := !x + size*10/7 ; + + if require_bracket g then draw_bracket_open !x y0 size ~retval:x ; + aux g ; + if require_bracket g then draw_bracket_close !x y0 size ~retval:x ; + in + aux f ;; \ No newline at end of file diff --git a/maeth.o b/maeth.o new file mode 100644 index 0000000..2b2da2f Binary files /dev/null and b/maeth.o differ diff --git a/main.cmi b/main.cmi index 3636734..7eb4cdc 100644 Binary files a/main.cmi and b/main.cmi differ diff --git a/main.cmx b/main.cmx index 6878c9b..dc512ff 100644 Binary files a/main.cmx and b/main.cmx differ diff --git a/main.ml b/main.ml index c77ce52..9d1006f 100644 --- a/main.ml +++ b/main.ml @@ -1,17 +1,26 @@ open Graphics ;; - Random.self_init () ;; let mainloop () = open_graph Csts.open_string ; set_window_title "Maeth"; - while true do - auto_synchronize false ; - clear_graph () ; - Menus.print_current_interface () ; - auto_synchronize true ; - Menus.action_on_interface () ; - Unix.sleepf Csts.sleep_d - done ;; + try + while true do + auto_synchronize false ; + clear_graph () ; + Menus.print_current_interface () ; + auto_synchronize true ; + Menus.action_on_interface () ; + Unix.sleepf Csts.sleep_d + done ; + () + with + | Menus.MenuStart -> + clear_graph () ; + Maeth.print_calc (Maeth.generate_random_calc 8 1 20) (10) (Csts.__height__/2) 20 ; + while true do + Unix.sleepf Csts.sleep_d + done ;; + mainloop () ;; \ No newline at end of file diff --git a/main.o b/main.o index 1db3669..0d688fc 100644 Binary files a/main.o and b/main.o differ diff --git a/math b/math index 0817605..b2ac13c 100755 Binary files a/math and b/math differ diff --git a/math.cmx b/math.cmx index 6a06f1e..210928b 100644 Binary files a/math.cmx and b/math.cmx differ diff --git a/math.o b/math.o index 987d55b..b25fa5e 100644 Binary files a/math.o and b/math.o differ diff --git a/menus.cmi b/menus.cmi index 2e6c492..6f9a376 100644 Binary files a/menus.cmi and b/menus.cmi differ diff --git a/menus.cmx b/menus.cmx index d2dd277..22d9be9 100644 Binary files a/menus.cmx and b/menus.cmx differ diff --git a/menus.ml b/menus.ml index 2959e86..0acd1c2 100644 --- a/menus.ml +++ b/menus.ml @@ -1,5 +1,6 @@ open Graphics ;; exception MenuExit ;; +exception MenuStart ;; let current_button_index = ref 0 and current_interface_index = ref 0 ;; @@ -145,6 +146,12 @@ let add_button_to_current (b_id : int) = let is_within (r : Csts.rect) (x : int) (y : int) = r.x <= x && r.y < y && x <= r.x + r.w && y <= r.y + r.h ;; +let draw_closest (r : int) (g : int) (b : int) = + if r+g+b <= 384 then + set_color white + else + set_color black ;; + let print_button (b : button) = let (mx, my) = mouse_pos () in if is_within b.pos mx my then begin @@ -153,17 +160,19 @@ let print_button (b : button) = end; set_color (rgb b.red b.green b.blue) ; fill_rect b.pos.x b.pos.y b.pos.w b.pos.h ; - set_color (rgb (255 - b.red) (255 - b.green) (255 - b.blue)) ; + draw_closest b.red b.green b.blue ; set_line_width 2 ; Drawing.draw_string (b.pos.x+3) (b.pos.y + b.pos.h/2) b.text (min (3*b.pos.h/4-2) ((7*(b.pos.w*3/4-4)/(max 1 (String.length b.text)))/10)) ;; let print_interface (it : interface) = List.iter print_button it.bts ; + set_color black ; Drawing.draw_string 20 (Csts.__height__ - 40) it.title 30 ;; let print_current_interface () = let interf = Dynamic.dyn_mem arr_interfaces !current_it_id in List.iter print_button interf.bts ; + set_color black ; Drawing.draw_string 20 (Csts.__height__ - 40) interf.title 30 ;; (* --------------------------------------------------------------------------------------------------------------------------------------------- *) @@ -186,7 +195,7 @@ let move_interface (b : button) = match b.actn with fill_rect (3*Csts.__width__/10) (3*Csts.__height__/10) (2*Csts.__width__/5) (2*Csts.__height__/5) ; set_color white ; Drawing.draw_string_centered (Csts.__width__/2) (13*Csts.__height__/20) b.text 20 ; - Drawing.draw_integer (5+Csts.__width__/2) (Csts.__height__/2) !(val_ptr.ptr) 40 ; + Drawing.draw_integer (5+Csts.__width__/2) (Csts.__height__/2) !(val_ptr.ptr) 60 ; Drawing.draw_string (5+3*Csts.__width__/10) (11*Csts.__height__/20) "Min" 20 ; Drawing.draw_integer_alignedleft (5+3*Csts.__width__/10) (Csts.__height__/2) val_ptr.min 18 ; Drawing.draw_string (7*Csts.__width__/10-5-3*20*10/7) (11*Csts.__height__/20) "Max" 20 ; @@ -200,7 +209,13 @@ let move_interface (b : button) = match b.actn with | _ -> () done end - | Warp ne -> if ne = -1 then raise MenuExit else current_it_id := ne ;; + | Warp ne -> + if ne = -1 then + raise MenuExit + else if ne = -2 then + raise MenuStart + else + current_it_id := ne ;; let action_on_button (mox : int) (moy : int) (b : button) = if is_within b.pos mox moy then begin @@ -237,6 +252,7 @@ let action_on_interface () = build_empty_interface "Main Menu" 128 128 128 ;; build_empty_interface "Options " 32 32 32 ;; +build_button "Start" {x = Csts.__width__/2 - 150; y = 270; w = 300; h = 100} 32 255 32 (Warp (-2)) ;; build_button "Options" {x = Csts.__width__/2 - 150; y = 160; w = 300; h = 100} 255 32 255 (Warp 1) ;; build_button "Exit" {x = Csts.__width__/2 - 150; y = 50; w = 300; h = 100} 1 1 1 (Warp (-1)) ;; build_button "Length" {x = Csts.__width__/2 - 150; y = 380; w = 300; h = 100} 255 32 32 (Tweak (build_tweak Csts.calc_length 2 20 1)) ;; @@ -247,7 +263,8 @@ build_button "Back" {x = Csts.__width__/2 - 150; y = 50; w = 300; h = 100} (*Printf.printf "(B : %d ; I : %d)\n" arr_buttons.len arr_interfaces.len ;;*) add_button_to_interface 0 0 ; add_button_to_interface 0 1 ; -add_button_to_interface 1 2 ; +add_button_to_interface 0 2 ; add_button_to_interface 1 3 ; add_button_to_interface 1 4 ; -add_button_to_interface 1 5 ; \ No newline at end of file +add_button_to_interface 1 5 ; +add_button_to_interface 1 6 ; \ No newline at end of file diff --git a/menus.o b/menus.o index 499d44d..9943652 100644 Binary files a/menus.o and b/menus.o differ