diff --git a/csts.cmi b/csts.cmi index 81ee728..eb77b58 100644 Binary files a/csts.cmi and b/csts.cmi differ diff --git a/csts.cmx b/csts.cmx index c8386c8..5d9cdd5 100644 Binary files a/csts.cmx and b/csts.cmx differ diff --git a/csts.ml b/csts.ml index 2e15574..9f3a913 100644 --- a/csts.ml +++ b/csts.ml @@ -28,11 +28,23 @@ let sleep_d = 0.01 ;; let difficulty = ref 1 ;; let calc_length = ref 3 ;; let time_to_ans = ref 10 ;; +let total_time = ref 60 ;; let n_inf = ref 1 ;; let n_sup = ref 10 ;; let time_to_ans_f = ref 10.0 ;; +let total_time_f = ref 60.0 ;; + +let start_0 = ref 0. ;; let score = ref 0 and combo = ref 0 -and max_combo = ref 0 ;; \ No newline at end of file +and max_combo = ref 0 ;; + +let plus_ch = ref 10 +and minus_ch = ref 8 +and prod_ch = ref 2 ;; + +let ans = ref 0 ;; + +let max_score = ref 1000 ;; \ No newline at end of file diff --git a/csts.o b/csts.o index 4a2e8a0..da981fd 100644 Binary files a/csts.o and b/csts.o differ diff --git a/drawing.cmi b/drawing.cmi index 686d574..804bc81 100644 Binary files a/drawing.cmi and b/drawing.cmi differ diff --git a/drawing.cmx b/drawing.cmx index 27b633c..2d26a75 100644 Binary files a/drawing.cmx and b/drawing.cmx differ diff --git a/drawing.ml b/drawing.ml index 5b90251..2e200f2 100644 --- a/drawing.ml +++ b/drawing.ml @@ -4,33 +4,38 @@ let fodder = ref 0 ;; 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 + let size = Math.ln_b 10 (Math.abs n0) in let len = r/3 in let offset = size*(len*11/7)/2 in + if !n < 0 then begin + n := !n * (-1) ; + let xx = x0 + offset - (size+1)*(len*11/7) + 20 in + draw_poly_line [|(xx-3*len/2, y); (xx-len/2, y)|] + end ; for i = 0 to size do - let x = x0 + offset - i*(len*11/7) in - if Array.mem (!n mod 10) [|0; 4; 5; 6; 7; 8; 9|] then - draw_poly_line [|(x-len/2 + !(Csts.camx), y+len + !(Csts.camy)); (x-len/2 + !(Csts.camx), y + !(Csts.camy))|]; + let x = x0 + offset - i*(len*11/7) in + if Array.mem (!n mod 10) [|0; 4; 5; 6; 7; 8; 9|] then + draw_poly_line [|(x-len/2 + !(Csts.camx), y+len + !(Csts.camy)); (x-len/2 + !(Csts.camx), y + !(Csts.camy))|]; - if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 7; 8; 9|] then - draw_poly_line [|(x-len/2 + !(Csts.camx), y+len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y+len + !(Csts.camy))|]; + if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 7; 8; 9|] then + draw_poly_line [|(x-len/2 + !(Csts.camx), y+len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y+len + !(Csts.camy))|]; - if Array.mem (!n mod 10) [|0; 1; 2; 3; 4; 7; 8; 9|] then - draw_poly_line [|(x+len/2 + !(Csts.camx), y+len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y + !(Csts.camy))|]; + if Array.mem (!n mod 10) [|0; 1; 2; 3; 4; 7; 8; 9|] then + draw_poly_line [|(x+len/2 + !(Csts.camx), y+len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y + !(Csts.camy))|]; - if Array.mem (!n mod 10) [|2; 3; 4; 5; 6; 8; 9|] then - draw_poly_line [|(x-len/2 + !(Csts.camx), y + !(Csts.camy)); (x+len/2 + !(Csts.camx), y + !(Csts.camy))|]; + if Array.mem (!n mod 10) [|2; 3; 4; 5; 6; 8; 9|] then + draw_poly_line [|(x-len/2 + !(Csts.camx), y + !(Csts.camy)); (x+len/2 + !(Csts.camx), y + !(Csts.camy))|]; - if Array.mem (!n mod 10) [|0; 1; 3; 4; 5; 6; 7; 8; 9|] then - draw_poly_line [|(x+len/2 + !(Csts.camx), y-len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y + !(Csts.camy))|]; + if Array.mem (!n mod 10) [|0; 1; 3; 4; 5; 6; 7; 8; 9|] then + draw_poly_line [|(x+len/2 + !(Csts.camx), y-len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y + !(Csts.camy))|]; - if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 8; 9|] then - draw_poly_line [|(x-len/2 + !(Csts.camx), y-len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y-len + !(Csts.camy))|]; + if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 8; 9|] then + draw_poly_line [|(x-len/2 + !(Csts.camx), y-len + !(Csts.camy)); (x+len/2 + !(Csts.camx), y-len + !(Csts.camy))|]; - if Array.mem (!n mod 10) [|0; 2; 6; 8|] then - draw_poly_line [|(x-len/2 + !(Csts.camx), y-len + !(Csts.camy)); (x-len/2 + !(Csts.camx), y + !(Csts.camy))|]; + if Array.mem (!n mod 10) [|0; 2; 6; 8|] then + draw_poly_line [|(x-len/2 + !(Csts.camx), y-len + !(Csts.camy)); (x-len/2 + !(Csts.camx), y + !(Csts.camy))|]; - n := !n/10; + n := !n/10; done ; rv := x0 + offset + size*(len*11/7) ;; diff --git a/drawing.o b/drawing.o index d973d3b..4980ad0 100644 Binary files a/drawing.o and b/drawing.o differ diff --git a/dynamic.cmx b/dynamic.cmx index 9e5b0d8..649777d 100644 Binary files a/dynamic.cmx and b/dynamic.cmx differ diff --git a/dynamic.o b/dynamic.o index 0bf5156..901cfbd 100644 Binary files a/dynamic.o and b/dynamic.o differ diff --git a/maeth.cmi b/maeth.cmi index 36384f4..4c3c423 100644 Binary files a/maeth.cmi and b/maeth.cmi differ diff --git a/maeth.cmx b/maeth.cmx index 7bcbcaa..d55291e 100644 Binary files a/maeth.cmx and b/maeth.cmx differ diff --git a/maeth.ml b/maeth.ml index b3d511f..8ef8a3d 100644 --- a/maeth.ml +++ b/maeth.ml @@ -119,14 +119,119 @@ let print_calc (f : operation) (x0 : int) (y0 : int) (size : int) = in aux f ;; +let init_odds () = match !(Csts.difficulty) with + | 1 -> + Csts.plus_ch := 10 ; + Csts.minus_ch := 0 ; + Csts.prod_ch := 0 ; + | 2 -> + Csts.plus_ch := 0 ; + Csts.minus_ch := 1 ; + Csts.prod_ch := 0 ; + | 3 -> + Csts.plus_ch := 7 ; + Csts.minus_ch := 5 ; + Csts.prod_ch := 0 ; + | 4 -> + Csts.plus_ch := 5 ; + Csts.minus_ch := 5 ; + Csts.prod_ch := 0 ; + | 5 -> + Csts.plus_ch := 5 ; + Csts.minus_ch := 5 ; + Csts.prod_ch := 1 ; + | 6 -> + Csts.plus_ch := 6 ; + Csts.minus_ch := 4 ; + Csts.prod_ch := 1 ; + | 7 -> + Csts.plus_ch := 7 ; + Csts.minus_ch := 2 ; + Csts.prod_ch := 2 ; + | 8 -> + Csts.plus_ch := 7 ; + Csts.minus_ch := 2 ; + Csts.prod_ch := 3 ; + | 9 -> + Csts.plus_ch := 6 ; + Csts.minus_ch := 1 ; + Csts.prod_ch := 4 ; + | 10 -> + Csts.plus_ch := 5 ; + Csts.minus_ch := 0 ; + Csts.prod_ch := 5 ; + | _ -> failwith "unsupported\n" ;; + +let sign = function + | k when k < 0 -> -1 + | _ -> 1 ;; + +let update_ans () = + if key_pressed () then begin + match Char.code (read_key ()) with + | 45 (* minus *) -> + Csts.ans := !Csts.ans * (-1) + | 32 (* space *) | 8 (* del *) -> + let s = sign !Csts.ans in + Csts.ans := !Csts.ans * s ; + Csts.ans := !Csts.ans / 10 ; + Csts.ans := !Csts.ans * s ; + | k when k >= 48 && k <= 57 (* digit *) -> + let s = sign !Csts.ans in + Csts.ans := !Csts.ans * s ; + Csts.ans := 10 * !Csts.ans + k - 48 ; + Csts.ans := !Csts.ans * s ; + | x -> ()(*Printf.printf "%d -> %c\n" x (Char.chr x)*) + end ;; + +let grad_color (remt : float) (initt : float) = + set_color (rgb (int_of_float (255. *. remt /. initt)) (int_of_float (255. *. (1. -. (remt /. initt)))) 0) ;; + let maeth_main () = let start = Unix.gettimeofday () in + let cur = ref (Unix.gettimeofday ()) in let found = ref false in - while not !found && (Unix.gettimeofday() -. start <= !Csts.time_to_ans_f) do - () + let equ = generate_random_calc ~plus_w:(!Csts.plus_ch) ~minus_w:(!Csts.minus_ch) ~prod_w:(!Csts.prod_ch) !Csts.calc_length !Csts.n_inf !Csts.n_sup in + let resu = calculate equ in + while not !found && (!cur -. start <= !Csts.time_to_ans_f) do + auto_synchronize false ; + clear_graph () ; + + set_color black ; + print_calc equ 10 (Csts.__height__/2) 15 ; + + Drawing.draw_string_centered (Csts.__width__/2) (Csts.__height__/3) "Answer" 50 ; + Drawing.draw_integer (Csts.__width__/2) (Csts.__height__/3 - 100) !Csts.ans 75 ; + + fill_rect (Csts.__width__ - 70) (Csts.__height__/4) 70 (Csts.__height__/2) ; + set_color (rgb 129 255 0) ; + fill_rect (Csts.__width__ - 60) (Csts.__height__/4 + 10) 50 (max 0 (Csts.__height__/2 - 20 - int_of_float (float_of_int (Csts.__height__/2 - 20) *. (!cur -. !Csts.start_0) /. !Csts.total_time_f))) ; + + grad_color (!cur -. start) (!Csts.time_to_ans_f) ; + fill_rect 0 (7*Csts.__height__/8) (int_of_float ((float_of_int Csts.__width__) *. (!cur -. start) /. !Csts.time_to_ans_f)) (Csts.__height__/8+1) ; + + set_color (rgb 192 192 0) ; + Drawing.draw_string 10 (Csts.__height__ - 40) "Score" 35 ; + set_color (rgb 128 128 0) ; + Drawing.draw_integer_alignedleft (10+(5+1)*35*10/7) (Csts.__height__ - 40) !Csts.score 35 ; + + set_color (rgb 0 128 128) ; + Drawing.draw_integer_alignedright (Csts.__width__ - 20) (10+35) !Csts.combo 35 ; + set_color (rgb 0 192 192) ; + Drawing.draw_string (Csts.__width__ - 20 - 35*(5+1 + Math.ln_b 10 !Csts.combo)*11/7) (10+35) "Combo" 35 ; + + auto_synchronize true ; + update_ans () ; + if !Csts.ans = resu then + found := true ; + Unix.sleepf Csts.sleep_d ; + cur := Unix.gettimeofday () ; done ; + Csts.ans := 0 ; if !found then begin incr Csts.combo ; + Csts.max_combo := max (!Csts.max_combo) !Csts.combo ; + Csts.score := !Csts.score + int_of_float ((float_of_int !Csts.max_score) *. (!Csts.time_to_ans_f -. (!cur -. start)) /. !Csts.time_to_ans_f) ; true end else begin diff --git a/maeth.o b/maeth.o index fc5c70c..31b8f9f 100644 Binary files a/maeth.o and b/maeth.o differ diff --git a/main.cmi b/main.cmi index 744e235..7d1a064 100644 Binary files a/main.cmi and b/main.cmi differ diff --git a/main.cmx b/main.cmx index 1cc2ecd..406c9f4 100644 Binary files a/main.cmx and b/main.cmx differ diff --git a/main.ml b/main.ml index 87a7d00..2d1036f 100644 --- a/main.ml +++ b/main.ml @@ -17,11 +17,26 @@ let mainloop () = with | Menus.MenuStart -> Csts.time_to_ans_f := float_of_int (!Csts.time_to_ans) ; + Csts.total_time_f := float_of_int (!Csts.total_time) ; + Csts.start_0 := Unix.gettimeofday () ; + Csts.max_score := + 100 + * !Csts.difficulty + * !Csts.calc_length * int_of_float (Float.sqrt (float_of_int !Csts.calc_length)) + * (90 / !Csts.time_to_ans) * int_of_float (Float.sqrt (float_of_int (90 / !Csts.time_to_ans))) ; + Maeth.init_odds () ; 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 + ignore (Maeth.maeth_main ()) ; done ;; +(* +let difficulty = ref 1 ;; +let calc_length = ref 3 ;; +let time_to_ans = ref 10 ;; +let total_time = ref 60 ;; +let n_inf = ref 1 ;; +let n_sup = ref 10 ;; +*) mainloop () ;; \ No newline at end of file diff --git a/main.o b/main.o index 46ad304..7e9d3a1 100644 Binary files a/main.o and b/main.o differ diff --git a/math b/math index 2e9b48d..f295913 100755 Binary files a/math and b/math differ diff --git a/math.cmi b/math.cmi index affc95e..b1f0508 100644 Binary files a/math.cmi and b/math.cmi differ diff --git a/math.cmx b/math.cmx index 739422b..1459ffb 100644 Binary files a/math.cmx and b/math.cmx differ diff --git a/math.ml b/math.ml index cbde8b1..94d8254 100644 --- a/math.ml +++ b/math.ml @@ -18,6 +18,10 @@ let rec ln_b b n = match n with let convexf x y theta = (1.0 -. theta) *. x +. theta *. y ;; +let abs = function + | x when x < 0 -> - x + | x -> x ;; + let absf = function | x when x < 0.0 -> -. x | x -> x ;; diff --git a/math.o b/math.o index 8fa32ed..5e44d3d 100644 Binary files a/math.o and b/math.o differ diff --git a/menus.cmi b/menus.cmi index 5218deb..4376146 100644 Binary files a/menus.cmi and b/menus.cmi differ diff --git a/menus.cmx b/menus.cmx index adbefec..11f1c5a 100644 Binary files a/menus.cmx and b/menus.cmx differ diff --git a/menus.ml b/menus.ml index 822483d..3e985ac 100644 --- a/menus.ml +++ b/menus.ml @@ -310,7 +310,7 @@ let move_interface (b : button) = match b.actn with 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 ; - Drawing.draw_integer_alignedleft (7*Csts.__width__/10-5-3*20*10/7) (Csts.__height__/2) val_ptr.max 18 ; + Drawing.draw_integer_alignedright (7*Csts.__width__/10-5) (Csts.__height__/2) val_ptr.max 18 ; auto_synchronize true ; use_incrdecr val_ptr ; Unix.sleepf Csts.sleep_d ; @@ -369,12 +369,13 @@ build_button "Start" {x = Csts.__width__/2 - 150; y = 270; w = 300; h = 100 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 "Minimum" {x = Csts.__width__/2 - 200; y = 600; w = 400; h = 100} 255 128 32 (Tweak (build_tweak Csts.n_inf 1 1000 1 [] [Csts.n_sup])) ;; -build_button "Maximum" {x = Csts.__width__/2 - 200; y = 490; w = 400; h = 100} 128 255 32 (Tweak (build_tweak Csts.n_sup 1 1000 1 [Csts.n_inf] [])) ;; -build_button "Length" {x = Csts.__width__/2 - 200; y = 380; w = 400; h = 100} 255 32 32 (Tweak (build_tweak Csts.calc_length 2 20 1 [] [])) ;; -build_button "Time" {x = Csts.__width__/2 - 200; y = 270; w = 400; h = 100} 32 255 32 (Tweak (build_tweak Csts.time_to_ans 3 30 1 [] [])) ;; -build_button "Difficulty" {x = Csts.__width__/2 - 200; y = 160; w = 400; h = 100} 32 32 255 (Tweak (build_tweak Csts.difficulty 1 10 1 [] [])) ;; -build_button "Back" {x = Csts.__width__/2 - 200; y = 50; w = 400; h = 100} 32 32 32 (Warp 0) ;; +build_button "Minimum" {x = Csts.__width__/2 - 200; y = 710; w = 400; h = 100} 255 128 32 (Tweak (build_tweak Csts.n_inf 1 1000 1 [] [Csts.n_sup])) ;; +build_button "Maximum" {x = Csts.__width__/2 - 200; y = 600; w = 400; h = 100} 128 255 32 (Tweak (build_tweak Csts.n_sup 1 1000 1 [Csts.n_inf] [])) ;; +build_button "Length" {x = Csts.__width__/2 - 200; y = 490; w = 400; h = 100} 255 32 32 (Tweak (build_tweak Csts.calc_length 2 20 1 [] [])) ;; +build_button "Answer time" {x = Csts.__width__/2 - 200; y = 380; w = 400; h = 100} 32 192 32 (Tweak (build_tweak Csts.time_to_ans 3 30 1 [] [])) ;; +build_button "Total time" {x = Csts.__width__/2 - 200; y = 270; w = 400; h = 100} 32 255 32 (Tweak (build_tweak Csts.total_time 15 600 1 [] [])) ;; +build_button "Difficulty" {x = Csts.__width__/2 - 200; y = 160; w = 400; h = 100} 32 32 255 (Tweak (build_tweak Csts.difficulty 1 10 1 [] [])) ;; +build_button "Back" {x = Csts.__width__/2 - 200; y = 50; w = 400; h = 100} 32 32 32 (Warp 0) ;; (*Printf.printf "(B : %d ; I : %d)\n" arr_buttons.len arr_interfaces.len ;;*) add_button_to_interface 0 0 ; @@ -385,4 +386,5 @@ add_button_to_interface 1 4 ; add_button_to_interface 1 5 ; add_button_to_interface 1 6 ; add_button_to_interface 1 7 ; -add_button_to_interface 1 8 ; \ No newline at end of file +add_button_to_interface 1 8 ; +add_button_to_interface 1 9 ; \ No newline at end of file diff --git a/menus.o b/menus.o index 1973d72..c13eb5e 100644 Binary files a/menus.o and b/menus.o differ