Welcome to the Forum Archive!

We've now moved over to Boards where there's conversations going on right now, but within these archives you find over a years worth of cool and locally relevant conversations and content.

GO TO BOARDS


Need help with some code

1
Comment below rating threshold, click here to show it.

Keiran Darkwill

Junior Member

11-21-2014

So basically I'm new to coding and I'm trying to code a Yahtzee game in Pascal on Turbo Delphi
and I've set up some variables that randomize between 1 to 6 for the dice.
However, they values keep resulting in 0 for some reason and I can't find where the program is making them 0
Those variables are Dice1 - Dice5
Here is the code (I've bolded where I believe the only changes in the variables occur):

unit Yahtzee_Game;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, jpeg, Menus;

type
TForm2 = class(TForm)
UpSection_Img: TImage;
Hold_lbl: TLabel;
Roll_Btn: TButton;
LowSection_Img: TImage;
Hold1_Chk: TCheckBox;
Hold2_Chk: TCheckBox;
Hold5_Chk: TCheckBox;
Hold4_Chk: TCheckBox;
Hold3_Chk: TCheckBox;
Die1_lbl: TLabel;
Die2_lbl: TLabel;
Die3_lbl: TLabel;
Die4_lbl: TLabel;
Die5_lbl: TLabel;
Die1_Img: TImage;
Die2_Img: TImage;
Die3_Img: TImage;
Die4_Img: TImage;
Die5_Img: TImage;
OneScr_lbl: TLabel;
TwoScr_lbl: TLabel;
ThreeScr_lbl: TLabel;
UpScr1_lbl: TLabel;
FiveScr_lbl: TLabel;
SixScr_lbl: TLabel;
UpScr2_lbl: TLabel;
FourScr_lbl: TLabel;
BonusScr_lbl: TLabel;
ThreeKind_lbl: TLabel;
FourKind_lbl: TLabel;
FullHouse_lbl: TLabel;
SmlStraight_lbl: TLabel;
LrgStraight_lbl: TLabel;
Yahtzee_lbl: TLabel;
Chance_lbl: TLabel;
Bonus_lbl: TLabel;
FinalLowScr_lbl: TLabel;
FinalScr_lbl: TLabel;
RollsLeft_lbl: TLabel;
Rolls_lbl: TLabel;
Ones_Btn: TButton;
Twos_Btn: TButton;
Threes_Btn: TButton;
Fours_Btn: TButton;
Fives_Btn: TButton;
Sixes_Btn: TButton;
UpperSection_lbl: TLabel;
LowerSection_lbl: TLabel;
Side2_Img: TImage;
Side3_Img: TImage;
Side4_Img: TImage;
Side5_Img: TImage;
Side6_Img: TImage;
Side1_Img: TImage;
ThreeKind_Btn: TButton;
FourKind_Btn: TButton;
FullHouse_Btn: TButton;
SmlStraight_Btn: TButton;
LrgStraight_Btn: TButton;
Yahtzee_Btn: TButton;
Chance_Btn: TButton;
UpScr1Cap_lbl: TLabel;
BonusScrCap_lbl: TLabel;
UpScr2Cap_lbl: TLabel;
BonusCap_lbl: TLabel;
FinalLowScrCap_lbl: TLabel;
FinalScrCap_lbl: TLabel;
MainMenu1: TMainMenu;
MainMenu_lbl: TMenuItem;
Game_lbl: TMenuItem;
EndGame_lbl: TMenuItem;
Leaderboard_lbl: TMenuItem;
Help_lbl: TMenuItem;
Settings_lbl: TMenuItem;
Instructions_lbl: TMenuItem;
About_lbl: TMenuItem;
Exit_lbl: TMenuItem;
Image1: TImage;
RollsValue_lbl: TLabel;
TurnsLeft_lbl: TLabel;
TurnsValue_lbl: TLabel;
procedure EndGame_lblClick(Sender: TObject);
procedure Exit_lblClick(Sender: TObject);
procedure MainMenu_lblClick(Sender: TObject);
procedure Roll_BtnClick(Sender: TObject);
procedure Hold1_ChkClick(Sender: TObject);
procedure Hold2_ChkClick(Sender: TObject);
procedure Hold3_ChkClick(Sender: TObject);
procedure Hold4_ChkClick(Sender: TObject);
procedure Hold5_ChkClick(Sender: TObject);
procedure Ones_BtnClick(Sender: TObject);
procedure Twos_BtnClick(Sender: TObject);
procedure Threes_BtnClick(Sender: TObject);
procedure Fours_BtnClick(Sender: TObject);
procedure Fives_BtnClick(Sender: TObject);
procedure Sixes_BtnClick(Sender: TObject);
procedure Chance_BtnClick(Sender: TObject);
procedure Yahtzee_BtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;
//global variables
RollsLeft : integer;
TurnsLeft : integer;
Dice1 : integer;
Dice2 : integer;
Dice3 : integer;
Dice4 : integer;
Dice5 : integer;
FinalScore : integer;
FinalLowScore : integer;
FinalUpScore : integer;
BonusScore : integer;
UpScore : integer;
Hold1 : boolean;
Hold2 : boolean;
Hold3 : boolean;
Hold4 : boolean;
Hold5 : boolean;
Path : string;


implementation

{$R *.dfm}

uses
Settings, Leaderboard, Instructions, Game_Over, Main_Menu, About;

procedure start_up;
begin
RollsLeft := 3;
TurnsLeft := 13;
path := 'images\';
FinalScore := 0;
FinalLowScore := 0;
BonusScore := 0;
UpScore := 0;
end;

procedure TForm2.Exit_lblClick(Sender: TObject);
begin
Close;
end;

procedure TForm2.Hold1_ChkClick(Sender: TObject);
begin
Hold1 := true
end;

procedure TForm2.Hold2_ChkClick(Sender: TObject);
begin
Hold2 := true
end;

procedure TForm2.Hold3_ChkClick(Sender: TObject);
begin
Hold3 := true
end;

procedure TForm2.Hold4_ChkClick(Sender: TObject);
begin
Hold4 := true
end;

procedure TForm2.Hold5_ChkClick(Sender: TObject);
begin
Hold5 := true
end;

procedure TForm2.MainMenu_lblClick(Sender: TObject);
begin
Main_Menu.Form1.Show;
Yahtzee_Game.Form2.Hide;
end;

procedure TForm2.Ones_BtnClick(Sender: TObject);
var
OnesScore : integer;
begin
if RollsLeft < 3 then
begin
Ones_Btn.Enabled := false; //disable button
TurnsLeft := TurnsLeft - 1; //lower Turns Left
RollsLeft := 3; //reset rolls
OnesScore := 0;
if Dice1 = 1 then
OnesScore := OnesScore +1;
if Dice2 = 1 then
OnesScore := OnesScore +1;
if Dice3 = 1 then
OnesScore := OnesScore +1;
if Dice4 = 1 then
OnesScore := OnesScore +1;
if Dice5 = 1 then
OnesScore := OnesScore +1;
UpScore := UpScore + OnesScore;
If UpScore >= 63 then
BonusScore := 35;
FinalUpScore := UpScore + BonusScore;
FinalScore := FinalUpScore + FinalLowScore;
end;
end;

procedure TForm2.Twos_BtnClick(Sender: TObject);
var
TwosScore : integer;
begin
if RollsLeft < 3 then
begin
Twos_Btn.Enabled := false; //disable button
TurnsLeft := TurnsLeft - 1; //lower Turns Left
RollsLeft := 3; //reset rolls
TwosScore := 0;
if Dice1 = 2 then
TwosScore := TwosScore +2;
if Dice2 = 2 then
TwosScore := TwosScore +2;
if Dice3 = 2 then
TwosScore := TwosScore +2;
if Dice4 = 2 then
TwosScore := TwosScore +2;
if Dice5 = 2 then
TwosScore := TwosScore +2;
UpScore := UpScore + TwosScore;
If UpScore >= 63 then
BonusScore := 35;
FinalUpScore := UpScore + BonusScore;
FinalScore := FinalUpScore + FinalLowScore;
end;
end;

procedure TForm2.Yahtzee_BtnClick(Sender: TObject);
var YahtzeeScore : integer;
begin
if RollsLeft < 3 then
begin
Yahtzee_Btn.Enabled := false;
TurnsLeft := TurnsLeft - 1;
RollsLeft := 3;
if (Dice1 = Dice2) and (Dice1 = Dice3) and (Dice1 = Dice4) and (Dice1 = Dice5)
then YahtzeeScore := 50;
FinalLowScore := finalLowScore + YahtzeeScore;
FinalScore := FinalLowScore + FinalUpScore;
end;
end;

procedure TForm2.Threes_BtnClick(Sender: TObject);
var
ThreesScore : integer;
begin
if RollsLeft < 3 then
begin
Threes_Btn.Enabled := false; //disable button
TurnsLeft := TurnsLeft - 1; //lower Turns Left
RollsLeft := 3; //reset rolls
ThreesScore := 0;
if Dice1 = 3 then
ThreesScore := ThreesScore +3;
if Dice2 = 3 then
ThreesScore := ThreesScore +3;
if Dice3 = 3 then
ThreesScore := ThreesScore +3;
if Dice4 = 3 then
ThreesScore := ThreesScore +3;
if Dice5 = 3 then
ThreesScore := ThreesScore +3;
UpScore := UpScore + ThreesScore;
If UpScore >= 63 then
BonusScore := 35;
FinalUpScore := UpScore + BonusScore;
FinalScore := FinalUpScore + FinalLowScore;
end;
end;

procedure TForm2.Fours_BtnClick(Sender: TObject);
var
FoursScore : integer;
begin
if RollsLeft < 3 then
begin
Fours_Btn.Enabled := false; //disable button
TurnsLeft := TurnsLeft - 1; //lower Turns Left
RollsLeft := 3; //reset rolls
FoursScore := 0;
if Dice1 = 4 then
FoursScore := FoursScore +4;
if Dice2 = 4 then
FoursScore := FoursScore +4;
if Dice3 = 4 then
FoursScore := FoursScore +4;
if Dice4 = 4 then
FoursScore := FoursScore +4;
if Dice5 = 4 then
FoursScore := FoursScore +4;
UpScore := UpScore + FoursScore;
If UpScore >= 63 then
BonusScore := 35;
FinalUpScore := UpScore + BonusScore;
FinalScore := FinalUpScore + FinalLowScore;
end;
end;

procedure TForm2.Fives_BtnClick(Sender: TObject);
var
FivesScore : integer;
begin
if RollsLeft < 3 then
begin
Fives_Btn.Enabled := false; //disable button
TurnsLeft := TurnsLeft - 1; //lower Turns Left
RollsLeft := 3; //reset rolls
FivesScore := 0;
if Dice1 = 5 then
FivesScore := FivesScore +5;
if Dice2 = 5 then
FivesScore := FivesScore +5;
if Dice3 = 5 then
FivesScore := FivesScore +5;
if Dice4 = 5 then
FivesScore := FivesScore +5;
if Dice5 = 5 then
FivesScore := FivesScore +5;
UpScore := UpScore + FivesScore;
If UpScore >= 63 then
BonusScore := 35;
FinalUpScore := UpScore + BonusScore;
FinalScore := FinalUpScore + FinalLowScore;
end;
end;

procedure TForm2.Sixes_BtnClick(Sender: TObject);
var
SixesScore : integer;
begin
if RollsLeft < 3 then
begin
Ones_Btn.Enabled := false; //disable button
TurnsLeft := TurnsLeft - 1; //lower Turns Left
RollsLeft := 3; //reset rolls
SixesScore := 0;
if Dice1 = 6 then
SixesScore := SixesScore +6;
if Dice2 = 6 then
SixesScore := SixesScore +6;
if Dice3 = 6 then
SixesScore := SixesScore +6;
if Dice4 = 6 then
SixesScore := SixesScore +6;
if Dice5 = 6 then
SixesScore := SixesScore +6;
UpScore := UpScore + SixesScore;
If UpScore >= 63 then
BonusScore := 35;
FinalUpScore := UpScore + BonusScore;
FinalScore := FinalUpScore + FinalLowScore;
end;
end;

procedure TForm2.Roll_BtnClick(Sender: TObject);
var
tmp : string;
begin
if RollsLeft > 0 then //if not the beginning of a turn
begin
begin
if Hold1 = false then
Dice1 := 1
end;
begin
if Hold2 = false then
Dice2 := Random(6)+1
end;
begin
if Hold3 = false then
Dice3 := Random(6)+1
end;
begin
if Hold4 = false then
Dice4 := Random(6)+1
end;
begin
if Hold5 = false then
Dice5 := Random(6)+1
end;
end;
//Display Dice faces
begin
tmp := 'images\' + Inttostr(Dice1) + '.jpg';
Die1_Img.Picture.LoadFromFile(tmp);
tmp:= 'images\' + Inttostr(Dice2) + '.jpg';
Die2_Img.Picture.LoadFromFile(tmp);
tmp := 'images\' + Inttostr(Dice3) + '.jpg';
Die3_Img.Picture.LoadFromFile(tmp);
tmp := 'images\' + Inttostr(Dice4) + '.jpg';
Die4_Img.Picture.LoadFromFile(tmp);
tmp := 'images\' + Inttostr(Dice5) + '.jpg';
Die5_Img.Picture.LoadFromFile(tmp);
end;
begin
Die1_lbl.Caption := InttoStr(Dice1);
Die2_lbl.Caption := InttoStr(Dice2);
Die3_lbl.Caption := InttoStr(Dice3);
Die4_lbl.Caption := InttoStr(Dice4);
Die5_lbl.Caption := InttoStr(Dice5);
end;
begin
RollsLeft := RollsLeft - 1; //subtract Rolls in the turn
RollsValue_lbl.Caption := Inttostr(RollsLeft);
If RollsLeft = 0 then
Roll_Btn.Enabled := false //disable roll button
end;
end;

procedure TForm2.Chance_BtnClick(Sender: TObject);
var
ChanceScore : integer;
begin
if RollsLeft < 3 then
begin
Chance_Btn.Enabled := false;
TurnsLeft := TurnsLeft - 1; //lower Turns Left
RollsLeft := 3; //reset rolls
ChanceScore := Dice1 + Dice2 + Dice3 + Dice4 + Dice5;
FinalLowScore := FinalLowScore + ChanceScore;
FinalScore := FinalLowScore + FinalUpScore;
end;
end;

procedure TForm2.EndGame_lblClick(Sender: TObject);
begin
Game_Over.Form6.Show;
Yahtzee_Game.Form2.Hide;
end;

end.


1