added functionnal game ans scoreV1 with combo

This commit is contained in:
Alexandre 2024-12-24 15:55:35 +01:00
parent 68fca51497
commit a9a449bb32
27 changed files with 173 additions and 30 deletions

BIN
csts.cmi

Binary file not shown.

BIN
csts.cmx

Binary file not shown.

14
csts.ml
View File

@ -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 ;;
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 ;;

BIN
csts.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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) ;;

BIN
drawing.o

Binary file not shown.

Binary file not shown.

BIN
dynamic.o

Binary file not shown.

BIN
maeth.cmi

Binary file not shown.

BIN
maeth.cmx

Binary file not shown.

109
maeth.ml
View File

@ -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

BIN
maeth.o

Binary file not shown.

BIN
main.cmi

Binary file not shown.

BIN
main.cmx

Binary file not shown.

19
main.ml
View File

@ -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 () ;;

BIN
main.o

Binary file not shown.

BIN
math

Binary file not shown.

BIN
math.cmi

Binary file not shown.

BIN
math.cmx

Binary file not shown.

View File

@ -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 ;;

BIN
math.o

Binary file not shown.

BIN
menus.cmi

Binary file not shown.

BIN
menus.cmx

Binary file not shown.

View File

@ -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 ;
add_button_to_interface 1 8 ;
add_button_to_interface 1 9 ;

BIN
menus.o

Binary file not shown.