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

Nenhum comentário:

Postar um comentário

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