unit Question3TripleTTyresU;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids, ExtCtrls, Buttons,
  jpeg;

type
  TfrmBonanzaQuestion = class(TForm)
    pnlMain: TPanel;
    Label1: TLabel;
    lstPrizes: TListBox;
    Label2: TLabel;
    strgPrizePlay: TStringGrid;
    Label3: TLabel;
    imgPrizePic: TImage;
    Label4: TLabel;
    Image1: TImage;
    BitBtn1: TBitBtn;
    Label5: TLabel;
    Label6: TLabel;
    edtRow: TEdit;
    edtCol: TEdit;
    btnDraw: TButton;
    Label7: TLabel;
    procedure FormShow(Sender: TObject);
    procedure btnDrawClick(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }


  end;

var
  frmBonanzaQuestion: TfrmBonanzaQuestion;
  var arrPrizeGrid : array [1..10,1..10] of integer;

implementation

{$R *.dfm}



procedure TfrmBonanzaQuestion.btnDrawClick(Sender: TObject);
 var
   go : boolean;
   col, code : integer;
   R , C : Integer;
begin
   edtRow.Text := UpperCase(edtRow.Text);
   edtRow.Text := Trim(edtRow.Text);
   edtCol.Text := Trim(edtCol.Text);

   go := false;

   if ((edtRow.Text[1] >= 'A')  and  (edtRow.Text[1] <= 'Z')) then
     begin
        go := true;
        val(edtCol.Text,col,code);
        if (code = 0) then
         begin
           if ((col >= 1)  and  (col <= 10)) then
             begin
               go := true;
             end
           else
             begin
               ShowMessage('You must enter an 1 to 10 for the column');
               go := false;
               edtCol.Clear;
               edtCol.SetFocus;
             end;
         end
        else
         begin
               ShowMessage('You must enter an 1 to 10 for the column');
               go := false;
               edtCol.Clear;
               edtCol.SetFocus;
        end;
     end
   else
     begin
       ShowMessage('You must enter an A to Z for the row');
       edtRow.Clear;
       go := false;
       edtRow.SetFocus;
     end;

  if go then
    begin

      case (edtRow.Text[1]) of    // could have used ord   (better)
       'A': R := 1;
       'B': R := 2;
       'C': R := 3;
       'D': R := 4;
       'E': R := 5;
       'F': R := 6;
       'G': R := 7;
       'H': R := 8;
       'I': R := 9;
       'J': R := 10;
      end;

      C := StrToInt(edtCol.Text);

      if (strgPrizePlay.Cells[C,R] <> '?') then
        begin
          ShowMessage('That block has already been played');
        end
      else
        begin
          if (arrPrizeGrid[R,C] > 0) then // won prize
            begin
              imgPrizePic.Picture.LoadFromFile(lstPrizes.Items[arrPrizeGrid[R,C]-1] + '.bmp');
              ShowMessage('Congratulations!! You won a ' + lstPrizes.Items[arrPrizeGrid[R,C]-1]);
              arrPrizeGrid[R,C] := 0;
              imgPrizePic.Refresh;
              strgPrizePlay.Cells[C,R] := 'W';
              Sleep(2000);
              imgPrizePic.Picture.LoadFromFile('qmark.bmp');
            end
          else
            begin
              strgPrizePlay.Cells[C,R] := 'X';
              imgPrizePic.Picture.LoadFromFile('noprize.bmp');
              imgPrizePic.Refresh;
              ShowMessage('Sjaaim - No Prize today (Better Luck Next Time)');
              Sleep(1500);
              imgPrizePic.Picture.LoadFromFile('qmark.bmp');
            end;
        end;

    end;

end;

procedure TfrmBonanzaQuestion.FormShow(Sender: TObject);
var
  R , C : Integer;
  L : char;
  placer : integer;
begin
 // given code
  strgPrizePlay.FixedCols := 1;
  strgPrizePlay.FixedRows := 1;
  strgPrizePlay.RowCount := 11;
  strgPrizePlay.ColCount := 11;
  strgPrizePlay.DefaultColWidth := 26;
  strgPrizePlay.DefaultRowHeight := 26;
  edtRow.Text := '';
  edtCol.Text := '';
 // code from here



 // Fill the listbox with the prizes
 lstPrizes.Items.LoadFromFile('Prizes.txt');

 // Write the code to add the headings to the stringgrid
 // using a loop
 // Fill the arrPrizeGrid elements with all 0's and set all the white cells
 // (non fixed cells) of the stringgrid to  '?'
 L := 'A';
 for R := 1 to 10 do
   begin
     strgPrizePlay.Cells[0,R] :=  L;
     strgPrizePlay.Cells[R,0] :=  IntToStr(R);
     inc(L);
     for C  := 1 to 10 do
       begin
         arrPrizeGrid[R,C] := 0;
         strgPrizePlay.Cells[C,R] := '?';
       end;
   end;

// Load picture

  imgPrizePic.Picture.LoadFromFile('qmark.bmp');

// write the code to randomly place the values 1 to 10
// in the arrPrizeGrid, ensure that the value apear only
// once and that all the values are placed

   placer := 1;
   Randomize; //oops ;)   amper  vergeet....

   while placer < 11 do
     begin
       R := Random(10) + 1;
       C := Random(10) + 1;
       while (arrPrizeGrid[R,C] > 0) do
         begin
            R := Random(10) + 1;
            C := Random(10) + 1;
            
         end;
         arrPrizeGrid[R,C] := placer;
         //net om te sien of dit werk
         //strgPrizePlay.Cells[C,R] := '#';
         inc(placer);
     end;

end;

end.


