Betting

by Alex Griffiths: L6 Age ~16

Introduction

This horse race betting program not only keeps a record of how much money you win or lose and have remaining but also gives you a choice of either betting on a win or making an each way bet. The odds for each race are generated using random numbers. The program makes use of arrays and if statements within nested loops. Delays added with the sleep function are used to good effect. Have a flutter!

The Program

program Betting;
{$APPTYPE CONSOLE}

{
    Copyright (c) 2010 Alex Griffiths
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}

uses
  Classes,
  SysUtils;
var
  money, choice, randomcount, oddscount, bethorse, bettype, betamount, ranwin, winner, checkwin, second, winnings, continue, staggercount1, staggercount2, staggercount3 : integer;
  HorseOdd : array[1..7] of integer;
  StaggeredOdds : array[1..28] of integer;

begin
  writeln('Welcome to the Small National!');
  money := 100;
repeat
  repeat
    writeln;
    writeln('You have ', char(156) , money, ', what would you like to do?');
    writeln('1: Bet on a horse,');
    writeln('2: Quit,');
    readln(choice);
  until (choice = 1) or (choice = 2);
    if choice = 2 then
      begin
        halt;
      end else
  randomize;
  for randomcount := 1 to 7 do
    begin
      HorseOdd[randomcount] := random(19) + 2;
    end;
    writeln;
  for oddscount := 1 to 7 do
    begin
      writeln('Horse ', oddscount, ' - ', HorseOdd[oddscount], ' to 1');
    end;
// stagger odds
  staggercount3 := 1;
  for staggercount1 := 1 to 20 do
    begin
      for staggercount2 := 0 to 7 do
        begin
          if HorseOdd[staggercount2] = (21 - staggercount1) then
            begin
              StaggeredOdds[staggercount2] := staggercount3;
              inc(staggercount3);
            end;
        end;
    end;
    writeln;

// stagger odds
  repeat
    writeln('Which Horse would you like to bet on?');
    readln(bethorse);
  until (bethorse > 0) and (bethorse < 8);
  writeln;
  repeat
    writeln('Would you like to place a Win Bet or a Place Bet?');
    writeln('1: Win Bet');
    writeln('2: Place Bet');
    readln(bettype);
  until (bettype = 1) or (bettype = 2);
  writeln;
  repeat
    writeln('How much would you like to bet on Horse ', bethorse, '? Your balance is ', char(156), money);
    readln(betamount);
  until (betamount > 0) and (betamount <= money);
  writeln;
  money := money - betamount;
  writeln('You have placed a ', char(156), betamount, ' wager on Horse ', bethorse);
  writeln('The race is about to start...');
  sleep(1000);
  writeln('And they are off!');
  writeln;
  sleep(2000);
  randomize;
  ranwin := random(25) + 1;

    if (ranwin = 1) then
    begin
      for checkwin := 1 to 7 do
        begin
          if StaggeredOdds[checkwin] = 1 then
            begin
              winner := checkwin;
            end;
        end;
    end else
    if (ranwin = 2) or (ranwin = 3) then
    begin
      for checkwin := 1 to 7 do
        begin
          if StaggeredOdds[checkwin] = 2 then
            begin
              winner := checkwin;
            end;
        end;
    end else
    if (ranwin > 3) and (ranwin < 7) then
    begin
      for checkwin := 1 to 7 do
        begin
          if StaggeredOdds[checkwin] = 3 then
            begin
              winner := checkwin;
            end;
        end;
    end else
    if (ranwin > 6) and (ranwin < 11) then
    begin
      for checkwin := 1 to 7 do
        begin
          if StaggeredOdds[checkwin] = 4 then
            begin
              winner := checkwin;
            end;
        end;
    end else
    if (ranwin > 10) and (ranwin < 16) then
    begin
      for checkwin := 1 to 7 do
        begin
          if StaggeredOdds[checkwin] = 5 then
            begin
              winner := checkwin;
            end;
        end;
    end else
    if (ranwin > 15) and (ranwin < 22) then
    begin
      for checkwin := 1 to 7 do
        begin
          if StaggeredOdds[checkwin] = 6 then
            begin
              winner := checkwin;
            end;
        end;
    end else
    if (ranwin > 21) and (ranwin < 29) then
    begin
      for checkwin := 1 to 7 do
        begin
          if StaggeredOdds[checkwin] = 7 then
            begin
              winner := checkwin;
            end;
        end;
    end;

  repeat
    randomize;
    second := random(7) + 1;
  until not (second = winner);
  writeln('The winning Horse is Horse ', winner);
  writeln('Horse ', second, ' came second');
  writeln;
  case bettype of
  1: begin
       if bethorse = winner then
         begin
           winnings := (betamount * HorseOdd[bethorse]) + betamount;
           money := money + winnings;
           writeln('Congratulations! You have won ', char(156), winnings, '! You now have ', char(156), money);
         end else
         begin
           writeln('Unfortunately, you lost the wager. You now have ', char(156), money);
         end;
     end;
  2: begin
       if (bethorse = winner) or (bethorse = second) then
         begin
           winnings := ((betamount * HorseOdd[bethorse]) div 4) + betamount;
           money := money + winnings;
           writeln('Congratulations! You have won ', char(156), winnings, '! You now have ', char(156), money);
         end else
         begin
           writeln('Unfortunately, you lost the wager. You now have ', char(156), money);
         end;
     end;
  end;
  writeln;
  if (money > 0) then
    begin
      repeat
        writeln('Would you like to continue betting?');
        writeln('1: Yes');
        writeln('2: No');
        readln(continue);
      until (continue = 1) or (continue = 2);
    end else
    begin
      writeln('You have no money left!');
      sleep(3000);
      halt;
    end;
until (continue = 2);
   halt;

  readln;
end.

Remarks

Can you think of a program like this that you could write?

Programming - a skill for life!

Student programs to inspire you!