# BettingGame

by Arnav Rawat: Y10 Age ~15

## Introduction

Arnav had a suitably challenging idea for one of his first programs to practise a range of language facilities. This interesting game uses random numbers combined with a weighting system. Arnav makes good use of routines and with more experience will further improve the efficiency of his code. You have the advantage of being able to see the source code before playing it so that you can plan an effective strategy. We think we are making good progress:

Game in progress

We also fared well on the Raspberry Pi:

Game in progress on the Pi

Note that the use of GBP instead of the pound sign is the easiest fix to display amounts of sterling on the Pi.

Unfortunately, the odds of a bookie using this program are very low! If you are planning to record your score you need to have a file named Leaderboard.txt in your program folder. This code supplied by Arnav will generate the file for first use.

```program SaveFile;
{\$APPTYPE CONSOLE}
uses
SysUtils;
var
f : text;

begin
rewrite(f); //Open File for writing
closeFile(f);
end.

```

## The Program

```program BettingGame;
{

use this file except in compliance with the License, as described at
}

{\$APPTYPE CONSOLE}

uses
SysUtils;

const
NumberOfTeams = 20;

var
Teams : array[1 .. NumberOfTeams] of string = ('Manchester City', 'Manchester United', 'Chelsea', 'Tottenham',
'Arsenal', 'Everton', 'Swansea', 'Newcastle', 'West Bromwich Albion',
'Fulham', 'Liverpool', 'Stoke City', 'West Ham', 'Norwich City',
'Sunderland', 'Aston Villa', 'Wigan Athletic', 'Southampton',
Rating : array[1 .. NumberOfTeams] of integer = (85, 84, 82, 81, 81, 80, 76, 75, 75, 75, 74, 73, 73, 73, 72, 72, 71, 71, 70, 69);
TeamOne, TeamTwo, LowerBound, UpperBound, TeamOneOdds, TeamTwoOdds, TeamSelection : integer;
UserMoney, BettingMoney, TeamOneChance, TeamTwoChance, WinningNumber, Result, DrawOdds, DrawChance : integer;
TFile : text;

//Function that produces a random number between two different numbers
function RandomFn(Low : Integer; High : Integer) : integer;
var
LocalRandomNumber : Integer;
begin
randomize;
LocalRandomNumber := (Random(High - Low + 1)) + Low;
RandomFn := LocalRandomNumber;
end;

procedure OddsGenerator;
begin
//Generates odds for all three scenarios
if (Rating[TeamOne] - Rating[TeamTwo]) > -20 then
begin
TeamOneOdds := 24;
DrawOdds := 12;
TeamTwoOdds := 2;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > -15 then
begin
TeamOneOdds := 16;
DrawOdds := 8;
TeamTwoOdds := 2;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > -10 then
begin
TeamOneOdds := 12;
DrawOdds := 4;
TeamTwoOdds := 3;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > -5 then
begin
TeamOneOdds := 8;
DrawOdds := 6;
TeamTwoOdds := 4;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > 0 then
begin
TeamOneOdds := 5;
DrawOdds := 3;
TeamTwoOdds := 7;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > 2 then
begin
TeamOneOdds := 5;
DrawOdds := 3;
TeamTwoOdds := 8;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > 5 then
begin
TeamOneOdds := 4;
DrawOdds := 6;
TeamTwoOdds := 8;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > 10 then
begin
TeamOneOdds := 3;
DrawOdds := 4;
TeamTwoOdds := 12;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > 15 then
begin
TeamOneOdds := 2;
DrawOdds := 8;
TeamTwoOdds := 16;
end;
if (Rating[TeamOne] - Rating[TeamTwo]) > 20 then
begin
TeamOneOdds := 2;
DrawOdds := 12;
TeamTwoOdds := 24;
end;
end;

procedure FixtureGenerator;
begin
//Generates A Fixture
randomize;
LowerBound := 1;
UpperBound := 20;
//Repeat loop makes sure the two teams aren't the same
repeat
TeamOne := RandomFn(LowerBound, UpperBound);
TeamTwo := RandomFn(LowerBound, UpperBound);
until TeamOne <> TeamTwo;
writeln(Teams[TeamOne], ' vs ', Teams[TeamTwo]);
writeln;
end;

procedure ResultGenerator;
begin
//Generates Result
WinningNumber := RandomFn(1, 100);
if Rating[TeamOne] > Rating[TeamTwo] then
begin
TeamOneChance := 50 + (Rating[TeamOne] - Rating[TeamTwo]);
TeamTwoChance := 50 - (Rating[TeamOne] - Rating[TeamTwo]);
if TeamTwoChance > WinningNumber then
begin
//Result Variable stores the Result as an integer variable
Result := 1;
end;
DrawChance := RandomFn(1, 2);
if DrawChance = 2 then
begin
Result := 2;
end;
end
else
begin
TeamTwoChance := 50 + ((Rating[TeamTwo] - Rating[TeamOne]) * 2);
TeamOneChance := 50 - ((Rating[TeamTwo] - Rating[TeamOne]) * 2);
if TeamTwoChance < WinningNumber then
begin
Result := 1;
end;
DrawChance := RandomFn(1, 2);
if DrawChance = 2 then
begin
Result := 2;
end;
end;
end;

procedure Credits;
begin
//Outputs Credits at the end of the game
writeln('You have run out of money.');
writeln;
writeln('Do not be disheartened as this can happen to anyone.');
writeln;
writeln('Why not play again?');
writeln;
sleep(500);
writeln('Credits');
writeln;
sleep(500);
writeln('Produced by Arnav,');
writeln;
sleep(500);
writeln('Designed by Arnav,');
writeln;
sleep(500);
writeln;
sleep(5000);
end;

procedure WhileLoop;
begin
while usermoney > 0 do
begin
writeln('********************************************************************************');
writeln('Incoming Fixture...');
writeln;
sleep(600);
FixtureGenerator;
writeln('Here are the odds...');
writeln;
OddsGenerator;
writeln('The odds for a ', Teams[TeamOne], ' win are ', TeamOneOdds, '/1');
writeln;
writeln('The odds for a ', Teams[TeamTwo], ' win are ', TeamTwoOdds, '/1');
writeln;
writeln('The odds for a draw are ', DrawOdds, '/1');
writeln;
writeln('Enter <1> if you think ', Teams[TeamOne], ' will win.');
writeln;
writeln('Enter <2> if you think ', Teams[TeamTwo], ' will win.');
writeln;
writeln('Enter <3> if you think it will be a draw.');
writeln;
writeln('Enter <4> if you would like to leave with ', CHR(156), usermoney, '.');
writeln;
writeln;
ResultGenerator;
case TeamSelection of
1 :
begin
//Following Code happens if the User selects Team One Win
writeln('You have ', CHR(156), usermoney, '.');
writeln;
repeat
writeln('How much money would you like to bet?');
writeln;
writeln;
until bettingmoney < usermoney + 1;
if Result = 1 then
begin
writeln('Incoming Result...');
writeln;
sleep(1000);
writeln('Congratulations! ', Teams[TeamOne], ' won.');
writeln;
usermoney := usermoney + (bettingmoney * TeamOneOdds);
writeln;
writeln('That means you now have ', CHR(156), usermoney, '.');
end
else
begin
writeln('Incoming Result...');
writeln;
sleep(1000);
writeln('We are sorry to tell you that ', Teams[TeamOne], ' lost.');
writeln;
usermoney := usermoney - bettingmoney;
writeln('That means you now have ', CHR(156), usermoney, '.');
writeln;
end;
end;
2 :
begin
//Following code happens if the user selects Team Two Win
writeln('You have ', CHR(156), usermoney, '.');
writeln;
repeat
writeln('How much money would you like to bet?');
writeln;
writeln;
until bettingmoney < usermoney + 1;
if Result = 1 then
begin
writeln('Incoming Result...');
writeln;
sleep(1000);
writeln('We are sorry to tell you that ', Teams[TeamTwo], ' lost.');
writeln;
usermoney := usermoney - bettingmoney;
writeln('That means you now have ', CHR(156), usermoney, '.');
writeln;
end
else
begin
writeln('Incoming Result...');
writeln;
sleep(1000);
writeln('Congratulations! ', Teams[TeamTwo], ' won.');
writeln;
usermoney := usermoney + (bettingmoney * TeamTwoOdds);
writeln(('That means you now have '), CHR(156), usermoney, '.');
writeln;
end;
end;
3:
begin
//Following code happens if the user selects a draw
writeln('You have ', CHR(156), usermoney, '.');
writeln;
repeat
writeln('How much money would you like to bet?');
writeln;
writeln;
until bettingmoney < usermoney + 1;
if Result = 2 then
begin
writeln('Incoming Result...');
writeln;
sleep(1000);
writeln('Congratulations! ', Teams[TeamOne], ' drew with ', Teams[TeamTwo]);
writeln;
usermoney := usermoney + (bettingmoney * TeamTwoOdds);
writeln(('That means you now have '), CHR(156), usermoney, '.');
writeln;
end
else
begin
if Result = 1 then
begin
writeln('Incoming Result...');
writeln;
sleep(1000);
writeln('We are sorry to tell you that ', Teams[TeamOne], ' won.');
writeln;
usermoney := usermoney - bettingmoney;
writeln('That means you now have ', CHR(156), usermoney, '.');
writeln;
end
else
begin
writeln('Incoming Result...');
writeln;
sleep(1000);
writeln('We are sorry to tell you that ', Teams[TeamTwo], ' won.');
writeln;
usermoney := usermoney - bettingmoney;
writeln('That means you now have ', CHR(156), usermoney, '.');
writeln;
end;
end;
end;
4 :
begin
//Saves user's final score into a file.
writeln('You are going to leave with ', CHR(156), usermoney, '.');
writeln;
writeln;
writeln;
append(TFile);
writeln(TFile, username, ' finished with a total of £', usermoney);
writeln;
closeFile(TFile);
writeln;
sleep(1000);
halt;
end;
end;
end;
end;

procedure TeamAndRatingDisplay;
var
i : integer;
begin
//Shows the user the ratings of each of the twenty teams.
writeln('Do you want to see the ratings of each team?');
writeln;
writeln('Enter <Yes> if you want to see the ratings.');
writeln;
writeln('Enter <No> if you do not want to see the ratings.');
writeln;
writeln;
if (LowerCase(RatingsDisplay) = 'yes') then
begin
writeln('Here are the twenty teams and their consequent ratings');
writeln;
for i  :=  1 to 20 do
begin
writeln(Teams[i], ' have a rating of ', Rating[i]);
writeln;
sleep(300);
end;
end
else
begin
writeln;
end;
end;

begin
//Main Block of Program
usermoney := 10;
writeln('Welcome to my football betting game.');
writeln;
writeln('It consists of the twenty current teams of the Barclays Premier League.');
writeln;
TeamAndRatingDisplay;
writeln(('You will start off with '), CHR(156), ('10'));
writeln;
WhileLoop;
Credits;
end.

```

## Remarks

Can you develop a program for generating likely outcomes of matches?

Programming - a skill for life!

Student programs to inspire you!