with Ada.Text_IO; use Ada.Text_IO; -- This program aims to run a simulation of an American roulette -- game and will output its result to a text file, namely -- "martingale.txt". Feel free to modify the source code if desired. procedure Martingale is -- Here we store our current money. Money : Integer := 1000; -- This is used only for creating random numbers between -- 0 and 37. I will explain its usage later. type Pepe is array (1 .. 1000) of Integer; Matriz : Pepe; -- This indicates the number of our current play. Count : Natural := 0; -- Our current bet. Bet : Positive := 1; -- Our output file. File : File_Type; begin Create (File, Out_File, "martingala.txt"); -- We can only play while we have more money than we have to bet, -- We will also stop the loop if we have reached 1000 hands. But -- this doesn't happen very often. while Money > Bet and Count < 1000 loop Count := Count + 1; -- This is the most difficult part of the code to understand, -- but it is the less relevant. I am just trying to get random -- numbers by allocating an array of 1000 elements without -- giving values to them, and only get what is stored in -- memory, which is fairly random. -- Then we get its absolute value (e.g., if we have either -38 -- or 38, its absolute value is 38). Once we have done that, -- we will divide it by 38 and take its "resto" (I don't know -- how to call it in English), which will be a number between -- 0 and 37. As we have 18 possibilities out of 38, I've -- chosen that the numbers between 0 and 17 (they're 18 -- possible results) mean that we have won our bet and, if -- it's 18 or higher (20 possibilities), we will have lost. if abs (Matriz (Count)) mod 38 < 18 then Money := Money + Bet; Bet := 1; else Money := Money - Bet; Bet := Bet * 2; end if; -- Each time that we play a bet, we will write to the text -- file how much money we have and our current bet. Put_Line (File, "Money:" & Integer'Image (Money) & " Bet:" & Positive'Image (Bet)); end loop; Close (File); exception when others => Close (File); end Martingale; |