terça-feira, 10 de novembro de 2020

C++ builder - o jogo das palavras



Este programa foi criado num projeto C++,
e a interface gráfica foi criada no C++ Builder
da Embarcadero.
Quando um caractere é inserido na entrada,
ele é pesquisado dentro da matriz bidimensional 
de caracteres, se ele existe, todas as suas ocorrências,
são marcadas, mas se a matriz não contém
o caractere inserido, nada acontece.
A rotina de pesquisa não utiliza nenhuma
função nativa nem do C e nem do C++,
foi criada na mão e pode ser adaptada
em diversas aplicações.
O programa está configurado para não aceitar
mais de um caractere em sua entrada.




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

#include <vcl.h>
#pragma hdrstop
#define lin  7
#define col 18
#include "Unit1.h"
#include <map>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
String str;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
std::map < String, int > mp = {
{ " Nome de uma ave    ", 4 },
{ " Nome de uma fruta  ", 6 },
{ " Nome de um carro   ", 1 },
{ " Nome de um mamífero", 2 },
{ " Nome de um país    ", 3 },
{ " Nome de uma pessoa ", 7 },
{ " Nome de uma flor   ", 5 }};

char texto [ 7 ] [ 18 ] = {
"C H E V E T T E",
"E L E F A N T E",
"G U A T E M A L A",
"A V E S T R U Z",
"M A R G A R I D A",
"M E L A N C I A",
"H E N R I Q U E"};
  //---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 173, 216, 230 ) );
Canvas -> Font-> Style = TFontStyles ( ) << fsItalic;
Canvas -> Font -> Size = 11;
    Canvas -> Font -> Name = "Arial";
Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 90, 210, "Por: " );
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 130, 210, "Samuel Lima" );
Canvas -> Font -> Color = clBlue;
Canvas -> TextOut ( 90, 225, "sa_sp10@hotmail.com" );
}
 //---------------------------------------------------------------------------
