//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } int dicevalue; int heightit[12]; //--------------------------------------------------------------------------- void cleargraph(){ for (int i = 0; i <= 12; i++) { heightit[i] = 2; } } int throwdice(void){ //rand() generates random number between 0 and ~32k. Divide by 6 and add // 1 to get equivalent dice value return (rand()%6 + 1 + rand()%6 + 1); } void display(int dice){ //display dice value in our TEdit Box Form1->EditDice->Text = dice; //if we roll a 2 display snake eyes, if 12 display box cars if (dice == 2) { Form1->Label1->Caption="Snake-Eyes!"; } if (dice == 12) { Form1->Label1->Caption="Box-Cars!!"; } } void run(){ //reset our values dicevalue = 0; Form1->EditDice->Text = 0; Form1->Label1->Caption = ""; //call throwdice and then display dicevalue = throwdice(); display(dicevalue); //increase our iterator for the value rolled heightit[dicevalue]++; //draw a point on graph Form1->PaintBox1->Canvas->Pixels[dicevalue * 18][136 - heightit[dicevalue] * 2] = clBlack; } void __fastcall TForm1::ButtonDiceClick(TObject *Sender) { //when user rolls dice, call run run(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { //re-seed rand() function randomize(); //clear forms when program opens Form1->EditDice->Text = 0; Form1->Label1->Caption = ""; cleargraph(); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1Paint(TObject *Sender) { //draw a border around our canvas Form1->PaintBox1->Canvas->MoveTo(0, 0); Form1->PaintBox1->Canvas->LineTo(251, 0); Form1->PaintBox1->Canvas->LineTo(251,136); Form1->PaintBox1->Canvas->LineTo(0, 136); Form1->PaintBox1->Canvas->LineTo(0,0); } //---------------------------------------------------------------------------