クラスプロジェクト用の小さなアンチミサイルシミュレーターを開発しています。
アイデアは、異なるプロセス/スレッドが互いに通信することです。
Missile Launcher アプリケーションの作業を開始しました。そのミサイルはスレッドでなければなりません。
だから、これは私が作った開始コードです:
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 関数をテストしたいと思いました。
しかし、一時停止したり待機したりしないと、明らかに印刷が速すぎます。そのため、while 反復のたびにスレッドを 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;;
ありがとうございました