void __fastcall TForm1::Label_Manual ( TObject *Sender ) {
Label1 -> Visible = true;
Label1 -> Font -> Size = 13;
Label1 -> Font -> Name = "Arial";
Label1 -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic;
//Label1 -> Font -> Color = clBlack;
Label1 -> Font -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
//Label1 -> Color = static_cast < TColor > ( RGB ( 0, 255, 255 ) );
Label1 -> Transparent = True;
Label1 -> AutoSize = False;
Label1 -> Width = 200;
Label1 -> Height = 150;
Label1 -> Left = 50;
Label1 -> Top = 35;

Label2 -> Visible = true;
Label2 -> Font -> Size = 13;
Label2 -> Font -> Name = "Arial";
Label2 -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic;
//Label1 -> Font -> Color = clBlack;
Label2 -> Font -> Color = static_cast < TColor > ( RGB ( 255, 0, 0 ) );
//Label2 -> Color = static_cast < TColor > ( RGB ( 0, 255, 55 ) );
Label2 -> Transparent = True;
    Label2 -> AutoSize = False;
Label2 -> Width = 20;
Label2 -> Height = 150;
Label2 -> Left = 248;
Label2 -> Top = 35;

Edit1 -> SetFocus ( );
Edit1 -> Font -> Size = 10;
Edit1 -> Width = 70;
Edit1 -> Height = 19;
Edit1 -> Left = 90;
Edit1 -> Top = 260;

BitBtn1 -> Visible = true;
BitBtn1 -> Font -> Size = 12;
BitBtn1 -> Font -> Color = clBlue;
BitBtn1 -> Font-> Style = TFontStyles ( ) << fsItalic ;
BitBtn1 -> Top = 260;
BitBtn1 -> Left = 170;
BitBtn1 -> Height = 21;
BitBtn1 -> Width = 30;
BitBtn1 -> Caption = "Ok";
}
int x = 0, y = 0;
//---------------------------------------------------------------------------
void __fastcall TForm1::grade ( TObject *Sender ) {
//Ajustando a largura das bordas do RoundRect em torno do texto
Canvas -> Pen -> Width = 5;
//Colorindo as bordas do roundrect com rgb
Canvas -> Pen -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
//Colorindo o roundrect com rgb
Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 221, 160, 221 ) );
Canvas -> RoundRect ( 290, 50, 530, 280, 20, 20 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender ) {
 Canvas -> Font -> Size = 16;
 Canvas -> Font -> Name = "Arial";
 //Alterando o estilo da fonte para Bold e Itálico
 Canvas -> Font-> Style = TFontStyles ( ) << fsBold << fsItalic;
 //Alterando a cor do form ( Colorindo o roundrect com rgb )
 Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 255, 255, 205 ) );
  //Ajustando a largura das bordas do RoundRect Em torno do form ( Moldura )
 Canvas -> Pen -> Width = 10;
 //Colorindo as bordas do roundrect ( Moldura ) com rgb
 Canvas -> Pen -> Color = static_cast < TColor > ( RGB ( 231, 220, 202 ) );
 Canvas -> RoundRect ( 05, 05, 595, 295, 25, 25 );

 SetTextColor ( Canvas -> Handle, RGB ( 255, 25, 2 ) );
 Canvas -> TextOut ( 100, 10, "C++ BUILDER - O JOGO DAS PALAVRAS" );
 //Ajustando a largura das bordas do RoundRect em torno do texto
 Canvas -> Pen -> Width = 5;
 //Colorindo as bordas do roundrect com rgb
 Canvas -> Pen -> Color = static_cast < TColor > ( RGB ( 0, 0, 255 ) );
  //Colorindo o roundrect com rgb
 Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 255, 255, 0 ) );
 Canvas -> RoundRect ( 49, 50, 270, 195, 20, 20 );
 grade ( Sender );
 Informe ( Sender );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow ( TObject *Sender ) {
   Label_Manual ( Sender );
   String str_1;
   String str_2;

for ( auto p : mp ) {
if ( p.second % 1 == 0 ) {
str_1 += "\n";
str_2 += "\n";
}
str_1 += p.first;
str_2 += " ";
str_2 += p.second;
}
Label1 -> Caption = str_1;
Label2 -> Caption = str_2;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click ( TObject *Sender ) {
    int a = 0, b = 0, g = 0, i = 0, j = 0, k = 0;
    String str_2;
    str_2 += Edit1 -> Text;
#define SIZE 40
    char st [ SIZE ];
    char conv [ 3 ];
    wcstombs ( st, str_2.c_str ( ), SIZE );
//buffer dinâmico
    char *pt;
    pt = new char [ str_2.Length ( ) + 1 ];
wcstombs ( pt, str_2.c_str ( ), str_2.Length ( ) + 1 );

Canvas -> Font -> Size = 16;
Canvas -> Font -> Name = "Consolas";

int lenv = strlen ( pt );

for ( i = 0; i < lin; i++ ) {
   for ( j = 0; j < col; j++ ) {
   k = 0;
   while ( texto [ i ] [ j + k ] == pt [ k ] && pt [ k ] != '\0'
&& texto [ i ] [ j + k ] != '\0' )
   k++;
   if ( pt [ k ] == '\0' ) {
   for ( g = 0; g < lenv; g++ ) {
Canvas -> Font -> Color = static_cast < TColor > ( RGB ( 0, 0, 0 ) );
Canvas -> Brush -> Color = static_cast < TColor > ( RGB ( 0, 191, 255 ) );
Canvas -> TextOut ( 300 + ( j * 13 ), 55 + ( i * 30 ), texto [ i ] [ j ] );
  }
   }
   }
   }
   Canvas -> Refresh ( );
       Edit1 -> Clear ( );
       Edit1 -> SetFocus ( );
   free ( pt );
}
//---------------------------------------------------------------------------


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

#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.Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TBitBtn *BitBtn1;
TEdit *Edit1;
TLabel *Label1;
TLabel *Label2;
void __fastcall Informe ( TObject *Sender );
void __fastcall grade ( TObject *Sender );
    void __fastcall Label_Manual ( TObject *Sender );
void __fastcall OnPaint ( TObject *Sender );
void __fastcall BitBtn1Click ( TObject *Sender );
void __fastcall FormShow ( TObject *Sender );
private: // User declarations
public:  // User declarations
 __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.