Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 10.74 KB | None | 0 0
  1. open Printf
  2. open Unix
  3. open Board
  4. open State
  5.  
  6. module A = ANSITerminal
  7.  
  8. let rec blank_spacer spaces acc = begin
  9.     if spaces = 0 then acc else blank_spacer (spaces - 1) (" "^acc)
  10.     end
  11. (* let rec auto_center string =
  12.     let x, y = A.size() in
  13.     let difference = (let vale = (x - (String.length string)) in
  14.     if vale mod 2 != 0 then (vale - 1) else (vale)) in
  15.     (blank_spacer (difference/2) "")^string *)
  16.  
  17. let rec auto_center string = begin
  18.     let x, y = A.size() in
  19.     let difference = (x - (String.length string)) in
  20.     (blank_spacer (difference/2) "")^string
  21.     end
  22.  
  23. let rec newline number acc = begin
  24.     if number = 0 then acc else newline (number - 1) ("\n")^acc
  25.     end
  26.  
  27. let rec color_check col = begin
  28.     match col with
  29.     | Red -> [A.Background A.Red; A.Foreground A.White;]
  30.     | Blue -> [A.Background A.Blue; A.Foreground A.White;]
  31.     end
  32. (* let check_state c s = begin
  33.     if s = Hidden then A.print_string (color_check c) ("|███|"); 1 else A.print_s *)
  34.  
  35. (** Goes through a list of list of strings (lst) for one side of the board
  36.     and prints out the pieces. My plan will be to use something like this to
  37.     print out the entire board including different colors depending on the
  38.     piece type once we gain some progress into basic functionality*)
  39. let rec board_machine_9000 lst= begin
  40.         printf ("                           ");
  41.         match lst with
  42.         | a::t -> begin
  43.             let rec lst_reader item =
  44.             match item with
  45.             | [] -> printf "\n";
  46.             | h::t -> begin
  47.                 let simplify drawing c s =
  48.                     if s = Hidden then A.print_string (color_check c) ("|███|") else A.print_string (color_check c) ("| "^drawing^" |"); lst_reader t;
  49.                     in
  50.                 match h with
  51.                     | One (c,s) -> simplify "1" c s;
  52.                     | Two (c,s) -> simplify "2" c s;
  53.                     | Three (c,s) -> simplify "3" c s;
  54.                     | Four (c,s) -> simplify "4" c s;
  55.                     | Five (c,s) -> simplify "5" c s;
  56.                     | Six (c,s) -> simplify "6" c s;
  57.                     | Seven (c,s) -> simplify "7" c s;
  58.                     | Eight (c,s) -> simplify "8" c s;
  59.                     | Nine (c,s) -> simplify "9" c s;
  60.                     | Ten (c,s) -> simplify "G" c s;
  61.                     | Bomb (c,s) -> simplify "B" c s;
  62.                     | Flag (c,s) -> simplify "F" c s;
  63.                     | River -> A.print_string [A.Background A.Cyan; A.Foreground A.Black;] ("| "^" "^" |"); lst_reader t;
  64.                     | Empty -> A.print_string [A.Background A.Black; A.Foreground A.Black;] ("| "^" "^" |"); lst_reader t;
  65.                     | LeftArrow -> A.print_string [A.Background A.Black; A.Foreground A.Green;] ("| "^"<"^" |"); lst_reader t;
  66.                     | RightArrow -> A.print_string [A.Background A.Black; A.Foreground A.Green;] ("| "^">"^" |"); lst_reader t;
  67.                     | UpArrow -> A.print_string [A.Background A.Black; A.Foreground A.Green;] ("| "^"^"^" |"); lst_reader t;
  68.                     | DownArrow -> A.print_string [A.Background A.Black; A.Foreground A.Green;] ("| "^"v"^" |"); lst_reader t;
  69.                     | Explosion -> A.print_string [A.Background A.Black; A.Foreground A.Red;] ("| "^"*"^" |"); lst_reader t;
  70.                 end in lst_reader a; A.print_string [A.Background A.Black; A.Foreground A.White] (auto_center "+------------------------------------------------+"); printf "\n"; board_machine_9000 t end
  71.         | _ -> printf "" end
  72.  
  73.  
  74.  
  75. let rec blueScreen state = begin
  76.     A.erase A.Screen;
  77.     A.print_string [A.Bold; A.Background A.Blue; A.Foreground A.White;] ((newline 14 "")^(auto_center "BLUE PLAYER TURN\n"));
  78.     A.print_string [A.Background A.Blue; A.Foreground A.Black;] (auto_center "Please Pass Device to the Blue Player\n");
  79.     A.print_string [A.Background A.Blue; A.Foreground A.Black;] ((auto_center "Press ENTER when Ready\n")^(newline 13 ""));
  80.     ignore(read_line());
  81.     A.erase A.Screen;
  82.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] (auto_center "                    StrategOcaml                   \n");
  83.     A.print_string [A.Background A.Black; A.Foreground A.White] (auto_center "+------------------------------------------------+");
  84.     printf "\n";
  85.     board_machine_9000 ((State.get_blue_state state) |> Board.expose);
  86.     end
  87.  
  88. let rec redScreen state = begin
  89.     A.erase A.Screen;
  90.     A.print_string [A.Bold; A.Background A.Red; A.Foreground A.White;] ((newline 14 "")^(auto_center "RED PLAYER TURN\n"));
  91.     A.print_string [A.Background A.Red; A.Foreground A.Black;] (auto_center "Please Pass Device to the Blue Player\n");
  92.     A.print_string [A.Background A.Red; A.Foreground A.Black;] ((auto_center "Press ENTER when Ready\n")^(newline 13 ""));
  93.     ignore(read_line());
  94.     A.erase A.Screen;
  95.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] (auto_center "                    StrategOcaml                   \n");
  96.     A.print_string [A.Background A.Black; A.Foreground A.White] (auto_center "+------------------------------------------------+");
  97.     printf "\n";
  98.     board_machine_9000 ((State.get_red_state state) |> Board.expose);
  99.     end
  100.  
  101. let rec parse_piece state =
  102.     print_endline "\n \n Select the piece you would like to move: \n";
  103.     print_string "> ";
  104.     match read_line() with
  105.         | user_response -> begin
  106.             try Command.parse user_response with
  107.             | Command.Invalid -> print_endline "Error in command."; parse_piece state
  108.         end
  109.  
  110. let rec parse_movement state =
  111.     print_endline "\n \n Where would you like to move to: \n";
  112.     print_string "> ";
  113.     match read_line() with
  114.         | user_response -> begin
  115.             try Command.parse user_response with
  116.             | Command.Invalid -> print_endline "Error in command."; parse_piece state;
  117.         end
  118.  
  119. let rec game_logic state = begin
  120.     (if ((State.get_turns state) mod 2) = 0 then redScreen state else blueScreen state);
  121.     let pieceSelect = (parse_piece state) in
  122.     let movementSelect = (parse_movement state) in
  123.     let newState =
  124.         match pieceSelect with
  125.             | Command.Select pos -> begin
  126.                 match movementSelect with
  127.                     | Command.Select pos2 -> begin try (State.move pos pos2 state) with
  128.                         | MoveDiagonal -> print_endline "Error - MoveDiagonal."; state;
  129.                         | MoveMTO -> print_endline "Error - MoveMTO."; state;
  130.                         | MoveRiver -> print_endline "Error - MoveRiver."; state;
  131.                         | MoveSelf -> print_endline "Error - MoveSelf."; state;
  132.                         | MovePast -> print_endline "Error - MovePast."; state;
  133.                         | _ -> print_endline "Error - Has no color"; state; end
  134.                     | Command.Surrender -> state;
  135.                     | _ -> state; end
  136.             | Command.Surrender -> state;
  137.             | _ -> state;
  138.      in game_logic newState; end
  139.  
  140. let initialization () = begin
  141.     A.erase A.Screen;
  142.     A.resize 104 30;
  143.     A.erase A.Screen;
  144.     A.print_string [A.Bold; A.Background A.White; A.Foreground A.Black;] ((newline 16 "")^(auto_center "Press ENTER to do a coin toss to see which side goes first!\n")^(newline 14 ""));
  145.     ignore(read_line());
  146.     Random.self_init ();
  147.  
  148.     let init = State.init_state (from_json_red (Yojson.Basic.from_file "b1.json") (Yojson.Basic.from_file "b2.json")) (from_json_blue (Yojson.Basic.from_file "b1.json") (Yojson.Basic.from_file "b2.json"))
  149.     in
  150.     (* if (Random.int 2) = 1 then blueScreen init else redScreen init; *)
  151.     game_logic init;
  152.     end
  153.  
  154. let introduction () = begin
  155.     A.erase A.Screen;
  156.     A.resize 104 30;
  157.  
  158.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] ((newline 6 "")^" ███████╗████████╗██████╗  █████╗ ████████╗███████╗ ██████╗  ██████╗  ██████╗ █████╗ ███╗   ███╗██╗     \n");
  159.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] " ██╔════╝╚══██╔══╝██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝ ██╔═══██╗██╔════╝██╔══██╗████╗ ████║██║     \n ";
  160.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] "███████╗   ██║   ██████╔╝███████║   ██║   █████╗  ██║  ███╗██║   ██║██║     ███████║██╔████╔██║██║     \n ";
  161.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] "╚════██║   ██║   ██╔══██╗██╔══██║   ██║   ██╔══╝  ██║   ██║██║   ██║██║     ██╔══██║██║╚██╔╝██║██║     \n ";
  162.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] "███████║   ██║   ██║  ██║██║  ██║   ██║   ███████╗╚██████╔╝╚██████╔╝╚██████╗██║  ██║██║ ╚═╝ ██║███████╗\n ";
  163.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.Red;] ("╚══════╝   ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝   ╚═╝   ╚══════╝ ╚═════╝  ╚═════╝  ╚═════╝╚═╝  ╚═╝╚═╝     ╚═╝╚══════╝"^newline 10 "");
  164.  
  165.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.White; A.Blink;] (auto_center "____                 ___ _  _ _____ ___ ___   _         ___          _       \n");
  166.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.White; A.Blink;] (auto_center "| _ \_ _ ___ ______ | __| \| |_   _| __| _ ) | |_ ___  | _ )___ __ _(_)_ _   \n");
  167.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.White; A.Blink;] (auto_center "|  _| '_/ -_(_-(_-< | _|| .` | | | | _||   / |  _/ _   | _ / -_/ _` | | '  | \n");
  168.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.White; A.Blink;] (auto_center "|_| |_| \___/__/__/ |___|_|\_| |_| |___|_|_)  \__\___/ |___\___\__, |_|_||_| \n");
  169.     A.print_string [A.Bold; A.Background A.Black; A.Foreground A.White; A.Blink;] ((auto_center "                                                               |___/         \n")^newline 5 "");
  170.     ignore(read_line());
  171.     initialization();
  172.     end
  173.  
  174. let () =
  175.     introduction ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement