我正在为一个班级项目开发一个小型反导弹模拟器。
这个想法是让不同的进程/线程相互通信。
我开始研究 Missile Laucher 应用程序。它的导弹必须是一根线。
所以这是我制作的起始代码:
open Missile;;
(* Name of the Station *)
let launcher_name = "Pacific";;
(* Gravitational Constant *)
let g = 9.8;;
(* Temporal Reference *)
let time = ref 0.0;;
(* Calculates the next position in time of the missile *)
let calculate_position () =
let nX = 10.0 *. !time and nY = (0.5) *. g**2.0 in (nX,nY);;
(* Launches the thread missile and updates its position *)
let launch mis =
while true do
let pos = mis#position () in
Printf.printf "%s\n" pos;
let (a,b) = calculate_position () in
mis#moveto (a,b);
time := !time +. 1.0;
(* Why isnt this working? *)
Thread.delay 0.25
done;;
(* test function *)
let launchMissile () =
Printf.printf "Launch Station %s launched a missile. \n" launcher_name;
let mis = new missile "Test" 1 "USA" in
let t1 = Thread.create launch mis in
Thread.join t1;;
launchMissile ();;
完成此操作后,我想通过随着时间的推移打印值来测试我的 CalculatePosition 函数。
但显然,没有暂停或等待,它们打印出来的速度太快了。所以我考虑在每次迭代中将线程延迟 0.25 秒。
我究竟做错了什么?我尝试了很多不同的东西。它唯一表现奇怪的 Thread.delay 函数。
我可以这样做吗?
如果您能提供帮助,那就太好了,我对此已经没有想法了……并开始重读我在 Ocaml 上的所有书籍。
如果对您分析问题也很重要,这里是我的导弹类:
class missile name id owner =
object
val mutable name = name;
val id = id;
val owner = owner;
val mutable x:float = 0.0
val mutable y:float = 0.0
method get_x = x
method get_y = y
method get_point = (x,y)
method moveto (a,b) = x <- a; y <- b
method rmoveto (dx, dy) = x <- x +. dx ; y <- y +. dy
method to_string () = "( " ^ "I am Missile " ^ (string_of_int id) ^ " and my owner is " ^ (owner) ^ ")";
method position () = "(" ^ (string_of_float x) ^ "," ^ (string_of_float y) ^ ")";
method distance () = sqrt (x*.x +. y*.y)
end;;
谢谢