sábado, 26 de outubro de 2019

C++ builder - Jogo da velha

Programando um jogo da velha
no C++ builder da Embarcadero.
Dois jogadores.



//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstopFlag
#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int vet [ 3 ] [ 3 ];
int w;
bool b = false;
bool c = false;
int a = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
: TForm ( Owner )
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::grade ( TObject *Sender ) {
    int x = 0, y = 0;
   for ( x = 3; x < 6; x++ ) {
      for ( y = 1; y < 4; y++ ) {
            Canvas -> Pen -> Width = 5;
            Canvas -> Pen -> Color = clRed;
            Canvas -> Rectangle ( x * 70, y * 70, x * 70 + 70, y * 70 + 70 );
      }
   }
   Enabled ( Sender );

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
   w++;
   a++;
    if ( w % 2 == 1 ) {
     Button1 -> Caption = "X";
     Button1 -> Enabled = false;
     vet [ 0 ] [ 0 ] = 4;
    } else {
     Button1 -> Caption = "O";
     Button1 -> Enabled = false;
     vet [ 0 ] [ 0 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    w++;
    a++;
    if ( w % 2 == 1 ) {
     Button2 -> Caption = "X";
     Button2 -> Enabled = false;
     vet [ 0 ] [ 1 ] = 4;
    } else {
     Button2 -> Caption = "O";
     Button2 -> Enabled = false;
      vet [ 0 ] [ 1 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click ( TObject *Sender )
{
   w++;
   a++;
    if ( w % 2 == 1 ) {
     Button3 -> Caption = "X";
     Button3 -> Enabled = false;
      vet [ 0 ] [ 2 ] = 4;
    } else {
     Button3 -> Caption = "O";
     Button3 -> Enabled = false;
     vet [ 0 ] [ 2 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
    w++;
    a++;
    if ( w % 2 == 1 ) {
     Button4 -> Caption = "X";
     Button4 -> Enabled = false;
     vet [ 1 ] [ 0 ] = 4;
    } else {
     Button4 -> Caption = "O";
     Button4 -> Enabled = false;
      vet [ 1 ] [ 0 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
    w++;
    a++;
    if ( w % 2 == 1 ) {
     Button5 -> Caption = "X";
     Button5 -> Enabled = false;
      vet [ 1 ] [ 1 ] = 4;
    } else {
     Button5 -> Caption = "O";
      Button5 -> Enabled = false;
      vet [ 1 ] [ 1 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
    w++;
    a++;
    if ( w % 2 == 1 ) {
     Button6 -> Caption = "X";
     Button6 -> Enabled = false;
      vet [ 1 ] [ 2 ] = 4;
    } else {
     Button6 -> Caption = "O";
     Button6 -> Enabled = false;
     vet [ 1 ] [ 2 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
    w++;
    a++;
    if ( w % 2 == 1 ) {
     Button7 -> Caption = "X";
     Button7 -> Enabled = false;
     vet [ 2 ] [ 0 ] = 4;
    } else {
     Button7 -> Caption = "O";
     Button7 -> Enabled = false;
     vet [ 2 ] [ 0 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
    w++;
    a++;
    if ( w % 2 == 1 ) {
     Button8 -> Caption = "X";
     Button8 -> Enabled = false;
     vet [ 2 ] [ 1 ] = 4;
    } else {
     Button8 -> Caption = "O";
     Button8 -> Enabled = false;
     vet [ 2 ] [ 1 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click(TObject *Sender)
{
    w++;
    a++;
    if ( w % 2 == 1 ) {
     Button9 -> Caption = "X";
     Button9 -> Enabled = false;
     vet [ 2 ] [ 2 ] = 4;
    } else {
     Button9 -> Caption = "O";
     Button9 -> Enabled = false;
     vet [ 2 ] [ 2 ] = 3;
    }
    Label2 -> Caption = IntToStr ( w );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Testa_Vencedores_Empates ( TObject *Sender ) {
     String str_1 = " ";
     String str_2 = " ";
    int i, j, lin = 0, col = 0, diag_1 = 0, diag_2 = 0, resp = 0;
        //Diagonal secundária
        for ( j = 0; j < 3; j++ ) {
            for ( i = 0; i < 3; i++ )
                if ( i + j == 2 )
                diag_1 += vet [ i ] [ j ];
               resp = diag_1;
        }

        str_1 += " ";
        str_1 +=  diag_1;
        for ( i = 0; i < 3; i++ ) {
            for ( j = 0; j < 3; j++ )
                lin += vet [ i ] [ j ];
                resp = diag_1 + lin;
            str_1 += " ";
            str_1 +=  lin;
            lin = 0;
        }

        //Diagonal primária
        for ( i = 0; i < 3; i++ ) {
        for ( j = 0; j < 3; j++ )
            if ( i == j )
                diag_2 += vet [ j ] [ j ];
                resp = diag_2 + diag_1 + lin;
        }
        str_1 += " ";
        str_1 +=  diag_2;

        for ( i = 0; i < 3; i++ ) {
            for ( j = 0; j < 3; j++ )
                col += vet [ j ] [ i ];
                resp = col + diag_2 + diag_1 + lin;
            str_1 += " ";
            str_1 +=  col;
           col = 0;
        }
            str_2 += resp;
        if ( str_1.Pos("12") ) {
           Label1 -> Caption = "Jogador 1 ganhou o jogo";
              NoEnabled ( Sender );
              c = true;
        }
        if ( str_1.Pos("9") ) {
           Label1 -> Caption = "Jogador 2 ganhou o jogo";
              NoEnabled ( Sender );
              c = true;
        }
        if ( a == 9 && c == false ) {
           Label1 -> Caption = "Ninguém ganhou o jogo";
              NoEnabled ( Sender );
              c = true;
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NoEnabled ( TObject *Sender )
{
       Button1 -> Enabled = false;
       Button2 -> Enabled = false;
       Button3 -> Enabled = false;
       Button4 -> Enabled = false;
       Button5 -> Enabled = false;
       Button6 -> Enabled = false;
       Button7 -> Enabled = false;
       Button8 -> Enabled = false;
       Button9 -> Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Enabled ( TObject *Sender )
{
       Button1 -> Enabled = true;
       Button2 -> Enabled = true;
       Button3 -> Enabled = true;
       Button4 -> Enabled = true;
       Button5 -> Enabled = true;
       Button6 -> Enabled = true;
       Button7 -> Enabled = true;
       Button8 -> Enabled = true;
       Button9 -> Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NoVisible ( TObject *Sender )
{
       Button1 -> Visible = false;
       Button2 -> Visible = false;
       Button3 -> Visible = false;
       Button4 -> Visible = false;
       Button5 -> Visible = false;
       Button6 -> Visible = false;
       Button7 -> Visible = false;
       Button8 -> Visible = false;
       Button9 -> Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Visible ( TObject *Sender )
{
       Button1 -> Visible = true;
       Button2 -> Visible = true;
       Button3 -> Visible = true;
       Button4 -> Visible = true;
       Button5 -> Visible = true;
       Button6 -> Visible = true;
       Button7 -> Visible = true;
       Button8 -> Visible = true;
       Button9 -> Visible = true;
}
//---------------------------------------------------------------------------
 void __fastcall TForm1::Button_Clear ( TObject *Sender )
  {
       Button1 -> Caption = " ";
       Button2 -> Caption = " ";
       Button3 -> Caption = " ";
       Button4 -> Caption = " ";
       Button5 -> Caption = " ";
       Button6 -> Caption = " ";
       Button7 -> Caption = " ";
       Button8 -> Caption = " ";
       Button9 -> Caption = " ";
       Label1 -> Caption = " ";
       Label2 -> Caption = " ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button11Click(TObject *Sender)
{
exit ( 0 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
       Button_Clear ( Sender );
       w = 0;
       a = 0;
       c = false;
        int i, j;
        for ( i = 0; i < 3; i++ ) {
            for ( j = 0; j < 3; j++ ){
                vet [ j ] [ i ] = '\0';
        }
     }
      grade ( Sender );
      Enabled ( Sender );
      Visible ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint ( TObject *Sender )
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clBlue;
    Canvas -> Rectangle ( 05, 05, 595, 320 );
    Canvas -> Font -> Size = 16;
    Canvas -> Font -> Name = "arial";
    Canvas -> Font -> Color = clRed;
    Canvas -> Font -> Style = TFontStyles ( ) << fsBold  ;
    Canvas -> TextOut ( 150, 12, "C++ BUILDER - JOGO DA VELHA" );
    if ( b == false ) {
    NoVisible ( Sender );
    NoEnabled ( Sender );
    b = true;
    }
    Testa_Vencedores_Empates ( Sender );
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Vcl.Graphics.hpp>
#include <Vcl.Grids.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
    TButton *Button2;
    TButton *Button3;
    TButton *Button4;
    TButton *Button5;
    TButton *Button6;
    TButton *Button7;
    TButton *Button8;
    TButton *Button9;
    TLabel *Label1;
    TLabel *Label2;
    TButton *Button10;
    TButton *Button11;
    void __fastcall grade ( TObject *Sender );
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
    void __fastcall Button4Click(TObject *Sender);
    void __fastcall Button5Click(TObject *Sender);
    void __fastcall Button6Click(TObject *Sender);
    void __fastcall Button7Click(TObject *Sender);
    void __fastcall Button8Click(TObject *Sender);
    void __fastcall Button9Click(TObject *Sender);
    void __fastcall Testa_Vencedores_Empates ( TObject *Sender );
    void __fastcall FormPaint(TObject *Sender);
    void __fastcall NoEnabled ( TObject *Sender );
    void __fastcall Enabled ( TObject *Sender );
    void __fastcall NoVisible ( TObject *Sender );
    void __fastcall Visible ( TObject *Sender );
    void __fastcall Button11Click(TObject *Sender);
    void __fastcall Button10Click(TObject *Sender);
    void __fastcall Button_Clear ( TObject *Sender );
private:    // User declarations
public:        // User declarations
    __fastcall TForm1 ( TComponent* Owner );

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 470
  Top = 162
  Caption = 'C++ BUILDER - JOGO DA VELHA'
  ClientHeight = 325
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 192
    Top = 287
    Width = 6
    Height = 23
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 40
    Top = 40
    Width = 6
    Height = 23
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Button1: TButton
    Left = 216
    Top = 75
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 284
    Top = 75
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 352
    Top = 75
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 2
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 216
    Top = 145
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 3
    OnClick = Button4Click
  end
  object Button5: TButton
    Left = 284
    Top = 145
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 4
    OnClick = Button5Click
  end
  object Button6: TButton
    Left = 352
    Top = 145
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 5
    OnClick = Button6Click
  end
  object Button7: TButton
    Left = 216
    Top = 213
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 6
    OnClick = Button7Click
  end
  object Button8: TButton
    Left = 284
    Top = 213
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 7
    OnClick = Button8Click
  end
  object Button9: TButton
    Left = 352
    Top = 213
    Width = 62
    Height = 62
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -29
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 8
    OnClick = Button9Click
  end
  object Button10: TButton
    Left = 24
    Top = 288
    Width = 75
    Height = 25
    Caption = 'In'#237'cio'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 9
    OnClick = Button10Click
  end
  object Button11: TButton
    Left = 504
    Top = 288
    Width = 75
    Height = 25
    Caption = 'Sair'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 10
    OnClick = Button11Click
  end
end

quinta-feira, 24 de outubro de 2019

C++ builder - Par ou Ímpar?

Escolhendo uma das duas opções em
dois RadioButtons, que são Par ou ìmpar,
este programinha roda aleatoriamente,
num tempo determinado pelo evento OnTimer,
um contador numa na faixa muito ampla,
e no término, isto é, quando o botão parar
é pressionado ele informa se o último número
é par ou ímpar, indicando num label se o usuário
acertou ou não na escolha.



//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int i;
bool a = false;
bool b = false;
String str_1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
   : TForm ( Owner )
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Verifica_Par_Impar ( TObject *Sender ) {
      if (  ( i % 2 ) == 0 ) {
         Edit1 -> Clear ( );
         Form1 -> Edit1 -> Text = "Par";
     }
     else {
        Edit1 -> Clear ( );
        Form1 -> Edit1 -> Text = "Ímpar";
    }
    if ( str_1 == Edit1 -> Text ) {
            Label2 -> Caption = "Acertou";
    }
    else {
     Label2 -> Caption = "Errou";
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Sorteia_Par_Impar ( TObject *Sender ) {
     for ( i = 0; i <= random ( 10000 ); i++ ) {
         Form1 -> Edit1 -> Text = i;
     }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click ( TObject *Sender )
{
          Label2 -> Caption = " ";
          if ( b == false ) {
           Timer1 -> Interval = 0;
           Label2 -> Caption = "Escolha Par ou Ímpar";
          }
          Timer1 -> Interval = 100;
          a = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click ( TObject *Sender )
{        Label2 -> Caption = " ";
    if ( a == true && b == true ) {
      Timer1 -> Interval = 0;
      Verifica_Par_Impar ( Sender );
      }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
    if ( a == true && b == true ) {
    Sorteia_Par_Impar ( Sender );
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clRed;
    Canvas -> Rectangle ( 05, 05, 595, 295 );
    Canvas -> Font -> Size = 14;
    Canvas -> Font -> Name = "arial";
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 170, 10, "C++ BUILDER - PAR OU ÍMPAR" );
}
//---------------------------------------------------------------------------
//Par
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
Label2 -> Caption = " ";
str_1 = "Par";
b = true;
}
//---------------------------------------------------------------------------
//Ímpar
void __fastcall TForm1::RadioButton2Click(TObject *Sender)
{
Label2 -> Caption = " ";
str_1 = "Ímpar";
b = true;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TTimer *Timer1;
    TButton *Button1;
    TEdit *Edit1;
    TButton *Button2;
    TBevel *Bevel1;
    TLabel *Label1;
    TRadioButton *RadioButton1;
    TRadioButton *RadioButton2;
    TLabel *Label2;

    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall Timer1Timer(TObject *Sender);
    void __fastcall FormPaint(TObject *Sender);
    void __fastcall RadioButton1Click(TObject *Sender);
    void __fastcall RadioButton2Click(TObject *Sender);
    void __fastcall Sorteia_Par_Impar ( TObject *Sender );
    void __fastcall Verifica_Par_Impar ( TObject *Sender );

private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 342
  Top = 165
  Caption = 'C++ BUILDER - PAR OU '#205'MPAR'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  PixelsPerInch = 600
  TextHeight = 13
  object Bevel1: TBevel
    Left = 200
    Top = 40
    Width = 210
    Height = 209
  end
  object Label1: TLabel
    Left = 248
    Top = 51
    Width = 134
    Height = 23
    Caption = 'PAR OU '#205'MPAR?'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label2: TLabel
    Left = 248
    Top = 176
    Width = 4
    Height = 16
    Color = clBlue
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentColor = False
    ParentFont = False
  end
  object Button1: TButton
    Left = 216
    Top = 211
    Width = 75
    Height = 25
    Caption = 'In'#237'ciar'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 257
    Top = 129
    Width = 94
    Height = 41
    Color = clOlive
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -27
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
  end
  object Button2: TButton
    Left = 319
    Top = 211
    Width = 75
    Height = 25
    Caption = 'Parar'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 2
    OnClick = Button2Click
  end
  object RadioButton1: TRadioButton
    Left = 248
    Top = 80
    Width = 43
    Height = 17
    Caption = 'Par'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 3
    OnClick = RadioButton1Click
  end
  object RadioButton2: TRadioButton
    Left = 248
    Top = 103
    Width = 57
    Height = 17
    Caption = #205'mpar'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 4
    OnClick = RadioButton2Click
  end
  object Timer1: TTimer
    Interval = 100
    OnTimer = Timer1Timer
    Left = 512
    Top = 32
  end
end

sábado, 19 de outubro de 2019

C++ builder - console Application

Para quem deseja usar as funções da conio2.h,
e acaba tendo dificuldade para instalar,
seus problemas podem ser resolvidos
utilizando Rad Studio da Embarcadero,
basta criar um projeto console Application,
e testar o exemplo passado abaixo:





#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void Janela ( ) {
     int l, c;
     for ( l = 1; l <= 30 ; l++ ) {
         for ( c = 1; c < 80 ; c++ ) {
              gotoxy ( c , l );
              if ( l == 2 ) {
                   cprintf ( " " );
              }
              if ( c == 1 ) {
                   cprintf ( "  " );
              }
              if ( c == 79 ) {
                   textattr ( 200 );
                   cprintf ( "  " );
              }
         }
     }
}
struct Titulos {
     char *titulos;
};
struct Escola {
     char *nomes;
     int idade;
     float notas;
};
float res = 0;
int main ( ) {
     system ( "title MATRIZ DE STRUCT" );
     Janela ( );
     textbackground ( BLACK );
     int i, j;
     struct Titulos Titu_los [ 4 ] [ 6 ] =
             { {"Ordem "  },
              { "Nomes  "  },
              { "Idades "  },
              { "Notas  " }};
     struct Escola Alunos [ 10 ] [ 13 ] =
             { { "Ana Celia    ", 19, 10.0},
              { "Eder Costa     ", 21, 9.9 },
              { "Humberto Gomes ", 18, 9.7 },
              { "Dijalma Lacerda", 22, 9.5 },
              { "Caroline Silva ", 18, 9.3 },
              { "Igor Goncalves ", 21, 9.0 },
              { "Bruna Carla    ", 19, 8.9 },
              { "Fabio Quadros  ", 22, 8.5 },
              { "Geany Barros   ", 19, 8.4 },
              { "Jaqueline Vega ", 22, 8.0 } };
     textcolor ( LIGHTRED );
     gotoxy ( 33 , 3 );
     cprintf ( "MATRIZ DE STRUCT" );
     j = 0;
     textcolor ( LIGHTBLUE );
     gotoxy ( 18 , 5 );
     cprintf ( "%2s" , Titu_los [ 0 ] [ j ].titulos );
     gotoxy ( 28 , 5 );
     cprintf ( "%2s" , Titu_los [ 1 ] [ j ].titulos );
     gotoxy ( 43 , 5 );
     cprintf ( "%2s" , Titu_los [ 2 ] [ j ].titulos );
     gotoxy ( 54 , 5 );
     cprintf ( "%2s" , Titu_los [ 3 ] [ j ].titulos );
     for ( i = 0; i < 10 ; i++ ) {
         textcolor ( LIGHTRED );
         gotoxy ( 20 , i + 7 );
         cprintf ( "%d°" , i + 1 );
         j = 0;
         textcolor ( YELLOW );
         gotoxy ( 25 , i + 7 );
         cprintf ( "%2s" , Alunos [ i ] [ j ].nomes );
         gotoxy ( 45 , i + 7 );
         textcolor ( LIGHTRED );
         cprintf ( "%d" , Alunos [ i ] [ j ].idade );
         textcolor ( LIGHTRED );
         gotoxy ( 55 , i + 7 );
         cprintf ( "%0.1f" , Alunos [ i ] [ j ].notas );
         res =  res + Alunos [ i ] [ j ].notas;
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 19 );
     Sleep ( 1800 );
     cprintf ( "Por: " );
     textcolor ( LIGHTMAGENTA );
     cprintf ( "Samuel Lima" );
     textcolor ( LIGHTGREEN );
     gotoxy ( 20 , 21 );
     cprintf ( "sa_sp10@hotmail.com" );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 37 , 23 );
     cprintf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
}

Triturando e montando uma imagem

Inserimos uma imagem em um bitimap,
e dividimos em 312 quadrados de 20x20 pixels.
Criamos a função Posiciona_Aleatório ( void );
que posiciona cada pedacinho de nossa imagem
de forma aleatória dentro da janela,
por um tempo determinado pelo evento Ontimer,
auxiliado por uma variável inteira que serve
como contagem.
A matriz tritura ( [ i ] [ j ] ); está sendo
chamada de dentro de um switch com apenas um caso,
e quando a imagem está completa na tela,
o intervalo dos ciclos recebe 0 e o evento
Ontimer é finalizado. 



//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstopFlag
#include "Unit1.h"
#include <memory>
#define ALEATORIO 1

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool b = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
: TForm ( Owner )
{
}
//---------------------------------------------------------------------------
int tritura [ 28 ] [ 28 ];
int i, j;
int a = 0;
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 230, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 230, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 250, "sa_sp10@hotmail.com" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Tritura_Imagem ( TObject *Sender ) {
    std::auto_ptr < Graphics::TBitmap > BrushBmp_1 ( new Graphics::TBitmap );
    BrushBmp_1 -> LoadFromFile ( "C:\\Users\\ManMachine\\Downloads\\"
    "\\Triturando e remontando uma imagem\\Napa-Valley.bmp");
    Canvas -> Pen -> Width = 0;
   for ( i = 2; i < 28; i++ ) {
      for ( j = 2; j < 14; j++ ) {
         switch ( tritura [ i ] [ j ] ) {
            case ALEATORIO:
               Form1 -> Canvas -> Brush -> Bitmap = BrushBmp_1.get ( );
               Form1 ->    Canvas -> Rectangle ( i * 20, j * 20, i * 20 + 20, j * 20 + 20 );
               break;
         }
      }
   }
}
//----------------------------------------------------------------------
void Posiciona_Aleatorio ( void ) {
   // 28 posições em modo aleatório
   //srand ( time ( NULL ) );
   for ( int i = 0; i < 28; i++ ) {
      tritura [ random ( 28 ) ] [ random ( 28 ) ] = ALEATORIO;
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click ( TObject *Sender )
{
   if ( a == 180 ) {
      exit ( 0 );
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0, 255, 255 ) );
    Canvas -> Rectangle ( 05, 05, 595, 317 );
    Canvas -> Font -> Size = 16;
    Canvas -> Font -> Name = "arial";
    Canvas -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic << fsUnderline;
    SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
    Canvas -> TextOut ( 90, 12, "TRITURANDO E MONTANDO UMA IMAGEM" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
b = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if ( b == true ) {
     a++;
   Posiciona_Aleatorio ( );
   Tritura_Imagem ( Sender );
   Label1 -> Caption = StrToInt ( a );
   if ( a == 160 ) {
       Timer1 -> Interval = 0;
       a = 180;
       Informe ( Sender );
   }
   }
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <Vcl.Imaging.GIFImg.hpp>
#include <Vcl.Imaging.pngimage.hpp>
#include <Vcl.Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
    TButton *Button2;
    TTimer *Timer1;
    TLabel *Label1;
    void __fastcall Tritura_Imagem  ( TObject *Sender );
    void __fastcall Button2Click ( TObject *Sender );
    void __fastcall FormPaint ( TObject *Sender );
    void __fastcall Informe ( TObject *Sender );
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Timer1Timer(TObject *Sender);
private:    // User declarations
public:        // User declarations
        __fastcall TForm1 (TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


object Form1: TForm1
  Left = 441
  Top = 103
  Caption = 'TRITURANDO E MONTANDO U8MA IMAGEM'
  ClientHeight = 322
  ClientWidth = 600
  Color = clWhite
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  KeyPreview = True
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 290
    Top = 290
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Button1: TButton
    Left = 72
    Top = 284
    Width = 75
    Height = 25
    Caption = 'In'#237'cio'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 486
    Top = 284
    Width = 75
    Height = 25
    Caption = 'Sair'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 1
    OnClick = Button2Click
  end
  object Timer1: TTimer
    Interval = 100
    OnTimer = Timer1Timer
    Left = 544
    Top = 24
  end
end

quarta-feira, 16 de outubro de 2019

C++ builder - detectando colisão entre dois quadrados

Uma dos recursos mais importantes em jogos, 
é um bloco detector de colisão.
Detectar colisão é fundamental para que outras funções
ou comandos possam ser executados.
Neste exemplo estamos detectando colisão das bordas da janela, 
e entre dois quadrados, móvel, acompanhem no vídeo.



//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool a = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner )
        : TForm ( Owner ), currentx ( 0 ), currenty ( 0 ), dragging ( false ) {
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
    Canvas -> Font -> Name = "Garamond";
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 190, "Por: " );
    Canvas -> Font -> Color = clRed;
    Canvas -> TextOut ( 240, 190, "Samuel Lima" );
    Canvas -> Font -> Color = clBlack;
    Canvas -> TextOut ( 200, 210, "sa_sp10@hotmail.com" );
    Canvas -> Font -> Name = "Garamond";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender )
{
    Canvas -> Pen -> Width = 10;
    Canvas -> Pen -> Color = clBlue;
    Canvas -> Rectangle ( 05, 05, 595, 295 );
    Canvas -> Font -> Size = 16;
    Canvas -> Font -> Name = "SF Theramin Gothic Shaded Oblique";
    Canvas -> Font -> Color = clRed;
    Canvas -> Font -> Style = TFontStyles ( ) << fsBold  ;
    Canvas -> TextOut ( 20, 12, "DETECTANDO COLISÃO ENTRE DOIS QUADRADOS" );
    Informe ( Sender );
}
//---------------------------------------------------------------------------
 


//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Vcl.Graphics.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TShape *Shape1;
    TShape *Shape2;
    TLabel *Label1;
    TLabel *Label2;
    TLabel *Label3;
    TLabel *Label4;
    TLabel *Label5;
    void __fastcall Shape1MouseMove ( TObject *Sender, TShiftState Shift, int X, int Y );
    void __fastcall Shape1MouseDown ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Shape1MouseUp ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Shape2MouseMove ( TObject *Sender, TShiftState Shift, int X, int Y );
    void __fastcall Shape2MouseDown ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Shape2MouseUp ( TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y );
    void __fastcall Detect_Colisao ( TObject *Sender );
    void __fastcall OnPaint ( TObject *Sender );
    void __fastcall Informe ( TObject *Sender );
private:    // User declarations
        int currentx;
        int currenty;
        bool dragging;
public:        // User declarations
        __fastcall TForm1 ( TComponent* Owner );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
 


object Form1: TForm1
  Left = 455
  Top = 118
  Caption = 'DETECTANDO COLIS'#195'O ENTRE DOIS QUADRADOS'
  ClientHeight = 300
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Shape1: TShape
    Left = 160
    Top = 72
    Width = 65
    Height = 65
    Brush.Color = clBlue
    Pen.Width = 4
    OnMouseDown = Shape1MouseDown
    OnMouseMove = Shape1MouseMove
    OnMouseUp = Shape1MouseUp
  end
  object Shape2: TShape
    Left = 408
    Top = 72
    Width = 65
    Height = 65
    Brush.Color = clRed
    Pen.Width = 4
    OnMouseDown = Shape2MouseDown
    OnMouseMove = Shape2MouseMove
    OnMouseUp = Shape2MouseUp
  end
  object Label1: TLabel
    Left = 504
    Top = 60
    Width = 7
    Height = 24
    Color = clBlue
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentColor = False
    ParentFont = False
  end
  object Label2: TLabel
    Left = 504
    Top = 90
    Width = 7
    Height = 24
    Color = clRed
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentColor = False
    ParentFont = False
  end
  object Label3: TLabel
    Left = 190
    Top = 260
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clGreen
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 40
    Top = 60
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clYellow
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label5: TLabel
    Left = 40
    Top = 90
    Width = 7
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
end