이우석
가입: 2007년 9월 26일 올린 글: 72
|
올려짐: 2009년11월23일 16:00 주제: 오늘자 (11/23) 실습 뼈대코드 |
|
|
* module You 내부를 구현해주시면 됩니다.
* 현재 10000번 게임을 하게 되어있고, 승률이 0.5가 넘어야 합니다.
| 코드: |
module type PLAYER =
sig
type selection = int (* 0 : ROCK 1 : PAPER 2 : SCISSOR *)
type submission = selection * selection
val submit : unit -> selection * selection
val select : submission -> selection
end
module Com : PLAYER =
struct
type selection = int
type submission = selection * selection
let mine = ref (0,0)
let submit () =
let a = Random.int(3) in
let b = Random.int(3) in
let _ = mine := (a,b) in
(a,b)
let select os =
let choice = Random.int(2) in
if (choice = 0) then (fst !mine) else (snd !mine)
end
module You : PLAYER =
struct
type selection = int
type submission = selection * selection
...
end
module Simul =
struct
let round = ref 0
let comwin = ref 0
let youwin = ref 0
let init () = round := 0; comwin := 0; youwin := 0
let game playtime =
while (!round < playtime) do
let comopt = Com.submit () in
let youopt = You.submit () in
let com = Com.select youopt in
let you = You.select comopt in
let _ = incr round in
match (com, you) with
| (0, 1) -> incr youwin; print_endline "You win!";
| (0, 2) -> incr comwin; print_endline "Com win!";
| (1, 0) -> incr comwin; print_endline "Com win!";
| (1, 2) -> incr youwin; print_endline "You win!";
| (2, 0) -> incr youwin; print_endline "You win!";
| (2, 1) -> incr comwin; print_endline "Com win!";
| _ -> print_endline "Draw!";
done;
(!comwin, !youwin)
end
let init () =
let _ = Random.init (Int32.to_int (Int32.of_float (Unix.time ()))) in
Simul.init ()
let main () =
let playtime = 10000 in
let _ = init () in
let (comwin,youwin) = Simul.game playtime in
let winrate = (float_of_int youwin) /. (float_of_int playtime) in
print_string ("Your Win Rate : " ^(string_of_float winrate)^"\n")
let _ = main ()
|
|
|