Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Created November 23, 2009 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinklepsch/241421 to your computer and use it in GitHub Desktop.
Save martinklepsch/241421 to your computer and use it in GitHub Desktop.
program CrapsGame;
uses UCrapsGame;
var Game: TCrapsGame;
win, lose: TSetType;
begin
win[1]:=2;
win[2]:=6;
win[3]:=4;
lose[1]:=3;
lose[2]:=7;
lose[3]:=11;
Game := TCrapsGame.setGameSettings(100, 10, win, lose);
Game.getNames();
Game.getBets();
end.
Unit UCrapsGame;
INTERFACE
type
TSetType = Array[1..3] of Byte;
TSettingsType = record {seperate type to be able to pass settings ti a function}
startbudget: integer; {how much money each player will have in the beginning}
rounds: byte; {how much rounds will be played}
winset: TSetType; {which numbers are needed to win the game}
loseset: TSetType; {and which to lose it}
end;
TCrapsGamerType = record
name: String;
bet : Integer;
money : Integer
end;
TCrapsGame = class(TObject)
Private
Settings : TSettingsType;
Players : Array of TCrapsGamerType;
Public
{set all needed parameters to play the game}
constructor setGameSettings(startbudget: Integer; rounds: Byte; winset, loseset: TSetType);
function newPlayer(submittedname: string):boolean;
function getBets():integer;
procedure getNames();
end;
IMPLEMENTATION
uses SysUtils, StrUtils;
// functions & procedures
constructor TCrapsGame.setGameSettings(startbudget: Integer; rounds: Byte; winset, loseset: TSetType);
begin
with self.settings do begin
startbudget := startbudget;
rounds := rounds;
winset := winset;
loseset := loseset;
end;
end;
{returns whether the name existed or not // not == SUCCESS}
function TCrapsGame.newPlayer(submittedname: string): boolean;
var i: integer;
begin
i:= 0;
result:= TRUE;
{search for empty slot}
repeat
i:=i+1;
if self.Players[i].name=submittedname then
result := FALSE;
until self.Players[i].name='';
{create new player}
if result=TRUE then begin
self.Players[i].name:=submittedname;
self.Players[i].money:=self.settings.startbudget;
end;
end;
{get names for each player}
procedure TCrapsGame.getNames();
var name: string;
newPlayerSuccess: boolean;
begin
repeat
WriteLn('Enter your name. Press C and Enter when you are done.');
ReadLn(name);
//AnsiProperCase(name, [' ']); {make name/C capitalized}
if name<>'C' then begin {create new player if name!=C}
newPlayerSuccess := self.newPlayer(name);
if newPlayerSuccess then
WriteLn('A player with the name', name, 'was created for you.')
else {the name exists...}
WriteLn('A player with the name', name, 'already exists. Please try another one.');
end;
until name='C'; {repeat when name was not C}
end;
{returns the key for the player who starts}
function TCrapsGame.getBets():integer;
var i: integer;
begin
for i:=low(self.Players) to high(self.Players) do begin {collect bet for every player}
WriteLn (self.Players[i].name, 'place your bet!');
ReadLn(self.Players[i].bet);
if (i>1) and (self.Players[i].bet > self.Players[i-1].bet) then
result:=i; {player with highest bet}
end;
end;
begin
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment